8051 Timers
8051 Timers
Introduction
Clock
Every Timer needs a clock to work, and 8051 provides it from an external crystal which
is the main clock source for Timer. The internal circuitry in the 8051 microcontrollers
provides a clock source to the timers which is 1/12th of the frequency of crystal
attached to the microcontroller, also called Machine cycle frequency.
For example, suppose we have a crystal frequency of 11.0592 MHz then the
microcontroller will provide 1/12th i.e.
Timer
8051 has two timers Timer0 (T0) and Timer1 (T1), both are 16-bit wide. Since 8051 has
8-bit architecture, each of these is accessed by two separate 8-bit registers as shown in
the figure below. These registers are used to load timer count.
8051 has a Timer Mode Register and Timer Control Register for selecting a mode of
operation and controlling purpose.
TMOD register
TMOD is an 8-bit register used to set timer mode of timer0 and timer1.
Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
These are Timer/Counter mode select bit as per the below table
M1 M0 Mode Operation
0 0 0 (13-bit timer mode) 13-bit timer/counter, 8-bit of THx & 5-bit of TLx
TCON Register
TCON is an 8-bit control register and contains a timer and interrupt flags.
1 = Timer1 overflow occurred (i.e. Timer1 goes to its max and roll over back to
zero).
It is cleared through software. In the Timer1 overflow interrupt service routine, this bit
will get cleared automatically while exiting from ISR.
1 = Timer0 overflow occurred (i.e. Timer0 goes to its max and roll over back to
zero).
0 = Timer0 overflow not occurred.
It is cleared through software. In the Timer0 overflow interrupt service routine, this bit
will get cleared automatically while exiting from ISR.
Timer Modes
Timers have their operation modes which are selected in the TMOD register using M0 &
M1 bit combinations.
Mode 0 (13-bit timer mode)
Mode 0 is a 13-bit timer mode for which 8-bit of THx and 5-bit of TLx (as Prescaler) are
used. It is mostly used for interfacing possible with old MCS-48 family microcontrollers.
As shown in the above figure, 8-bit of THx and lower 5-bit of TLx used to form a total 13-
bit timer. Higher 3-bits of TLx should be written as zero while using timer mode0, or it
will affect the result.
Example
Let's generate a square wave of 2mSec period using an AT89C51 microcontroller with
timer0 in mode0 on the P1.0 pin of port1. Assume xtal oscillator frequency of 11.0592
MHz.
As the Xtal oscillator frequency is 11.0592 MHz we have a machine cycle of 1.085uSec.
Hence, the required count to generate a delay of 1mSec. is,
The maximum count of Mode0 is 2^13 (0 - 8191) and the Timer0 count will increment
from 0 – 8191. So we need to load value which is 921 less from its maximum count i.e.
8191. Also, here in the below program, we need an additional 13 MC (machine cycles)
from call to return of delay function. Hence value needed to be loaded is,
1C74 = 0001 1100 0111 0100 b, now load lower 5-bit in TL0 and next 8-bit in TH0
so here we get,
1. Load Tmod register value i.e. TMOD = 0x00 for Timer0/1 mode0 (13-bit timer
mode).
2. Load calculated THx value i.e. here TH0 = 0xE3.
3. Load calculated TLx value i.e. here TL0 = 0x14.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
Mode 1 is a 16-bit timer mode used to generate a delay, it uses 8-bit of THx and 8-bit of
TLx to form a total 16-bit register.
Example
Let’s generate a square wave of 2mSec time period using an AT89C51 microcontroller
with timer0 in mode1 on the P1.0 pin of port1. Assume Xtal oscillator frequency of
11.0592 MHz.
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec.
And mode1 has a max count is 2^16 (0 - 65535) and it increments from 0 to 65535 so
we need to load value which is 921 less from its max. count i.e. 65535. Also, here in the
below program, we need an additional 13 MC (machine cycles) from call to return of
delay function. Hence value needed to be loaded is,
So we need to load FC74 Hex value higher byte in TH0 and lower byte in TL0 as,
1. Load Tmod register value i.e. TMOD = 0x01 for Timer0 mode1 (16-bit timer
mode).
2. Load calculated THx value i.e. here TH0 = 0xFC.
3. Load calculated TLx value i.e. here TL0 = 0x74.
4. Start the timer by setting a TRx bit. i.e. here TR0 = 1.
5. Poll TFx flag till it does not get set.
6. Stop the timer by clearing TRx bit. i.e. here TR0 = 0.
7. Clear timer flag TFx bit i.e. here TF0 = 0.
8. Repeat from step 1 to 7 for the delay again.
Program for timer mode1
/*
* 8051_Timer_Mode1
* http://www.electronicwings.com
*/
Mode 2 is an 8-bit auto-reload timer mode. In this mode, we have to load the THx-8 bit
value only. when the Timer gets started, the THx value gets automatically loaded into
the TLx and TLx starts counting from that value. After the value of TLx overflows from
the 0xFF to 0x0, the TFx flag gets set and again value from the THx gets automatically
loaded into the TLx register. That’s why this is called the auto-reload mode.
Example
Here we are generating a square wave on PORT1.0 with 200uSec. time period using
Timer1 in mode2. We will use 11.0592 MHz Xtal oscillator frequency.
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required
count to generate a delay of 1mSec. is,
And mode2 has a max count is 2^8 (0 - 255) and it increment from 0 – 255 so we need
to load value which is 92 less from its max. count i.e. 255. Hence value need to be load
is,
TH1 = 0xA4
1. Load Tmod register value i.e. TMOD = 0x20 for Timer1 mode2 (8-bit timer auto
reload mode).
2. Load calculated THx value i.e. here TH1 = 0xA4.
3. Load same value for TLx i.e. here TL1 = 0xA4.
4. Start the timer by setting a TRx bit. i.e. here TR1 = 1.
5. Poll TFx flag till it does not get set.
6. Clear timer flag TFx bit i.e. here TF1 = 0.
7. Repeat from step 5 and 6 for the delay again.
void main()
{
TMOD = 0x20; /* Timer1 mode2 (8-bit auto reload timer mode) */
TH1 = 0xA4; /* Load 8-bit in TH1 */
TL1 = 0xA4; /* Load 8-bit in TL1 once */
TR1 = 1; /* Start timer1 */
while(1)
{
test = ~test; /* Toggle test pin */
while(TF1 == 0); /* Wait until timer1 flag set */
TF1 = 0; /* Clear timer1 flag */
}
}
8051 has two timer interrupts assigned with different vector address. When Timer
count rolls over from its max value to 0, it sets the timer flag TFx. This will interrupt the
8051 microcontroller to serve ISR (interrupt service routine) if global and timer interrupt
is enabled.
The timer overflow interrupt assigned with the vector address shown in the table. 8051
microcontroller jumps directly to the vector address on the occurrence of a
corresponding interrupt.
Example
Here we will generate a square wave of 10Hz on PORT1.0 using Timer0 interrupt. We
will use Timer0 in mode1 with 11.0592 MHz oscillator frequency.
As Xtal is 11.0592 MHz we have a machine cycle of 1.085uSec. Hence, the required
count to generate a delay of 50mSec. is,
So we need to load 4C00 Hex value in a higher byte and lower byte as,
Note that the TF0 flag no need to clear by software as a microcontroller clears it after
completing the ISR routine.
void Timer_init()
{
TMOD = 0x01; /* Timer0 mode1 */
TH0 = 0x4C; /* 50ms timer value */
TL0 = 0x00;
TR0 = 1; /* Start timer0 */
}
int main(void)
{
EA = 1; /* Enable global interrupt */
ET0 = 1; /* Enable timer0 interrupt */
Timer_init();
while(1);
}
COUNTER PROGRAMMING
In the last section we used the timer/counter of the 8051 to generate time delays.
These timers can also be used as counters counting events happening outside the 8051.
The use of the timer/counter as an event counter is covered in this section. As far as
the use of a timer as an event counter is concerned, everything that we have talked
about in programming the timer in the last section also applies to programming it as a
counter, except the source of the frequency. When the timer/counter is used as a timer,
the 8051′s crystal is used as the source of the frequency. When it is used as a counter,
however, it is a pulse outside the 8051 that increments the TH, TL registers. In counter
mode, notice that the TMOD and TH, TL registers are the same as for the timer
discussed in the last section; they even have the same names. The timer’s modes are
the same as well.
C/T bit in TMOD register
Recall from the last section that the C/T bit in the TMOD register decides the source of
the clock for the timer. If C/T = 0, the timer gets pulses from the crystal. In contrast,
when C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051.
Therefore, when C/T = 1, the counter counts up as pulses are fed from pins 14 and 15.
These pins are called TO (Timer 0 input) and Tl (Timer 1 input). Notice that these two
pins belong to port 3. In the case of Timer 0, when C/T = ], pin P3.4 provides the clock
pulse and the counter counts up for each clock pulse coming from that pin. Similarly, for
Timer 1, when C/T = 1 each clock pulse coming in from pin P3.5 makes the counter
count up.
Table 9-1: Port 3 Pins Used For Timers 0 and 1
Example 9-18
Assuming that clock pulses are fed into pin Tl, write a program for counter 1 in mode 2
to count the pulses and display the state of the TL1
count on P2.
In Example 9-18, we use Timer 1 as an event counter where it counts up as clock pulses
are fed into pin 3.5. These clock pulses could represent the number of people passing
through an entrance, or the number of wheel rotations, or any other event that can be
converted to pulses.
In Example 9-18, the TL data was displayed in binary. In Example 9-19, the TL registers
are converted to ASCII to be displayed on an LCD.
Figure 9-5. (a) Timer 0 with External Input (Mode 1)
(b) Tinier 1 with External Input (Mode 1)
Example 9-19
Assume that a 1-Hz frequency pulse is connected to input pin 3.4. Write a program to
display counter 0 on an LCD. Set the initial value of THO to -60.
Solution:
To display the TL count on an LCD, we must convert 8-bit binary data to ASCII. See
Chapter 6 for data conversion.
By using 60 Hz we can generate seconds, minutes, hours.
Note that on the first round, it starts from 0, since on RESET, TLO = 0. To solve this
problem, load TLO with -60 at the beginning of the program.
Figure 9-6. Timer 0 with External Input (Mode 2) Figure 9-7. Timer 1 with External Input
(Mode 2)
As another example of the application of the timer with C/T = 1, we can feed an external
square wave of 60 Hz frequency into the timer. The program will generate the second,
the minute, and the hour out of this input frequency and display the result on an LCD.
This will be a nice digital clock, but not a very accurate one.
Before we finish this chapter, we need to state two important points.
1. You might think that the use of the instruction “JNB TFx, target” to mon
itor the raising of the TFx flag is a waste of the microcontroller’s time. You
are right. There is a solution to this: the use of interrupts. By using interrupts
we can go about doing other things with the microcontroller. When the TF flag
is raised it will inform us. This important and powerful feature of the 8051 is
discussed in Chapter 11.
2. You might wonder to what register TRO and TR1 belong. They belong to a
register called TCON, which is discussed next.
Table 9-2: Equivalent Instructions for the Timer Control Register (TCON)
TCON register
In the examples so far we have seen the use of the TRO and TR1 flags to turn on or off
the timers. These bits are part of a register called TCON (timer control). This register is
an 8-bit register. As shown in Table 9-2, the upper four bits are used to store the TF and
TR bits of both Timer 0 and Timer 1. The lower four bits are set aside for controlling the
interrupt bits, which will be discussed in Chapter 11. We must notice that the TCON
register is a bit-addressable register. Instead of using instructions such as “SETB TR1″
and “CLR TR1″, we could use “SETB TCON. 6″ and “CLR TCON. 6″, respectively. Table 9-2
shows replacements of some of the instructions we have seen so far.