Blink with a Timer0 interrupt
Use Timer0 and its overflow interrupt to toggle an LED every 0.49 s on a PIC16F84A, without a delay loop. Covers OPTION_REG, the prescaler, INTCON and RETFIE.
Open the example in the simulator
Why use an interrupt?
A delay loop keeps the CPU busy doing nothing. With Timer0 counting in hardware, the main program is free; the timer interrupts it when it overflows, and a short interrupt routine toggles the LED.
Setting up Timer0
Timer0 is the 8-bit register TMR0. It is configured through OPTION_REG (in bank 1). The example writes 0x05:
T0CS = 0: count instruction cycles (1 µs each at 4 MHz).PSA = 0: the prescaler belongs to Timer0, not the watchdog.PS2:PS0 = 101: prescaler 1:64, soTMR0goes up once every 64 µs.
Counting from 0 to 255 and back to 0 takes 256 × 64 µs = 16.384 ms. Each overflow sets the T0IF flag in INTCON. The timer calculator does this arithmetic for other settings.
Turning the interrupt on
bsf INTCON, T0IE enables the Timer0 interrupt and bsf INTCON, GIE enables interrupts globally. From then on, every overflow makes the PIC finish the current instruction, push the return address, clear GIE and jump to address 0x0004.
The interrupt routine
The routine at 0x0004 first clears T0IF; otherwise the interrupt would fire again as soon as it returns. It then counts down timer_count with DECFSZ. Only on every 30th overflow (30 × 16.384 ms = 491.5 ms) does it reload the counter and flip RA0 with XORLW.
Both exits use RETFIE, which returns and sets GIE again in one instruction.
This routine can skip saving W and STATUS only because the main loop is a bare goto main_loop that uses neither. In a real program, save both at the start of the routine (use SWAPF for STATUS) and restore them before RETFIE.
Why not SLEEP in the main loop?
It is tempting to replace goto main_loop with SLEEP to save power. It doesn't work: SLEEP stops the oscillator, Timer0 stops with it, and its interrupt never comes. Waking from SLEEP needs the watchdog, RB0/INT, a PORTB change or a peripheral with its own clock.
Try it
- Open the example, assemble it and press Run. Switch the widget dock to the CPU tab to see the Interrupt Table and
TMR0counting. - Set a breakpoint on the
bcf INTCON, T0IFline (click its line number, or put the cursor there and press F9). Run, and the simulator stops each time an interrupt is taken. - Change
movlw 0x05tomovlw 0x07(prescaler 1:256): each overflow now takes 65.5 ms and the LED toggles about every 2 s.
The complete program
This is examples/timer-interrupt-blink.asm, the same file the simulator loads.
; Timer Interrupt Blink Example
; LED blinking using Timer0 interrupt for PIC16F84A
; LED on RA0 toggles about every 0.5 s (4 MHz crystal). With the 1:64
; prescaler Timer0 overflows every 256 x 64 us = 16.384 ms; 30 overflows = 0.49 s.
list p=16f84a
include <p16f84a.inc>
; Configuration bits
__CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_ON & _CP_OFF
; Variables
cblock 0x20
timer_count
endc
org 0x00
goto start
org 0x04
; Interrupt service routine
bcf INTCON, T0IF ; Clear Timer0 interrupt flag
decfsz timer_count, f
retfie
; 30 overflows (about 0.49 s) have passed: toggle the LED
movlw 0x1E ; Reload the counter: 30 x 16.384 ms = 491.5 ms
movwf timer_count
movf PORTA, w
xorlw 0x01 ; Toggle bit 0
movwf PORTA
retfie
start:
; Set RA0 as output
bsf STATUS, RP0 ; Select Bank 1
bcf TRISA, 0 ; Set RA0 as output
bcf STATUS, RP0 ; Select Bank 0
; Configure Timer0
bsf STATUS, RP0 ; Select Bank 1
movlw 0x05 ; T0CS = 0 (instruction clock), PSA = 0, PS = 101: 1:64
movwf OPTION_REG
bcf STATUS, RP0 ; Select Bank 0
; Initialize Timer0
clrf TMR0
movlw 0x1E ; 30 overflows of 16.384 ms = 491.5 ms
movwf timer_count
; Enable interrupts
bsf INTCON, T0IE ; Enable Timer0 interrupt
bsf INTCON, GIE ; Enable global interrupts
; Main loop (just wait for interrupts)
; Don't use SLEEP here: the oscillator stops during SLEEP, so Timer0
; stops counting and its interrupt can never wake the PIC.
main_loop:
goto main_loop ; Wait; the ISR toggles the LED
end