Microprocessing Systems - Chapter 4: Timer Operation - Lê Chí Thông

• 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

pdf24 trang | Chuyên mục: Vi Xử Lý – Vi Điều Khiển | Chia sẻ: tuando | Lượt xem: 529 | Lượt tải: 0download
Tóm tắt nội dung Microprocessing Systems - Chapter 4: Timer Operation - Lê Chí Thông, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 TF=0. When TH-TL roll over to 0000 from 
FFFFH (overflow), the TF is set to 1.
• If we enable interrupt, TF=1 will trigger ISR.
TCON Register (2)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
(MSB) (LSB)
19Lê Chí Thông
12 MHz Crystal  fCLK = 12MHz/12 = 1MHz 
 1 MC = 1/1MHz = 1 μs 
 tDelay = 100 μs = 100 MC
Use Timer 1 to count from -100 to 0
• Timer 1 mode 1, timer operation 
TMOD = 00010000B or 10H
• Initial count TH1:TL1 = -100
-100 = FF9CH
 TH1 = FFH and TL1 = 9CH
20Lê Chí Thông
Delay 100 µs using Timer 1 (12 MHz crystal)
TMOD  10H
TH1:TL1  -100
Run Timer 1
Overflow?
(TF1=1?)
N
Y
Clear overflow flag
Stop Timer 1
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 11
MOV TMOD, #00010000B; Timer 1, mode1
MOV TL1, #9CH ; Initial count
MOV TH1,#0FFH ; -100 = FF9CH
SETB TR1 ; start Timer 1
WAIT: JNB TF1, WAIT ; wait for overflow
CLR TF1 ; clear overflow flag
CLR TR1 ; stop Timer 1
Note:
MOV TL1,#9CH = MOV TL1,#LOW(-100)
MOV TH1,0FFH = MOV TH1,#HIGH(-100)
WAIT: JNB TF1, WAIT = JNB TF1,$
21Lê Chí Thông
Delay 100 µs using Timer 1 (12 MHz crystal)
TMOD  10H
TH1:TL1  -100
Run Timer 1
Overflow?
(TF1=1?)
N
Y
Clear overflow flag
Stop Timer 1
Your Turn!
22Lê Chí Thông
Delay 25 µs using Timer 0 (24 MHz crystal)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 12
MOV TMOD, #01H ; Timer 0, mode 1: 16 bit
MOV TH0,#HIGH -50 ; Load high byte
MOV TL0, #LOW -50 ; Load low byte
SETB TR0 ; start Timer 0
JNB TF0, $ ; wait for overflow
CLR TF0 ; clear Timer 0 overflow flag
CLR TR0 ; stop Timer 0
23Lê Chí Thông
Delay 25 µs using Timer 0 (24 MHz crystal)
Write a program using Timer 0 
to create a 10 Hz square wave on P1.0
24Lê Chí Thông
10-Hz Square Wave
Delay 50,000 μs
P1.0  NOT (P1.0)
Timer 0 mode 1
Initial count = -50,000
Start Timer 1
Overflow?
(TF1=1?)
N
Y
Clear overflow flag
Stop Timer 1
P1.0  NOT (P1.0)
tH tL
T
f = 10 Hz
T = 100,000 μs
tH = tL = 50,000 μs
(schematic)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 13
Write a program using Timer 0 to create a 10 Hz square wave on P1.0
MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode)
LOOP: MOV TH0, #HIGH(-50000); high byte of -50,000
MOV TL0, #LOW(-50000) ; low byte of -50,000
SETB TR0 ; start timer
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
25Lê Chí Thông
10-Hz Square Wave
Delay 50,000 μs
P1.0  NOT (P1.0)
(source)
Write a program using Timer 0 to create a 10 kHz square wave on P1.0
26Lê Chí Thông
10-kHz Square Wave
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 14
Write a program using Timer 0 to create a 10 kHz square wave on P1.0
MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode)
LOOP: MOV TH0, #HIGH(-50); high byte of -50
MOV TL0, #LOW(-50); low byte of -50
SETB TR0 ; start timer
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TR0 ; stop timer
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP LOOP ; repeat
27Lê Chí Thông
10-kHz Square Wave
Mode 1: 16-bit timer
Delay 50 μs
P1.0  NOT (P1.0)
Write a program using Timer 0 
to create a 10 kHz square wave on P1.0
28Lê Chí Thông
10-kHz Square Wave using Mode 2
Delay 50 μs
P1.0  NOT (P1.0)
Timer 0 mode 2
Reload value = -50
Start Timer 1
Overflow?
(TF1=1?)
N
Y
Clear overflow flag
P1.0  NOT (P1.0)
Mode 2: 8-bit auto-reload timer
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 15
Write a program using Timer 0 to create a 10 kHz square wave on P1.0
MOV TMOD, #02H ; Timer 0, mode 2 (8-bit auto-reload)
MOV TH0, #-50 ; reload value
SETB TR0 ; start timer
WAIT: JNB TF0, WAIT ; wait for overflow
CLR TF0 ; clear timer overflow flag
CPL P1.0 ; toggle port bit
SJMP WAIT ; repeat
29Lê Chí Thông
10-kHz Square Wave using Mode 2
Mode 2: 8-bit auto-reload timer
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.
30Lê Chí Thông
Buzzer Interface
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 16
HUNDRED EQU 100
COUNT EQU -10000
ORG 0000H
MOV TMOD, #01H
LOOP: JNB P1.6, LOOP ;wait for high level
WAIT: JB P1.6, WAIT ;wait for low level
SETB P1.7
CALL DELAY
CLR P1.7
SJMP LOOP
31Lê Chí Thông
Buzzer Interface
DELAY: MOV R7,#HUNDRED
AGAIN: MOV TH0,#HIGH COUNT
MOV TL0,#LOW COUNT
SETB TR0
JNB TF0,$
CLR TF0
CLR TR0
DJNZ R7,AGAIN
RET
END
• When reading the content of counter, is the data correct?
• 16-bit timer/counter, but 8-bit reading (MOV instruction) 
 2 read operations
 A “phase error” may occur.
