MODULE IV
TIMER MODULES IN PIC 16F877A
TIMER0 module Block Diagram - Timer Calculation and TMR0
Register – Configuring the TIMER0 module using the INTCON
and OPTION registers – TIMER1 module Block Diagram -
Timer Calculation and TMR1H:TMR1L Register - Register
Configuring the TIMER1using T1CON, PIR1 and PIE1control
registers – TIMER2 module Block Diagram - Timer Calculation
and TMR2 Register - Register Configuring the TIMER2 using
T2CON, PIR1 and PIE1control registers - sample codes to
generate time delay.
What is a timer?
• 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
TIMER
• 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.
PIC Timer Module
Pic16f877a has three independent timer which can be
used as timer, Counters or for PWM generation.
Timer Size Control Count
Register Register
TIMER0 8-bit OPTION_ TMR0
REG
TIMER1 16-bit T1CON TMR1H,
TMR1L
TIMER2 8-bit T2CON TMR2
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 0xFFh to 0x00h.
Edge select for external clock.
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 This registers is used to configure the TIMER0
_REG Prescaler, Clock Source etc
This register will hold the count value. When this
TMR0 register overflows (FF to 00) then an interrupt will be
generated.
This register contains the Timer0 overflow
INTCON flag(TMR0IF) and corresponding Interrupt 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
INTEDG: NA 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 Timer0
PS2:PS0: Prescaler Rate Select bits
A watchdog timer (WDT) is a timer that monitors microcontroller
(MCU) programs to see if they are out of control or have stopped
operating.
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
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: NA for Timers
RBIE: NA for Timers
TMR0IF: TMR0 Overflow Interrupt Flag bit
1-TMR0 register has overflowed (must be cleared in
software)
0-TMR0 register did not overflow
INTF: NA for Timers
RBIF: NA for Timers
Work Flow of TIMER 0
Timer 0 can be operated both as a timer and counter.
The Timer 0 stores the value TMR0 registers
All the parts to the left are the clock source circuits.
T0CON (Timer 0 Control) register is used to program the timer
and includes the bits shown in figure (T0CS, PSA etc.)
The clock source can be internal or external and is controlled by
T0CS bit.
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.
The prescaler is a circuit that divides the signal frequency by 2, 4,
8, 16, …, 256.
The prescaler is activated by bit PSA. Prescaler value is written to
TMR0 register.
The block diagram of timer-0 module is given in the figure above.
The timer mode is normally selected by clearing the T0CS bit in
the register. T0CS = 0
In Timer mode, when the Timer 0 Module increases with every
instruction cycle, the TMR0 register is written, the increment is
inhibited for the following two instruction cycles.
The user can work around this by writing an adjusted value to the
TMR0 register.
Counter mode is selected by setting bit T0CS (T0CS=1) in Counter
mode.
Timer 0 will increment either on every rising or falling edge of
pin RA4/T0CKI.
The incrementing edge is determined by the Timer 0 Source Edge
Select bit, T0SE. Clearing bit T0SE selects the rising edge.
The pre-scaler is mutually exclusively shared between the Timer0
module and the Watchdog Timer.
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.
Formula for all the three Timers.
Timer Size Formula for delay calculation
TIMER0 8-bit RegValue = 256-((Delay * Fosc)/(Prescaler*4))
TIMER1 16-bit RegValue = 65536-((Delay * Fosc)/(Prescaler*4))
TIMER2 8-bit RegValue = 256-((Delay * Fosc)/(Prescaler*4))
• 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.
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)/(Prescalar*4))
= 256-((1ms * 20Mhz)/(32*4))
= 256-156=100
The values are reloaded again to start timer
for same delay. Before filling this value timer
registers should be configured.
A simple program with 1 ms delay using Timer 0.
Timer 1
The timer TMR1 module is an 16-bit timer/counter with
the following features:
16-bit timer/counter with two 8-Bit register
TMR1H/TMR1L Readable and writable.
Software programmable prescaler upto 1:8
Internal or external clock select.
Interrupt on overflow from FFFFh to 0000h.
Edge select for external clock.
CODING FOR TIMER 0:
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
T1OSCEN: Timer1 Oscillator Enable Control bit
1-Oscillator is enabled
0-Oscillator is shut-off
T1SYNC: Timer1 External Clock Input Synchronization Control bit
1-Do not synchronize external clock input
0-Synchronize external clock input
TMR1CS: Timer1 Clock Source Select bit
1-External clock from pin RC0/T1OSO/T1CKI (on the rising edge)
0-Internal clock (FOSC/4)
TMR1ON: Timer1 On bit
1-Enables Timer1
0-Stops Timer1
PIE1 Register
The PIE1 register contains the individual enable bits for
the peripheral interrupts.
PSPIE: Parallel Slave Port Read/Write Interrupt Enable
bit(1)
1 = Enables the PSP read/write interrupt
0 = Disables the PSP read/write interrupt
Note (1): PSPIE is reserved on PIC16F873A/876A devices; always
maintain this bit clear.
ADIE: A/D Converter Interrupt Enable bit
1 = Enables the A/D converter interrupt
0 = Disables the A/D converter interrupt
RCIE: USART Receive Interrupt Enable bit
1 = Enables the USART to receive interrupt
0 = Disables the USART receive interrupt
TXIE: USART Transmit Interrupt Enable bit
1 = Enables the USART to transmit interrupt
0 = Disables the USART transmit interrupt
SSPIE: Synchronous Serial Port Interrupt Enable bit
1 = Enables the SSP interrupt
0 = Disables the SSP interrupt
CCP1IE: CCP1 Interrupt Enable bit
1 = Enables the CCP1 interrupt
0 = Disables the CCP1 interrupt
TMR2IE: TMR2 to PR2 Match Interrupt Enable bit
1 = Enables the TMR2 to PR2 match interrupt
0 = Disables the TMR2 to PR2 match interrupt
TMR1IE: TMR1 Overflow Interrupt Enable bit
1 = Enables the TMR1 overflow interrupt
0 = Disables the TMR1 overflow interrupt
PIR1 Register
The PIR1 register contains the individual flag bits for the
peripheral interrupts.
Note: Interrupt flag bits are set when an interrupt condition
occurs regardless of the state of its corresponding enable bit or
the global enable bit, GIE (INTCON<7>). User software should
ensure the appropriate interrupt bits are clear prior to enabling
an interrupt.
PSPIF: Parallel Slave Port Read/Write Interrupt Flag bit(1)
1 = A read or a write operation has taken place (must be cleared in
software)
0 = No read or write has occurred
Note (1): PSPIF is reserved on PIC16F873A/876A devices; always
maintain this bit clear.
ADIF: A/D Converter Interrupt Flag bit
1 = An A/D conversion completed
0 = The A/D conversion is not complete
RCIF: USART Receive Interrupt Flag bit
1 = The USART receive buffer is full
0 = The USART receive buffer is empty
TXIF: USART Transmit Interrupt Flag bit
1 = The USART transmit buffer is empty
0 = The USART transmit buffer is full
SSPIF: Synchronous Serial Port (SSP) Interrupt Flag bit
CCP1IF: CCP1 Interrupt Flag bit
TMR2IF: TMR2 to PR2 Match Interrupt Flag bit
1 = TMR2 to PR2 match occurred (must be cleared in software)
0 = No TMR2 to PR2 match occurred
TMR1IF: TMR1 Overflow Interrupt Flag bit
1 = TMR1 register overflowed (must be cleared in software)
0 = TMR1 register did not overflow
EEIE: EEPROM Write Operation Interrupt Enable bit
1 = Enable EEPROM write interrupt
0 = Disable EEPROM write interrupt
BCLIE: Bus Collision Interrupt Enable bit
1 = Enable bus collision interrupt
0 = Disable bus collision interrupt
CCP2IE: CCP2 Interrupt Enable bit
1 = Enables the CCP2 interrupt
0 = Disables the CCP2 interrupt
Timer 1 clock source(TMR1CS) bit is used to select the clock source.
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.
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.
Prescaler bit is used to divide the incoming clock any one of the
factors(1,2,4,8)
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.
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
Time delay is calculated by the timer 1 .
Example: Given that a time delay of 100 ms is to be generated and a
20MHz crystal oscillator is connected with PIC. Find the value to be
loaded in TMR1H and TMR1L.
the TMR1 register value
= 65536-((Delay * Fosc)/(Prescalar*4))
= 65536-((100ms * 20Mhz)/(8*4))
= 3036
• Example of configuring Timer 1 to generate an
interrupt every 1 second at a 4 MHz clock
speed.
• the TMR1 register value
• = 65536-((Delay * Fosc)/(Prescalar*4))
• = 65536-((1s * 4Mhz)/(8*4))
• =
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 up to 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
TMR2 This register holds the timer count value which will be incremented
depending on prescalar configuration
PIR1 This register contains the Timer2 overflow flag(TMR2IF).
PIE1 This register contains the Timer2 Interrupt Enable flag(TMR2IE).
PR2 The register in which the final or the maximum count value is
written.
T2CON Register
TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits
0000 = 1:1 postscale
0001 = 1:2 postscale
0010 = 1:3 postscale
• Postscaler: A circuit that slows the
• rate of the interrupt generation(or WDT
• reset)from a counter/timer by dividing
1111 = 1:16 postscale it down.
TMR2ON: Timer2 On bit
1-Timer2 is on
0-Timer2 is off
T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits
00 = Prescaler is 1
01 = Prescaler is 4
1x = Prescaler is 16
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)
Then this clock frequency is given to the TMR2 register, it
will count the incoming clock pulse.
As it is counting the incoming clock pulses the value
present in the TMR2 register is also compared with PR2
register.
If TMR2 and PR2 register are equal, the output of the
comparator will reset the TMR2 register.
Then this equal output is given to the input of the
Postscaler.
It is not generated an interrupt immediately.
When the Postscaler bit obtains any of the value, it will
generate some delay and TMR2 Interrupt Flag bit set.
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))$$
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.
Below table provides the formula for all the three Timers.
Timer Size Formula for delay calculation
TIMER0 8-bit $$RegValue = 256-((Delay * Fosc)/(Prescalar*4))$$
TIMER1 16-bit $$RegValue = 65536-((Delay * Fosc)/(Prescalar*4))$$
TIMER2 8-bit $$RegValue = 256-((Delay * Fosc)/(Prescalar*4))$$
ow to Program Timer-0 as Counter in PIC16F877A
Oscillator Configurations
• The Oscillator provides the required clock signal for the operation of
the microcontroller.
• The pic16f877a has four oscillator modes namely LP, XT, HS, and RC.
• The modes LP, XT, HS utilizes a crystal or a ceramic resonator and it is
connected to the clock input pins to generate the required
oscillations.
• The LP mode is designed to drive 32KHZ Crystal.
• The XT mode is selected for the crystals in the range of 4MHZ or
lower and in the case of HS mode the crystal range is above 4MHZ.
• The RC Mode is selected when the RC network is used to create the
clock input for the MCU. The oscillation frequency accuracy is less in
RC Network. It can be used in time-insensitive applications for cost-
saving.
Watchdog Timer (WDT)
• Watchdog timer detects when the microcontroller is stuck in an endless
loop.
• It is a free-running counter that generates devices reset when the WDT
Time out occurs.
• In the sleep mode operation, WDT time out wake up the device to
continue its operation.
• The program writes zero every time.
• Watchdog timer detects the software malfunctioning and resets the
controller.
• watchdog timer counts from an initial value set by the controller to the
zero value. WDT Before reaching zero.
• In the software malfunctioning events,
• The embedded software restarts the counter reaches zero and it generates
the reset signal.
Brown-out Reset (BOR)
• Brown out reset allows the microcontroller to reset
when the supply voltage drops below a specific voltage.
• IF the VDD falls below VBOR(~4V) For a duration of
TBOR (100 µS) it reset the device.
• The device will remain in a reset state in the BROWN out
reset case until the VDD again rises above VBOR and
after that, the Power-up Timer keeps the device in reset
for a time duration of TPWRT ( 72 mS).
• The device which falls below VBOR In the TPWRT
initiates BROWN out restart when VDD rises above
VBOR with the Power-up Timer Reset. Regardless of the
state of the PWRT configuration bit, the power-up timer
is enabled with the Brown-out Reset activation.