Timer 8051
Timer 8051
A:
TMOD = 00000001, mode 1 of Timer 0 is selected.
TMOD = 00100000, mode 2 of Timer 1 is selected.
TMOD = 00010010, mode 2 of Timer 0, and mode 1 of
Timer 1 are selected.
Clock Source for Timer
Every timer needs a clock pulse to tick.
What is the source of the clock pulse for the 8051 timers?
If C T = 0. the crystal frequency attached to the 8051 is the
source of the clock for the timer.
This means that the size of the crystal frequency attached to
the 8051 also decides the speed at which the 8051 timer
ticks.
The frequency for the timer is always 1/12th the frequency
of the crystal attached to the 8051, regardless of the
Machine Cycle time.
Example
GATE of TMOD Register
What is its purpose of GATE bit of TMOD register?
Every timer has a means of starting and stopping. Some timers
do this by software, some by hardware, and some have both
software and hardware controls.
The timers in the 8051 have both.
The start and stop of the timer are controlled by way of
software by the TR (timer start) bits TR0 and TR1 of TCON
register.
This is achieved by the instructions “SETB TR1″ and “CLR
TR1″ for Timer 1, and “SETB TR0” and “CLR TR0” for Timer
0.
The SETB instruction starts it, and it is stopped by the CLR
instruction.
GATE of TMOD Register Cont.
These instructions start and stop the timers as long as GATE =
0 in the TMOD register.
The hardware way of starting and stopping the timer by an
external source is achieved by making GATE = 1 in the
TMOD register.
However, to avoid further confusion for now, we will make
GATE = 0, meaning that no external hardware is needed to
start and stop the timers.
In using software to start and stop the timer where GATE = 0.
all we need are the instructions “SETB TRx” and “CLR TRx”.
The use of external hardware to stop or start the timer will be
discussed when interrupts would be discussed.
TCON Register
How are timers 0 and 1 started and stopped by
instructions?
The timers are started by using instructions to set timer
start bits TR0 and TR1, which are called timer run
control bits.
Timers can be cleared/stopped by clearing these bits.
When a counter counts to its maximum value, it sets a
flag TF0 or TF1.
TCON Register Cont.
The lower four bits of TCON cater to interrupt functions,
but the upper four bits are for timer operations.
MSB LSB
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
1. TMOD is loaded.
2. FFF2H is loaded into TH0 – TL0
3. P1.5 is toggled for the high and low portions of the pulse.
4. The DELAY subroutine using the timer is called.
5. In the DELAY subroutine, Timer 0 is started by the “SETB
TR0” instruction.
Example Solution Cont.
6. Timer 0 counts up with the passing of each clock, which is
provided by the crystal oscillator. As the timer counts up, it
goes through the states of FFF3, FFF4, FFF5, FFF6, FFF7,
FFF8, FFF9, FFFA, FFFB, and so on until it reaches FFFFH.
One more clock rolls it to 0, raising the timer flag (TF0 =
1). At that point, the JNB instruction falls through.
Solution
The timer works with a clock frequency of 1/12 of the XTAL frequency;
therefore, we have 11.0592 MHz / 12 = 921.6 kHz as the timer
frequency. As a result, each clock has a period of T = 1 / 921.6 kHz =
1.085 us. In other words, Timer 0 counts up each 1.085 us resulting in
delay = number of counts x 1.085 us.
The number of counts for the rollover is FFFFH – FFF2H = 0DH (13
decimal). However, we add one to 13 because of the extra clock needed
when it rolls over from FFFF to 0 and raises the TF flag. This gives 14 x
1.085 us = 15.19 us for half the pulse.
For the entire period T = 2 x 15.19 us = 30.38 us gives us the time delay
generated by the timer.
Timer Delay Calculation for XTAL = 11.0592 MHz
We can develop a formula for delay calculations using mode 1
(16-bit) of the timer for a crystal frequency of XTAL =
11.0592 MHz.
(a) In HEX
[(FFFF –YYXX + 1)]10 x 1.085 us where YYXX are TH, TL
initial values respectively.
(b) In Decimal
A:
Example Solution Cont.