Bài giảng Microprocessing Systems - Chapter 4: Timer operation - Lê Chí Thông
ĐH Bách Khoa TP.HCM
Lê Chí Thông
The 8051 Microcontroller
Chapter 4
Timer Operation
Lê Chí Thông
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
Lê Chí Thông
1
Review of 3-bit Up Counter
000ꢀ001ꢀ…ꢀ111ꢀ000
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
C/T
Clock
Function
0
1
Internal Timer (interval timing, delay)
External Counter (event counting)
Lê Chí Thông
5
Timer 1 mode 1 (16 bit)
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
Gate TRx
INTx Function
0
1
0
1
X
X
X
1
Clock Disable ꢀ Timer Stops
0
1
Clock Enable ꢀ Timer Runs
Clock Disable ꢀ Timer Stops
Clock Enable ꢀ Timer Runs
TRx: Bit TR0 or TR1; INTx: pin INT0 or pin INT1
Lê Chí Thông
7
Timer Run/Stop
Gate TRx
0
INTx Function
X
X
X
1
Clock Disable ꢀ Timer Stops
0
1
Clock Enable ꢀ Timer Runs
Clock Disable ꢀ Timer Stops
Clock Enable ꢀ Timer Runs
0
1
1
•
•
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 bit: More details
• 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
Timer Timer
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
Gate C/T Application
0
1
0
Delay
0
1
Event counting; Frequency meter
Pulse width meter
Lê Chí Thông
10
sites.google.com/site/chithong
5
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Timer Registers
• TH0, TL0 registers
• TH1, TL1 registers
• TMOD register
• TCON register
Lê Chí Thông
11
Timer 0 and Timer 1 Registers
Timer 0
TH0
TL0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
Timer 1
TH1
TL1
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
Lê Chí Thông
12
sites.google.com/site/chithong
6
ĐH Bách Khoa TP.HCM
Lê Chí Thông
TMOD Register
TMOD: Timer Mode Register
(MSB)
(LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
GATE
C/T
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
0 :
1 :
Timer operation (clock : Machine cycle)
Counter operation (clock : Tx input pin)
Lê Chí Thông
13
M1, M0: mode setting bits
M1 M0 Mode Operating mode
13-bit timer mode
0
0
1
0
1
0
0
1
2
8-bit THx + 5-bit TLx (x= 0 or 1)
16-bit timer mode
8-bit THx + 8-bit TLx
8-bit auto-reload mode
8-bit auto reload timer/counter;
THx holds a value which is to be reloaded into
TLx each time it overflows.
1
1
3
Split timer mode
Lê Chí Thông
14
sites.google.com/site/chithong
7
ĐH Bách Khoa TP.HCM
Lê Chí Thông
An Example
TMOD: Timer Mode Register
(MSB)
(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
TMOD: Timer Mode Register
(MSB)
(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
Mode 0 : 13-bit counter
(4048 mode)
Mode 1 : 16-bit counter
Mode 2 : 8-bit auto reload counter
Mode 3 : two 8-bit counter
the other counter will not
output overflow (interrupt)
Lê Chí Thông
17
TCON Register (1)
• Timer control register: TCON
– Upper nibble : timer/counter
– Lower nibble : interrupts
• TR (run control bit)
– TR0 : Timer/counter 0
– TR1: Timer/counter 1.
– Turn timer/counter on/off.
• TR=0: off (stop)
• TR=1: on (start)
(MSB)
(LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1
Timer0
for Interrupt
Lê Chí Thông
18
sites.google.com/site/chithong
9
ĐH Bách Khoa TP.HCM
Lê Chí Thông
TCON Register (2)
• TF (timer overflow flag)
– TF0 : for Timer/counter 0
– TF1 : for Timer/counter 1.
– Originally, 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.
(MSB)
(LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
Lê Chí Thông
19
Delay 100 µs using Timer 1 (12 MHz crystal)
12 MHz Crystal ꢀ fCLK = 12MHz/12 = 1MHz
ꢀ 1 MC = 1/1MHz = 1 μs
TMOD 10H
ꢀ tDelay = 100 μs = 100 MC
TH1:TL1 -100
ꢀ Use Timer 1 to count from -100 to 0
• Timer 1 mode 1, timer operation ꢀ
Run Timer 1
TMOD = 00010000B or 10H
• Initial count TH1:TL1 = -100
-100 = FF9CH
N
Overflow?
(TF1=1?)
ꢀ TH1 = FFH and TL1 = 9CH
Y
Clear overflow flag
Stop Timer 1
Lê Chí Thông
20
sites.google.com/site/chithong
10
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Delay 100 µs using Timer 1 (12 MHz crystal)
MOV TMOD, #00010000B; Timer 1, mode1
MOV TL1, #9CH
MOV TH1,#0FFH
SETB TR1
; Initial count
TMOD 10H
; -100 = FF9CH
; start Timer 1
TH1:TL1 -100
WAIT: JNB TF1, WAIT
; wait for overflow
; clear overflow flag
Run Timer 1
CLR
CLR
TF1
TR1
; stop Timer 1
N
Note:
Overflow?
(TF1=1?)
MOV TL1,#9CH = MOV TL1,#LOW(-100)
MOV TH1,0FFH = MOV TH1,#HIGH(-100)
WAIT: JNB TF1, WAIT = JNB TF1,$
Y
Clear overflow flag
Stop Timer 1
Lê Chí Thông
Delay 25 µs using Timer 0 (24 MHz crystal)
Your Turn!
Lê Chí Thông
22
sites.google.com/site/chithong
11
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Delay 25 µs using Timer 0 (24 MHz crystal)
MOV TMOD, #01H
; Timer 0, mode 1: 16 bit
; Load high byte
MOV TH0,#HIGH -50
MOV TL0, #LOW -50
SETB TR0
; Load low byte
; start Timer 0
JNB TF0, $
; wait for overflow
; clear Timer 0 overflow flag
; stop Timer 0
CLR
CLR
TF0
TR0
Lê Chí Thông
23
10-Hz Square Wave
Write a program using Timer 0
to create a 10 Hz square wave on P1.0
Timer 0 mode 1
Initial count = -50,000
Start Timer 1
f = 10 Hz
T = 100,000 μs
tH tL
tH = tL = 50,000 μs
T
N
Overflow?
(TF1=1?)
Delay 50,000 μs
Y
Clear overflow flag
P1.0 NOT (P1.0)
Stop Timer 1
P1.0 NOT (P1.0)
(schematic)
Lê Chí Thông
24
sites.google.com/site/chithong
12
ĐH Bách Khoa TP.HCM
Lê Chí Thông
10-Hz Square Wave
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
; stop timer
CLR
CLR
CPL
TR0
TF0
; clear timer overflow flag
; toggle port bit
; repeat
P1.0
SJMP LOOP
Delay 50,000 μs
(source)
P1.0 NOT (P1.0)
Lê Chí Thông
25
10-kHz Square Wave
Write a program using Timer 0 to create a 10 kHz square wave on P1.0
Lê Chí Thông
26
sites.google.com/site/chithong
13
ĐH Bách Khoa TP.HCM
Lê Chí Thông
10-kHz Square Wave
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
; stop timer
CLR
CLR
CPL
TR0
TF0
P1.0
; clear timer overflow flag
; toggle port bit
; repeat
SJMP LOOP
Delay 50 μs
Mode 1: 16-bit timer
P1.0 NOT (P1.0)
Lê Chí Thông
27
10-kHz Square Wave using Mode 2
Write a program using Timer 0
to create a 10 kHz square wave on P1.0
Timer 0 mode 2
Reload value = -50
Start Timer 1
Mode 2: 8-bit auto-reload timer
N
Overflow?
(TF1=1?)
Delay 50 μs
Y
Clear overflow flag
P1.0 NOT (P1.0)
P1.0 NOT (P1.0)
Lê Chí Thông
28
sites.google.com/site/chithong
14
ĐH Bách Khoa TP.HCM
Lê Chí Thông
10-kHz Square Wave using Mode 2
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
SETB TR0
; reload value
; start timer
WAIT: JNB TF0, WAIT
; wait for overflow
; clear timer overflow flag
; toggle port bit
; repeat
CLR
CPL
TF0
P1.0
SJMP WAIT
Mode 2: 8-bit auto-reload timer
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
DELAY: MOV R7,#HUNDRED
AGAIN: MOV TH0,#HIGH COUNT
HUNDRED EQU 100
COUNT EQU -10000
MOV TL0,#LOW COUNT
SETB TR0
JNB TF0,$
CLR TF0
CLR TR0
DJNZ R7,AGAIN
RET
END
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
Lê Chí Thông
31
Reading a timer “On the Fly”
• 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
Lê Chí Thông
32
sites.google.com/site/chithong
16
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Techniques for Programming Timed Intervals
• 12 MHz operation
Maximum
interval [μs]
Technique
≈10
Software tuning
256
8-bit timer with auto-reload
16-bit timer
65536
No limit
16-bit timer plus software loops
Lê Chí Thông
33
Very Short Intervals
Very short intervals (i.e. high frequencies) can be
programmed without using timers.
LOOP:
SETB P1.0
CLR P1.0
SJMP LOOP
Lê Chí Thông
34
sites.google.com/site/chithong
17
ĐH Bách Khoa TP.HCM
Lê Chí Thông
Delay 1 s using Timer 1 (12 MHz crystal)
• 1 s delay subroutine: 1 s = 20 x 50 ms
MOV
…
TMOD, #00010000B
; Timer 1, mode1
DELAY1S:
PUSH 07
MOV R7,#20
; Push R7 to stack
; 20 loops
LOOP: MOV
MOV
SETB
JNB
TL1, #LOW(-50000)
; Initial count = -50000
;
TH1,#HIGH(-50000)
TR1
; start Timer 1
; wait for overflow
; clear overflow flag
; stop Timer 1
TF1, $
TF1
CLR
CLR
TR1
DJNZ
POP
R7,LOOP
07
; Pop R7 from stack
RET
Lê Chí Thông
35
Counter Operation
Internal clock fCLK = fCrystal / 12
to Timer
C/T
0
Clock
Function
Internal Timer (interval timing, delay)
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
external clock
input
Overflow
flag
TH0
TL0
TF0
(P3.4/T0)
TF0 goes high
when FFFF ꢀ 0
C/T = 1
TR0
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
MOV P1,A
SJMP LOOP
END
;Read Timer
;Display on Bar-LED
(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
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Microprocessing Systems - Chapter 4: Timer operation - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_microprocessing_systems_chapter_4_timer_operation.pdf

