Timers
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


MODES and TMOD register

Both timer0 & timer1 use the same register ‘TMOD’ for setting their mode.
1.) M1/MO (mode selector)

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

4.) How do we start and stop the timers?
- By software we can do so by setting ‘GATE=0’

The TR0, TR1 are the part of TCON (timer control) 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).
MODE 1
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)
MODE2
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:-

Timer1 & Timer0 work identically.
COUNTER PROGRAMMING
- 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.

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.



Comments
24 March 2009
3 weeks 2 days
1s= (.071105475)*14.063614651
So the above timer need to repeat a maximum cycle nearly 14 second to acquire the time
Study this program
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
ACALL DELAY1
DELAY2: SETB TR0
------
-----
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, 11/02/2009 - 17:11.
18 February 2009
5 hours 9 min
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.)
28 October 2009
43 weeks 3 days
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.. ?
16 April 2009
17 weeks 4 days
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...
28 October 2009
43 weeks 3 days
hello,
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
HERE: CPL A,
MOV P1,A
MOV TL0 #55
ACALL DELAY
SJMP HERE
DELAY : SETB TR0
AGAIN : JNB TF0, AGAIN
CLR TRO
CLR TFO
RET.
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.
6 May 2009
37 weeks 3 days
plz give sm examples of timers n other thngs related to 8051 in C also...
29 June 2009
1 year 6 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
21 February 2009
24 weeks 1 day
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.
29 June 2009
1 year 6 weeks
how to designe