Anda di halaman 1dari 10

H Bch Khoa TP.

HCM

L Ch Thng www.tinyurl.com/thongchile

The 8051 Microcontroller Chapter 4 Timer Operation

L Ch Thng Ref. I. Scott Mackenzie, The 8051 Microcontroller

Timer/Counter
Timer : counting the internal clock Counter : counting the external signal Generate interrupt 2 16-bit counters for 8051

Ref. I. Scott Mackenzie

L Ch Thng

H Bch Khoa TP.HCM

L Ch Thng www.tinyurl.com/thongchile

FIGURE 43

Clocking source

Internal clock To Counter/Timer

Falling edge-trigger

External clock

Ref. I. Scott Mackenzie

L Ch Thng

FIGURE 41

A 3-bit timer. (a) Schematic. (b) Timing diagram.

Operation of UP-counter

Ref. I. Scott Mackenzie

L Ch Thng

H Bch Khoa TP.HCM

L Ch Thng www.tinyurl.com/thongchile

Timer Register
Timer 0: TL0, TH0 (8AH, 8CH) Timer 1: TL1, TH1 (8BH, 8DH)

Timer Mode Register : TMOD (89H)


GATE 1, 0 : Bit 7, 3 - G=1 only run when ~INT1=1 C/~T 1, 0: Bit 6, 2 (C/1 counter, ~T/0 timer) Bit 5,4 : operation modes (T1) Bit 1,0 : operation modes (T0)

Ref. I. Scott Mackenzie

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.

Ref. I. Scott Mackenzie

L Ch Thng

H Bch Khoa TP.HCM

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)

Ref. I. Scott Mackenzie

L Ch Thng

Timer Control Register: TCON (88H)


bit-addressable TF 1, 0 : Bit 7, 5 - timer overflow TR 1,0 : Bit 6, 4 timer RUN set-run, clear-stop IE 1, 0 : Bit 3, 2 External interrupt edge flag (~INT1, ~INT0) HW set, SW clear IT 1, 0 : Bit 1,0 - External interrupt type flag set - falling edge clear low-level

Ref. I. Scott Mackenzie

L Ch Thng

H Bch Khoa TP.HCM

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

Starting and stopping the timers

Timer RUN/STOP TR0 8CH TR1 8FH

Ref. I. Scott Mackenzie

L Ch Thng

10

H Bch Khoa TP.HCM

L Ch Thng www.tinyurl.com/thongchile

FIGURE 45

Timer 1 operating in mode 1

Ref. I. Scott Mackenzie

L Ch Thng

11

Programming Timer
Delay 100 s using Timer (12 MHz crystal)

MOV MOV MOV SETB WAIT: JNB CLR CLR

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

Note: MOV TL1, #9CH MOV TH1, 0FFH


Ref. I. Scott Mackenzie

= =
L Ch Thng

MOV TL1,#LOW(-100) MOV TH1,#HIGH(-100)


12

H Bch Khoa TP.HCM

L Ch Thng www.tinyurl.com/thongchile

16-bit counter mode


Write a program using Timer 0 to create a 1 kHz square wave on P1.0

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

Ref. I. Scott Mackenzie

L Ch Thng

13

Auto-reload mode
Write a program using Timer 0 to create a 10 kHz square wave on P1.0

MOV MOV SETB WAIT: JNB CLR CPL SJMP

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

Ref. I. Scott Mackenzie

L Ch Thng

14

H Bch Khoa TP.HCM

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.

Ref. I. Scott Mackenzie

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

H Bch Khoa TP.HCM

L Ch Thng www.tinyurl.com/thongchile

Reading a Timer On the Fly


; you read the content of counter, is the data correct? AGAIN:MOV A, TH1 MOV R6, TH0 CJNZ A, TH1, AGAIN MOV R7, A

Generate periodic signal (12 MHz operation)


10 s 256 s 65536 s No limit software 8-bit counter 16-bit counter 16-bit counter + software loops
L Ch Thng 17

Ref. I. Scott Mackenzie

Very short intervals (i.e. high frequencies) can be programmed without using timers. LOOP: SETB CLR SJMP P1.0 P1.0 LOOP

Ref. I. Scott Mackenzie

L Ch Thng

18

H Bch Khoa TP.HCM

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

Anda mungkin juga menyukai