Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

Microprocessors and Microcontrollers Lab


Lab # 07
Title: Design a circuit to blink led precise delay using timer0 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.

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.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

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

The main features of Timer 0 is given below:

 8-bit timer/counter with prescaler.


 Readable and Writable
 Internal or external Clock set
 Build 8-bit software programmable prescaler.
 Edge select for external clock
 Interrupt on overflow from 0XFF to 0X00

 Timer 0 Registers
Timer 0 has a register called TMR0 Register, which is 8 bits of size. The below table shows the
registers associated with PIC16f877A Timer 0 module.

Register Description
OPTION_RE This registers is used to configure the TIMER0 Prescalar, Clock Source etc
G
TMR0 This register will hold the count value.When this register overflows (FF to 00)
then an interrupt will be generated.

INTCON This register contains the Timer0 overflow flag(TMR0IF) and corresponding
Interrupt Enable flag(TMR0IE).

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

 Structure of OPTION_REG :

 RBPU: Not Applicable for Timers


 INTEDG: Not Applicable for Timers
 T0CS (TMR0 Clock Source Select bit)
1-Transition on T0CKI pin
0-Internal instruction cycle clock (CLKO)
 T0SE (TMR0 Source Edge Select bit)
1-Increment on high-to-low transition on T0CKI pin
0-Increment on low-to-high transition on T0CKI pin
 PSA (Prescaler Assignment bit)
1-Prescaler is assigned to the WDT
0-Prescaler is assigned to the Timer 0
 PS2:PS0 (Prescaler Rate Select bits)

 Structure of INTCON :

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

 GIE (Global Interrupt Enable bit)


1-Enables all unmasked interrupts
0-Disables all interrupts
 PIE (Peripheral Interrupt Enable bit)
1-Enables all unmasked peripheral interrupts
0-Disables all peripheral interrupts
 TMR0IE (TMR0 Overflow Interrupt Enable bit)
1-Enables the TMR0 interrupt
0-Disables the TMR0 interrupt
 INTE (RB0/INT External Interrupt Enable Bit)
1-Enables the RB0/INT external interrupt
0-Disables the RB0/INT external interrupt
 RBIE(RB Port Change Interrupt Enable Bit)
1-Enables the RB port change interrupt
0-Disables the RB port change interrupt
 TMR0IF (TMR0 Overflow Interrupt Flag bit)
1-TMR0 register has overflowed (must be cleared in software)
0-TMR0 register did not overflow
 INTF (RB0/INT External Interrupt Flag Bit)
1-RB0/INT External interrupt occurred(must be cleared in software))
0-RB0/INT External interrupt did not occur
 RBIF (RB Port Change Interrupt Flag Bit)
1-At least one of the RB7 – RB4 pins changed state; a mismatch condition will
continue to set line bit. Reading PORT B will end the mismatch condition and allow
the bit to be cleared(must be cleared in software)
0-None of the RB7 – RB4 pins have changed state.

. Work Flow of TIMER 0 .


Working process to implement Timer 0

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

1. Timer 0 can be operated both as a timer and counter.


2. The Timer 0 stores the value TMR0 registers
3. All the parts to the left are the clock source circuits.
4. T0CON (Timer 0 Control) register is used to program the timer and includes the bits
shown in figure (T0CS, PSA etc.)
5. The clock source can be internal or external and is controlled by T0CS bit.
6. In some cases, the clock coming from the oscillator could be too fast for our
applications: we can lower it by using the frequency prescaler.
7. The prescaler is a circuit that divides the signal frequency by 2, 4, 8, 16, …, 256.
8. The prescaler is activated by bit PSA. Prescaler value is written to TMR0 register.

. Delay Calculation .
It is important to learn how time delay is calculated by the timer since the exact delay generation
is the most common application of timer.

Example: Given that a time delay of 1 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.

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

Clock source frequency (Crystal)


Fosc = 20 MHz = 20000000 Hz

Therefore, Timer frequency :

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

If Prescaler =1:32, then

Ftimer= 5000000 / 256 = 19531.25 Hz

So, the TMRO register value

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

= 256-((1ms * 20Mhz)/(32*4))

= 256-156=100

Procedure:
I. Open the MPLAB X IDE and create a new project by clicking the file option on the upward left
corner and selecting new project.
II. Then select the XC 8 compiler and write the project name and create your object.
III. Select the source file and create the extension file for the project to write your code.
IV. Design the circuit on proteus using PIC16F877A.
V. Then choose the hex file by clicking on microcontroller component on proteus.
VI. You will get the result of integration between software.

MP Lab Code:

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

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

unsigned char sec=0,sec10=0,count=0;


void main ()
{
TRISB=0x00;
PORTB=0x00;
TMR0=0;
T1CON=0X31;
while(1)
{
count=TMR0;
sec10=count/10;
sec=count%10;
PORTB=sec;
RB4=1;
PORTB=sec10;
RB5=1;
if(count>59)
{
TMR0=0;
}
}
}

Output:

Saif Tariq UW-19-MTS-BSC-003


Microprocessors and Microcontrollers Lab Lab#07 Date:02/11/2021

Hardware:

Conclusion:
The purpose of this lab is to learn about the timer0, 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 timer0 as a counter with pic16f877a and make two
digit counter in proteus using two seven segments.

Saif Tariq UW-19-MTS-BSC-003

You might also like