PIC16F877A TImer0 Led Blink

By Kunal Kapoor
12-05-2018
403

TIMER0 is one of the 3 timer modules in PIC17F877A MCU. It is a 8 bit timer and is only used for time operations

OPTION_REG register

 

The key configuration to be made is using Prescaler bits

Explanation

Timer 0 is 8 bit timer ie it gives us 2^8 bits=256 units of counter before the TMR0IF flag bit is raised.

Value of oscillator frequency=16MHz

Value of prescaler=256

 

Thereby to calculate the value after which TIMER0 operation will count 1 second===

Count=(Oscillator freq/4)/(PreScaler value * Timer 0 counter)

 

In this case

Count= (16000000/4)/(256*256)

Ie count=61

 

In our code hence we will be running counter for 61 times to get accurate time of 1 second

 

Project:


Circuit diagram:


IDE used: MPLAB by Microchip

Code Snippet:

main.h:

#include 
#define _XTAL_FREQ	16000000


//DEFINE MACROS
__CONFIG (0x2F0A);


//FUNCTION PROTOTYPES
void main(void);


main.c:

#include"main.h"


void main(void)
{
	TMR0=0;
	T0CS=0;
	T0SE=0;
	PSA=0;
	PS0=1;
	PS1=1;
	PS2=1;
	//TMR0IE=1;
//GIE=1;
TRISB =0x00;
TRISC=0;
//PORTB=0xff;
while(1)
{
unsigned int i=0;
	PORTB=0x00;
	PORTC=0;
while(i<10)
{	
	while(T0IF!=1);	
	
	//T0CS=1;
    T0IF=0;
	//TMR0=0;
	i++;
}    
PORTB=0xff;
PORTC=0xff;
unsigned int i=0;
while(i<10)
{	
	while(T0IF!=1);	
	
	//T0CS=1;
    T0IF=0;
	//TMR0=0;
	i++;
}    
}
}	

Output Video:





Related Tutorials