| Author | Eur van Andel, Copyright © 2010, all rights reserved. |
| Adapted-by | |
| Compiler | 2.4o |
This Interrupt Service Routine updates the variable seconds about once per second. The precision is 0.2 ppm, the accuracy depends on the Xtal used. The main program should check if seconds are > 60 and if so, increase minutes, hours, days, months, etc. calendar.jal does this
http://www.romanblack.com/one_sec.htm
http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
This is the Bresenham Line Algorithm, invented at IBM in 1962,
which gets an accurate end result by summing the small errors resulting of
taking discrete steps and correcting when the error gets too large. This
means that individual second lengths may show some jitter, but that long-term
timekeeping is accurate.
.
For instance: if xtal=20 MHz, then 5 MHz clock, so 5 000 000 clocks/second
TMR3 runs on internal clock speed and interrupts on overflow
every TMR3 interrupt decreases _rtc_bresenham by 0x1_00_00, or 2^16
if _rts_bresenham < 0x1_00_00 then 5_000_000 is added
so the jitter is 2^15/5e6 = 0.6%
Total second count is accurate, since the lower 15 bits stay intact and add up
.
Example of use:
init_timer3 () ;initialize TMR3
INTCON_GIE = on ;enable interrupts
PIE2_TMR3IE = on ;enable TMR3 interrupts
forever loop
; use seconds for whatever you need
end loop
No dependency found
var volatile dword _rtc_bresenham = _rtc_cycles_per_second
var volatile byte seconds
const _rtc_cycles_per_second = (target_clock / 4)
var volatile dword _rtc_bresenham = _rtc_cycles_per_second
No documentation found
var volatile byte seconds
global variable updated by ISR
const _rtc_cycles_per_second = (target_clock / 4)
No documentation found
RTC()
No documentation found
init_timer3()
No documentation found
| 18f2620 | 18f2620_rtc_tmr3.jal |