You are on page 1of 8

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

Microprocessors and Microcontrollers Lab


Lab # 08
Title: To study and configure timer 1 to generate precise delays 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#08 Date: 09/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 1 .

The main features of Timer 1 is given below:

 16-bit timer/counter. It consists of two 8 bit (8+8) registers (TMR1H, TMR1L)


 Readable and writable
 Internal or External clock select.
 1,2,4,8 programmable Prescaler
 External can be syn. or asyn.
 Interrupt on overflow from FFFFh to 0000h
 Second Crystal Permitted

 TIMER 1 REGISTERS
Timer 1 has a register called TMR1 register, which is 16 bits of size. The below table shows the
registers associated with PIC16f877A Timer 1 module.

Saif Tariq UW-19-MTS-BSC-003


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

 Structure of T1CON :

 Bit 7 and Bit 6 (unimplemented) : Read as ‘0’


 T1CKPS1:T1CKPS0 (Timer1 Input Clock Prescale Select bits)
11 = 1:8 prescale value
10 = 1:4 prescale value
01 = 1:2 prescale value
00 = 1:1 prescale value
 T1OSCEN (Timer 1 Oscillator Enable Control bit)
1-Oscillator is enabled
0-Oscillator is shut-off
 T1SYNC (Timer 1 External Clock Input Synchronization Control bit)
1-Do not synchronize external clock input
0-Synchronize external clock input
 TMR1CS (Timer 1 Clock Source Select bit)
1-External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
0-Internal clock (FOSC/4)
 TMR1ON (Timer 1 On bit)
1-Enables Timer 1
0-Stops Timer 1

. Work Flow of Timer 1 .

Saif Tariq UW-19-MTS-BSC-003


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

1. Timer 1 clock source(TMR1CS) bit is used to select the clock source.


2. If TMR1CS becomes 0, then the internal clock is given to the input of the timer peripheral.
If we programmed on to 1, it will get the clock source from the external source. That
external source can be T1CKI or a crystal oscillator.
3. When it can be a crystal oscillator, then T1OSC is logic one if it is enabled it will get the
clock pulse from the crystal oscillator. If it is disabled it will get the clock pulse directly
from the T1CKI pin.
4. Prescaler bit is used to divide the incoming clock any one of the factors(1,2,4,8)
5. T1SYNC bit is logic 1 it will not be synchronized. The external clock will not be
synchronized with the internal oscillator clock. If TISYNC is 0, it will be synchronized and
it will be given to the AND gate.
6. Another bit of the AND gate is TMR1 ON bit. If TMRI becomes 1 then the clock moves to
the TMR1 register and it will become 0 the AND gate also becomes 0(TMR1 will not
counting).

. Delay Calculation .
Example: Given that a time delay of 100 ms is to be generated and a 20MHz crystal
oscillator is connected with PIC.
The timer is related to the internal frequency which is always Fosc/4.

Clock source frequency (Crystal)


Fosc = 20 MHz = 20000000 Hz

Therefore, Timer frequency :

Saif Tariq UW-19-MTS-BSC-003


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

Ftimer = Fosc / 4 = 20000000 / 4 = 5000000 Hz = 5 MHz

If Prescaler =1:8, then

Ftimer= 5000000 / 256 = 19531.25 Hz

So, the TMRO register value

= 65536-((Delay * Fosc)/(Prescalar4))

= 65536-((100ms * 20Mhz)/(8*4))

= 3036

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.
6. You will get the result of integration between software.

Task:
Write a code to make a counter with timer 0 using pic16F877A.

Code:
#include <xc.h>

#include <stdio.h>

#include <stdlib.h>

#define _XTAL_FREQ 20000000 //define crystal frequency to 20MHz

__PROG_CONFIG(1,0x3F32);

char value = 0;

#define SBIT_PS1 5

Saif Tariq UW-19-MTS-BSC-003


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

#define SBIT_PS0 4

void interrupt timer_isr()

if(TMR1IF==1)

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

TMR1H=0x0B; // Load the time value(0xBDC) for 100ms delay

TMR1H=0x0B; // Load the time value(0xBDC) for 100ms delay

TMR1L=0xDC;

TMR1IF=0; // Clear timer interrupt flag

void main()

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

T1CON = (1<<SBIT_PS1) | (1<<SBIT_PS0); // Timer0 with external freq and 8 as prescalar

TMR1H=0x0B; // Load the time value(0xBDC) for 100ms delay

TMR1L=0xDC;

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

GIE=1; //Enable Global Interrupt

PEIE=1; //Enable the Peripheral Interrupt

TMR1ON = 1; //Start Timer1

while(1)

PORTD = value;

Saif Tariq UW-19-MTS-BSC-003


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

Output:

Hardware:

Saif Tariq UW-19-MTS-BSC-003


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

Conclusion:
The purpose of this lab is to learn about the timer1, its working and specifications of timers. Also
study about the how to use timers. We successfully implemented our code onto MP-lab for the
timer0 as a timer with pic16f877a and make a circuit on the proteus to blink a led with a delay.

.END.

Saif Tariq UW-19-MTS-BSC-003

You might also like