You are on page 1of 8

Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

Microprocessors and Microcontrollers Lab


Lab # 09
Title: Design a circuit to blink led precise delay using timer2 with PIC 16F877A .

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.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

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.

 PIC16F877A timer modules


The PIC16F877A basically has three timer modules. These timer module terminals are also
multiplexed with other functions for handling alternate functions. These three-timer modules as
named as TIMER 0, TIMER 1 and TIMER 2. These modules help to perform various timer,
Counter or PWM Generation.

. TIMER 2 .

The main features of Timer 0 is given below:

 Two 8-bit registers (TMR2 and PR2)


 Readable and Writable
 A Prescaler and a Postscaler
 Connected only to an internal clock – 4 MHz crystal
 Interrupt on overflow

 TIMER 2 REGISTERS
PIC16F877A Timer 2 module registers are shown below:

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

 Structure of T2CON :

 TOUTPS3:TOUTPS0 (Timer 2 Output Postscale Select bits)


0000 = 1:1 postscale
0001 = 1:2 postscale
0010 = 1:3 postscale


1111 = 1:16 postscale
 TMR2ON (Timer 2 On bit)
1-Timer 2 is on
0-Timer 2 is off
 T2CKPS1:T2CKPS0 (Timer 2 Clock Prescale Select bits)
00 = Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16

. Work Flow of Timer 2 .

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)

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

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:

Delay = 256 * (Prescaler*4)/Fosc

= 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.

Delay Calculations for 500usec @20Mhz with Prescaler as 16:

Reg Value = 256-(Delay * Fosc)/(Prescalar*4))

= 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.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

6. You will get the result of integration between software.

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 PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)

#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)

#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming


Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)

#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;

void interrupt timer_isr()

if(TMR2IF==1)

TMR2 = 101; /*Load the timer Value, (Note: Timervalue is 101 instead of 100 as
the

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

Timer2 needs two instruction Cycles to start incrementing TMR2 */

TMR2IF=0; // Clear timer interrupt flag

if(count>=2000) //500us * 2000=1000000us=1sec

count=0;

value=~value; // complement the value for blinking the LEDs

else

count++; // Keep incrementing the count till it reaches 2000 to generate 1sec
delay

void main()

TRISD=0x00; //COnfigure PORTD as output to blink the LEDs

T2CON = (1<<SBIT_T2CKPS1); // Timer2 with external freq and 16 as prescalar

TMR2=100; // Load the timer value for 500us delay

TMR2IE=1; //Enable timer interrupt bit in PIE1 register

GIE=1; //Enable Global Interrupt

PEIE=1; //Enable the Peripheral Interrupt

TMR2ON = 1;

while(1)

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

PORTD = value;

Output:

Hardware:

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#09 Date: 16/11/2021

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.

Saif Tariq UW-19-MTS-BSC-003

You might also like