The 8052 comes equipped with three timers, all of which may be controlled, set, read, and configured
individually. The timers have three general functions:
1) Keeping time and/or calculating the amount of
2) Counting the events themselves, or
3) Generating baud rates for the serial port.
Using timers to generate precise time delays...
One of the most important use of Timers is to generate delays that are very precise.
Most of the 8051 family microcontrollers completes one machine cycle using 12 cpu cycles.
Meaning to say most of the instructions takes total of 12 clock pulses for their execution.
we are using master clock of 11.30592 MHz that means , time period for 1 machine cycle is 1.085uSec. How???
12 Cycles required for one machine cycle
Finstruction=(11.0592/12) MHz => 921.6 KHz
Time period of each machine cycle = 1/921.6 msec.
so... Tmachine cycle = 1.085 uSec
It needs 1.085 usec to execute one instruction.
Timer counts up by one in 1.085 uSec
so that Desired delay = No of counts x 1.085 usec
All we need to know is No of counts required to generate a particular amount of delay.
For example we need to generate a delay of 20 mSec. For that first we need to calculate amount of timer counts required to pass 20 msec.
Timer counts required = 20000 / 1.085 => 18433 counts
Now lets see how to implement this whole delay generation logic in a 'C' program.
First we need to know the various registers needed to control timer operations. 8051 derivative has total of 2, 16 - bit timers. while 8051 has 3 timers. But we are going to discuss 8051 only.
Both Timer0 and Timer1 shares two common SFR's known as TMOD and TCON which controls timer operation.
In addition, each timer also has two SFR dedicated to its own operation only.
TH0,TL0 for Timer0 and TH1,TL1 for Timer1.
Timer0 High Byte
Timer0 Low Byte
Timer1 High Byte
Timer1 Low Byte
Timer Control Register
Timer Mode Control
Lets first talk about our first control SFR: TMOD (Timer Mode). The TMOD SFR is used to control the mode of operation of both timers. Each bit of the SFR gives the microcontroller specific information
concerning how to run a timer. The high four bits (bits 4 through 7) relate to Timer 1 whereas the low four bits (bits 0 through 3) perform the exact same functions, but for timer 0.
The individual bits of TMOD have the following functions:
13 Bit Timer Mode
When the timer is in 13-bit mode, TLx will count from 0 to 31. When TLx is incremented from 31, it will
"reset" to 0 and increment THx. Thus, effectively, only 13 bits of the two timer bytes are being used: bits
0-4 of TLx and bits 0-7 of THx. This also means, in essence, the timer can only contain 8192 values. If
you set a 13-bit timer to 0, it will overflow back to zero 8192 instruction cycles later.
16 Bit Timer Mode
Timer mode "1" is a 16-bit timer. This is a very commonly used mode. It functions just like 13-bit mode
except that all 16 bits are used.
TLx is incremented from 0 to 255. When TLx is incremented from 255, it resets to 0 and causes THx to be
incremented by 1. Since this is a full 16-bit timer, the timer may contain up to 65536 distinct values. If you
set a 16-bit timer to 0, it will overflow back to 0 after 65,536 machine cycles.
8 Bit Timer Mode
Timer mode "2" is an 8-bit auto-reload mode. What is that, you may ask? Simple. When a timer is in mode
2, THx holds the "reload value" and TLx is the timer itself. TLx starts counting up. When TLx reaches
255 and is subsequently incremented instead of resetting to 0 (as in the case of modes 0 and 1) it will be
reset to the value stored in THx.
Split Timer Mode
Timer mode "3" is a split-timer mode. When Timer 0 is placed in mode 3, it essentially becomes two
separate 8-bit timers. That is to say, Timer 0 is TL0 and Timer 1 is TH0. Both timers count from 0 to 255
and overflow back to 0. All the bits that are related to Timer 1 will now be tied to TH0 and all the bits
related to Timer 0 will be tied to TL0.
While Timer 0 is in split mode, the real Timer 1 (i.e. TH1 and TL1) can be put into modes 0, 1 or 2
normally--however, you may not start or stop the real timer 1 since the bits that do that are now linked to
TH0. The real timer 1, in this case, will be incremented every machine cycle no matter what.
The only real use I can see of using split timer mode is if you need to have two separate timers and,
additionally, a baud rate generator. In such case you can use the real Timer 1 as a baud rate generator and
use TH0/TL0 as two separate timers.
Example program to demonstrate the working of Timers as precise deley generator. Toggle a LEd connected to P1_0 at a precise time intervel
Timer Mode used is 16 bit Mode
Suppose required delay is of 50ms
Total count required = 50000/1.085 = 46083 counts
So Timer0 must count 46083 times before it overflows
Hex equivalent of 46083 = 0xB403
so Initial Timer values = 0xFFFF - 0xB403 = 0x4BFC
so TH0=0x4B; and TL0=0xFC;
TMOD=0b00000001; // 16 bit mode for timer0 ; '0x01' in Hex
TR0=1; // Turn ON timer0
while(TF0==0); // Wait for timer0 to overflow
TR0=0; // Turn Off Timer0
TF0=0; //Clear TF0 for mext timer operation
P1_0=1; // initially LED Off (Sinking Mode)
while(1=1) // Loop forever
P1_0=0; // LEd On
Timer_delay(); // Timer delay interval
P1_0=0; // LED Off
I am leaving 'Timer as Counter' logic to Do It Yourself. Try it ; ask your queries if you face any kind of problems.
Best viewed at Firefox 1024x768 resolution