Timers are an essential part of any microcontroller. Quite often, in microcontroller programming, there is a need to generate accurate delays as well as execute certain events periodically. This is where a timer is of utmost importance. A Timer, when initiated, counts the internal clock pulses and thus the microcontroller can be made to execute ‘something’ when it overflows.

         8051 has two 16-bit timers, which can also be used as counters by setting the appropriate bits in the TMOD registers. When used as counters, the microcontroller counts the external clock pulses provided on pin 3.4(T0) and 3.5(T1). Each 16-bit timer/counter is divided into 2, 8-bit registers TH1-TL1 and TH0-TL0. These registers hold the count values which are incremented during each machine cycle. There are 2 registers associated with 8051 timers which are Timer control register and Timer mode register.


TCON – Timer control



TF1 and TF0: (Timer overflow flag)
TF1 and TF0 are set when their respective timer overflows. When Timer interrupt is enabled, TF1 and TF0 are cleared when their ISR’s are executed. When the interrupt is disabled, the programmer should clear these bits.

TR1 and TR0: (Timer run bit)
Timer 1 and Timer 0 will start counting when their respective run bit is set. When cleared, the counting stops.

IE1 and IE0: (External interrupt flags)
It is set when an external interrupt signal is received at INT1 or INT0. It is cleared when respective ISR is executed.

IT1 and IT0: (External interrupt type bit)
When set to 1, interrupt occurs, when the signal at INT1 and INT0 is -ve edge triggered. If set to 0, interrupt occurs when signal at INT1 and INT0 is level triggered.

IE1, IE0, and IT1, IT0 are used for external interrupt programming.


      TMOD – Timer Mode control

Gate C/T M1 M0 Gate C/T M1 M0


In the above register, the first four bits are for timer 1, and the next 4 bits are for timer 0.

Gate: (gate enable control)
This bit is used to control the timer action(Start and stop) using interrupt on external interrupt pin INT0 and INT1. When gate bit is set to 1, and an external high signal is given to INT0/INT1(along with TR0/TR1 being set), the timer will start incrementing.

C/T: (Counter/Timer)
When set, timer/counter acts as Counter. It is set to zero for timer operation.

M1 and M0: (Mode selection bits)
There are 4 timer modes which can be selected using these bits.


M1 M0 Timer mode
0 0 Mode 0
0 1 Mode 1
1 0 Mode 2
1 1 Mode 3


Mode 0:  It is a 13-bit mode. 5 bits of lower(TL0/TL1) register and 8 bits of the higher register(TH0/TH1) are used.
When the counting begins, TLx will be incremented. When TLx reaches 31, It is cleared and THx will be incremented.
Thus the total counts that can be achieved using mode 0, is 2^13 i.e. 8192 counts(1FFFH).


Mode 1: This mode is similar to mode 1. In this mode, all the 8 bits of lower as well as higher register are used for counting. On each count, timer is incremented. The overflow flag(TF0/TF1) is set when the timer counts reach FFFFH.
i.e. 2^16 which gives 65536 counts.


Mode 2: In this mode, the lower and the higher register have a separate role to play. The lower 8-bit register is used for counting and thus can count up to FFh since it is an 8-bit register(00H-FFH). The higher 8-bit register is used as an auto-reloader. It holds the value to be copied into the lower register. For example, consider timer0. The value stored in TH0 is FAH. So initially TL0 will be incremented from FAH to FFH. When TL0 reaches FFH, the value contained in TH0 i.e FAH will be copied into TL0 and the process will be repeated. The user can enable timer interrupt and have a certain piece of code executed in the fixed interval without the need of writing timer initialization code in a loop.


Mode 3: In Mode 3, The lower and higher register of timer 0 work as 2 separate timers. TL0 uses the control bits TF0, TR0 and can thus count up to FFH. Similarly Higher register TH0 uses the control bits TF1, TR1 and can also count up to FFH. Both of them working independently of one another. In mode 3 Timer 1 can be made to work in Mode 0, 1 or 2 but will not generate an interrupt.


8051 Assembly program for LED blink using timer.

org 0000h

mov p0, #00h

setb p0.1
call timer
clr p0.1
call timer
jmp top

mov Tmod, #01h
mov Th0, #4bh
mov Tl0, #0fch
setb TR0
JNB Tf0, $
clr tf0



8051 C program for LED blink using timer without interrupt.

#include <REG51F.H>

sbit LED1 = P0^4;
void timer0_init();

void main()
  LED1 = 1;
  LED1 = 0;

void timer0_init()
TMOD = 0x01;
TH0 = 0x4B;
TL0 = 0xfc;
TR0 = 1;
TF0 = 0;


8051 C program for LED blink using timer using interrupt.


#include <REG51F.H>
void timer0(void);
sbit LED1 = P0^0;

void isr_timer0() interrupt 1
LED1 = ~LED1;

void main()
P0 = 0x00;
EA = 1;

void timer0()
TMOD = 0x01;
TH0 = 0x4b;
TL0 = 0xfc;
TR0 = 1;
ET0 = 1;


Electronics engineer graduated from M.H. Saboo Siddik college of engineering. Currently working as Jr. Innovative engineer. Skilled in 8051, PIC and ARM microcontrollers. Circuit analyzation and Debugging. Constantly looking to acquire more skills which would help myself to become more proficient in embedded domain. Founder and blogger at techetrx.com LinkedIn profile: https://www.linkedin.com/in/moiz-shaikh-305294137


Leave a Reply

Your email address will not be published. Required fields are marked *