Timers of 8051
Timers of 8051
Timers of 8051
be used as:
❑ Timers to generate a time delay
❑ Event counters to count events happening
outside the microcontroller
Both Timer 0 and Timer 1 are 16 bits wide
❑ Since 8051 has an 8-bit architecture, each
16-bit timer is accessed as two separate
registers of low byte and high byte
The low byte register is called TL0/TL1
and the high byte register is called TH0/TH1
2. Find the timer’s clock frequency and its period for various 8051-based
systems with the crystal frequency 12 MHz when C/T bit of TMOD is 0.
This hardware way allows to start or stop the timer externally at any
time via a simple switch.
TLX 5 bits THX 8 bits TFX
It is a 16-bit timer; therefore, it allows value of
0000 to FFFF H to be loaded into the timer’s register
TL and TH
After TH and TL are loaded with a 16-bit
initial value, the timer must be started. This is done
by SETB TR0 for timer 0 and SETB TR1 for timer 1
After the timer is started, it starts to count up
It counts up until it reaches its limit of FFFFH
When it rolls over from FFFFH to 0000, it sets high
a flag bit called TF (timer flag). Each timer has its
own timer flag: TF0 for timer 0, and TF1 for timer 1
This timer flag can be monitored. When this timer
flag is raised, one option would be to stop the
timer with the instruction CLR TR0 or CLR TR1, for
timer 0 and timer 1, respectively.
After the timer reaches its limit and rolls over, in
order to repeat the process, TH and TL must be
reloaded by the count.
1. Load the TMOD register indicating which timer
(timer 0 or timer 1) is to be used and which
timer mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB
TFx, instruction to see if it is raised. Get out of
the loop when TF becomes high
5. Stop the timer
6. Clear the TF flag for the next round
7. Go back to Step 2 to load TH and TL again
In the following program, we create a square wave of 50% duty
cycle on the P1.5 bit. Timer 0 is used to generate the
time delay. Analyze the program.