11 Timers Counters 03 02 2025
11 Timers Counters 03 02 2025
22
Dr. K. Ghosh
TIMERS
Timer 1 Registers
23
Dr. K. Ghosh
TMOD REGISTER
24
Dr. K. Ghosh
TMOD REGISTER
25
Dr. K. Ghosh
26
Dr. K. Ghosh
Example
Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H
Solution:
We convert the value from hex to binary. From Figure 9-3 we have:
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b) TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are
selected.
27
Dr. K. Ghosh
TCON REGISTER
28
Dr. K. Ghosh
PROGRAMMING
29
Dr. K. Ghosh
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
30
Dr. K. Ghosh ▪ TF must be reloaded to 0
TO GENERATE A TIME DELAY
4. Keep monitoring the timer flag (TF) with the JNB TFx,target
instruction to see if it is raised
▪ Get out of the loop when TF becomes high
DELAY:
SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0
;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
32
Dr. K. Ghosh
Example T-B
In Example T-A, 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 (13 decimal).
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.
33
Dr. K. Ghosh
Example T-C
In Example T-B, calculate the frequency of the square wave generated on pin P1.5.
Solution:
In the timer delay calculation of Example T-B, we did not include the overhead due to
instruction in the loop. To get a more accurate timing, we need to add clock cycles due
to this instructions in the loop. To do that, we use the machine as shown below.
Cycles
Find the delay generated by timer 0 in the following code. Do not include the
overhead due to instruction.
Solution:
(a) (FFFFH – B83E + 1) = 47C2H = 18370 in decimal and 18370
1.085 us = 19.93145 ms
35
Dr. K. Ghosh
Example T-E
Modify TL and TH in Example T-D to get the largest time delay possible.
Find the delay in ms. In your calculation, exclude the overhead due to the
instructions in the loop.
Solution:
To get the largest delay we make TL and TH both 0. This will count up from
0000 to FFFFH and then roll over to zero.
CLR P2.3 ;Clear P2.3
MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#0 ;TL0=0, the low byte
MOV TH0,#0 ;TH0=0, the high byte
SETB P2.3 ;SET high P2.3
SETB TR0 ;Start timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear timer 0 flag
CLR P2.3
Making TH and TL both zero means that the timer will count from
0000 to FFFF, and then roll over to raise the TF flag. As a result, it
goes through a total Of 65536 states. Therefore, we have delay =
65536 1.085 us = 71.1065ms.
36
Dr. K. Ghosh
Example T-F
The following program generates a square wave on P1.5 continuously using
timer 1 for a time delay. Find the frequency of the square wave if XTAL =
11.0592 MHz. In your calculation do not include the overhead due to
Instructions in the loop.
Solution:
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us. This means
that out of many 1.085 us intervals we must make a 5 ms pulse. To get that, we
divide one by the other. We need 5 ms / 1.085 us = 4608 clocks. To Achieve that
we need to load into TL and TH the value 65536 – 4608 = EE00H. Therefore,
we have TH = EE and TL = 00.
Assume that XTAL = 11.0592 MHz, write a program to generate a square wave
of 2 kHz frequency on pin P1.5.
Solution:
Look at the following steps.
Solution:
Look at the following steps.
(a) T = 1 / 50 = 20 ms, the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 10 ms.
(c) 10 ms / 1.085 us = 9216 and 65536 – 9216 = 56320 in decimal,
and in hex it is DC00H.
(d) TL = 00 and TH = DC (hex).
Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.
41
Dr. K. Ghosh
Example T-K
Solution:
42
Dr. K. Ghosh
Example T-L
Generate a square wave with an ON time of 3ms and an OFF time of 10ms
on all pins of port 0. Assume an XTAL of 22MHz
Solution:
MOV TMOD,#01H ;Timer 0, mod 1
BACK: MOV TL0,#075H ;to generate the OFF time
MOV TH0,#0B8H ;load OFF time value
MOV P0,#00 ;make port bits low
ACALL DELAY ;call delay routine
MOV TL0,#8AH ;to generate the ON time
TH0,#0EAH ;load ON time value
MOV P0,#0FFH ;make port bits high
ACALL DELAY ;call delay routine
SJMP BACK ;continuous square wave
ORG 300H
DELAY: SETB TR0 ;start the counter
AGAIN: JNB TF0,AGAIN ;check the timer overflow
CLR TR0
CLR TF0
RET
43
Dr. K. Ghosh
MODE 2 PROGRAMMING
▪ To repeat the process, we must simply clear TF and let it go without any
need by the programmer to reload the original value
▪ This makes mode 2 an auto-reload, in contrast with mode 1 in
which the programmer has to reload TH and TL
44
Dr. K. Ghosh
TO GENERATE A TIME DELAY
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
45
Dr. K. Ghosh
Example T-M
Assume XTAL = 11.0592 MHz, find the frequency of the square wave generated
on pin P1.0 in the following program
Solution:
First notice the target address of SJMP. In mode 2 we do not need to reload TH
since it is auto-reload. Now (256 - 05) 1.085 us = 251 1.085 us = 272.33
us is the high portion of the pulse. Since it is a 50% duty cycle square wave,
the period T is twice that; as a result T = 2 272.33 us = 544.67 us and the
frequency = 1.83597 kHz
46
Dr. K. Ghosh
Example T-N
Solution:
T = 2 ( 250 256 1.085 us ) = 138.88ms, and frequency = 72 Hz
47
Dr. K. Ghosh
Example T-O
Assuming that we are programming the timers for mode 2, find the value (in
hex) loaded into TH for each of the following cases.
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.
50
Dr. K. Ghosh
Example T-P
Assuming that clock pulses are fed into pin T1, write a program for counter 1 in
mode 2 to count the pulses and display the state of the TL1 count on P2, which
connects to 8 LEDs.
Solution:
MOV TM0D,#01100000B ;counter 1, mode 2,
;C/T=1 external pulses
MOV TH1,#0 ;clear TH1
SETB P3.5 ;make T1 input
AGAIN: SETB TR1 ;start the counter
BACK: MOV A,TL1 ;get copy of TL
MOV P2,A ;display it on port 2
JNB TF1,Back ;keep doing, if TF = 0
CLR TR1 ;stop the counter 1
CLR TF1 ;make TF=0
SJMP AGAIN ;keep doing it
Notice in the above program the role of the instruction SETB P3.5.
Since ports are set up for output when the 8051 is powered up, we
make P3.5 an input port by making it high. In other words, we must
configure (set high) the T1 pin (pin P3.5) to allow pulses to be fed
into it.
51
Dr. K. Ghosh