Professional Documents
Culture Documents
Components:
Microcontroller PIC16F877A
Crystal – 20MHz
Capacitor – 22uF
Push Button
Resistor – 1K
Resistor – 470 ohms
Transistor (BC547)
Software:
MPLABX IDE
Proteus
Introduction:
. Timers ,
The Timer is used to measure the time or generate an accurate time delay. It is an important
application in an embedded system. It maintains the timing of operation in sync with a system
clock or an external clock. The timer is used to count cycles and perform a particular action at a
specified moment or optionally start an interrupt cycle. The digital cycles counted by the timer
can be supplied internally through the peripheral clock or externally through a crystal.
The timer is nothing but a simple binary counter that can be configured to count clock
pulses(Internal/External). Once it reaches the max value, it will roll back to zero setting up an
Overflow flag and generates the interrupt if enabled.
Prescaler: Prescaler is a block that presents inside the timer module and it is used to divide the
clock frequency by a constant. It allows the timer to be clocked at the rate a user desires.
. TIMER 2 .
TIMER 2 REGISTERS
PIC16F877A Timer 2 module registers are shown below:
Structure of T2CON :
1. Get the clock from the FOSC/4 and then going to the Prescaler. The Prescaler divides
the incoming clock frequency from some factors(it maybe 1,4,16)
2. Then this clock frequency is given to the TMR2 register, it will count the incoming clock
pulse.
3. As it is counting the incoming clock pulses the value present in the TMR2 register is also
compared with PR2 register.
4. If TMR2 and PR2 register are equal, the output of the comparator will reset the TMR2
register.
5. Then this equal output is given to the input of the Postscaler. It is not generated an
interrupt immediately.
6. When the Postscaler bit obtains any of the value, it will generate some delay and TMR2
Interrupt Flag bit set.
. Delay Calculation .
Example: As the timer 2 is 8-bit and supports 1:16 prescaler, it is not possible
to directly generate the delay of 1 sec. The max delay with 1:16 Prescaler will
be:
= 256 * 16*4/20Mhz
=819us
Now 500us can be generated using timers which will be used to increment a counter
2000 times to get 1 sec delay.
= 256-((500us* 20Mhz)/(16*4))
= 256-156=100
PROCEDURE:
1. Open the MPLAB X IDE and create a new project by clicking the file option on the
upward left corner and selecting new project.
2. Then select the XC 8 compiler and write the project name and create your object.
3. Select the source file and create the extension file for the project to write your code.
4. Design the circuit on proteus using PIC16F877A.
5. Then choose the hex file by clicking on microcontroller component on proteus.
Code:
Task:
Write a code to generate precise delays with timer 2 using pic16F877A.
Code:
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM
code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write
protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code
protection off)
#include<pic16f877a.h>
#define SBIT_T2CKPS1 1
char value = 0;
int count = 0;
if(TMR2IF==1)
TMR2 = 101; /*Load the timer Value, (Note: Timervalue is 101 instead of 100 as
the
count=0;
else
count++; // Keep incrementing the count till it reaches 2000 to generate 1sec
delay
void main()
TMR2ON = 1;
while(1)
PORTD = value;
Output:
Hardware:
Conclusion:
The purpose of this lab is to learn about the timer2, that Timer is used to measure the time or
generate an accurate time delay. It is an important application in an embedded system. its
working and specifications of timers. Also study about the how to use timers. We successfully
implemented our code onto MP-lab for the timer2 as a timer with pic16f877a and make circuit to
blink led with precise delay.
.END.