Professional Documents
Culture Documents
HCM
L Ch Thng www.tinyurl.com/thongchile
Timer/Counter
Timer : counting the internal clock Counter : counting the external signal Generate interrupt 2 16-bit counters for 8051
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
FIGURE 43
Clocking source
Falling edge-trigger
External clock
L Ch Thng
FIGURE 41
Operation of UP-counter
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
Timer Register
Timer 0: TL0, TH0 (8AH, 8CH) Timer 1: TL1, TH1 (8BH, 8DH)
L Ch Thng
TMOD Register
Gate : When set, timer only runs while INT(0,1) is C/T =0 : Timer mode; C/T =1 : Counter mode M1 : Mode bit 1. M0 : Mode bit 0.
high.
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
FIGURE 42
Timer modes (a) Mode 0 (b) Mode 1 (c) Mode 2 (d) Mode 3
Mode 0 : 13-bit counter (4048 mode) Mode 1 : 16-bit counter Mode 2 : 8-bit reloadable counter Mode 3 : two 8-bit counter the other counter will not output overflow (interrupt)
L Ch Thng
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
TCON Register
TF1: Timer 1 overflow flag. TR1: Timer 1 run control bit. TF0: Timer 0 overflag. TR0: Timer 0 run control bit. IE1: External interrupt 1 edge flag. IT1: External interrupt 1 type flag. IE0: External interrupt 0 edge flag. IT0: 0 type Ref. I. Scott Mackenzie External interrupt L Ch Thng flag.
FIGURE 44
L Ch Thng
10
L Ch Thng www.tinyurl.com/thongchile
FIGURE 45
L Ch Thng
11
Programming Timer
Delay 100 s using Timer (12 MHz crystal)
TMOD, #00010000B ; Timer1 mode1 TL1, #9CH ; FF9CH = -100 TH1,#0FFH ; or count = 100 TR1 ; start Timer 1 TF1, WAIT ; wait for overflow TF1 ; clear Timer 1 overflow flag TR1 ; stop Timer 1
= =
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
MOV LOOP: MOV MOV SETB WAIT: JNB CLR CLR CPL SJMP
TMOD, #01H TH0, #0FEH TL0, #0CH TR0 TF0, WAIT TR0 TF0 P1.0 LOOP
; Timer1 mode 1 (16-bit timer mode) ; high byte of -500 ; low byte of -500 ; start timer ; wait for overflow ; stop timer ; clear timer overflow flag ; toggle port bit ; repeat
L Ch Thng
13
Auto-reload mode
Write a program using Timer 0 to create a 10 kHz square wave on P1.0
TMOD, #02H TH0, #-50 TR0 TF0, WAIT TF0 P1.0 WAIT
; Timer 0 mode 2 (8-bit auto-reload) ;-50 reload value in TH0 ; start timer ; wait for timer overflow ; clear timer overflow flag ; toggle port bit ; repeat
L Ch Thng
14
L Ch Thng www.tinyurl.com/thongchile
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
15
HUNDRED EQU 100 COUNT EQU 10000 ORG 0000H MOV TMOD, #01H LOOP: JNB P1.6, LOOP WAIT: JB P1.6, WAIT SETB P1.7 CALL DELAY CLR P1.7 SJMP LOOP 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
Ref. I. Scott Mackenzie
L Ch Thng
16
L Ch Thng www.tinyurl.com/thongchile
Very short intervals (i.e. high frequencies) can be programmed without using timers. LOOP: SETB CLR SJMP P1.0 P1.0 LOOP
L Ch Thng
18
L Ch Thng www.tinyurl.com/thongchile
References
I. Scott Mackenzie, The 8051 Microcontroller Cc ti li u trn Internet khng trch d n ho c khng ghi tc gi
L Ch Thng
19
10