Timers can be used for: 

  • As timers for generating a time delay
  • Counters to count events happening outside the microcontrollers.

There are two timers in 8051 i.e. Timer0 & Timer1. They both have a register that is 16 bit long. The lower byte is stored in TL & higher in TH. These registers are SFR and can be accessed just like any other register. 

For e.g.: MOV TL0, #50H


Timer0 Register


Timer1 Register


MODES and TMOD register


TMOD Register


Both timer0 & timer1 use the same register ‘TMOD’ for setting their mode.

1.) M1/MO (mode selector) 


Modes of timer


There are four different modes of timer. In this tutorial we shall concentrate only on mode1 (16-bit timer) and mode2 (8 bit auto-reload).

2.) C/T (clock/ timer)

  • C/T= 0; for delay generation
  • C/T= 1; an event counter.


3.) Clock source for timers


Clock source for timers


4.) How do we start and stop the timers?

  • By software we can do so by setting  ‘GATE=0’


Starting and stopping timer


The TR0, TR1 are the part of TCON (timer control) register. 


TCON Register


  • If we want to control timers through hardware we can do so by setting ‘GATE=1’  (it will be discussed afterwards)


5.) Time of delay

Consider the following example – 

TL0 =11H 

TH0= 3EH 

  • The maximum value the timer0 register can hold is FFFF (hex).
  • (FFFF- 3E11+1) hex = 49647 (decimal)
  • Time= 49646* time of each cycle
  • Now suppose if the crystal frequency is 11.0592 MHZ
  • Frequency of timer= 11.0592/12; time of one cycle 1.085µs
  • Time of delay= 49647* 1.085µs= 538.66ms

So we can assign the value to TL & TH corresponding to the time of delay we want to create.


6.) How will the µcontroller know that the timer register has reached its maximum value?

  • It monitors the over flow flag (TF), when the timer reaches its maximum value it is raised to one. 
  • TF0 & TF1, over flow flags are part of TCON register.


7.) If we wish that the timer should roll over?

  • Mode1: We have to clear the over flow flag and load the value of timer register (i.e. TL &TH) again.
  • Mode2: We have to only clear the over flow flag. (Remember in this mode we don’t have to load the value of timer register again).


Let’s see an example to understand how the mode1 is programmed. 

Suppose we want to create a duty cycle of 50% at port1 using timer0.

Duty cycle means (the ratio of time for which it is on to the total time)


Mode1 8051 timer code 



In this mode we have to only give value to TH (high byte of timer register) and the value of TH is copied to TL by system.  This mode accepts only eight bit and is auto-reload meaning that we don’t have to assign a value to timer register after it rolls over; therefore this is also called 8 bit auto-reload.

Now suppose we want to create a square wave (50% duty cycle) at P1.5 using timer0 in mode 2. Suppose we wish to repeat the process 100 times only:-


Mode 2 8051 tmer code

Timer1 & Timer0 work identically.




  • It is used to count some external event
  • P3.4 (pin14) is used to count pulses for timer0
  • P3.5 (pin15) is used to count pulses for timer1 
  • These pins are called timer inputs: T0 (P3.4) & T1 (P3.5)
  • C/T=1 for counter

Let us see an example of the counter programming using timer0 in mode2. The value of counter is displayed at port zero.


Counter Programming code

We can use a set of eight LED or LCD at port zero to display the value of counter. In this way timers can be used for counting events.






Member since:
17 October 2010
Last activity:
6 years 27 weeks


sir i want to count the exact time for which we had 0 input or 1 input...

how can i calculate tht....because for ex- if a switch is pressed it b 1 for say few mili seconds...i wanna calculate this time and display tht on lcd...

please help me ..

thank u 

Member since:
24 March 2009
Last activity:
6 years 15 weeks

1s= (.071105475)*14.063614651


So the above timer need to repeat a maximum cycle nearly 14 second to acquire the time


Study this program




Mode 2 8051 tmer code

Now I can modify the program to this:


MOV    TH0,#0H   ;load timer 

MOV    R1, #DH    ;14 in hexadecimal system

                               ;will server as a counter 

; for some event call delay1

; then it again call delay2






DELAY1:  DJNZ    R2, DELAY2     

; i believe this create an enough hint for you to create a nested loop

;with another register & timer2 to create longer delay





Note:- This post has been edited by bOtskOOl at Mon, 2009-11-02 22:41.

Member since:
18 February 2009
Last activity:
1 year 12 weeks

Hi helpdesk, 

As shweta has explained above a single bit change in your TMOD#2 (timer) register will produce a delay of 1.085µs since your frequency is 11.0592MHz. Now this timer register can toggle between 00000 to 65535 which gives you a total delay of .071105475s (=1.085µs*65535). Once the timer register reaches its maximum value it again starts counting from the beginning. So if you want to generate a delay greater than this value you should use another register along with this timer register which increments when timer2 register completes its one full cycle (from 00000 to 65535) and in this way each bit change of second register will give you a delay of .071105475s. Now snce this second register can toggle itself between 00000 to 65535 you will get a total delay of 4659.897304125s(=.071105475*65535) which I guess will solve your problem.

Conclusion: You need to use a second register apart from timer register and they should run in a nested loop giving you the required delay. (Second register toggles whenever the timer register completes one full cycle.)

Founder and Admin

Member since:
28 October 2009
Last activity:
7 years 25 weeks

Hmm... didnt quite understand what u really mean but if u show with a simple code what u mean, that would be great. if i cant generate timing up to 5min with TMOD #2 , how abt if i want to generate a 1sec delay using mode 2 and a clock frequency of 11.05.. ?

Member since:
16 April 2009
Last activity:
6 years 51 weeks

The maximum value that can be achieved by Timer2 is

==> 1.085µs*65535 =.071105475s

(65535= FFFF: the maximum value that register TH1 &TL0 register)


(As explained above while calcilating time of delay)


Therefore, it will be necessar to load other registers and ultimately generate a nested loops to generate larger value of delay


Refer this to study loops: http://www.botskool.com/tutorials/electronics/8051/jump-loop-and-call-in...

Member since:
28 October 2009
Last activity:
7 years 25 weeks



thanks once again for the reply..


suppose i want to generate a 5min delay using timer mode 2, timer0; clock frequency = 11.0592 MHZ; now using exactly your code :


                     MOV A, #55

               MOV TMOD #2

HERE:           CPL A,

                    MOV P1,A

                    MOV TL0 #55

                   ACALL DELAY

                   SJMP HERE


DELAY :        SETB TR0


                    CLR TRO

                    CLR TFO




how do i modify this code to produce a 5min delay? also what is the advantages of using mode 2 over mode 1 and vice versa. what is essential to generating the delay? that is, if i now decide to generate a 3min delay, what do i just calculate using the same code? i need to fully understand the timers very well as i am currently working on a micro project that would require different timing delays. thanks.

Member since:
6 May 2009
Last activity:
7 years 19 weeks

 plz give sm examples of timers n other thngs related to 8051 in C also...

Member since:
29 June 2009
Last activity:
7 years 40 weeks

based on relay  it can work four hour without electricity,

when light goes off tube light glow automatic and when light come tube light goes off automatically

Member since:
21 February 2009
Last activity:
7 years 5 weeks

You need to elaborate more. What do you exactly want to know? Are you trying to design an automatic emergency tube light using 8051 microcontroller? In what sense is it automatic? And how is it related to 8051 and this 8051 timer tutorial? Be more specific and repost your query.