Professional Documents
Culture Documents
8051 - Timers
8051 - Timers
8051 - timers
• The 8051 comes equipped with two timers, both of which may be
controlled, set, read, and configured individually.
• The 8051 timers have three general functions:
1. Keeping time and/or calculating the amount of time between
events,
2. Counting the events themselves, or
3. Generating baud rates for the serial port.
8051 - timers
• The 8051 comes equipped with two timers, both of which may be
controlled, set, read, and configured individually.
• Since this is a modulo-8 set up we are concerned with 256 numbers
in the range 0 to 255 (28 =256).
• The counter will count in a continuous sequence as follows:
How does a timer count
• A timer always counts up. It doesn't matter whether the timer is
being used as a timer, a counter, or a baud rate generator.
• A timer is always incremented by the microcontroller.
• Programming Tip: Some derivative chips actually allow the
program to configure whether the timers count up or down.
However, since this option only exists on some derivatives It is
only mentioned here in the event that you absolutely need a
timer to count backwards, you will know that you may be able to
find an 8051-compatible microcontroller that does it.
• When a timer is used to measure time it is also called an
"interval timer" since it is measuring the time of the interval
between two events.
8051 - timers
An 8-bit counter can count 255 events before overflow, and overflows on
the 256th. event. When initialized with a predefined value of say 252 it
overflows after counting just four events. Thus the number of events to
be counted can be programmed by preloading the counter with a given
number value.
8051 - timers
• The 8051 internally divides the processor clock by 12. If a 12 MHz.
processor clock is used then a 1 MHz. instruction rate clock, or a
pulse once every microsecond, is realized internally within the chip.
If this 1 microsecond pulse is connected to a Timer/Counter input.
• The counter will count events, up to 255 events before overflow, and
the timer will count time pulses, thus creating delays up to 255
microseconds in our example.
11,059,000 / 12 = 921,583
• Hence for a crystal frequency of 11.0592 the timer will be
incremented 921,583 times per second
• Unlike instructions--some of which require 1 machine cycle, others
2, and others 4--the timers are consistent.
• They will always be incremented once per machine cycle. Thus if a
timer has counted from 0 to 50,000 you may calculate:
50,000 / 921,583 = .0542 seconds
• Thus to implement the delay of 1 sec the above the timer has to
count from 0 to 50,000 for 18.45 times.
HOW DO WE PROGRAM THE 8-BIT TIMER/COUNTER?
THO TLO
DB15 DB0
Timer SFRs
• When you enter the name of an SFR into an assembler, it internally
converts it to a number. For example, the command:
TMOD register
Gate: Gating control when set timer/counter is enabled only while
the INTx pin is high and TRx pin is set. When cleared, the timer is
enabled whenever the TRx control bit is set.
2) After TH and TL are loaded with 16 bit initial value. The timer must
be started. This is done by SETB TR0 for SETB TR1 for T1.
4) After the timer reaches its limit and rolls over, in order to repeat the
process the register TH and TL must be reloaded with the original
value and TF must be reset to 0.
Xtal 12 TH TL TF
osc
To generate a time delay, using time mode, the following steps are
taken:
Keep monitoring the status of timer flag (TF). Come out of the loop,
when TF becomes HIGH.
Go back to step 2.
ORG 0H
MOV TMOD,#01H
HERE: MOV TL0, #0F2 H 2
MOV TH0, #OFFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY: SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 1
27
In the above program, observe the following:
TMOD is loaded.
P1.5 is toggled for the high and low portions of the pulse.
Xtal 12 TL TF
osc
NAME EXAMPLE14
ORG 0000H
MOV TMOD,#2H ;
MOV TH0,#0H
AGAIN: MOV R5,#250
ACALL Delay
CPL P1.0
SJMP AGAIN
Delay: SETB TR0
Back: JNB TF0, Back
CLR TR0
CLR TF0
DJNZ R5, Delay
RET
END
In the above program, time period is, T= 2(250*256*1.085µS)
= 138.8mS
Counter programming:
1. In the counter function, the register T0 and T1 are incremented in
response to a 1 to 0 transition at its external inputs T0, T1.In this
function , the external input is sampled during S5P2 of every
machine cycle.
2. When the samples show a high in one cycle and a low in the next
cycle, the count is incremented.
3. The new count value appears in the register during S3P1 of the
cycle following the one in which the transition was detected.
4. Since it takes two machine cycles( 24 oscillator periods) to
recognize a 1 to 0 transition.
5. The maximum count rate is 1/24 of the oscillator frequency. It is to
ensure that given level is sampled at least once before it changes, it
should be held for at least one machine cycle.
P3.4 is used as T0, timer/counter 0 external input.
P3.5 is used as T1, timer/counter 0 external input.
Example
Q. Assuming that clock pulses are fed in to pin T1, write a
program for counter 1 in mode 2 to count the pulses and
display the state of TL1 count on P2.
Solution:
MOV TMOD,#01100000B ;counter 1, mode 2, C/T=1
;external pulses
MOV TH1, #0 ;clear TH1
SETB P3.5 ; make T1 input
AGAIN:SETB TR 1 ; start the counter
BACK: MOV A, TL1 ; get copy of count TL1
MOV P2, A ; display it on port 2
JNB TF1, BACK ; keep doing it if TF=0
CLR TR 1 ;stop the counter 1
CLR TF 1 ;make TF=0
SJMP AGAIN ; keep doing it
Notice in the above program the role of the instruction
“SETB P3.5”. Since ports are set up for output when 8051
is powered up, we make P3.5 an input port by making it
high. In other words, we must configure (set high) the T1 pin
(Pin3.5) to allow pulses to be fed into it.
T0
TH0 TL0 TF0
TF 1 TR1 TF 0 TR 0 IE 1 IT 1 IE 0 IT 1
•The case of gate = 1 in TMOD:
So far, the task of timer is started through software using SETB TR0
and TR1 instruction.
If gate bit in TMOD is set to 1,the start and stop of a the timer are
done externally through pins P3.2 and P3.3 of T0 & T1.
TR 0
Gate O
INT0 Pin
Pin3.2