where Prescaler is the selected prescaler value, and TMR0L is the value loaded into timer register TMR0L to generate timer interrupts every Time period.
In our application the clock frequency is 4MHz, that is, clock period = 0.25µs, and Time = 5ms. Selecting a prescaler value of 32, the number to be loaded into TMR0Lcan be calculated as follows:
Thus, TMR0L should be loaded with 100. The value to be loaded into TMR0 control register T0CON can then be found as:
Thus, T0CON register should be loaded with hexadecimal 0xC4. The next register to be configured is the interrupt control register INTCON, where we will disable priority based interrupts and enable the global interrupts and TMR0 interrupts:
Taking the don’t-care entries (X) as 0, the hexadecimal value to be loaded intoregister INTCON is 0xA0.
When an interrupt occurs, the program automatically jumps to the interrupt service routine. Inside this routine we have to reload register TMR0L, reenable the TMR0 interrupts, and clear the TMR0 interrupt flag bit. Setting INTCON register to 0x20 reenables the TMR0 interrupts and at the same time clears the TMR0 interrupt flag.
The operations to be performed can thus be summarized as follows:
In the main program:
- Load TMR0L with 100
- Set T0CON to 0xC4
- Set INTCON to 0xA0
- Increment the counter with 1-second delays
- Re-load TMR0L to 100
- Refresh displays
- Set INTCON to 0x20 (reenable TMR0 interrupts and clear timer interrupt flag)