0% found this document useful (0 votes)
23 views

Part 1MODULE 3 Merged

Uploaded by

Suma
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)
23 views

Part 1MODULE 3 Merged

Uploaded by

Suma
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/ 48

Programming of 8051 Timers:

Timers/Counters are used generally for


 Time reference
 Creating delay
 Wave form properties measurement
 Periodic interrupt generation
 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

8051 has two timers, Timer 0 and Timer 1.

 Timer 0 and Timer 1 are 16 bits.


 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of 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.

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.

If Master CLK=12 MHz,


Timer Clock frequency = Master CLK/12 = 1 MHz
Timer Clock Period = 1micro second
This indicates that one increment in count will take 1 microsecond.
The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each
timer also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).

The following are timer related SFRs in 8051.


TMOD Register
 Both timers 0 and 1 use the same register, called TMOD (timer mode), to
set the various timer operation modes
 TMOD is an 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.

TCON (timer control) register

 TCON (timer control) register is an 8bit register


Example 9-1
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.
Example 9-2
Find the timer’s clock frequency and its period for various 8051-
based system, with the crystal frequency 11.0592 MHz when C/T bit
of TMOD is 0.

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.

Steps to Program in mode1

To generate a time delay :


• 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 .
• Load registers TL and TH with initial count value
• Start the timer
• 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
• Stop the timer
• Clear the TF flag for the next round
• Go back to Step 2 to load TH and TL again

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

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

In the above program notice the following step.


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

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

Steps to program in 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

You might also like