[go: up one dir, main page]

0% found this document useful (0 votes)
7 views43 pages

Timer 8051

Uploaded by

SANTOSH VERMA
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)
7 views43 pages

Timer 8051

Uploaded by

SANTOSH VERMA
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/ 43

Microprocessors & Microcontrollers

L21: 8051 Timer Programming


Outline
 Timer Programming of 8051
 Counter Programming of 8051
Programming 8051 Timer
Introduction
 The 8051 has two timers: Timer 0 and Timer 1.
 They can be used either as timers to generate a time delay
or as counters to count events happening outside the
microcontroller.
Basic Registers of the Timer
 Both Timer 0 and Timer 1 are 16 bits wide.
 Since the 8051 has an 8-bit architecture, each 16-bit timer is
accessed as two separate registers of low byte and high byte.
 The 16-bit register of Timer 0 is accessed as TL0 (Timer 0 low
byte) and TH0 (Timer 0 high byte).
 The 16-bit register of Timer 1 is accessed as TL1 (Timer 1 low
byte) and TH1 (Timer 1 high byte).
TMOD (Timer Mode) Register
 Both timers 0 and 1 use the same register, called TMOD,
to set the various timer operation modes.
 TMOD is an 8-bit register in which the lower 4 bits are
set aside for Timer 0 and the upper 4 bits for Timer 1.
 In each case, the lower 2 bits are used to set the timer
mode and the upper 2 bits to specify the operation.
TMOD (Timer Mode) Register Cont.
M0, M1 of TMOD Register
 M0 and M1 select the timer mode.
 There are three modes: 0, 1, and 2.
 Mode 0 is a 13-bit timer, mode 1 is a 16-bit timer, and
mode 2 is an 8-bit timer.
 We will concentrate on modes 1 and 2 since they are the
ones used most widely.
C/T (Clock/Timer) of TMOD Register
 This bit in the TMOD register is used to decide whether
the timer is used as a delay generator or an event counter.
 If C/T = 0, it is used as a timer for time delay generation.
 The clock source for the time delay is the crystal
frequency of the 8051.
Example
Q: 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

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

BIT SYMBOL FUNCTION


TCON.7 TF1 Timer 1 overflow flag
TCON.6 TR1 Timer 1 run control bit
TCON.5 TF0 Timer 0 overflow flag
TCON.4 TR0 Timer 0 run control bit
Example
 Find the value for TMOD if we want to program Timer 0
in mode 2, use 8051 XTAL for the clock source, and use
instructions to start and stop the timer.
Mode 1 Programming
The following are the characteristics and operations of
mode 1:
 It is a 16-bit timer; therefore, it allows values of 0000 to
FFFFH to be loaded into the timer’s registers TL and TH.
 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.
 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).
 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.
Characteristics and Operations of Mode 1
Cont.
 It must be noted that each timer has its own timer flag: TF0
for Timer 0, and TF1 for Timer 1.
 After the timer reaches its limit and rolls over, in order to
repeat the process the registers TH and TL must be
reloaded with the original value, and TF must be reset to 0.
Steps to Program in Mode 1
To generate a time delay, using the timer’s mode 1, the
following steps are taken:

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


or Timer 1) is to be used and which timer mode (1 or 2) is
selected.
2. Load registers TL and TH with initial count values.
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.
Example
In the following program, we are creating a square wave of
50% duty cycle on the P1.5 bit. Timer 0 is used to generate
the time delay. Analyse the program.
Example Solution
In the above program notice the following steps.

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.

7. Timer 0 is stopped by the instruction “CLR TR0”. The


DELAY subroutine ends, and the process is repeated.
Notice that to repeat the process, we must reload the TL and TH
registers and start the timer again.
Example
For the previous Example, calculate the amount of time delay in the DELAY
subroutine generated by the timer. Assume that 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.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

Convert YYXX values of the TH,TL register to decimal to get a


NNNNN decimal number,
then (65536 – NNNNN) x 1.085 us
Finding Values to be Loaded into the Timer
 Assuming that we know the amount of timer delay we need,
the question is how to find the values needed for the TH, TL
registers.
 Assuming XTAL = 11.0592 MHz, we can use the following
steps for finding the TH, TL registers values.

1. Divide the desired time delay by 1.085 us.


2. Perform 65536 – n, where n is the decimal value we got in
Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial
hex value to be loaded into the timer’s registers.
4. Set TL = xx and TH = yy.
Example
Q: Assume that XTAL = 11.0592 MHz.What value do we
need to load into the timer’s registers if we want to have a
time delay of 5 ms? Show the program for Timer 0 to create
a pulse width of 5 ms on P2.3.
A:
 Since XTAL = 11.0592 MHz, the counter counts up
