$regfile = "atXtiny412.dat"
$crystal = 20000000
$hwstack = 16
$swstack = 16
$framesize = 24
'set the system clock and prescaler
Config Sysclock = 16_20mhz , Prescale = 1
'config port
config porta.6=output
'set the referenfes
Config Vref = Dummy , Adc0 = 4v3
waitus 25
'set the ADC
Config Adc0 = Single , Resolution = 10bit , Reference = Internal , Prescaler = 64 , Sample_len = 3 , Sample_cap = Above_1v , Init_delay = 0,window_comp=OUTSIDE,sample_accu=4,mux=1'in pin for ADC PA1
'beware of window_comp and sample_accu settings, they are badly formulated in help
'is the difference between a description and a data file,
adc0_evctrl=&H01 'ADC we will send using event
Enable ADC0_WCOMP 'allowing interrupts from the window comparator
On ADC0_WCOMP ADC0_WCOMP_isr
start adc0
'setting values for the window comparator
const V_REF=4340
const LIMIT_LOW =3200 'uV
const LIMIT_HIGH=4200 'uV
const accumulation =4
const LOW_LIMIT= (LIMIT_LOW*1023*accumulation)\V_REF
const HIGH_LIMIT =(LIMIT_HIGH*1023*accumulation)\V_REF
ADC0_WINLT = LOW_LIMIT
ADC0_WINHT = HIGH_LIMIT
'set the PIT
RTC_CLKSEL = &H01 'set clock source for PIT - internal 1kHz from ULP oscillator
while RTC_PITSTATUS.0 =1 ' waiting for write to registry
wend
RTC_PITCTRLA = &H01 'launches "blank" PIT (without allowed interrupt)
'set the EVENT
config porta.2=output
portmux_ctrla=&H01
EVSYS_ASYNCCH3 = &H0F 'load PIT output into ASYNCCH3 event channel (1k/256 = ~ 4Hz)
EVSYS_ASYNCUSER1 = &H06 'Asyncuser1 = ADC0, takes events from ASYNCCH3 (i.e. from PIT)
Enable Interrupts
Do
loop
ADC0_WCOMP_isr:
dim pom as word
pom=adc0_res
if pom >=low_limit then
set porta.6
else
reset porta.6
end if
return |