Part 1MODULE 3 Merged
Part 1MODULE 3 Merged
Timer in 8051 is used as timer, counter and baud rate generator. Timer always counts up irrespective
of whether it is used as timer, counter, or baud rate generator: Timer is always incremented by the
microcontroller. The time taken to count one digit up is based on master clock frequency.
MODE 1 PROGRAMMING
• The following are the characteristics and operations of mode1:
1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the
timer’s register TL and TH .
2. After TH and TL are loaded with a 16-bit initial value, the timer must be started ƒ
This is done by “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
3. After the timer is started, it starts to count up. ƒ It counts up until it reaches its limit of
FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF
(timer flag) – Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1
– This timer flag can be monitored ƒ When this timer flag is raised, one option would
be to stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and
timer 1, respectively.
4. After the timer reaches its limit and rolls over, in order to repeat the process TH and
TL must be reloaded with the original value, and TF must be reloaded to 0.
Example 4-3
In the following program, we create a square wave of 50% duty cycle (with equal
portions high and low) on the P1.5 bit. Timer 0 is used to generate the time delay.
Analyze the program
Example 4-4
In Example 9-4, calculate the amount of time delay in the DELAY subroutine generated by
the timer. Assume XTAL = 11.0592 MHz.
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.6kHz = 1.085us. In other words, Timer 0 counts up each 1.085 us resulting
in delay = number of counts × 1.085us.
The number of counts for the roll over is FFFFH – FFF2H = 0DH (13decimal).
However, we add one to 13 because of the extra clock needed when it rolls over from
FFFF to 0 and raise the TF flag. This gives 14× 1.085us = 15.19us for half the pulse.
For the entire period it is T = 2× 15.19us = 30.38us as the time delay generated by
the timer.
MODE 2 PROGRAMMING
The following are the characteristics and operations of mode 2:
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is
to be used, and the timer mode (mode 2) is selected
2. Load the TH registers with the initial count value
3. Start timer
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to
see whether it is raised
� Get out of the loop when TF goes high
5. Clear the TF flag
6. Go back to Step4, since mode 2 is autoreload
Example 9-16
Assuming that we are programming the timers for mode 2, find the
value (in hex) loaded into TH for each of the following cases.
(a) MOV TH1,#-200 (b) MOV TH0,#-60
(c) MOV TH1,#-3 (d) MOV TH1,#-12
(e) MOV TH0,#-48
Solution:
You can use the Windows scientific calculator to verify the result provided by
the assembler. In Windows calculator, select decimal and enter 200. Then select
hex, then +/- to get the TH value. Remember that we only use the right two
digits and ignore the rest since our data is an 8-bit data.
Decimal 2’s complement (TH value)
-3 FDH
-12 F4H
-48 D0H
-60 C4H
-200 38H
COUNTER PROGRAMMING:
Timers can also be used as counters counting events happening outside the
8051:
� When it is used as a counter, it is a pulse outside of the 8051 that increments
the TH, TL registers
� TMOD and TH, TL registers are the same as for the timer discussed
previously
� Programming the timer in the last section also applies to programming it
as a counter
� Except the source of the frequency
The C/T bit in the TMOD registers decides the source of the clock for the
timer
� When C/T = 1, the timer is used as a counter and gets its pulses from outside
the 8051
� The counter counts up as pulses are fed from pins 14 and 15, these pins are
called T0 (timer 0 input) and T1 (timer 1 input)
The case of GATE-1 in TMOD