You are on page 1of 10

Microcontroller & its

Applications
More on Timers/Counters
Large Delays
Assume XTAL=22MHz
Largest Time Delay in Mode 1 Timer 0
TH0=TL0=00H
Pulse Frequency= 1/12X22 MHz= 1.833 MHz
Pulse Period=1/1.8333x10^-6=545.45ns
Maximum Time Delay= FFFFH x 545.45ns
0.036 secs
1 Second Delay
1/0.036=27.97 almost 28
MOV TMOD, #01H; Timer 0 mode 1
MOV R0,#28; No of times the timer runs for 1sec
delay
BACK: MOV TL0,#00H
MOV TH0,#00H
SETB TR0
AGAIN: JNB TF0 AGAIN
CLR TR0
CLR TF0
DJNZ R0, BACK
Mode 0
Mode 0 is exactly like Mode 1 except that it is
a 13-bit timer instead of 16 bit. It can hold
values between 0000H-1FFFH in TH-TL.
Therefore when the timer reaches 1FFFH it
rolls over to 0000H and TF is raised.
Mode 2 Timer
It is an 8-bit timer, therefore it allows only 00 to
FFH to be loaded into the timers register TH.
After TH is loaded with the 8-bit value, the 8051
gives a copy of it to TL. Then the timer must be
started by the instructions SETB TR0 or SETB TR1.
After starting it counts up by incrementing the TL
register until it reaches FFH. Then it rolls over to
00 setting TF high.
After TF is set to 1, TL is automatically reloaded
with the TH value. To repeat the process we must
simply clear TF
Mode 2-Example
With XTAL=11.0592MHz find (a) the frequency of
the square wave generated on Pin P1.0 in the
following program (b) The smallest frequency
achievable in the program and the value of TH for
this frequency
MOV TMOD,#20H
MOV TH1,#5
SETB TR1
BACK: JNB TF1, BACK
CPL P1.0
CLR TF1
SJMP BACK
SOLUTION
(a) 256-5=251
251x1.085s=272.33s
The period= 272.33 x 2=544.66s.
Frequency=1/544.66=1.83597kHz
(b) To get the smallest frequency T=256 x 2
x1.085s= 555.52s
Frequency= 1.8kHz
Counter Programming
Timers can also be used as counters for
counting events happening outside the 8051.
For using the timer as an event counter, the
procedure is the same as that discussed
except the source of the frequency. In a
counter the source of the frequency is the
external pin T0/T1 (pin no 14/15) for
Timer0/Timer 1. The C/T bit in the TMOD
register is set to 1 for counters
Example
Design a counter for counting the number of
pulses of an input signal in one second. The
pulses are fed in pin 14. XTAL= 22MHz
ORG 0000H
RPT: MOV TMOD 15H; Timer 1 Mode 1, Counter 0 Mode 1
SETB P3.4; Configure P3.4 as input
MOV TL0, #00H
MOV TH0,#00H
SETB TR0
MOV R0, #28; No of times timer runs for 1 sec delay
AGAIN: MOV TL1,#00H
MOV TH1,#00H
SETB TR1
BACK: JNB TF1, BACK
CLR TF1
CLR TR1
DJNZ AGAIN; End of 1 sec delay loop
MOV A,TL0
MOV P2,A; sending lower byte of counter
MOV A,TH0
MOV P1,A; sending higher byte of counter
SJMP RPT; for the next second
END

You might also like