Академический Документы
Профессиональный Документы
Культура Документы
Objective
8051 2 Timer/Counter
Timer Counter Timer Counter
Sections
9.1 Programming 8051 timers 9.2 Counter programming 9.3 Programming timers 0 and 1 in 8051 C
8051 (8031)
On-chip RAM
Timer 1 Timer 0
Counter Inputs
CPU
Serial Port
OSC
Bus Control
4 I/O Ports
P0 P1 P2 P3
TxD RxD
Timers /Counters
The 8051 has 2 timers/counters: timer/counter 0 and timer/counter 1. They can be used as 1. The timer is used as a time delay generator.
The clock source is the internal crystal frequency of the 8051.
2. An event counter.
External input from input pin to count the number of events on registers. These clock pulses cold represent the number of people passing through an entrance, or the number of wheel rotations, or any other event that can be converted to pulses.
Timer
Set the initial value of registers Start the timer and then the 8051 counts up. When the registers equal to 0, the 8051 sets a bit to denote time out 8051 timers use 1/12 of XTAL frequency, regardless of machine cycle. 8051
XTAL oscillator 12 Timer
P1
Set Timer 0
TH0 TL0
to LCD
8
Counter
Count the number of events
Show the number of events on registers External input from T0 input pin (P3.4) for Counter 0 External input from T1 input pin (P3.5) for Counter 1 External input from Tx input pin. 8051 We use Tx to denote T0 or T1.
TH0
P1
TL0
Vcc
to LCD
P3.4 a switch T0
9
10
Each 16-bit timer can be accessed as two separate registers of low byte and high byte.
11
Timer Registers
TH0
D15 D14 D13 D12 D11 D10 D9 D8
TL0
D7 D6 D5 D4 D3 D2 D1 D0
Timer 0
TH1
D15 D14 D13 D12 D11 D10 D9 D8
TL1
D7 D6 D5 D4 D3 D2 D1 D0
Timer 1
12
IE1
(LSB) IT0
13
IE1
(LSB) IT0
14
= = = =
TMOD Register
Timer mode register: TMOD
MOV TMOD,#21H An 8-bit register Set the usage mode for two timers
Set lower 4 bits for Timer 0 Set upper 4 bits for Timer 1 (Set to 0000 if not used) (Set to 0000 if not used)
Not bit-addressable
(LSB) M0
16
C/T (Clock/Timer)
This bit is used to decide whether the timer is used as a delay generator or an event counter. C/T = 0 : timer C/T = 1 : counter
18
Gate
Every timer has a mean of starting and stopping.
GATE=0
Internal control The start and stop of the timer are controlled by software. Set/clear the TR for start/stop timer.
GATE=1
External control The hardware way of starting and stopping the timer by software and an external source. Timer/counter is enabled only while the INT pin is high and the TR control pin is set (TR). INT0: P3.2, pin 12; INT1: P3.3, pin 13.
19
M1, M0
M0 and M1 select the timer mode for timers 0 & 1.
M1 M0 Mode 0 0 0 0 1 1 0 1 2 Operating Mode 13-bit timer mode 8-bit THx + 5-bit TLx (x= 0 or 1) 16-bit timer mode 8-bit THx + 8-bit TLx 8-bit auto reload 8-bit auto reload timer/counter; THx holds a value which is to be reloaded into TLx each time it overflows. Split timer mode
20
1 1
Example 9-1
Indicate which mode and which timer are selected for each of the following. (a) MOV TMOD,#01H (b) MOV TMOD,#20H (c) MOV TMOD,#12H
Solution:
timer 1 timer 0
(b) TMOD = 00100000, mode 2 of timer 1 is selected. (c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.
21
Example 9-2
Find the timers clock frequency and its period for various 8051based systems, with the following crystal frequencies. (a) 12 MHz (b) 16 MHz (c) 11.0592 MHz
Solution: XTAL oscillator
12
(a) 1/12 12 MHz = 1 MHz and T = 1/1 MHz = 1 s (b) 1/12 16 MHz = 1.333 MHz and T = 1/1.333 MHz = 0.75 s (c) 1/12 11.0592 MHz = 921.6 KHz; T = 1/921.6 KHz = 1.085 s
22
Example 9-3
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 XTAL clock source (timer) gate = 0 to use internal (software) start and stop method.
23
Timer Mode 1
In following, we all use timer 0 as an example. 16-bit timer (TH0 and TL0) TH0-TL0 is incremented continuously when TR0 is set to 1. And the 8051 stops to increment TH0-TL0 when TR0 is cleared. The timer works with the internal system clock. In other words, the timer counts up every 12 clocks from XTAL. When the timer (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0 is raised. Programmer should check TF0 and stop the timer 0.
24
Mode 1 Programming
XTAL oscillator
12
C/T = 0
TH
Start timer
TL
TF
TR
overflow flag
25
TH0
TL0
FFFC
TF = 0
FFFD
TF = 0
TF
FFFE TF = 0
FFFF
TF = 0
0000
TF = 1
27
The delay = 4 MCs for 89C51 If MC=1.085 s, then the delay = 4.34 s Figure 9-4 show a formula for delay calculations using mode 1 of the timer for a crystal frequency of XTAL=11.0592 MHz. Examples 9-4 to 9-9 show how to calculations the delay generated by timer.
29
FFF2
TF0 = 0
FFF3
TF0 = 0
FFF4
TF0 = 0
FFFF
TF0 = 0
0000
TF0 = 1
32
33
34
Example 9-5
In Example 9-4, calculate the amount of time delay in the DELAY subroutine generated by the timer. Assume that XTAL = 11.0592 MHz. Solution:
The timer works with the internal system clock. frequency of internal system clock = 11.0592/12 = 921.6 KHz machine cycle = 1 /921.6 KHz = 1.085 s (microsecond) The number of counts = FFFFH FFF2H +1 = 14 (decimal). The delay = number of counts 1.085 s = 14 1.085 s = 15.19 s for half the clock. For the entire period of a clock, it is T = 2 15.19 s = 30.38 s as the time delay generated by the timer.
35
36
1 14 1 1 2 0 28
37
38
TH0=TL0=0 means that the timer will count from 0000 to FFFF, and then roll over to raise the TF0 flag. As a result, it goes through a total of 65536 states. Therefore, we have delay = (65536 0) 1.085 s = 71.1065 ms.
40
41
42
43
How to find the inter values needed for the TH, TL?
Divide the desired time delay by 1.085 s. Perform 65536 n, where n is the decimal value we got in Step 1. Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timers registers. Set TH = yy and TL = xx. Example 9-10
44
45
;Timer 0,mode 1
P2.3
46
48
The largest time delay is achieved by making TH=TL=0. What if that is not enough? Example 9-13 show how to achieve large time delay.
51
Example 9-13
Examine the following program and find the time delay in seconds. Exclude the overhead due to the instructions in the loop. MOV TMOD,#10H MOV R3,#200 AGAIN: MOV TL1,#08 MOV TH1,#01 SETB TR1 BACK: JNB TF1,BACK CLR TR1 CLR TF1 DJNZ R3,AGAIN Solution: TH TL = 0108H = 264 in decimal 65536 264 = 65272. One of the timer delay = 65272 1.085 s = 70.820 ms Total delay = 200 70.820 ms = 14.164024 seconds
52
Timer Mode 0
Mode 0 is used to compatible with MSC-48 Mode 0 is exactly like mode 1 except that it is a 13bit timer instead of 16-bit.
8-bit TH0 + 5-bit TL0
We set the initial values TH0-TL0 to count up. When the timer reaches its maximum of 1FFFH, it rolls over to 0000, and TF0 is raised.
53
Timer Mode 2
8-bit timer.
TL0 is incremented (or roll over) continuously when TR0=1.
Auto-reloading
TH0 is loaded into TL0 automatically when TL0=00H. TH0 is not changed . You need to clear TF0 after TL0 rolls over.
In the following example, we want to generate a delay with 200 MCs on timer 0. See Examples 9-14 to 9-16
54
Mode 2 programming
XTAL oscillator
12
C/T = 0
TL
reload
TF
overflow flag
TR
TH
5. After TH0 is loaded with the 8-bit value, the 8051 gives a copy of it to TL0.
TL0=TH0=38H
56
7. When TL0 rolls over from FFH to 00, the 8051 set TF0=1. Also, TL0 is reloaded automatically with the value kept by the TH0.
TL0= FEH, FFH, 00H (Now TF0=1) The 8051 auto reload TL0=TH0=38H. Go to Step 6 (i.e., TL0 is incrementing continuously).
Note that we must clear TF0 when TL0 rolls over. Thus, we can monitor TF0 in next process. Clear TR0 to stop the process.
57
58
Example 9-15
Find the frequency of a square wave generated on pin P1.0. Solution: MOV TMOD,#2H ;Timer 0,mode 2 MOV TH0,#0 AGAIN:MOV R5,#250 ;count 250 times ACALL DELAY CPL P1.0 SJMP AGAIN DELAY:SETB TR0 ;start BACK: JNB TF0,BACK CLR TR0 ;stop CLR TF0 ;clear TF DJNZ R5,DELAY ;timer 2: auto-reload RET T = 2 (250 256 1.085 s) = 138.88 ms, and frequency = 72 Hz.
60
or
MOV TH1,#-200
Way 1: 256-200 = 56 = 38H Way 2: -200 = -C8H 2s complement of 200 = 100H C8H = 38 H
61
Example 9-16
Assuming that we are programming the timers for mode 2, find the value (in hex) loaded into TH for each of the following cases. (a) MOV TH1,#-200 (b) MOV TH0,#-60 (c) MOV TH1,#-3 (d) MOV TH1,#-12 (e) MOV TH0,#-48 Solution: Some 8051 assemblers provide this way. -200 = -C8H 2s complement of 200 = 100H C8H = 38 H Decimal -200 = - C8H - 60 = - 3CH - 3 - 12 - 48 2s complement (TH value) 38H C4H FDH F4H D0H
62
;start
;stop ;clear TF
64
65
Counter
These timers can also be used as counters counting events happening outside the 8051. When the timer is used as a counter, it is a pulse outside of the 8051 that increments the TH, TL. When C/T=1, the counter counts up as pulses are fed from
T0: timer 0 input (Pin 14, P3.4) T1: timer 1 input (Pin 15, P3.5)
66
14 15 (MSB) GATE
P3.4 P3.5
T0 T1
C/T=1 M1 Timer 1
M0
GATE
C/T=1 M1 Timer 0
M0
67
Counter Mode 1
16-bit counter (TH0 and TL0) TH0-TL0 is incremented when TR0 is set to 1 and an external pulse (in T0) occurs. When the counter (TH0-TL0) reaches its maximum of FFFFH, it rolls over to 0000, and TF0 is raised. Programmers should monitor TF0 continuously and stop the counter 0. Programmers can set the initial value of TH0-TL0 and let TF0 as an indicator to show a special condition. (ex: 100 people have come).
68
69
70
Counter Mode 2
8-bit counter.
TL0 is incremented if TR0=1 and external pulse occurs.
Auto-reloading
TH0 is loaded into TL0 when TF0=1. It allows only values of 00 to FFH to be loaded into TH0. You need to clear TF0 after TL0 rolls over.
See Figure 9.6, 9.7 for logic view See Examples 9-18, 9-19
71
TF0
TR0
C/T = 1
72
TF1
TR1
TH1
73
74
T1
75
T0
R4
R3
R2
78
A Digital Clock
Example 9-19 shows a simple digital clock.
If we feed an external square wave of 60 Hz frequency into the timer/counter, we can generate the second, the minute, and the hour out of this input frequency and display the result on an LCD.
You might think that the use of the instruction JNB TF0,target to monitor the raising of the TF0 flag is a waste of the microcontrollers time.
The solution is the use of interrupt. See Chapter 11.
In using interrupts we can do other things with the 8051.
GATE=1 in TMOD
All discuss so far has assumed that GATE=0.
The timer is stared with instructions SETB TR0 and SETB TR1 for timers 0 and 1, respectively.
If GATE=1, we can use hardware to control the start and stop of the timers.
SETB TR0 and SETB TR1 for timers 0 and 1 are necessary. INT0 (P3.2, pin 12) starts and stops timer 0 INT1 (P3.3, pin 13) starts and stops timer 1 This allows us to start or stop the timer externally at any time via a simple switch.
80
12
C/T = 0
TR0
12
C/T = 0
TR1
Timer/Counter Mode 3
For Timer/Counter 0
As a Timer:
Two 8-bit timers (TH0/TL0)
As a Counter:
8-bit counter. (TL0)
For Timer/Counter 1
Not available
84
85
8051 Timers in C
In 8051 C we can access the timer registers TH, TL, and TMOD directly using the reg51.h header file.
See Example 9-20
86
87
88
91
The same codes put on AT89C51 and DS89C420 will generate different time delay.
The factors of C compiler and MC of others instructions
The C compiler is a factor in the delay size since various 8051 C compilers generate different hex code sizes. So, we still have approximate delay only.
See Examples 9-21
92
93
94
95
8051 Counters in C
External pulses to T0 (P3.4) and T1 (P3.5). See Examples 9-26 to 9-29.
96
P1
TL1
LEDs
P3.5
T1
97
99
Homework
Chapter 9 Problems9,20,22,26,31,36,44,47,48,53
100
, pass, ! , , . , , , !
101