..................

Spreadsheet

Modified 10/03/05



Click here for XLS spreadsheet.

Software

  • This spreadsheet is intended for code of the following structure...



    // DECLARE GLOBAL VARIABLE
    static char cOneSecondFlag;
    
    
    
    // MAIN ROUTINE void main ( void ) { . . // Timer0 (RTCC) roll is... // 256 * 1 / ( ClockFreq / PrescalerSetting / 4 ) setup_counters ( RTCC_INTERNAL, RTCC_DIV_64 ); . . // Enabling the RTCC (Timer0) interrupt will cause an RTCC // interrupt to occur every time the eight-bit timer rolls // from 255 to 0. For a 4MHz clock and prescaler of 64, // and when the RTCC counter is reset to 8 after each interrupt // (so that it starts out at eight and counts to 255), the // interrupt will occur every 15.872mS. The spreadsheet shows // that this will have an accuracy of 0.01%. // // And, a hint from Mike Rigby-Jones, 10/03/05, if you need to // pre-load the timer0 register to get a particular period, it's // a much better idea to add an offset to the current value rather // than just set a static value. For instance, TMR0 += 5 rather // than TMR0 = 5. The latter method will cause the period to vary // depending on how many cycles elapsed before the timer was loaded. // By adding an offset, the interrupt period becomes insensitive // to interrupt latencies or even the position of the statement // within the interrupt code. enable_interrupts ( INT_RTCC ); enable_interrupts ( GLOBAL ); . . while ( 1 ) // main loop is here { while ( cOneSecondFlag == 0 ) // interrupt turns this flag on { // drops thru to here when one second interval completed . . cOneSecondFlag = 0; // reset flag to wait for next second } } . . }
    // INTERRUPT ROUTINE #int_rtcc void Timer0Interrupt ( void ) { // Gets here every 15.872mS. // CtimerCount is incremented every time. // When it gets to 63 it has been 0.999936 seconds. if ( cTimer0Count < 63 ) { cTimer0Count++; } else { // turn on one-second flag, turned off later by main routine cOneSecondFlag = ON; // start count over again cTimer0Count = 0; } // reset timer for 248 counts, rather wrap at 255 set_rtcc ( 8 ); }