Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Window Comparator Mode ADC0

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 94

blank.gif
PostPosted: Sat Mar 06, 2021 9:38 am    Post subject: Window Comparator Mode ADC0 Reply with quote

Hi
A simple example of the ADC0 window comparator function.
I connected the two previously described PIT and EVENT systems to the ADC.
The program as designed to monitor the lion cell and can be used to charge or signal discharge or overcharge.

The program is designed for the AVR1 kernel series.
More in the program.

Code:

 $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


RS
Back to top
View user's profile
JC

Bascom Member



Joined: 15 Dec 2007
Posts: 504
Location: Cleveland, OH

usa.gif
PostPosted: Sun Mar 07, 2021 2:04 am    Post subject: Reply with quote

Nice job!

JC
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5166
Location: Holland

blank.gif
PostPosted: Tue Mar 09, 2021 9:10 pm    Post subject: Reply with quote

Thanks for this great example.
I see you make good usage of the xtiny capabilities.
Thanks for sharing Very Happy

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum