PIC16F84A Timer0 interrupt example with CCS C compiler

PIC16F84A blink without delay (Timer0 interrupt)

The microcontroller PIC16F84A has 1 timer which is Timer0, this timer has a resolution of 8 bits and an interrupt-on-overflow from FFh to 00h. This topic shows how to use Timer0 interrupt to blink an LED.
This interrupt is generated when the TMR0 register overflows from FFh to 00h.
To enable the Timer0 interrupt GIE bit (INTCON<7>) must be 1 which can be done using the following CCS C command:
enable_interrupts(GLOBAL) ;
Also T0IE bit (INTCON<7>) must be 1 which can be done using the following command:
enable_interrupts(INT_TIMER0) ;
Bit T0IF (INTCON<2>) must be cleared in software by the Timer0 module Interrupt Service Routine before re-enabling this interrupt and this is done using the following command:
clear_interrupt(INT_TIMER0) ;
We can set the timer0 preload value (initial value) using the following command:
set_timer0(preload_value) ;
where preload_value is an unsigned 8-bit number.
The clock source can be internal or external through RA4/T0CKI pin.
Prescaler rate of Timer0 can be: 2, 4, 8, 16, 32, 64, 128 or 256. The clock source and prescaler can be set using the following CCS command:
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256) ;
To compute the timer0 frequency use the following equation:
Timer0_freq = MCU_freq / {4 * Prescaler * (256 – TMR0)} 
where TMR0 is Timer0 preload value.
and: peroid = 1/Timer0_freq which is time to interrupt.

PIC16F84A Timer0 interrupt example circuit:
This is a simple example which uses Timer0 interrupt to make an LED connected to RA0 blinking at a frequency of 1Hz.
PIC16F84A Timer0 interrupt example

PIC16F84A Timer0 interrupt example CCS PIC C code:
The timer is used to interrupt every 50ms and to make the LED ON for 500ms and OFF for 500ms, the interrupt must be interrupted 10 times, that’s why a variable i is used.
HS oscillator is used with frequency of 4MHz.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.