The following code connects the 32768Hz crystal to the RTC, the overflow interrupt toggles the pin. TOP of RTC overflow is set by RTC_PER, which showed some odd behavior.
1) If the prescaler RTC_CTRL is set and the RTC is running, it seems not possible to set or change RTC_PER. For changing PER, the RTC has to be disconnected from the clock source
2) Values for PER of 0..1 result in a wrong timing, like the RTC unit has some latency.
[code:1:fde648b631]$regfile = "xm128a1def.dat"
$hwstack=100
$swstack=100
$framesize=100
$noramclear
Config Osc = Enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz
Config PortD.3 = Output
Const RTC_SYNCBUSY = 0
Const RTC_Enbl = 2 ^ 0
Const RTCSRC_0 = 2 ^ 1
Const RTCSRC_1 = 2 ^ 2
Const RTCSRC_2 = 2 ^ 3
Const RTC_TOSC32 = RTCSRC_2 or RTCSRC_0
Const RTC_RCOSC = RTCSRC_1
CLK_RTCCTRL = RTC_TOSC32 or RTC_Enbl ' ext. 32768Hz crystal and enable rtc
'CLK_RTCCTRL = RTC_RCOSC or RTC_Enbl ' int. 1024Hz from 32768Hz RC
while RTC_STATUS.RTC_SYNCBUSY = 1 : wend ' wait for sync ready
RTC_PER = 9 ' top of RTC
RTC_CTRL = 1 ' prescale 1
On RTC_OVF RTC_ovf_ISR NOSAVE ' nosave without saving registers has no negative effect because the mainloop is empty
Enable RTC_OVF , hi
Config Priority = Static , Vector = Application , hi = Enabled
Enable Interrupts
Do
Loop
RTC_ovf_ISR:
Toggle PortD.3
Return
' RTC_TOSC32 ext. crystal
' RTC_PER 0 = 91,8µS
' RTC_PER 1 = 122,0µS
' RTC_PER 2 = 91,8µS
' RTC_PER 3 = 122,0µS
' RTC_PER 4 = 152,8µS
' RTC_PER 9 = 306,0µS = 3276,9Hz * 10 (PER 9) = 32768Hz
' sequence to change RTC top
' RTC_CTRL = 0 ' disconnect clock
' while RTC_STATUS.RTC_SYNCBUSY = 1 : wend ' wait for sync ready
' RTC_PER = 19 ' set new top
' RTC_CTRL = 1 ' restore clock
[/code:1:fde648b631]
↧