0% found this document useful (0 votes)
10 views30 pages

11 Timers Counters 03 02 2025

The document discusses the timers and counters in the 8051 microcontroller, detailing their configuration and usage for generating time delays and counting events. It explains the structure of Timer 0 and Timer 1, including their registers (TL0, TH0, TL1, TH1), the TMOD register for setting timer modes, and the TCON register for controlling operations. Various programming examples illustrate how to utilize these timers for generating square waves and calculating delays based on a given clock frequency.

Uploaded by

luckymlcvl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views30 pages

11 Timers Counters 03 02 2025

The document discusses the timers and counters in the 8051 microcontroller, detailing their configuration and usage for generating time delays and counting events. It explains the structure of Timer 0 and Timer 1, including their registers (TL0, TH0, TL1, TH1), the TMOD register for setting timer modes, and the TCON register for controlling operations. Various programming examples illustrate how to utilize these timers for generating square waves and calculating delays based on a given clock frequency.

Uploaded by

luckymlcvl
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

TIMERS-COUNTERS

❑ The 8051 has two timers/counters, they can be used


either as
➢ Timers to generate a time delay or as event counters to count
events happening outside the microcontroller

❑ Both Timer 0 and Timer 1 are 16 bits wide


➢ Since 8051 has an 8-bit architecture, each 16-bits timer is
accessed as two separate registers of low byte and high byte

22
Dr. K. Ghosh
TIMERS

❑ Accessed as low byte and high byte


➢ The low byte register is called TL0/TL1 and
➢ The high byte register is called TH0/TH1
➢ Accessed like any other register
▪ MOV TL0,#4FH
▪ MOV R5,TH0
Timer 0 Registers

Timer 1 Registers

23
Dr. K. Ghosh
TMOD REGISTER

❑ Both timers 0 and 1 use the same register, called TMOD


(timer mode), to set the various timer operation modes
❑ TMOD is a 8-bit register
➢ The lower 4 bits are for Timer 0
➢ The upper 4 bits are for Timer 1
➢ In each case,
▪ The lower 2 bits are used to set the timer mode
▪ The upper 2 bits to specify the operation

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

TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

❑ While TMOD the timer mode, another register called the


TCON controls the timer/counter operations.

❑ TCON is a 8-bit register


➢ The lower 4 bits are for interrupt function
➢ The upper 4 bits are for timer operations.

28
Dr. K. Ghosh
PROGRAMMING

❑ Timers of 8051 do starting and stopping by either


software or hardware control
➢ In using software to start and stop the timer where GATE=0
▪ The start and stop of the timer are controlled by way of
software by the TR (timer start) bits TR0 and TR1
– The SETB instruction starts it, and it is stopped by the
CLR instruction
– 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

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

1. Load the TMOD value register indicating which timer (timer 0 or


timer 1) is to be used and which timer mode (0 or 1) is selected

2. Load registers TL and TH with initial count value

3. Start the timer

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

5. Stop the timer

6. Clear the TF flag for the next round

7. Go back to Step 2 to load TH and TL again


31
Dr. K. Ghosh
Example-T-A
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

MOV TMOD,#01 ;Timer 0, mode 1(16-bit mode)


HERE: MOV TL0,#0F2H ;TL0=F2H, the low byte
MOV TH0,#0FFH ;TH0=FFH, the high byte
CPL P1.5 ;toggle P1.5
ACALL DELAY
SJMP HERE

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

HERE: MOV TL0,#0F2H 2


MOV TH0,#0FFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY:
SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 2
Total 28
T = 2  28  1.085 us = 60.76 us and F = 16458.2 Hz 34
Dr. K. Ghosh
Example T-D

Find the delay generated by timer 0 in the following code. Do not include the
overhead due to instruction.

CLR P2.3 ;Clear P2.3


MOV TMOD,#01 ;Timer 0, 16-bitmode
HERE: MOV TL0,#3EH ;TL0=3Eh, the low byte
MOV TH0,#0B8H ;TH0=B8H, the high byte
SETB P2.3 ;SET high timer 0
SETB TR0 ;Start the timer 0
AGAIN: JNB TF0,AGAIN ;Monitor timer flag 0
CLR TR0 ;Stop the timer 0
CLR TF0 ;Clear TF0 for next round
CLR P2.3

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.

MOV TMOD,#10;Timer 1, mod 1 (16-bitmode)


AGAIN: MOV TL1,#34H ;TL1=34H, low byte of timer
MOV TH1,#76H ;TH1=76H, high byte timer
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CLR TR1 ;stop the timer 1
CPL P1.5 ;comp. p1. to get hi, lo
CLR TF1 ;clear timer flag 1
SJMP AGAIN ;is not auto-reload
Solution:
Since FFFFH – 7634H = 89CBH + 1 = 89CCH and 89CCH = 35276
clock count and 35276  1.085 us = 38.274 ms for half of the square
wave. The frequency = 13.064Hz.
Also notice that the high portion and low portion of the square wave pulse are
equal. In the above calculation, the overhead due to all the instruction in the
loop is not included.
37
Dr. K. Ghosh
Example T-G
Assume that XTAL = 11.0592 MHz. What value do we need to load the timer’s
register if we want to have a time delay of 5 ms (milliseconds)? Show the
program for timer 0 to create a pulse width of 5 ms on P2.3.

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.

