8051 Timers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

8051 Timers

Generating Delays

You can generate short delays using a register and


incrementing or decrementing its value
Example:
mov r1, #0ah
loop:

djnz r1, loop

How much delay is that?

Djnz is a 2-byte instruction it takes two machine cycles

One machine cycle is 1/12 of the system clock period

For a 12 MHz system clock that is:

Machine cycle = 12/12 = 1 MHz

Machine period = 1/(1 MHz) = 10^(-6) s = 1 s

Loop time = 10*2*1 s = 20 s

Generating longer delays

Each register is 8 bits long, so it can increment 256


times before overflowing
For larger delays, or when interrupts are required
8051 uses two timers

8051 Timers

8051 has two timers

Timer 0

Timer 1

Both Timer 0 and Timer 1 are 16 bits wide

Each 16-bit timer is accessed as two separate registers


of low byte and high byte

The Low byte register of Timer 0 is called TL0(8Ah)


and the high byte register of Timer 0 is called
TH0(8Ch)

Similarly TL1(8Bh) and TH1(8Dh) for Timer 1

Timer registers

Timer Registers

The registers TH0, TL0, TH1 and TL1 can be


accessed like any other register such as A,B,R1,R2
etc.

Ex: MOV TL0,#4Fh ; moves value 4F into TL0(low


byte of Timer 0)

These registers can also be read like any other


register.

Ex: MOV R5,TH0 ;saves TH0(higher byte of Timer 0)


in R5

Timer mode register (TMOD) Register (89H)

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.

TMOD Register

TMOD Register

Bit 7: Gate bit; when set, timer only runs while INT high (T0)
Bit 6: Counter/timer select bit; when set timer is an event counter when
cleared timer is an interval timer (T0)

Bit 5: Mode bit 1 (T0)

Bit 4: Mode bit 0 (T0)

Bit 3: Gate bit; when set, timer only runs While INT high (T1)

Bit 2: Counter/timer select bit; when set timer is an event counter when
cleared timer is an interval timer (T1)

Bit 1: Mode bit 1 (T1)

Bit 0: Mode bit 0 (T1)

TMOD Register

configuration

TMOD Register

Ex: 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

TMOD Register

Ex: Find the timers 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.
Sol:

Timer Control Register(TCON) (88H)

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

TCON Register

TF1: Timer 1 overflow flag.

TR1: Timer 1 run control bit.

TF0: Timer 0 overflag.

TR0: Timer 0 run control bit.

IE1: External interrupt 1 edge flag.

IT1: External interrupt 1 type flag.

IE0: External interrupt 0 edge flag.

IT0: External interrupt 0 type flag.

TCON Register

TCON register is a bit-addressable register

TCON Register

Bit 7 (TF1) 8FH : Timer 1 overflow flag; set by hardware upon


overflow, cleared by software
Bit 6 (TR1) 8EH: Timer 1 run-control bit; manipulated by
software - setting starts timer 1, resetting stops timer 1
Bit 5 (TF0) 8DH: Timer 0 overflow flag; set by hardware upon
overflow, cleared by software.
Bit 4 (TR0) 8CH: Timer 0 run-control bit; manipulated by
software - setting starts timer 0, resetting stops timer 0

Initializing and stopping timers

MOV TMOD, #16H ;initialization


SETB TR0
SETB TR1

;starting timers

CLR TR0
CLR TR1

; stop timer 0
; stop timer 1

MOV R7, TH0

; reading timers

MOV R6, TL0

17

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

3. (cont)

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

Mode 1 programming

Steps to program in mode 1

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


0 or Timer 1) is to be used and which timer mode 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 1

Creating a square wave of 50% duty cycle on the


P1.5 bit. Timer 0 is used to generate the time delay
MOV TMOD,#01

HR:MOV TL0,#0F2H

;Timer 0, mode 1(16-bit)

;TL0=F2H,the low byte

MOV TH0,#0FFH

;TH0=FFH,the high byte

CPL P1.5

;Toggle P1.5

ACALL DELAY
SJMP HR

;Load TH, TL again

[source: page no: 207 from Mazidi & Mazidi Text book]

Example 1 (cont..)
Delay:
AN:

SETB TR0

;Start Timer 0

JNB TF0,AN

;monitor Timer 0
;flag 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.

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 process is repeated

Example 2

Calculate the amount of delay in the DELAY


subroutine generated by the timer in example 1.
Assume that XTAL = 11.0592MHz

[source: page no: 207 from Mazidi & Mazidi Text book]

Example 3

Calculate the frequency of square wave generated


on P1.5
CYCLES

HERE:

MOV TL0,#0F2H
MOV TH0,#0FFH
CPL P1.5
ACALL DELAY
SJMP HERE
DELAY: SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0
CLR TF0
RET

2
2
1
2
2
1
14
1
1
2

[source: page no: 207 from Mazidi & Mazidi Text book]

Time delay calculation for


XTAL=11.0592MHz
(a)In Hex
(FFFF YYXX+1) X 1.085 us

(b) In decimal

Convert YYXX values of


the TH, TL register to
where YYXX are TH, TL decimal to get a NNNNN
initial values respectively. decimal
number,
then
Notice that values YYXX (65536 NNNNN) X 1.085
are in hex
us

Example 4

Assume XTAL=22MHz, and Calculate the delay


generated by timer(do not include the overhead due
to instructions)

AGAIN:

