















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















An n-bit Timer/Counter divides its input clock frequency by , Where n is no. of bits of timer
An n-bit Timer/Counter counts from 0 to
When a Timer/Counter reaches its maximum value it simply overflows to zero on the next clock
pulse
The overflow of the timer is indicated by an overflow flag
The overflow flag is usually automatically SET by the hardware on overflow and its has to
cleared in software
PIC16F877A has three independent timers
๏ Timer-0 (8-bit Timer)
๏ Timer-1 (16-bit Timer)
๏ Timer-2 (8-bit Timer)
๏ Suppose the microcontroller is clocked by a 20 MHz clock, thus for Timer-
The can also be called as overflows/sec
๏ if we apply an input clock of 5 MHz then the output clock will be approximately 19.53 KHz
๏ In other words the Counter will overflow 19531.25 times in one second
๏ Since Timer-0 overflow is indicated by an overflow flag therefore we can count the number of
overflows and thus generate a precise one second.
๏ This can be accomplished by constantly checking the Timer-0 Overflow flag, if the Overflow Flag
becomes HIGH then we clear the overflow flag and then increment a variable.
๏ When the incremented variable reaches a value equal to the calculated overflows/sec then this means
that exactly one second has passed
Mathematical Calculations
Has Timer
Overflowed
Initialize Timer-
Increment Overflow Variable
Is Variable
=
Overflows/s
ec
Clear Overflow Flag
Clear Overflow Variable
Execute any desired
function
(Remember this
function will be
executed every second)
Prescaler
If we use a prescaler of 1:16 with the same oscillator frequency, then
Overflows have reduced because of the prescaler
Other division ratios can be selected to further divide these overflows
OPTION Register
Clock Source
The external clock signal be programmed for
Rising OR Falling Edges
If we SET this bit then the clock source is External
If we CLEAR this bit, then the clock source is Internal
Prescaler Configuration
The prescaler provides a pre-divided clock to Timer-0 or the Watch Dog Timer (WDT)
The prescaler is either connected to the WDT timer or to Timer-
The PSA (Prescaler Assignment) bit decides whether the prescaler is connected to Timer-0 or
the WDT
Prescaler
8-bit Timer/Counter
Watch Dog Timer
(WDT)
PSA = 1
PSA = 0
๐ถ๐๐ ๐๐ข๐ก
=
๐ถ๐๐ ๐๐
256
๐๐๐ ๐๐๐ด = 1
๐ถ๐๐ ๐๐ข๐ก
=
๐ถ๐๐ ๐๐
( 256 ) (^) ร ( ๐๐๐๐ ๐๐๐๐๐ ๐ ๐๐ก๐๐ )
๐๐๐ ๐๐๐ด = 1
Prescaler Configuration โ Division Ratio
If the prescaler is assigned to Timer-0 then we have to configure the prescaler division ratio as
well
The first three bits in the OPTION register decide the prescaler division ratio
๏ Clock Source Internal
(T0CS = 0)
๏ Precaler connected with
Timer-0 (PSA = 0)
๏ Clock Source External
(T0CS = 1)
๏ Precaler connected with
WDT (PSA = 1)
Timer-0 Configuration
Timer-0 Configuration