View previous topic :: View next topic |
Author |
Message |
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Fri Dec 09, 2022 5:17 pm Post subject: How to use RTC |
|
|
Hello.
I would like to configure the RTC (real time counter) to wakeup my program once a minute or maybe much longer.
Here is my test code. No interrupts so far. Any help would be appreciated.
Code: |
$regfile = "xm256A3Udef.dat"
$crystal = 32000000
$hwstack = 100
$swstack = 100
$framesize = 100
$lib "xmega.lib"
Config Osc = disabled , 32mhzosc = Enabled , 32khzosc = Enabled ,32KHZPOWERMODE=LOW_POWER
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled , Med = disabled , Hi = disabled
'pin33 PD7, TXD1 & pin32 PD6, RXD1 - bascom COM4.
Config Com4 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "Com4:" For Binary As #4
print #4,
print #4,version(3)
Config Clock = user
'Config Clock = Soft , Rtc = 1KHZ_INT32KHZ_RCOSC , gosub = SecTic
CLK_RTCCTRL=5 'value from config soft
print #4,"rtcctrl:";hex(CLK_RTCCTRL)
'bitwait RTC_STATUS.0,reset
RTC_PER=1024 'value from config soft
print #4,"PER:";RTC_PER
'bitwait RTC_STATUS.0,reset
RTC_ctrl=1 'value from config soft 'prescale: 0=RTC stopped, 1=DIV1, 2=DIV2, 3=DIV8, 4=DIV16, 5=DIV64, 6=DIV256, 7=DIV1024
print #4,"ctrl:";rtc_ctrl
rtc_intctrl=1 'value from config soft 'lo pri interrupt
print #4,"intctrl:";rtc_intctrl
'bitwait RTC_STATUS.0,reset
rtc_comp=0 'value from config soft '1024 '32768Hz
print #4,"comp:";rtc_comp
on RTC_OVF RTC_isr
enable RTC_OVF
'on RTC_CMP RTC_isr
'enable rtc_CMP
enable interrupts
do
loop
RTC_isr:
print #4,"*";
return
sectic:
print #4,"!";
return |
(BASCOM-AVR version : 2.0.8.5 )
Last edited by Dave on Sat Dec 10, 2022 11:39 pm; edited 1 time in total |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Fri Dec 09, 2022 7:14 pm Post subject: |
|
|
Try it using soft clock.
Code: | '============================================
'Config SOFT Clock
'============================================
Config Clock = Soft , Rtc = 1khz_int32khz_ulp, Gosub = Sectic 'select internal 1 KHz clock from the 32KHz internal oscillator
Rtc_ctrl = xxxxx
'........
Sectic:
Print #4,"*"
Return |
ALSO, is there a reason you do not use,
Code: | $crystal = 32000000 ' or similar ????
|
|
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Fri Dec 09, 2022 7:43 pm Post subject: |
|
|
hello enniom
Thanks for the reply.
Yes, I have used the soft clock and I can make it work with longer interrupts periods. The reason I would prefer to not use soft clock is that the soft clock ISR expects an interrupt once a second.
So I if I set up Rtc_ctrl for say 64 second interrupts the soft clock ISR results are wrong. I can correct for that in sectic but it's not very efficient. Where as with USER clock I would just need to add 64 to a long variable to keep track of the time. Or for applications that don't need time of day the RTC is the best chose for periodically waking the uC.
Quote: | $crystal = 32000000 |
I don't understand. I am using $crystal = 32000000. |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sat Dec 10, 2022 11:21 pm Post subject: |
|
|
This is my latest attempt to get RTC interrupts. It still it's happening. I copied the settings of from config clock = soft settings.
All registers seem to read and write properly. I must be missing some register or something. What am I doing wrong?
Code: | $regfile = "xm256A3Udef.dat"
$crystal = 32000000
$hwstack = 100
$swstack = 100
$framesize = 100
$lib "xmega.lib"
Config Osc = disabled , 32mhzosc = Enabled , 32khzosc = Enabled ,32KHZPOWERMODE=LOW_POWER
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled , Med = disabled , Hi = disabled
'pin33 PD7, TXD1 & pin32 PD6, RXD1 - bascom COM4.
Config Com4 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "Com4:" For Binary As #4
print #4,
print #4,version(3)
Config Clock = user
'Config Clock = Soft , Rtc = 1KHZ_INT32KHZ_RCOSC , gosub = SecTic
CLK_RTCCTRL=5 'value from config soft
print #4,"rtcctrl:";hex(CLK_RTCCTRL)
bitwait RTC_STATUS.0,reset
RTC_PER=1024 'value from config soft
print #4,"PER:";RTC_PER
bitwait RTC_STATUS.0,reset
RTC_ctrl=1 'value from config soft 'prescale: 0=RTC stopped, 1=DIV1, 2=DIV2, 3=DIV8, 4=DIV16, 5=DIV64, 6=DIV256, 7=DIV1024
print #4,"ctrl:";rtc_ctrl
rtc_intctrl=1 'value from config soft 'lo pri interrupt
print #4,"intctrl:";rtc_intctrl
bitwait RTC_STATUS.0,reset
rtc_comp=0 'value from config soft '1024 '32768Hz
print #4,"comp:";rtc_comp
on RTC_OVF RTC_OVF
enable RTC_OVF
'on RTC_CMP RTC_isr
'enable rtc_CMP
enable interrupts
do
loop
RTC_isr:
print #4,"*";
return
sectic:
print #4,"!";
return |
Last edited by Dave on Sun Dec 11, 2022 1:46 am; edited 1 time in total |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 1:19 am Post subject: |
|
|
The code you posted has both clock sources commented out.
This works for me:
Code: | $regfile = "xm192a3udef.dat"
$hwstack = 64
$swstack = 64
$framesize = 64
$crystal = 32000000
Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Porta.5 = Output 'red led
Dim I As Byte
For I = 1 To 8
Toggle Porta.5
Waitms 500
Next
Wait 2
Config Clock = Soft , Rtc = 1khz_int32khz_rcosc , Gosub = Sectic
Bitwait Rtc_status.0 , Reset
Rtc_ctrl = &B0000_0011 'flash led every 8 sec
Config Priority = Static , Vector = Application , Lo = Enabled
Enable Interrupts
Do
Loop
End
Sectic:
Toggle Porta.5 'led
Return |
Use Table 17.1 Prescaler to get the delay you want.
E |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sun Dec 11, 2022 1:52 am Post subject: |
|
|
Quote: | The code you posted has both clock sources commented out. | Thanks for pointing that out. I fixed it.
I'll use soft clock if I can't get RTC working the way I hope.
Thanks. |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sun Dec 11, 2022 5:14 pm Post subject: |
|
|
PROBLEM SOLVED (sort of)
So the problem seems to be that the statement "ON RTC_OVF RTC_isr " is not currently supported by bascom.
I've come up with a work around. Mark will problem get a laugh out of this. I created my first lib. It replaces the soft clock isr interrupt routine. See comments in code.
In this example the uC wakes up every 8 seconds and adds 8 to l_sys_sec, prints the new time and goes back to sleep. The sleep time can be up to 1024 seconds in this example.
Code: | $regfile = "xm256A3Udef.dat"
$crystal = 32000000
$hwstack = 100
$swstack = 100
$framesize = 100
$lib "xmega.lib"
'For 32mhz clock.
Config Osc = disabled , 32mhzosc = enabled ,32khzosc = Enabled ,32KHZPOWERMODE=LOW_POWER
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled , Med = disabled , Hi = disabled
$lib "clock_bodge.lib"
'This is all there is in "clock_bodge.lib"
' [_SOFT_CLOCK]
' _Soft_clock:
'
' #IF _SECTIC = 1
' *@sectic
' #ENDIF
' Reti
' [END]
'pin33 PD7, TXD1 & pin32 PD6, RXD1 - bascom COM4.
Config Com4 = 115200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "Com4:" For Binary As #4
print #4,
print #4,version(3)
dim l_sys_sec as long
Config Clock = Soft , Rtc = 1KHZ_INT32KHZ_ULP , gosub = SecTic
bitwait RTC_STATUS.0,reset
RTC_ctrl=3 'prescale: 0=RTC stopped, 1=DIV1, 2=DIV2, 3=DIV8, 4=DIV16, 5=DIV64, 6=DIV256, 7=DIV1024
print #4,"ctrl:";rtc_ctrl
waitms 1
const c_sec_per_int=8
'on RTC_OVF RTC_isr '<-- This is currently not supported by Bascom.
'enable RTC_OVF
enable interrupts
do
Config Powermode = POWERSAVE
print #4,"time:";Time(l_Sys_Sec)
waitms 1 'some time to print.
loop
sectic:
l_sys_sec=l_sys_sec+c_sec_per_int
return
|
|
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 5:25 pm Post subject: |
|
|
Dave,
Its difficult to understand the problem you are solving.
Is it to provide the capability to independently set the incremental value,
Code: | const c_sec_per_int=8
|
E |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sun Dec 11, 2022 5:45 pm Post subject: |
|
|
enniom,
It's mostly about saving power in very lo power applications. As I tried to explain before, the problem is that soft clock expects an interrupt once a second. If you change the RTC to interrupt say every 8 seconds the time from soft clock will be garbage. Calling a routine that creates garbage every 8 seconds is a lot of wasted CPU time or wasted power over the course of several days. I can now use RTC to wake the uC once every n seconds instead of once a second. n can be up many hours or days for that matter. Does that make sense? |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 5:58 pm Post subject: |
|
|
Dave,
I don't think that's correct. This does exactly the same thing. Check the power consumption.
Code: | $regfile = "xm192a3udef.dat"
$hwstack = 64
$swstack = 64
$framesize = 64
$crystal = 32000000
Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Porta.5 = Output 'red led
Dim I As Byte
For I = 1 To 8
Toggle Porta.5
Waitms 500
Next
Wait 2
Config Clock = Soft , Rtc = 1khz_int32khz_rcosc , Gosub = Sectic
Bitwait Rtc_status.0 , Reset
Rtc_ctrl = &B0000_0011 'flash led every 8 sec
Config Priority = Static , Vector = Application , Lo = Enabled
Enable Interrupts
Do
Config Powermode = POWERSAVE
Loop
End
Sectic:
Toggle Porta.5 'led
Return |
|
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sun Dec 11, 2022 6:16 pm Post subject: |
|
|
enniom,
Not really, print time$ after waking up the uC and you will see that it make no sense. |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 6:20 pm Post subject: |
|
|
Right. So you're after time not power consumption.
E |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 6:32 pm Post subject: |
|
|
Consider:
Code: | Dim L as Long
Do
Config Powermode = POWERSAVE
L = L+8
Print Time(L)
Loop |
E |
|
Back to top |
|
|
Dave
Joined: 05 Feb 2005 Posts: 314 Location: OR
|
Posted: Sun Dec 11, 2022 6:36 pm Post subject: |
|
|
No, I'm trying to show you that your code is running an interrupt routine that is of little or no use. It's not a big effect. Running 80 or so line of assembly isn't much but your doing ever 8 seconds. Over time that adds up. Over the course of just one day that's 864000 lines of assemble code. |
|
Back to top |
|
|
enniom
Joined: 20 Oct 2009 Posts: 537
|
Posted: Sun Dec 11, 2022 6:40 pm Post subject: |
|
|
OK.
Code: | Dim L as Long
bitwait RTC_STATUS.0,reset
RTC_ctrl=7 'prescale: 0=RTC stopped, 1=DIV1, 2=DIV2, 3=DIV8, 4=DIV16, 5=DIV64, 6=DIV256, 7=DIV1024
Do
Config Powermode = POWERSAVE
L = L+1024
Print Time(L)
Loop
|
Doesn't this wake up every 1024 seconds?
E |
|
Back to top |
|
|
|
|
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
|
|