PIC16f877a Timer
PIC16f877a Timer
Contents
• 1 Timer Basics
• 2 PIC Timer Module
• 3 Timer Calculation
• 4 Timer 0
♦ 4.1 Timer0 Registers
♦ 4.2 Code
• 5 Timer 1
♦ 5.1 Timer1 Registers
♦ 5.2 Code
• 6 Timer 2
♦ 6.1 Timer2 Registers
♦ 6.2 Code
• 7 Downloads
Timer Basics
As the name suggests these are used to measure the time or generate the accurate time delay. The
microcontroller can also generate/measure the required time delays by running loops, but the
timer/counter relieves the CPU from that redundant and repetitive task, allowing it to allocate
maximum processing time for other tasks.
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.
PIC Timer Module
Pic16f877a has three indepenndent timer which can be used as timer,Counters or for PWM
generation.
Below table provides the details of the three Timers.
Timer Size Control Register Count Register Min Delay Max Delay
TIMER0 8-bit OPTION_REG TMR0 0.2usec 13.107ms
TIMER1 16-bit T1CON TMR1H,TMR1L 0.2usec 104.857ms
TIMER2 8-bit T2CON TMR2 0.2usec 819usec
Timer Calculation
PIC Oscillator frequency is divided by 4 and then fed to the controller, Now this this freq can be
further divided by presacalar to generate the range of delays.
Time to increment the Timer count by one(timer tick) can be determined as below.
tick = (Prescalar/(Fosc/4)
tick = (Prescalar/(20Mhz/4))
$$tick = (Prescalar * 4)/Fosc$$
Now the Timer value for the required delay can be calculated as below.
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/((Prescalar *4)/Fosc))
$$RegValue = TimerMax-((Delay * Fosc)/(Prescalar*4))$$
Below table provides the formula for all the three Timers.
Timer 0
The TMR0 module is an 8-bit timer/counter with the following features:
• 8-bit timer/counter
• Readable and writable
• 8-bit software programmable prescaler
• Internal or external clock select
• Interrupt on overflow from FFh to 00h
• Edge select for external clock
Timer0 Registers
The below table shows the registers associated with PIC16f877A Timer0 module.
Register Description
OPTION_REG This registers is used to configure the TIMER0 Prescalar, Clock Source etc
This register holds the timer count value which will be incremented depending on
TMR0 prescalar configuration
This register contains the Timer0 overflow flag(TMR0IF) and corresponding Inetrrupt
INTCON Enable flag(TMR0IE).
OPTION_REG
7 6 5 4 3 2 1 0
RBPU INTEDG T0CS T0SE PSA PS2 PS1 PS0
RBPU: NA for Timers
• Note:There is only one prescaler available which is mutually exclusively shared between the
Timer0 module and the Watchdog Timer. A prescaler assignment for the Timer0 module
means that there is no prescaler for the Watchdog Timer and vice versa. This prescaler is not
accessible but can be configured using PS2:PS0 bits of OPTION_REG.
INTCON
7 6 5 4 3 2 1 0
GIE PIE TMR0IE INTE RBIE TMR0IF INTF RBIF
GIE: Global Interrupt Enable bit
1-Enables all unmasked interrupts
0-Disables all interrupts
Code
Below are the steps for configuring and using the Timer0 for delay generation:
Below is the sample code to blink the LEDs with 1ms delay. Delay measured using CRO:
Timer 1
The timer TMR1 module is an 16-bit timer/counter with the following features:
Timer1 Registers
The below table shows the registers associated with PIC16f877A Timer1 module.
Register Description
T1CON: This registers is used to configure the TIMER1 Prescalar, Clock Source etc
TMRIH This register holds the higher 8-bits of timer value. TMR1H and TMR1L are used in pair to
increment from 0000 - FFFFh
TMRIL This register holds the lower 8-bits of timer value. TMR1H and TMR1L are used in pair to
increment from 0000 - FFFFh
PIR1 This register contains the Timer1 overflow flag(TMR1IF).
PIE1 This register contains the Timer1 Interrupt Enable flag(TMR1IE).
T1CON
7 6 5 4 3 2 1 0
? ? T1CKPS1 T1CKPS0 T1OSCEN T1SYNC TMR1CS TMR1ON
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
Code
Below are the steps for configuring and using the Timer1 for delay generation:
Below is the sample code to blink the LEDs with 100ms delay. Delay measured using CRO:
Timer 2
The TImer2 module is an 8-bit timer/counter with the following features:
• 8-bit timer/counter
• Readable and writable
• Software programmable prescaler/PostScaler upto 1:16
• Interrupt on overflow from FFh to 00h
Timer2 Registers
The below table shows the registers associated with PIC16f877A Timer0 module.
Register Description
T2CON This registers is used to configure the TIMER2 Prescalar, Clock Source etc
This register holds the timer count value which will be incremented depending on prescalar
TMR2
configuration
PIR1 This register contains the Timer2 overflow flag(TMR2IF).
PIE1 This register contains the Timer2 Interrupt Enable flag(TMR2IE).
T2CON
7 6 5 4 3 2 1 0
? TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits
0000 = 1:1 postscale
0001 = 1:2 postscale
0010 = 1:3 postscale
?
?
?
1111 = 1:16 postscale
Now 500us can be generated using timers which will be used to increment a counter 2000 times to
get 1sec delay.
Delay Calculations for 500usec @20Mhz with Prescalar as 16:
RegValue = 256-(Delay * Fosc)/(Prescalar*4)) = 256-((500us* 20Mhz)/(16*4)) = 256-156=100
Code
Below are the steps for configuring and using the Timer2 for delay generation:
Below is the sample code to blink the LEDs with 1sec(500us x2000times) delay. Delay measured
using CRO:
Downloads
Download the complete project folder from the below link:
Hardware design Files and Code Library
Have a opinion, suggestion , question or feedback about the article let it out here!