Professional Documents
Culture Documents
Timer Counter in 8051
Timer Counter in 8051
Timer Counter in 8051
8051
Dr. C. Suganthi Evangeline
1
Timers /Counters
2
Timer
P2 P1 to
Set LCD
Timer 0 TH0
TL0
3
Counter
5
Basic Registers of the Timer
6
Timer Registers
TH0 TL0
Timer 0
TH1 TL1
Timer 1
7
TMOD Register
(MSB) (LSB)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0
8
TMOD Register
10
Gate
11
M1, M0
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
XTAL
oscillator
÷ 12
Solution:
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
16
TCON Register (2/2)
(MSB) (LSB)
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Timer 1 Timer0 for Interrupt
17
Table 9-2: Equivalent Instructions for the
Timer Control Register
For timer 0
SETB TR0 = SETB TCON.4
CLR TR0 = CLR TCON.4
20
Steps of Mode 1 (2/3)
TR0=1 TR0=0
Start timer TH0 TL0
Stop timer
TF = 0 TF = 0 TF = 0 TF = 0 TF = 1
XTAL
oscillator ÷ 12 MC
C/T = 0
TH TL TF
Start timer
TF goes high overflow
TR when FFFF 0 flag
23
Initial Count Values
24
Figure 4. Timer Delay Calculation for
XTAL = 11.0592 MHz
25
Example 4 (1/4)
In the following program, we are creating a square wave of 50%
duty cycle (with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay.
Analyze the program.
29
Example 5
In Example 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.
30
Find Timer Values
31
Example 10 (1/2)
Solution:
XTAL = 11.0592 MHz MC = 1.085 s.
5 ms / 1.085 s = 4608 MCs.
To achieve that we need to load into TL and TH the value 65536
– 4608 = 60928 = EE00H.
32
Therefore, we have TH = EE and TL = 00.
Example 10 (2/2)
CLR P2.3
MOV TMOD,#01 ;Timer 0,mode 1
HERE: MOV TL0,#0
MOV TH0,#0EEH
SETB P2.3 ;SET high P2.3
SETB TR0 ;start
AGAIN: JNB TF0,AGAIN
CLR TR0 ;stop
CLR TF0 5ms
34
Timer Mode 0
35
Timer Mode 2
• 8-bit timer.
– It allows only values of 00 to FFH to be loaded into TH0.
• Auto-reloading
• TL0 is incremented continuously when TR0=1.
36
Mode 2 programming
XTAL
oscillator ÷ 12
C/T = 0
TL TF
overflow
flag
reload TF goes high
TR TH when FF 0
37
Example 14 (1/2)
Solution:
(a) First notice that target address of SJMP. In mode 2 we do not
need to reload TH since it is auto-reload.
Half period = (FFH – 05 +1) × 1.085 s = 272.33 s
Total period = 2 × 272.33 s = 544.67 s
Frequency = 1.83597 kHz.
(b) To get the smallest frequency, we need the largest period and
that is achieved when TH = 00.
Total period = 2 × 256 × 1.085 s = 555.52 s
Frequency = 1.8kHz.
39
Section 2
Counter Programming
40
Counter
41
Table 9-1: Port 3 Pins Used For Timers 0 and 1
(MSB) (LSB)
GATE C/T=1 M1 M0 GATE C/T=1 M1 M0
Timer 1 Timer 0
42
Counter Mode 1
overflow
Timer 0 flag
external TH0 TL0 TF0
input
Pin 3.4
TF0 goes high
C/T = 1 TR0 when FFFF 0
44
Figure 9-5. (b) Timer 1 with External Input
(Mode 1)
overflow
Timer 1 flag
external TH1 TL1 TF1
input
Pin 3.5
TF1 goes high
C/T = 1 TR1 when FFFF 0
45
Counter Mode 2
• 8-bit counter.
– It allows only values of 00 to FFH to be loaded into TH0.
• Auto-reloading
• TL0 is incremented if TR0=1 and external pulse
occurs.
• See Figure 9.6, 9.7 for logic view
• See Examples 9-18, 9-19
46
Figure 9.6: Timer 0 with External Input
(Mode 2)
overflow flag
Timer 0 TL0 TF0
external
input reload
Pin 3.4 TR0 TH0
47
Figure 9.7: Timer 1 with External Input
(Mode 2)
overflow flag
Timer 1 TL1 TF1
external
input reload
Pin 3.5 TR1 TH1
48
A Digital Clock
50
Example for GATE=1
51
Figure 8: Timer/Counter 0
XTAL
oscillator ÷ 12
C/T = 0
C/T = 1
T0 Pin
Pin 3.4 TR0
Gate
INT0 Pin
Pin 3.2
52
Figure 9: Timer/Counter 1
XTAL
oscillator ÷ 12
C/T = 0
C/T = 1
T1 Pin
Pin 3.5 TR1
Gate
INT1 Pin
Pin 3.3
53
Timer/Counter Mode 3
• For Timer/Counter 0
– As a Timer:
• Two 8-bit timers
– As a Counter:
• 8-bit counter.
• For Timer/Counter 1
– Not available
54