You are on page 1of 43

Chapter 9 Timers

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

5.1 Programming 8051 Timers


Two timers : Timer 0 ( T0 ), Timer 1 ( T1 )
Timers or event counters T0 : TH0 + TL0 T1 : TH1 + TL1

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 91

Timer 0 Registers

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 92

Timer 1 Registers

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

TMOD
Timer mode register M1, M0 : select timer modes C/T : clock / timer Gate :
1 : need an INTx to work with TRx 0 : only TRx controls the timer x

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 93

TMOD Register

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Table 92

Equivalent Instructions for the Timer Control Register (TCON)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 9.2
Find the timers clock frequency and its period for various 8051-based systems with the following crystal frequencies (a) 12 MHz (b) 16 MHz (c) 11.0592 MHz
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

NOTE:
The 8051 timers use 1/12 of XTAL frequency, regardless of machine cycle time.

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 82a

XTAL Connection to 8051

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 82b

XTAL Connection to an External Clock Source

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Mode 1
16-bit timer, values from 0000 to FFFFH After TH and TL are loaded, the timer must be started.
Done by SETB TR0 or SETB TR1

It starts to count up until FFFFH. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF.
TFx can be monitored or used as an interrupt source

After TFx is set, the timer must be RELOADED for a new round ot timing.
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Steps to program in mode 1


Load the TOMD Load TL and TH with initial count values Start timer Clear the TF for the next round GO back to load TH and TL again

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 94

Timer Delay Calculation for XTAL = 11.0592 MHz

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 9-4
MOV HERE: MOV MOV CPL CALL JMP TMOD,#01
TH0,#0F2H TL0,#0FFH P1.5 DELAY HERE

DELAY: SETB TR0 AGAIN: JNB TF0, AGAIN CLR TR0 CLR TF0 RET
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-5
Assume we use 11.0592 MHz as input signal, find the amount of time in the DELAY subroutine
11.0592 MHz 921.6 KHz 12 FFFFH - FFF2H =0DH (13+1)x1.085 s 15.19 s 15.19 sx2=30.38 s
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

1 T= 1.085 s 921.6 KHz

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-7
HERE:

AGAIN:

CLR P2.3 MOV TMOD,#01 MOV TL0,#03EH MOV TH0,#0B8H SETB P2.3 SETB TR0 JNB TF0, AGAIN JNB TF0,$ CLR TR0 CLR TF0 CLR P2.3
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-7
HERE:

AGAIN:

CLR P2.3 MOV TMOD,#01 MOV TL0,#0 MOV TH0,#0 SETB P2.3 SETB TR0 JNB TF0, AGAIN JNB TF0,$ CLR TR0 CLR TF0 CLR P2.3
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-9
AGAIN: BACK:

MOV MOV MOV SETB JNB CLR CPL CLR JMP

TMOD,#10H TL1,#34H TH1,#76H TR1 TF1,$ TR1 P1.5 TF1 AGAIN


2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Finding Values to be Loaded into the Timer


Assuming XTAL=11.0592 MHz
1. Divide the desired time delay by 1.085s 2. Perform 65536-n, where n is the decimal value we got in Step 1. 3. Convert the result of Step 2 to hex, we got yyxxH 4. Set TL=xx, TH=yy
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-10
Assuming Xtal=11.0592MHz Design a time delay of 5ms Create a pulse width of 5 ms on P2.3 with T0

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Sol:
HERE:

AGAIN:

CLR MOV MOV MOV SETB SETB JNB CLR CLR CLR

P2.3 TMOD,#01 TL0,#0 TH0,#Oeeh P2.3 TR0 TF0,$ P2.3 TR0 TF0
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-11
Assume XTAL=11.0592MHz Generate a square wave of 2 kHz on P1.5

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Sol:
1. T=1/f=1/(2 kHz)=500s 2. of T is 250s 3. 250s/1.085s=230 and 65536230=65306 ( FF1AH ) 4. TL=1AH, and TH=FFH

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Sol (cont)
AGAIN: BACK:

MOV MOV MOV SETB JNB CLR CPL CLR JMP

TMOD,#10H TL1,#1AH TH1,#0FFH TR1 TF1, BACK TR1 P1.5 TF1 AGAIN
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-12
1. 2. 3. 50Hz on P2.3 with 11.0592 MHz T=1/f=1/( 50 Hz)=20 ms of T is 10 ms 10 ms/1.085s=9216 and 655369216=56320 ( DC00H ) 4. TL=00H, and TH=DCH
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Sol
AGAIN: BACK:

MOV MOV MOV SETB JNB CLR CPL CLR JMP

TMOD,#10H TL1,#00H TH1,#0DCH TR1 TF1, BACK TR1 P2.3 TF1 AGAIN
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Ex. 9-13
MOV TMOD, #10H MOV R3, #200 MOV TL1,#08H MOV TH1,#01H SETB TR1 JNB TF1,$ CLR TR1 CLR TF1 DJNZ R3, AGAIN
2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

AGAIN:

BACK:

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

Sol :
TH-TL = 0108H = 264 65536-264=65272 65272 X 1.085 s = 70.82 ms 200 X 70.82 ms = 14.164024 seconds

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Mode 2 Programming
8-bit timers TH is loaded with the beginning value, then it is COPIED to TL the moment the timer starts When TL is rolled from FFH to 00H, the starting value in TH is loaded AUTOMATICALLY to TL and the counting continues
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Steps to Program in Mode 2


1. 2. 3. 4. 5. 6. Load TMOD Load TH with the initial count values Start the timer Keep monitoring the timer flag (TF) Clear the TF Go back to step 4 since mode 2 is autoloaded
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 9-14
XTAL=11.0592 MHz MOV TMOD,#20H MOV TH1,#5 SETB TR1 BACK: JNB TF1,$ CPL P1.0 CLR TF1 JMP BACK
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay 2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 9-15
MOV MOV AGAIN: MOV CALL CPL JMP
The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

TMOD,#2H TH0,#0 R5, #250 DELAY P1.0 AGAIN


2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

(CONT)
DELAY: BACK: SETB JNB CLR CLR DJNZ RET TR0 TF0,$ TR0 TF0 R5,DELAY

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Ex. 9-16
MOV MOV MOV MOV MOV TH1,#-200 TH0,#-60 TH1,#-3 TH1,#-12 TH0,#-48

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

9.2 Counter Programming

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 95a

Timer 0 with External Input (Mode 1)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 95b

Timer 1 with External Input (Mode 1)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 96

Timer 0 with External Input (Mode 2)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 97

Timer 1 with External Input (Mode 2)

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 98

Timer/Counter 0

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Figure 99

Timer/Counter 1

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458

Table 91

Port 3 Pins Used For Timers 0 and 1

The 8051 Microcontroller and Embedded Systems, 2e By Muhammad Ali Mazidi, Janice Gillispie Mazidi, and Rolin D. McKinlay

2006 Pearson Education, Inc. Pearson Prentice Hall Upper Saddle River, NJ 07458