Professional Documents
Culture Documents
8051 Chap4 Timer PDF
8051 Chap4 Timer PDF
Lê Chí Thông
chithong@hcmut.edu.vn
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
Lê Chí Thông 1
000
001
…
111
000
Lê Chí Thông 2
Overflow
sites.google.com/site/chithong 1
ĐH Bách Khoa TP.HCM Lê Chí Thông
Timer/Counter
• 8051 has 2 timers
– Timer 0
– Timer 1
• Each Timer is a 16-bit up counter
– Counts from 0000H to FFFFH
– FFFFH-to-0000H overflow: overflow flag is set
Timer Clock
Lê Chí Thông 3
Timer/Counter
• 3 Functions
1. Timer is used as time delay generator (interval
timing)
– Internal clock source
2. An event counter (event counting)
– External clock source
– For example :
• number of people passing through an entrance
• number of wheel rotations
• any other event that can be converted to pulses
3. Baud rate generation for serial port
Lê Chí Thông 4
sites.google.com/site/chithong 2
ĐH Bách Khoa TP.HCM Lê Chí Thông
Clock Source
Internal clock fCLK = fCrystal / 12
to Timer
External clock
Lê Chí Thông 5
Lê Chí Thông 6
sites.google.com/site/chithong 3
ĐH Bách Khoa TP.HCM Lê Chí Thông
Clock Enable/Disable
Clock Enable/Disable
Timer Run/Stop
Gate TRx INTx Function
0 X Clock Disable Timer Stops
0
1 X Clock Enable Timer Runs
0 X Clock Disable Timer Stops
1
1 1 Clock Enable Timer Runs
• When Gate is cleared (0)
• Timer runs when TRx = 1
• Timer stops when TRx = 0
• When Gate is set (1)
• Timer runs when TRx = 1 and signal at pin INTx goes high
• Timer stops when TRx = 0 or signal at pin INTx goes low
TRx: Bit TR0 or TR1; INTx: pin INT0 or pin INT1
Lê Chí Thông 8
sites.google.com/site/chithong 4
ĐH Bách Khoa TP.HCM Lê Chí Thông
• 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í Thông 9
Applications of Timer
0 Delay
0
1 Event counting; Frequency meter
Lê Chí Thông 10
sites.google.com/site/chithong 5
ĐH Bách Khoa TP.HCM Lê Chí Thông
Timer Registers
• TMOD register
• TCON register
Lê Chí Thông 11
Timer 0
TH0 TL0
Timer 1
TH1 TL1
Lê Chí Thông 12
sites.google.com/site/chithong 6
ĐH Bách Khoa TP.HCM Lê Chí Thông
TMOD Register
(MSB) TMOD: Timer Mode Register (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
GATE 0: Timer/counter counts only while TRx bit is set.
1: Timer/counter counts only while TRx bit is set and
INTx pin is high
Lê Chí Thông 13
Lê Chí Thông 14
sites.google.com/site/chithong 7
ĐH Bách Khoa TP.HCM Lê Chí Thông
An Example
(MSB) TMOD: Timer Mode Register (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
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.
Solution:
TMOD= 0000 0010 Timer 1 is not used.
Timer 0, mode 2
C/T = 0 to use internal clock source (timer)
GATE = 0 to use internal (software) start and
stop method.
Lê Chí Thông 15
More Examples
(MSB) TMOD: Timer Mode Register (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
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í Thông 16
sites.google.com/site/chithong 8
ĐH Bách Khoa TP.HCM Lê Chí Thông
4 Timer Modes
sites.google.com/site/chithong 9
ĐH Bách Khoa TP.HCM Lê Chí Thông
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
Lê Chí Thông 19
Stop Timer 1
Lê Chí Thông 20
sites.google.com/site/chithong 10
ĐH Bách Khoa TP.HCM Lê Chí Thông
Lê Chí Thông 22
sites.google.com/site/chithong 11
ĐH Bách Khoa TP.HCM Lê Chí Thông
Lê Chí Thông 23
Delay 50,000 μs Y
Clear overflow flag
sites.google.com/site/chithong 12
ĐH Bách Khoa TP.HCM Lê Chí Thông
(source)
P1.0 NOT (P1.0)
Lê Chí Thông 25
Lê Chí Thông 26
sites.google.com/site/chithong 13
ĐH Bách Khoa TP.HCM Lê Chí Thông
Start Timer 1
Mode 2: 8-bit auto-reload timer
N
Overflow?
(TF1=1?)
Delay 50 μs
Y
P1.0 NOT (P1.0) Clear overflow flag
sites.google.com/site/chithong 14
ĐH Bách Khoa TP.HCM Lê Chí Thông
Lê Chí Thông 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í Thông 30
sites.google.com/site/chithong 15
ĐH Bách Khoa TP.HCM Lê Chí Thông
Buzzer Interface
HUNDRED EQU 100 DELAY: MOV R7,#HUNDRED
COUNT EQU -10000 AGAIN: MOV TH0,#HIGH COUNT
MOV TL0,#LOW COUNT
ORG 0000H SETB TR0
MOV TMOD, #01H JNB TF0,$
LOOP: JNB P1.6, LOOP ;wait for high level CLR TF0
WAIT: JB P1.6, WAIT ;wait for low level CLR TR0
SETB P1.7 DJNZ R7,AGAIN
CALL DELAY RET
CLR P1.7 END
SJMP LOOP
Lê Chí Thông 31
Lê Chí Thông 32
sites.google.com/site/chithong 16
ĐH Bách Khoa TP.HCM Lê Chí Thông
Maximum Technique
interval [μs]
≈10 Software tuning
Lê Chí Thông 33
Lê Chí Thông 34
sites.google.com/site/chithong 17
ĐH Bách Khoa TP.HCM Lê Chí Thông
Counter Operation
Internal clock fCLK = fCrystal / 12
to Timer
External clock
Lê Chí Thông 36
sites.google.com/site/chithong 18
ĐH Bách Khoa TP.HCM Lê Chí Thông
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 Overflow
external clock flag
input
(P3.4/T0) TH0 TL0 TF0
Lê Chí Thông 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í Thông 38
sites.google.com/site/chithong 19
ĐH Bách Khoa TP.HCM Lê Chí Thông
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í Thông 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í Thông 40
sites.google.com/site/chithong 20
ĐH Bách Khoa TP.HCM Lê Chí Thông
Counter_7segLED
ORG 0000H
MAIN: MOV TMOD,#00000101B BCDTO7SEG:
MOV TH0,#0 MOV DPTR,#TABLE
MOV TL0,#0 MOVC A,@A+DPTR
SETB TR0 RET
LOOP: MOV A,TL0
CJNE A,#10,NEXT TABLE: DB 40h,79h,24h,30h,19h
CLR A DB 12h,02h,78h,00h,10h
MOV TL0,#0
NEXT: ACALL DISPLAY DONE: NOP
SJMP LOOP END
DISPLAY:
ACALL BCDTO7SEG
MOV P1,A
RET
(source)
Lê Chí Thông 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í Thông 42
sites.google.com/site/chithong 21
ĐH Bách Khoa TP.HCM Lê Chí Thông
Frequency Meter_7segLED
ORG 0000H
MAIN: 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)
Lê Chí Thông 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í Thông 44
sites.google.com/site/chithong 22
ĐH Bách Khoa TP.HCM Lê Chí Thông
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 common-
anode 7-segment LED connected to Port 1. (schematic)
Lê Chí Thông 45
Pulse_width_7segLED
ORG 0000H
MOV TMOD,#00001001B DISPLAY:
;Timer 0,16 bit,internal clock,GATE=1 ACALL BCDTO7SEG
MOV TH0,#0 MOV P1,A
MOV TL0,#0 RET
SETB TR0
AGAIN: MOV A,TH0 BCDTO7SEG:
CJNE A,TH0,AGAIN MOV DPTR,#TABLE
MOV B,#4 MOVC A,@A+DPTR
DIV AB RET
;A=Pulse width in us/256/4
;approximate /1000 TABLE: DB 40h,79h,24h,30h,19h
ACALL DISPLAY DB 12h,02h,78h,00h,10h
SJMP AGAIN
DONE: NOP
(source) END
Lê Chí Thông 46
sites.google.com/site/chithong 23
ĐH Bách Khoa TP.HCM Lê Chí Thông
References
Lê Chí Thông 47
sites.google.com/site/chithong 24