CLR P1.3
MOV TMOD,#10H
MOV TH1,#0FFH
MOV TL1,#00H
SETB P1.3
SETB TCON.6
JNB TCON.7, AGAIN
CLR TCON.6
CLR TCON.7
CLR P1.3

Example 5

Assume XTAL=22MHz, and Calculate the delay


generated by timer(do not include the overhead due
to instructions)
AGAIN:

BACK:

MOV TMOD,#00H
MOV TL1,#00H
MOV TH1,#0FH
SETB TR1
JNB TF1, BACK
CLR TR1
CPL P0.6
CLR TF1
SJMP AGAIN

Finding values to be loaded into the timer

Assuming XTAL=11.0592MHz

1. Divide the desired time delay by 1.085us

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 timers registers

4. Set TL=xx and TH=yy

Example 6

Q: Assume that XTAL=11.0592MHz. What value


do we need to load into the timers registers if we
want to have a time delay of 5ms? Show the
program for Timer 0 to create a pulse width of 5
ms on P2.3

Example 6

CLR P2.3
MOV TMOD,#01
HERE: MOV TL0,#0
MOV TH0,#0EEH
SETB P2.3
SETB TR0
AGAIN:JNB TF0,AGAIN
CLR P2.3
CLR TR0
CLR TF0
SJMP HERE
Sol:

Example 7

Q: With crystal frequency of 22MHz, generate a


frequency of 100KHz on pin P2.3. Use timer1 in
mode 1

Example 7

Sol: For a 100-KHz square wave,


a) T=1/f=0.01ms = 10us
b) of it for high and low portions each = 5us
c) 5us/0.546us = 9 cycles
d) 65536-9 = 65527 = FFF7H

Example 7

Sol(cont..)

MOV TMOD,#10H
BACK: MOV TL1,#0F7H
MOV TH1,#0FFH
SETB TR1
AGAIN: JNB TF1,AGAIN
CLR TF1
CPL P2.3
CLR TF1
SJMP BACK

Example 8

Q: Generate a square wave with an ON time of 3


ms and an OFF time of 10 ms on all pins of port 0.
Assume an XTAL of 22 MHz

Example 8

Sol:

For ON time calculation:


3ms/0.546us = 5494 cycles

65,536 5494 = 60,042 = EA8Ah


For OFF time calculation
10ms/0.546 = 18,315 cycles
65,536 18,315 = 47,221 = B875h

Example 8

Sol(cont..):
MOV TMOD,#01H
BACK: MOV TL0,#75H
MOV TH0,#0B8H
MOV P0,#00H
ACALL DELAY
MOV TL0,#8AH
MOV TH0,#0EAH
MOV P0,#0FFH
ACALL DELAY
SJMP BACK

Example 8

Sol(cont..):
DELAY: SETB TR0
AGAIN: JNB TF0,AGAIN
CLR TR0

CLR TF0
RET

Generating large time delay

Ex: Assuming XTAL = 22 MHz, write a program


to generate a pulse train of 2 seconds period on Pin
P2.4. Use Timer 1 in mode 1

Generating large time delay

Sol:
MOV TMOD,#10H
REPT: MOV R0,#28
CPL P2.4
BACK: MOV TL1,#00H
MOV TH1,#00H
SETB TR1
AGAIN:JNB TF1,AGAIN
CLR TR1
CLR TF1
DJNZ R0,BACK
SJMP REPT

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

Mode 2 programming

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

Mode 2 programming

To generate a time delay using the timers mode 2,


take the following steps:

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


Timer1) 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 9

Q: Assuming that XTAL = 11.0592MHz, find


A). The frequency of the square wave generated on
pin P1.0 in the following program, and
B). The smallest frequency achievable in this
program, and the TH value to do that.

Example 9

Q(cont..):
MOV TMOD,#20H
MOV TH1, #5
SETB TR1
BACK: JNB TF1,BACK
CPL P1.0
CLR TF1
SJMP BACK

Example 9

Sol:

A). In mode 2 we do not need to reload TH since it is auto reload.


Now (256-5)X1.085us = 272.33 us is the high portion of the pulse.
Since it is a 50% duty cycle,
T=2 X 272.33us = 544.67us and F=1.83597KHz
B). The smallest frequency (achieved when TH=00h) is
T=2X256X1.085us = 555.52us
F = 1.8KHz

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

Counter programming

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)

Counter programming

Example 10

Q: Design a counter for counting the pulses of an


input signal. The pulses to be counted are fed to pin
P3.4. XTAL=22MHz

Example 10

Sol:
RPT:

AGAIN:

BACK:

MOV TMOD,#15H
SETB P3.4
MOV TL0,#00
MOV TH0,#00
SETB TR0
MOV R0,#28
MOV TL1,#00
MOV TH1,#00
SETB TR1
JNB TF1, BACK
CLR TF1
CLR TR1
DJNZ R0, AGAIN
MOV A,TL0
MOV P2,A
MOV A,TH0
MOV P1,A
SJMP RPT

Example 11

Q: Assume that a 1-Hz frequency pulse is


connected to input pin 3.4. Write a program to
display counter 0 on an LCD.

Example 11

Sol:

AGAIN:
BACK:

MOV TMOD,#00000110B
MOV TH0,#00
SETB P3.4
SETB TR0
MOV A,TL0
ACALL DISPLAY
JNB TF0,BACK
CLR TR0
CLR TF0
SJMP AGAIN

References

1. The 8051 Microcontroller and embedded


systems by Mazidi M. A. & J. G. Mazidi, Pearson.
2002.

You might also like