Professional Documents
Culture Documents
Objective
Embedded Systems with Timer/Counter
(count up)(count down)
8051 2 Timer/Counter
Timer Counter Timer Counter
2
Sections
9.1 Programming 8051 timers 9.2 Counter programming 9.3 Programming timers 0 and 1 in 8051 C
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
8051 timers use 1/12 of XTAL frequency as the input of timers, regardless of machine cycle. Because the input of timer is a regular, fixedperiodic square wave, we can count the number of pulses and calculate the time delay.
XTAL oscillator
8051
12 Timer
P1
TH0
to LCD
7
Set Timer 0
TL0
Counter
Count the number of events External input from Tx input pin (x=0 or 1). We use Tx to denote T0 or T1.
External input from T0 input pin (P3.4) for Counter 0 External input from T1 input pin (P3.5) for Counter 1 8051
TH0
P1
TL0
Vcc
to LCD
8
a switch
T0
P3.4
8051 (8031)
12
C/T = 0 TH0 TL0
0:stop
Gate
INT0 Pin Pin 3.2 hardware control
Sec 9.2
10
0:stop
11
13
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
14
IE1
(LSB) IT0
15
IE1
(LSB) IT0
16
= =
= = = =
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
18
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
20
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 TR0 (or TR1) 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 INT0 (or INT1) pin has an 1 to 0 transition and the TR0 (or TR1) control pin is set. INT0: P3.2, pin 12; INT1: P3.3, pin 13.
21
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 (x= 0 or 1) 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
22
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 (LSB) Solution: (MSB)
GATE C/T M1 Timer 1 M0 GATE C/T M1 Timer 0 M0
timer 1
timer 0
(a) TMOD = 00000001, mode 1 of timer 0 is selected. (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.
23
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
24
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.
(MSB) (LSB) C/T M1 Timer 1 M0 GATE C/T M1 Timer 0 M0
Solution:
GATE
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.
25
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.
26
Mode 1 Programming
XTAL oscillator
12
C/T = 0
TH
Start timer
TL
TF
TR
overflow flag
27
TH0
TL0
FFFC
TF0=0
FFFD
TF0=0
TF0
FFFE TF0=0
FFFF
TF0=0
0000
TF0=1
29
Roll over
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.
31
33
FFF2
TF0 = 0
FFF3
TF0 = 0
FFF4
TF0 = 0
FFFF
TF0 = 0
0000
TF0 = 1
35
36
37
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.
38
1 14 1 1 2 0 28
40
41
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.
43
44
45
46
47
;Timer 0,mode 1
P2.3
48
50
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: TH1-TL1 = 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
53
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. The counter can hold values between 0000 to 1FFF
213-1= 2000H-1=1FFFH
When the timer reaches its maximum of 1FFFH, it rolls over to 0000, and TF0 is raised.
TH0
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5
TL0
D4 D3 D2 D1 D0
54
Timer 0
Timer Mode 3
For Timer/Counter 0
As a Timer:
TH0 and TL0 can be 8-bit timers.
As a Counter:
TL0 can be an 8-bit counter.
For Timer/Counter 1
Not available
55
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
56
Mode 2 programming
XTAL oscillator
12
C/T = 0
TL
reload
TF
overflow flag
TR
TH
auto reload
00 TF0 = 0
01
...
FE
FF
roll over 00H
FC
FD
TF0 = 0
TF0 = 0 TF0 = 0
TH0=FCH TL0=00H
FE
TF0 = 0
FF
FC TF0 = 1
FD TF0 = 1
59
TF0 = 0
Note that we must clear TF0 when TL0 rolls over. Thus, we can monitor TF0 in next process. Clear TR0 to stop the process.
60
61
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.
63
or
MOV TH1,#-200
Way 1: 256-200 = 56 = 38H Way 2: -200 = -C8H 2s complement of 200 = 100H C8H = 38 H
64
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
65
;start
;stop ;clear TF
67
68
Counter (1/2)
These timers can also be used as counters counting events happening outside the 8051 by setting 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) 8051
Counter 0
TH0
P1 to LCD
69
TL0
Vcc
a switch
T0
P3.4
Counter (2/2)
When the timer is used as a counter, it is a pulse outside of the 8051 that increments
TH0 & TL0 for counter 0. TH1 & TL1 for counter 1.
8051
Counter 1
TH1
P1 to LCD
70
TL1
Vcc
a switch
T1
P3.5
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
71
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).
72
73
74
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
75
TF0
TR0
C/T = 1
76
TF1
TR1
TH1
77
We use timer 1 as an event counter where it counts up as clock pulses are fed into pin3.5. 8051
P2 P3.5 to LEDs
T1
78
T0
81
R4
R3
R2
82
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.
84
86
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
87
88
89
92
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
93
94
95
96
8051 Counters in C
External pulses to T0 (P3.4) and T1 (P3.5). See Examples 9-26 to 9-29.
97
P1
TL1
LEDs
P3.5
T1
98
100
DigitMap Descriptor
A dialing plan
a digit map (11x|080xxxxxx|03xxxxxxx|002x.)
A start timer, to start A short timer, when more digits are needed A long timer, to differentiate different routing
Timers are applied by default values.
off-hook
5 7 2 1 0 4 3
short timer
long timer
101
CSMA/CD
1. frame 2. frame frame frame 3. 51.2 sframe collisionJamming Signal 4. r 51.2 s 1
102
timeout
103