every 1.085 us.
 5 ms /1 .085 us = 4608 clocks
 To achieve that we need to load into TL and TH the value
65536 – 4608 = 60928 = EE00H
 Therefore, we have TH = EE and TL = 00
Example Solution Cont.
Mode 0
 Mode 0 is exactly like mode 1 except that it is a 13-bit
timer instead of 16-bit.
 The 13-bit counter can hold values between 0000 to
1FFFH in TH – TL.
 Therefore, when the timer reaches its maximum of 1FFH,
it rolls over to 0000, and TF is raised.
Mode 2 Programming
The following are the characteristics and operations of
mode 2.
 It is an 8-bit timer; therefore, it allows only values of 00 to
FFH to be loaded into the timer’s register TH.
 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. This is
just like mode 1.
 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). If we are using Timer 0, TF0 goes high; if
we are using Timer 1, TF1 is raised.
Characteristics and Operations of Mode 2
 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.
 It must be emphasized that mode 2 is an 8-bit timer. However, it has
an auto-reloading capability. In auto-reload, TH is loaded with the
initial count and a copy of it is given to TL. This reloading leaves TH
unchanged, still holding a copy of the original value.
Steps to Program in Mode 2
To generate a time delay using the timer’s mode 2,
take the following steps:
1. Load the TMOD value register indicating which timer
(Timer 0 or Timer 1) is to be used, and select the timer
mode (mode 2).
2. Load the TH registers with the initial count value.
3. Start the 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.
Example
Q: Assuming that XTAL = 11.0592 MHz. find (a) the
frequency of the square wave generated on pin P 1.0 in the
following program, and (b) the smallest frequency
achievable in this program, and the TH value to do that.
Example Solution
a) 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) x 1.085 us = 251 x 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 x 272.33 us = 544.67 us
and the frequency = 1.83597 kHz.

b) To get the smallest frequency, we need the largest T and


that is achieved when TH = 00.
In that case, we have T = 2 x 256 x 1.085 us = 555.52
us and the frequency = 1.8kHz.
Programming 8051 Counter
Introduction
 The timers in 8051 can also be used as counters counting
events happening outside the 8051.
 As far as the use of a timer as an event counter is
concerned, everything that we have talked about in
programming the timer also applies to programming it as
a counter, except the source of the frequency.
 When the timer/counter is used as a timer, the 8051′s
crystal is used as the source of the frequency. When it is
used as a counter, however, it is a pulse outside the
8051 that increments the TH, TL registers.
 In counter mode, the TMOD and TH, TL registers are the
same as for the timer discussed.
C/T bit in TMOD Register
 The C/T bit in the TMOD register decides the source of the clock for the
timer.
 If C/T = 0, the timer gets pulses from the crystal.
 In contrast, when C/T = 1, the timer is used as a counter and gets its
pulses from outside the 8051.
 Therefore, when C/T = 1, the counter counts up as pulses are fed
from pins 14 and 15. These pins are called T0 (Timer 0 input) and Tl
(Timer 1 input).
 Notice that these two pins belong to port 3. In the case of Timer 0, when
C/T = 1, pin P3.4 provides the clock pulse and the counter counts up for
each clock pulse coming from that pin. Similarly, for Timer 1, when C/T
= 1 each clock pulse coming in from pin P3.5 makes the counter count
up.
Example
Q: 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.

A:
Example Solution Cont.

 In this example, we use Timer 1 as an event counter where it counts


up as clock pulses are fed into pin 3.5.
 These clock pulses could represent the number of people passing
through an entrance, or the number of wheel rotations, or any
other event that can be converted to pulses.
Block Diagrams of 8051 Counter for Mode 1
and Mode 2
Timer 0 and Timer 1 with External Input (Mode 1)

Timer 0 and Timer 1 with External Input (Mode 2)


The case of GATE = 1 in TMOD
 When GATE = 0, the timer is started with instructions
“SETB TR0” and “SETB TR1″, for Timers 0 and 1,
respectively.
 What happens if the GATE bit in TMOD is set to 1?
 The start and stop of the timer are done externally
through pins P3.2 and P3.3 for Timers 0 and 1,
respectively.
 This is in spite of the fact that TRx is turned on by the
“SETB TRx” instruction. This allows us to start or stop the
timer externally at any time via a simple switch.
The case of GATE = 1 in TMOD Cont.
 This hardware way of controlling the stop and start of the
timer can have many applications.
 For example, assume that an 8051 system is used in a
product to sound an alarm every second using Timer
0, perhaps in addition to many other things. Timer 0 is
turned on by the software method of using the “SETB
TR0” instruction and is beyond the control of the user of
that product.
 However, a switch connected to pin P3.2 can be used to
turn on and off the timer, thereby shutting down the
alarm.

You might also like