Author | Eur van Andel, Copyright © 2010, all rights reserved. |
Adapted-by | |
Compiler | 2.4m |
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 TMR2 runs on internal clock speed and interrupts on overflow every TMR2 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 . This RTC runs on TMR1. You must setup TMR1 yourself an example how to do this for a PIC16F876A is given here: prescaler = 1, internal clock, TMR1 = on PIE1_TMR1IE = on, INTCON_PEIE = on, INTCON_GIE = on . T1CON_T1CKPS = 0b00 -- 1:1 prescaler T1CON_T1OSCEN = off T1CON_TMR1CS = off T1CON_TMR1ON = on -- TMR1 on . PIE1_TMR1IE = on -- TMR2 interrupt enabled INTCON_PEIE = on -- peripheral interrupt enabled INTCON_GIE = on -- global interrupt enabled
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