# Timers

 Tweet

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.

 Tweet
Tags:

Member since:
7 August 2017
Last activity:
2 years 33 weeks

Member since:
9 February 2017
Last activity:
2 years 44 weeks

Member since:
9 February 2017
Last activity:
2 years 44 weeks

# moncler outlet

Member since:
17 October 2010
Last activity:
9 years 23 weeks

hello...

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...

thank u

Member since:
24 March 2009
Last activity:
9 years 11 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

Now I can modify the program to this:

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, 2009-11-02 22:41.

Member since:
18 February 2009
Last activity:
1 year 30 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.)

Member since:
28 October 2009
Last activity:
10 years 21 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:
9 years 47 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:
10 years 21 weeks

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

MOV TMOD #2

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.

Member since:
6 May 2009
Last activity:
10 years 15 weeks

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