Professional Documents
Culture Documents
8051 Chap4 Timer
8051 Chap4 Timer
HCM
L Ch Thng
000
001
111
000
L Ch Thng
Overflow
sites.google.com/site/chithong
L Ch Thng
Timer/Counter
8051 has 2 timers
Timer 0
Timer 1
L Ch Thng
Timer/Counter
3 Functions
1. Timer is used as time delay generator (interval
timing)
Internal clock source
sites.google.com/site/chithong
L Ch Thng
Clock Source
Internal clock fCLK = fCrystal / 12
to Timer
External clock
C/T
0
1
Clock
Function
Internal Timer (interval timing, delay)
External Counter (event counting)
L Ch Thng
L Ch Thng
sites.google.com/site/chithong
L Ch Thng
Clock Enable/Disable
Clock Enable/Disable
Gate TRx
INTx
Function
0
1
X
X
0
1
X
1
Timer Run/Stop
Gate TRx
0
1
0
1
0
1
INTx
Function
X
X
X
1
sites.google.com/site/chithong
L Ch Thng
Applications of Timer
Gate C/T Application
0
Delay
L Ch Thng
sites.google.com/site/chithong
10
L Ch Thng
Timer Registers
TH0, TL0 registers
TH1, TL1 registers
TMOD register
TCON register
L Ch Thng
11
TL0
D7 D6 D5 D4 D3 D2 D1 D0
Timer 1
TH1
TL1
D7 D6 D5 D4 D3 D2 D1 D0
L Ch Thng
sites.google.com/site/chithong
12
L Ch Thng
TMOD Register
TMOD: Timer Mode Register
(MSB)
GATE C/T M1 M0 GATE C/T M1
Timer 1
Timer 0
(LSB)
M0
GATE
0:
1:
C/T
0:
1:
L Ch Thng
13
sites.google.com/site/chithong
14
L Ch Thng
An Example
TMOD: Timer Mode Register
(MSB)
GATE C/T M1 M0 GATE C/T M1
Timer 1
Timer 0
(LSB)
M0
15
More Examples
TMOD: Timer Mode Register
(MSB)
GATE C/T M1 M0 GATE C/T M1
Timer 1
Timer 0
(LSB)
M0
Ex:
MOV TMOD,#00000001B ; Timer 0 mode 1, timer operation
MOV TMOD,#20H ; Timer 1 mode 2, timer operation
MOV TMOD,#12H ; Timer 1 mode 1, Timer 0 mode 2, both timer
; operation
MOV TMOD,#00000101B ; Timer 0 mode 1, counter operation
MOV TMOD,#00001001B ; Timer 0 mode 1, external control (GATE=1)
L Ch Thng
sites.google.com/site/chithong
16
L Ch Thng
4 Timer Modes
(MSB)
TF1 TR1
Timer 1
TF0 TR0
Timer0
IE1
L Ch Thng
sites.google.com/site/chithong
IT1 IE0
for Interrupt
(LSB)
IT0
18
L Ch Thng
(MSB)
TF1 TR1
Timer 1
TF0 TR0
Timer0
IE1
IT1 IE0
for Interrupt
(LSB)
IT0
L Ch Thng
19
TMOD 10H
TH1:TL1 -100
Run Timer 1
N
Overflow?
(TF1=1?)
Y
Clear overflow flag
Stop Timer 1
L Ch Thng
sites.google.com/site/chithong
20
10
L Ch Thng
L Ch Thng
sites.google.com/site/chithong
22
11
L Ch Thng
TMOD, #01H
TH0,#HIGH -50
TL0, #LOW -50
TR0
TF0, $
TF0
TR0
L Ch Thng
23
Timer 0 mode 1
Initial count = -50,000
tH
tL
f = 10 Hz
T = 100,000 s
tH = tL = 50,000 s
Delay 50,000 s
Overflow?
(TF1=1?)
Y
Clear overflow flag
Stop Timer 1
(schematic)
sites.google.com/site/chithong
Start Timer 1
24
12
L Ch Thng
(source)
25
L Ch Thng
sites.google.com/site/chithong
26
13
L Ch Thng
27
Timer 0 mode 2
Reload value = -50
Start Timer 1
Delay 50 s
Overflow?
(TF1=1?)
Y
Clear overflow flag
sites.google.com/site/chithong
28
14
L Ch Thng
TMOD, #02H
TH0, #-50
TR0
TF0, WAIT
TF0
P1.0
WAIT
L Ch Thng
29
Buzzer Interface
A buzzer is connected to P1.7 and a debounced switch is connected
to P1.6. Write a program that reads the logic level provided by the
switch and sounds the buzzer for 1 second for each 1-to-0 transition
detected.
L Ch Thng
sites.google.com/site/chithong
30
15
L Ch Thng
Buzzer Interface
HUNDRED EQU
COUNT EQU
LOOP:
WAIT:
100
-10000
ORG 0000H
MOV TMOD, #01H
JNB P1.6, LOOP ;wait for high level
JB P1.6, WAIT ;wait for low level
SETB P1.7
CALL DELAY
CLR P1.7
SJMP LOOP
L Ch Thng
31
L Ch Thng
sites.google.com/site/chithong
32
16
L Ch Thng
Technique
10
Software tuning
256
65536
No limit
16-bit timer
16-bit timer plus software loops
L Ch Thng
33
SETB P1.0
CLR
P1.0
SJMP LOOP
L Ch Thng
sites.google.com/site/chithong
34
17
L Ch Thng
DELAY1S:
PUSH
MOV
LOOP: MOV
MOV
SETB
JNB
CLR
CLR
DJNZ
POP
RET
TMOD, #00010000B
; Timer 1, mode1
07
R7,#20
TL1, #LOW(-50000)
TH1,#HIGH(-50000)
TR1
TF1, $
TF1
TR1
R7,LOOP
07
; Push R7 to stack
; 20 loops
; Initial count = -50000
;
; start Timer 1
; wait for overflow
; clear overflow flag
; stop Timer 1
; Pop R7 from stack
L Ch Thng
35
Counter Operation
Internal clock fCLK = fCrystal / 12
to Timer
External clock
C/T
0
1
Clock
Function
Internal Timer (interval timing, delay)
External Counter (event counting)
L Ch Thng
sites.google.com/site/chithong
36
18
L Ch Thng
Counter 0 Mode 1
C/T = 1
16-bit counter (TH0 and TL0)
TH0-TL0 is incremented when TR0 is set to 1 and an external
pulse (in T0) occurs.
Timer 0
external clock
input
(P3.4/T0)
C/T = 1
Overflow
flag
TH0
TL0
TF0
TR0
L Ch Thng
37
Counter_BarLED
A push button is connected to P3.4 (T0). Assume that there is no
contact bounce. Write a program that counts the pulses created by
push button and display on the LED-Bargraph connected to Port 1.
(schematic)
L Ch Thng
sites.google.com/site/chithong
38
19
L Ch Thng
Counter_BarLED
ORG 0000H
MAIN:
MOV TMOD,#00000101B;Timer 0, 16 bit, external clock
;(counter operation)
;Gate=0, C/T=1, M1 M0 = 01
MOV TH0,#0
MOV TL0,#0
SETB TR0
;Start Timer
LOOP: MOV A,TL0
;Read Timer
MOV P1,A
;Display on Bar-LED
SJMP LOOP
END
(source)
L Ch Thng
39
Counter_7segLED
A push button is connected to P3.4 (T0). Assume that there is no
contact bounce. Write a program that counts the pulses created by
push button and display on the common-anode 7-segment LED
connected to Port 1. (schematic)
L Ch Thng
sites.google.com/site/chithong
40
20
L Ch Thng
Counter_7segLED
MAIN:
LOOP:
NEXT:
ORG 0000H
MOV TMOD,#00000101B
MOV TH0,#0
MOV TL0,#0
SETB TR0
MOV A,TL0
CJNE A,#10,NEXT
CLR A
MOV TL0,#0
ACALL DISPLAY
SJMP LOOP
BCDTO7SEG:
MOV DPTR,#TABLE
MOVC A,@A+DPTR
RET
TABLE: DB 40h,79h,24h,30h,19h
DB 12h,02h,78h,00h,10h
DONE: NOP
END
DISPLAY:
ACALL BCDTO7SEG
MOV P1,A
RET
(source)
L Ch Thng
41
Frequency Meter_7segLED
A clock source is connected to P3.4 (T0). Write a program that
displays the frequency in KHz on the common-anode 7-segment
LED connected to Port 1. (schematic)
L Ch Thng
sites.google.com/site/chithong
42
21
L Ch Thng
Frequency Meter_7segLED
ORG 0000H
MOV TMOD,#00010101B
DISPLAY:
SETB P3.4 ;make T0 as input
ACALL BCDTO7SEG
AGAIN: MOV TL0,#0
MOV P1,A
MOV TL1,#LOW(-1000)
RET
MOV TH1,#HIGH(-1000)
SETB TR0
BCDTO7SEG:
SETB TR1
MOV DPTR,#TABLE
JNB TF1,$
MOVC A,@A+DPTR
CLR TF1
RET
CLR TR1
CLR TR0
TABLE: DB 40h,79h,24h,30h,19h
MOV A,TL0
DB 12h,02h,78h,00h,10h
ACALL DISPLAY
SJMP AGAIN
DONE: NOP
END
(source)
MAIN:
L Ch Thng
43
GATE=1
GATE=0
Internal control
The start and stop of the timer are controlled by the software
Ex: SETB TR1 ; Run Timer 1
CLR TR1 ; Stop Timer 1
GATE=1
External control
The hardware way of
starting and stopping
Timer Timer
the timer by software
stops runs
and an external source.
When GATE is set and TRx is set
(SETB TRx), Timer runs only while the INTx pin is high.
L Ch Thng
sites.google.com/site/chithong
44
22
L Ch Thng
Pulse_width_7segLED
A pulse source is connected to P3.2 (/INT0). Write a program that
displays the pulse width in ms (approximate) on the commonanode 7-segment LED connected to Port 1. (schematic)
L Ch Thng
45
Pulse_width_7segLED
ORG 0000H
MOV TMOD,#00001001B
;Timer 0,16 bit,internal clock,GATE=1
MOV TH0,#0
MOV TL0,#0
SETB TR0
AGAIN: MOV A,TH0
CJNE A,TH0,AGAIN
MOV B,#4
DIV AB
;A=Pulse width in us/256/4
;approximate /1000
ACALL DISPLAY
SJMP AGAIN
DISPLAY:
ACALL BCDTO7SEG
MOV P1,A
RET
BCDTO7SEG:
MOV DPTR,#TABLE
MOVC A,@A+DPTR
RET
TABLE: DB 40h,79h,24h,30h,19h
DB 12h,02h,78h,00h,10h
DONE: NOP
END
(source)
L Ch Thng
sites.google.com/site/chithong
46
23
L Ch Thng
References
I. Scott MacKenzie , The 8051 Microcontroller, 2nd
Edition, Prentice-Hall, 1995
Kenneth J. Ayala, The 8051 Microcontroller:
Architecture, Programming, and Applications, West
Publishing Company
hsabaghianb@kashanu.ac.jr , Lecture notes
L Ch Thng
sites.google.com/site/chithong
47
24