CLR P2.3 ;Clear P2.3


MOV TMOD,#01H ;Timer 0, 16-bitmode
HERE: MOV TL0,#0 ;TL0=0, the low byte
MOV TH0,#0EEH ;TH0=EE, 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
38
Dr. K. Ghosh
Example T-H

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.

(a) T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.


(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex
is FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follow.

MOV TMOD,#01H ;Timer 0, 16-bitmode


AGAIN: MOV TL1,#1AH ;TL1=1A, low byte of timer
MOV TH1,#0FFH ;TH1=FF, the high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR P1.5 ;Clear timer flag 1
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
39
Dr. K. Ghosh
Example T-I

Assume XTAL = 11.0592 MHz, write a program to generate a square wave of 50


Hz frequency on pin P2.3.

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).

MOV TMOD,#10H ;Timer 1, mod 1


AGAIN: MOV TL1,#00 ;TL1=00,low byte of timer
MOV TH1,#0DCH ;TH1=DC, the high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR P2.3 ;Comp. p2.3 to get hi, lo
CLR TF1 ;Clear timer 1 flag
SJMP AGAIN ;Reload timer
40
Dr. K. Ghosh
Example T-J

Examine the following program and find the time delay in seconds.
Exclude the overhead due to the instructions in the loop.

MOV TMOD,#10H ;Timer 1, mod 1


MOV R3,#200 ;counter for multiple delay
AGAIN: MOV TL1,#08H ;TL1=08,low byte of timer
MOV TH1,#01H ;TH1=01,high byte
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;until timer rolls over
CLR TR1 ;Stop the timer 1
CLR TF1 ;clear Timer 1 flag
DJNZ R3,AGAIN ;if R3 not zero then
;reload timer
Solution:
TH-TL = 0108H = 264 in decimal and 65536 – 264 = 65272. Now
65272  1.085 s = 70.820 ms, and for 200 of them we have
200 70.820 ms = 14.164024 seconds.

41
Dr. K. Ghosh
Example T-K

Assuming XTAL=22MHz,write a program to generate a pulse train of 2sec


period on pin P2.4. Use timer 1 in mode 1.

Solution:

MOV TMOD,#10H ;Timer 1, mod 1


REPEAT: MOV R0,#28 ;counter
CPL P2.4 ;complement
BACK: MOV TL1,#00H
MOV TH1,#00H
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R0,BACK
SJMP REPEAT

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

1. It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded


into the timer’s register TH
2. After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL
▪ Then the timer must be started
▪ This is done by the instruction SETB TR0 for timer 0 and SETB TR1 for
timer 1
3. After the timer is started, it starts to count up by incrementing the TL register
▪ It counts up until it reaches its limit of FFH
▪ When it rolls over from FFH to 00, it sets high the TF (timer flag)

4. When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded


automatically with the original value kept by the TH register

▪ 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

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 Step 4, since mode 2 is auto- reload

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

MOV TMOD,#20H ;T1/8-bit/auto reload


MOV TH1,#5 ;TH1 = 5
SETB TR1 ;start the timer 1
BACK: JNB TF1,BACK ;till timer rolls over
CPL P1.0 ;P1.0 to hi, lo
CLR TF1 ;clear Timer 1 flag
SJMP BACK ;mode 2 is auto-reload

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

Find the frequency of a square wave generated on pin P1.0.

MOV TMOD,#2H ;Timer 0, mod 2


;(8-bit, auto reload)
MOV TH0,#0
AGAIN: MOV R5,#250 ;multiple delay count
ACALL DELAY
CPL P1.0
SJMP AGAIN

DELAY: SETB TR0 ;start the timer 0


BACK: JNB TF0,BACK ;stay timer rolls over
CLR TR0 ;stop timer
CLR TF0 ;clear TF for next round
DJNZ R5,DELAY
RET

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.

(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
48
Dr. K. Ghosh
Example T-P
Find (a) the frequency of the square wave generated in the following code, and (b) the
duty cycle of this wave.
MOV TMOD,#2H ;Timer 0, mode 2
;(8-bit, auto-reload)
MOV TH0,#-150 ;TH0 = 6AH = 2’s comp of -150
AGAIN: SETB P1.3 ;P1.3 = 1
ACALL DELAY
ACALL DELAY
CLR P1.3 ;P1.3 = 0
ACALL DELAY
SJMP AGAIN
DELAY:
SETB TR0 ;start Timer 0
BACK: JNB TF0,BACK ;stay until timer rolls over
CLR TR0 ;stop Timer 0
CLR TF0 ;clear TF for next round
RET
For the TH value in mode 2, the conversion is done by the assembler as long as we enter a
negative number. This also makes the calculation easy. Since we are using 150 clocks, we have
time for the DELAY subroutine = 150 x 1.085 µs = 162.75 µs. The high portion of the pulse is
twice that of the low portion (66% duty cycle). Therefore, we have: T = high portion + low
portion = 325.5 µs + 162.25 µs = 488.25 µs and frequency = 2.048 kHz.
49
Dr. K. Ghosh
COUNTERS

❑ 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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy