Timer Programming in 8051

What is a Timer?

A timer in the 8051 microcontroller is a built-in 16-bit hardware peripheral used to measure time intervals, generate precise delays, or count external events. It works by counting clock pulses, either from the system clock (timer mode) or from an external source (counter mode).

The 8051 has two 16-bit timers/counters:

Timer  Description
Timer 0                 16-bit Timer/Counter
Timer 1                 16-bit Timer/Counter

What is the Work of Timer Register in 8051?

In the 8051 microcontroller, registers used in the timer play a key role in configuring, controlling, and tracking the timer’s operation.

Main Timer-Related Registers and Their Functions:

1. TMOD(Timer Mode Register):- The TMOD (Timer Mode Register) is a special function register in the 8051 microcontroller. Timer 0 and Timer 1 are the modes of operation that it is utilized to set. Whether a timer or counter needs to be set, it is done so using this register. 


1. Gate:- The GATE bit in the TMOD register allows the timer to be controlled by both software and external hardware. If it is set to high, then it is controlled by external hardware, and if it is set to low, it is controlled by software. 

2. C\T:- The C/T bit in the TMOD (Timer Mode) register of 8051 decides whether a timer acts as a Timer or a Counter. If it is set High, then it is in counter mode, and if set low, it is in timer mode. In counter mode, it will work when we press the switch in simple it can be called manual or people counter. In timer mode, it is done automatically.
3. M1 and M2:- This is used to select the modes for both timer 1 and timer 2.

Timer Mode Selection Table
M1   M0 Mode Description
0            0            Mode 0                 13-bit Timer (slow, rarely used)
0            1 Mode 1                 16-bit Timer (normal mode, commonly used)
1            0 Mode 2                     8-bit Auto-Reload Timer (reloads automatically)
1             1
Mode 3                Dual Timer.

The 8051 has two 16 timers: Timer 0 and Timer 1.
Each timer is split into 8-bit registers:
‣ Timer 0: TL0(Low byte), TH0(High byte).
‣ Timer1:  TL1(Low byte), TH1(High byte).
TH0:- TH0 stands for Timer High Byte. It stores the higher byte of the count value.
TL0:- TL0 stands for Timer Lower Byte. It stores the lower byte of the count value.

2. TCON(Timer Control Register):- The 8051 microcontroller has a unique function register called the TCON (Timer Control Register). In order to provide precise output, timers and counters are controlled by it. 


1. TF1:- TF1 stands for Timer Flag 1. It sets to 1 when it reaches the maximum value.
2. TR1:- TR1 stands for Timer Run control bit for Timer 1. It sets to 1 to start the timer and 0 to stop the timer.
3. TF0:- TF0 stands for Timer Flag 0. It sets to 1 when it reaches the maximum value.
4. TR0:- TR0 stands for Timer Run control bit for Timer 0. It sets to 1 to start the timer and 0 to stop the timer.

Eg:- To find max value 
Timer 0 and mode 1

We take power 16 because the mode one is 16-bit.

216=65,5352^{16} = 65,535

Now multiply 65,535 by 1.085ms × 10⁻³ (1.085 µs is the timer tick period — the time taken by the timer to increment by 1 count):

max value=65,535×1.085×103=71.105ms\text{max value} = 65,535 × 1.085 × 10^{-3} = 71.105 ms

Now divide the required value by 1.085:

50×103÷1.085×106=46082(Note: Don’t take floating values)50 × 10^{-3} ÷ 1.085 × 10^{-6} = 46082 \quad \text{(Note: Don't take floating values)}

Now subtract the obtained value from the max value:

65,53546,082=19,45365,535 - 46,082 = 19,453

This obtained value is the starting point of the pointer.
Now convert the obtained value to a hex value which is 4BFD.
Here the Higher bit is 4B and Lower bit is FD.


Task 1:- Blink the LED connected to the 8051 microcontroller using timer.

Code:-

#include<reg51.h>
sbit led=P2^0;
void timer();
void main()
{
while (1)
{
led=0;
timer();
led=1;
timer();
}
}
void timer()
{
int i;
for(i=0;i<=50;i++)
{
TMOD=0x02;
TH0=0x4B;
TL0=0xFD;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}
}


Output:-