# rtc_isr_tmr1

Real Time Clock running from ISR on TMR2

Author | Eur van Andel, Copyright © 2010, all rights reserved.
Adapted-by |
Compiler | 2.4m

Description

    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

Sources

    http://www.romanblack.com/one_sec.htm
             http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

Notes

    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

Dependencies

No dependency found

## Summary

### Global variables/contants

- `var volatile dword _rtc_bresenham = _rtc_cycles_per_second`
- `var volatile byte seconds`
- `const _rtc_cycles_per_second = (target_clock / 4)`

### Procedures

- `RTC()`

### Functions

## API details

### Global variables/contants

- `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

### Procedures

- `RTC()`

  No documentation found

### Functions

## Related samples

No sample found