• Solution
AGAIN:MOV A, TH1
MOV R6, TL1
CJNE A, TH1, AGAIN
MOV R7, A
32Lê Chí Thông
Reading a timer “On the Fly”
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 17
• 12 MHz operation
33Lê Chí Thông
Techniques for Programming Timed Intervals
Maximum
interval [μs]
Technique
≈10 Software tuning
256 8-bit timer with auto-reload
65536 16-bit timer
No limit 16-bit timer plus software loops
Very short intervals (i.e. high frequencies) can be 
programmed without using timers.
LOOP: SETB P1.0
CLR P1.0
SJMP LOOP
34Lê Chí Thông
Very Short Intervals
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 18
• 1 s delay subroutine: 1 s = 20 x 50 ms
MOV TMOD, #00010000B ; Timer 1, mode1
DELAY1S:
PUSH 07 ; Push R7 to stack
MOV R7,#20 ; 20 loops
LOOP: MOV TL1, #LOW(-50000) ; Initial count = -50000
MOV TH1,#HIGH(-50000) ; 
SETB TR1 ; start Timer 1
JNB TF1, $ ; wait for overflow
CLR TF1 ; clear overflow flag
CLR TR1 ; stop Timer 1
DJNZ R7,LOOP
POP 07 ; Pop R7 from stack
RET
35Lê Chí Thông
Delay 1 s using Timer 1 (12 MHz crystal)
to Timer
Internal clock fCLK = fCrystal / 12
External clock
36Lê Chí Thông
Counter Operation
C/T Clock Function
0 Internal Timer (interval timing, delay)
1 External Counter (event counting)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 19
37Lê 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 
external clock 
input 
(P3.4/T0)
TR0
TH0 TL0 TF0
TF0 goes high 
when FFFF  0
Overflow 
flag
C/T = 1
38Lê Chí Thông
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)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 20
39Lê 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)
40Lê Chí Thông
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)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 21
41Lê Chí Thông
Counter_7segLED
ORG 0000H
MAIN: MOV TMOD,#00000101B
MOV TH0,#0
MOV TL0,#0
SETB TR0
LOOP: MOV A,TL0
CJNE A,#10,NEXT
CLR A
MOV TL0,#0
NEXT: ACALL DISPLAY
SJMP LOOP
DISPLAY:
ACALL BCDTO7SEG
MOV P1,A
RET
(source)
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
42Lê Chí Thông
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)
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 22
43Lê Chí Thông
Frequency Meter_7segLED
ORG 0000H
MAIN: MOV TMOD,#00010101B
SETB P3.4 ;make T0 as input
AGAIN: MOV TL0,#0
MOV TL1,#LOW(-1000)
MOV TH1,#HIGH(-1000)
SETB TR0
SETB TR1 
JNB TF1,$
CLR TF1
CLR TR1
CLR TR0
MOV A,TL0 
ACALL DISPLAY
SJMP AGAIN
(source)
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
44Lê Chí Thông
• 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 
the timer by software
and an external source.
• When GATE is set and TRx is set
(SETB TRx), Timer runs only while the INTx pin is high.
GATE=1
Timer
runs
Timer
stops
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 23
45Lê 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)
46Lê Chí Thông
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
(source)
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
ĐH Bách Khoa TP.HCM Lê Chí Thông 
sites.google.com/site/chithong 24
47
References
Lê Chí Thông
• 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

File đính kèm:

  • pdfmicroprocessing_systems_chapter_4_timer_operation_le_chi_tho.pdf