Monday, January 12, 2015

PIC16F84A blink without delay

PIC16F84A LED blink without delay/Use of timer interrupt
Some times we have to make an led blinks at some frequency but we can not use the delay command, here we are going to use timer0 for pic16f84a to blink led attached to rb1 with a frequency about 1Hz.
First, our pic has only one timer which is timer0, this timer is an 8 bit timer, so it counts from 0 to 255 then an overflow occurs.
Timer calculator:
It is simple to calculate your preload value and interrupt frequency manually and without using any software. Here the timer frequency equals to the microcontroller frequency divided by 4, so we have the following equation relates the different variables:
TMR preload value = 256 - MCU_freq/(4 * Prescaler * Interrupt_freq).
Circuit & MikroC code:

//PIC16F84A LED blink without delay MikroC code
//Written by: BENCHEROUDA Okba
//Used crystal: 12MHz
//Use at your own risk

  unsigned char i;
 void interrupt(){
  TMR0 = 22;                      // initial value of TMRO
  INTCON.F2 = 0;                 // reset T0IF (flag bit)
 void main() {
 OPTION_REG = 0x07;          // Set timer TMR0 to 1:256 prescaler
 TMR0 = 22;                   //Preload timer = 256 - 12/(4*256*Hz)
 INTCON = 0xA0;              //Enabe internal interrupt
 TRISB.F1 = 0;              //Configure PORTB.F1 as output
 PORTB.F1 = 0;              //Initial value of PORTB.F1
  while(1) ;                //Endless loop