Académique Documents
Professionnel Documents
Culture Documents
Timer Operation
Timer Register (TH0, TL0 or TH1, TL1) incremented every m/c cycle.
Thus working at increment frequency of 1/12 of oscillator frequency
( for 12 oscillator machine cycle ). Any preset value i.e. initial count
can be loaded to TH0, TL0 or TH1, TL1.
For Example Clock frequency = 12 MHZ
Clock period = 1/12 sec
Machine cycle time = 1 sec
Thus timer register will be incremented every microsecond.
- If timer is initialized to 0000H
Max. count = FFFFH
max. time measured = 216 sec
= 26 x 210 sec
26 millisecond
64 millisecond
= 65.5 millisecond
Counter Operation
- Counts pulses occurring at T0 pin (Timer/Counter
0) and/or T1 pin (Timer/counter 1).
- May correspond to event like
Passing of railway coach from a point axle counter
Rotation of speedometer cable
speedometer of vehicle
No. of persons visiting exhibition.
2 m/c
cycle
In 12 MHz 8051 m/c cycle = 1 sec
- 8051 can count at the rate of 2 sec per count or higher
- Any event when takes less than 2 sec may go unnoticed
- C/T bit of TMOD selects Timer or counter operation for
Timer 0 or Timer 1.
- Timer/Counter operations are controlled by
- Gate bit of TMOD
- TR0 bit of TCON
When Gate = 0 then
TR0, TR1 act as
Timer run control bits.
Timer
Regist
er
Interru
pt
TF0
Progra
m
Timer
ISR
RETI
Timer modes
Mode 0
Mode 1
Mode 2
Mode 3
- 4 modes
13 bit counter
16 bit counter
8 bit counter + auto reload
Split operation Timer 0
OS
C
12
C/T =
0
C/T =
1
T0
pin
Contr
ol
TH0
(8
bits)
TL0
(5
bits)
TF0
Interru
pt
TR
0
Gat
e
INT0
pin
Timer
Starts
Timer
+
Stops
Interrupt
Generated
12
C/T =
0
C/T =
1
T0
pin
Contr
ol
TH0
(8
bits)
TL0
(8
bits)
TF0
Interru
pt
TR
0
Gat
e
INT0
pin
Figure
Timer 0, mode 1 - 16-bit
counter.
12
C/T =
0
C/T =
1
T0
pin
TR
0
Gat
e
INT0
pin
Contr
ol
TL0
(8
bits)
TF0
TH0
(8
bits)
Figure
Timer 0, mode 2 autoreload.
Interru
pt
12
(1/12)
fosc
T0
pin
Gat
e
(1/12)
fosc
C/T =
0
C/T =
1
Contr
ol
TL0
(8
bits)
TF0
TH0
(8
bits)
TF1
Interru
pt
TR
0
INT0
pin
(1/12)
fosc
TR
1
Contr
ol
Figure
Timer 0, mode 3-split to two
8-bit counters.
Interru
pt
7
Gat
e
Timer
6 1 5
C/T
M1
Timer 0
4
M0
Gat
e
C/T
M1
M0
13-bit counter
16-bit counter
Bit
no.
Symb
ol
TF1
TR1
TF0
TR0
IE1
IT1
IE0
IT0
TF
1:
TR
1:
TF
0:
TR
0:
Example
a. Configuring Timer/Counter using
TMOD
Timer
6 1 5
7
Gat
e
C/T
M1
Timer 0
M0
Gat
e
C/T
M1
M0
TL0,
TH0,
TL1,
TH1,
#CDH
#ABH
#00H
#00H
Timer
1
c. Start Timer/Counter
6
5TR1
4
3
through
TR0,
7
TCON
=
Timer
IE1
IT1
IE0
IT0
Interru
pt
- ISR
- clear flag TF0 or TF1
- load preset value
- Restart timer/counter
RETI
Step d will be different for different applications.
Example -1
- Generate a square wave of 50% duty cycle at pin
p1.7. Use Timer 1 to generate time delay.
Clock frequency = 12 MHz, 12
oscillator clock. Pulse width = 50 millisecond.
- Let us work out the initial preset value.
50
ms
50
ms
a. Configure Timer 1
7
0
6
0
5
0
4
1
3
0
2
0
1
0
0
0
b.
= 10
H
c. Complement P1.7
CPL P1.7
d. Start Timer 1 (TR1 = 1)
SETB TCON.6
e. Check for TF1=1 in loop
JNB TCON.7, $
f. TF1=1, Make TF1=0
CLR TCON.7
g. Stop Timer 1 Make TR1=0
CLR TCON.6
h. SJMP KK
To reload preset value
Complement
P1.7
Start
Timer 1.
Modified Program
MOV TMOD, #10H
CLR P1.7
AK. MOV TL1, #B0H
MOV TH1, #3CH
CPL P1.7
ACALL TDELY
SJMP KK
TDELY: SETB
TCON.6
JNB
TCON.7, $
CLR
TCON.7
CLR TCON.6
RET
Example -2
- Generate a square wave of ON time of 3
ms and OFF time of 2 ms on P1.0.
Clock frequency = 16 MHz, 12 clock m/c
cycle
- 16 MHz Frequency using Timer 1.
1 clock
period = 1/16 sec
0TMOD,
0
1
0
0
#10H
=
10H
Up time = 0.25 ms
Dn time = 0.25 ms
Up time = 0.25 ms = 0.25 x 10 3 sec
No. of m/c cycles in up time = ( x 10 3)/(2/3)
= x 103 x 3/2 = 3/8 x 103
= 3000/8 = (30 x 25)/2
= 15 x 25 = 375
MOV
TMOD, #02H
=
02H
=
05H
DELIM : MOV
MOV
MOV
DJNZ
DJNZ
DJNZ
R2,
R3,
R4,
R4,
R3,
R2,
256 meter.
13 bit operation
8192 meter
16 bit operation
65536 meter.
Considering that overflow takes place in 1
minute duration
= 15 H
DELIM Subroutine
DELIM: MOVR3, #3CH ; for 60 seconds
LOOP1: MOVR4, #10H ; for 16 times
repeat for 1 second
LOOP2: MOVTH1, #00H
MOVTL1, #00H
; Start Timer 1
SETB TCON.6
JNB TCON.7, $
CLR TCON.6
CLR TCON.7
DJNZ R4, LOOP2
DJNZ R3, LOOP1
RET
THE END