Proton BASIC Compiler - Interrupt driven clock timer 2


  • PicŪ Basic


  • Interrupt driven clock timer 2

    By : Olivier de Broqueville

    This routine is based on an interrupt triggered by TIMER0. The constants are calculated for a 20 Mhz Xtal.
    As this routine was created for a precision-not-sensitive project, the only concern was to be able to 'stay' fairly accurate on the long run (some minutes per month were acceptable) With the method of corrections, it was possible to reach easily a precision of a few seconds per day .
    The constants used here were obviously calculated for my prototype. The numbers can vary with other components, due to components-tolerance.
    The correction method works this way:
    • The interrupt routine lets the 'clock' to go a bit too slowly in order to simplify the corrections (adding some seconds is easier)
    • The interrupt is based on TIMER0. As it is a 8 bits TIMER, with a prescaler (*) we will use this prescaler to 'slow down' the TIMER. In our case we will use a ratio of 1:256. You can obviously play with this factor to improve the accuracy as it is one of the parameter influencing it.
    • The calculation is as follow:
      • Xtal = 20,000,000 Hz and a pulse is generated every 4 cycles. This means 5,000,000 pulses per second
      • As TIMER0 needs 256 pulses to overflow and generate an interrupt and as we use a prescaler factor of 1:256 we get increments of the TIME variable 5,000,000/(256*256)=76.29394 per seconds. The TIME variable LIMIT is then 76.
      • If we count to exactly 76, the clock would be slightly too fast, which is not what we wanted. We will then count one more (by testing for > ). The clock will then be slightly too slow.
      • The error factors could be approximated by using the fractional part of the above calculation. In my case it was calculated on a one day run of the clock. So I took in account the real error of my specific components.
    Inconvenients of this method:
    • Uses TIMER0. Then watchdog cannot be used. But this could be solved by using another 16F628's timer (It has 3 timers)
    • The clock jumps for a few seconds at each hour change and day change
    Advantage of this method:
    • Only 84 words without printing statements
    • Really accurate without using a RTC
    • Small routine embeddable in your application
    • Full control on all parameters
    • Very small interrupt routine (smaller is difficult...)
    Download the TIMER routine here
    Tim as also developed a TIMER routine based on the same correction principle (Download Tim's TIMER routine here). It uses TIMER1, counts at 1/100 of a sec and displays days count.


    Olivier de Broqueville
    (Last update : 27/11/03)

    (*) For people who do not know what 'prescaler' is : the prescaler is a divider which can divide by a certain factor the number of pulses needed to increment the TIMER counter by one unit.
    Example:
    • If the prescaler factor is 1:1 then each pulse will increment the TIMER counter by 1.
    • If the prescaler factor is 1:2 then you will need 2 pulses to increment the TIMER counter by 1.
    • Etc...
    Somewhere we can say that the prescaler 'slows down' the TIMER
    October 2003