on Thursday, 19 September 2013
In 8051 we have two timers/counters. Both of them use the same registers. Timers are used to generate time delay and counters are for counting any events outside the microcontroller. We only concern with the Timers in this post.

  • Before we use timer/counter register first we should know how to set them in timer mode or counter mode, in order to perform this operation we use register "TMOD" (timer mode)
  • Next we should know which timer we use i.e Timer0 or Timer1 or both, and for generating desired time delay we should assign corresponding values in the  "Timer0/Timer1" registers.
  • Timer0/1 are 16bit wide, accessed as THx , TLx i.e TH0, TL0 or TH1, TL1. 
  • After we start the timer, after generating the desired time delay we should know when the delay have been completed, for this purpose we use "TCON" (timer control) register.

Now let us look at all the corresponding registers to know how to use them in our programming.

TMOD Register: TMOD (Timer Mode) Register is of 8 bits. Both the Timer0 and Timer1 uses this register.so, lower nibble is used by Timer0 and higher nibble is used by Timer1. Look at the register below.

tmod 8051

Dont worry if you dont understand its operation, i'll explain you step by step.
M0 and M1:
Lower nibble uses the Timer0 and higher nibble uses the Timer1 as said before. In Timer0 and Timer1 M0 and M1 bits are used to set the mode operation.
If we want to set to Mode 0 then  M0 =0 and M1=0
For Mode 1 then M0= 1 and M1=0
Look at the Note above. In general we use mode1 and mode2 i.e 16bit mode and 8bit auto reload mode.

C/T bit is used to make known to microcontroller that, are we using Timer or Counter. If we are using Timer then C/T =0 and if counter then C/T =1.

It is used for the hardware purpose, we need not to know much about this point of time. Just know that we make this bit zero (0)

Above all the operations are easily understood from few examples given below.

Example1: Use Timer0 and Mode1 give the TMOD value which has to be assign.

tmod mode0

As we are using Timer0 so all the bits in Timer1 should be '0' i.e higher nibble is all zeros. We need to use Mode1 so M0= 1 and M1=0 , C/T =0 because we are using timer , Gate= 0

So we need to load 0000 0001 i.e 01h value in the TMOD register to work it as Timer0 and Mode1 operation.
                                             Mov TMOD,#01h

Example2: Use Timer1 with Mode2, assign the values in TMOD register.

tmod mode2

As we are using Timer1 so all the bits of Timer0 should be 0 i.e lower nibble is all zeros. We need to use Mode 2 so M0=0 and M1=1 , C/T=0 , Gate=0.
So we need to load 0010 0000 i.e 20H into TMOD register to work it as Timer1 with mode2.

                          Mov TMOD,#20H

Exampe3: Timer0 Mode2 and Timer1 Mode1

Mov TMOD,#12H

To start or stop the timer and to check when is the delay have been completed we use a register called as TCON i.e Timer control register as shown below.

We only concern with few bits which are required for our purpose.

  • To start the Timer0 then we should set the TR0 bit i.e setb TR0 or Setb Tcon.4
  • To start the Timer1 then Setb TR1 or Setb Tcon.6
  • When ever the predefined time delay have been completed then the timer flag will be set to indicate it. In case of Timer0 timer flag TF0 will be set i.e TF0=1 automatically after timer register (TH0&TL0) becomes 0, for Timer1 timer flag TF1 will be set i.e TF1=1 when TH1 & TL1 becomes 0. we need to check this flag continuously to know that defined time is over or not.
Timer Frequency:
Remember as i said you in my first post that the speed of operation of the microcontroller will depends on the crystal oscillator which is attached to it i.e more the crystal value more fast the operations will be. So, the timer's speed too depends on the crystal oscillator value.

To find the timer's clock frequency we need to divide the crystal oscillator value by 12 
Example: 12Mhz crystal 
12/12=1Mhz frequency
To know the time, reciprocal it 1/1 Mhz = 1us (i.e 1/f=T)

16Mhz crystal
16/12 Mhz = 1.33 Mhz frequency
Time = 1/1.33 Mhz = 0.75us 

11.0592 Mhz crystal
11.0592/12 Mhz= 921.6khz 
Time= 1/921.6 khz =1.085 us

Use Crystal of 11.0592 and generate pulse on p2.3 with the delay of 20ms (50% duty cycle )using timer0 with mode1. 

                   ORG 00h
                   Mov P2,#00h          ;port2 output or you can write setb p2.3
                   CLR P2.3                ;clear p2.3
                   Mov TMOD,#01     ;Timer 0 and Mode 1 i.e 16 bit mode
    Back    :  Mov TL0,#3EH       ; TL0 value 
                   Mov TH0,#0B8H    ;  TH0 value
                   SETB P2.3              ; p2.3=1
                   SETB TR.0             ; start timer0
   Here     :   JNB TF0, Here       ; untill Timer flag is '0' then stay here if '1' come out side loop
                   CLR TR0                ;stop timer
                   CLR P2.3               ; p2.3=0
                   SJMP Back            ; Go back and load the timer values
                   END                       ; End

You would have understood everything in the above program except the TL0 and TH0 , they are registers of timer0 , depending upon these values time delay will be generated.
Value B8 3E is to generate the delay of 19.9ms.
Now again you would have got the doubt, how that particular value produces the particular delay. We will look now...

Time delay Calculation:
For every Timer clock ticks, the value in the Timer register will be decremented. If we are using the 11.0592Mhz crystal oscillator then Timer Clk tick time is 1.085us (11.0592/12 =921.6khz, 1/921.6= 1.085us) i.e for every 1.085us the value in the timer register will be decremented.
For example the TH0=00 , TL0 =05 then 00 05 will be decremented for every 1.085us

00 05-------> 00 04 --------> 00 03 -------> 00 02 ------> 00 01 ------> 00 00
           1.085us           1.085us            1.085us            1.085us          1.085us       timer flag=1

If we add each delay we will get the total time delay i.e 1.085+1.085+1.085+1.085+1.085 =5.425us

Now look at the above example in which TH and TL are B8 3E , to know its delay just subtract from total i.e FF FF - B8 3E +1 =47C2 H (note plus 1 i.e to roll to zero) 47C2 in hexadecimal i.e 18370 in decimal.
18370x1.085us = 19.93145ms total delay(i.e total decrements x each delay).

TF0=0 when Timer value(TH0 TL0) is non zero
TF0=1 when timer values are zero.

"Here     :   JNB TF0, Here "
Jump if no bit 'here' , mean it stay in loop until Timer flag is zero and when it becomes 1 then comes out of the loop to indicate that timer delay have been completed.

Example2: TH and TL values are 00 00 then find the delay it produces (crystal 11.0592)

FF FF - 00 00 = FF FF = 65535 (we forget to add 1, now lets add) = 65535+1 = 65536
Delay = 65536 x 1.085us = 71106us = 71.106 milli sec

So maximum delay we can produce with 11.0592 in mode 1 is 71.106 ms , in order to produce more delay we use a loop repeating this delay.

Note: we are subtracting from FF FF(65535) because its the 16bit mode(mode1) , if we are using 8 bit mode(mode2) we subtract from FF (255) (i.e 8 bits) .

Example3: TH and TL values are 00 00 find the delay using 22Mhz crystal (Mode1 i.e 16bit mode).

FF FF - 00 00 = 65535 ; 65535+1 = 65536

65536 x 0.546us = 35782us = 35.782ms .

( i.e 22Mhz = 22/12 =1.833Mhz ; 1/1.833=0.546 us )

So maximum delay we can produce with 22Mhz crystal is 35.782ms, if we want more delay we need to use a loop.
Note that "High the value of crystal frequency  less the  maximum delay it produces ."

Example4: Find the delay produced by the following program with crystal 22Mhz

                   Mov TMOD,#01  ;timer0 mode 1
                   Mov TL0,#0F2h
                   Mov TH0,#0FFh
                   CPL P1.2
                   Acall delay             ;calling a subroutine
                   -------------           ;other code
Delay:         Setb TR0
here   :        JNB TF0, here
                  CLR TR0
                  CLR TF0

Solution: TH0 TL0 = FF F2 and 16bit mode

FF FF - FF F2 = D = 13+1 = 14
14 x 0.546us(i.e 22Mhz) =7.644us
So total delay we produce is 7.644us.

Till this point you would have understood how we find the total delay and how we use the timers in programming, but how should we know that which values should we load to produce our required delay. For example we want to generate some 45ms delay then how will we know the values to load in TH and TL register? Now look at this aspect

  1. Divide the desired time delay by Timer clk tick time (i.e for 11.0593 its 1.085, for 22Mhz = 0.546,etc)
  2. Now 65536-m, where m is the decimal result we get in step1
  3. Convert the result of step 2 to hex value, we get yyxx (suppose)
  4. Now TL=xx and TH =yy
Example5: Find the values of TH and TL to produce the time delay of 20ms using 11.0592Mhz crystal with timer mode1 i.e 16bit mode.

Step1:   20ms/1.085us = 20000us/1.085us = 18433

Step2:   65536-18433 = 47103

Step3: 47103 to hex i.e B7 FF

Step4: TL =FF and TH = B7

Example6: Write a program to produce 15ms delay with 22Mhz crystal using Timer0 and Mode1

  1. 15000/0.546 = 27472
  2. 65536- 27472 = 38064
  3. convert to hex 94 B0
  4. TH=94 and TL= B0

                                 Mov TMOD,#01
                                 Mov TL0,#0B0h
                                 Mov TH0,# 94H
                                 Acall delay
                     Delay  : Setb TR0
                     Here   : JNB TF0 , Here 

                                 Clr TR0
                                 Clr TF0

8Bit Mode i.e Mode2:

8Bit mode is similar to that of 16bit mode except, 8bit mode is auto reload, i.e whenever the timer stops then the values of TH and TL will be reloaded with its previous original values.Note that its only 8 bit function, so TH and TL combine becomes 16 bits, so for mode2 we dont use TL register we only use TH.

For example we load TH = 5 , as after every clock tick it is decremented and becomes 0 then timer flag bit is set , and timer stops, at this point TH=0, but it again reloads to 5, this is called auto reload.

Exampe7: Generate a square wave with 50% duty cycle at the pin p1.1 , high and low state should be for 0.2ms. Crystal 11.0591, Timer1, mode 2 i.e auto reload 8 bit timer.

  1. 0.2ms/1.085us = 200/1.085 = 184
  2. 256-184=72
  3. In hex 48h
  4. TH=48h
                                           CLR P1.1
                                           Mov TMOD,#20H           ;Timer2, mode2
                                           Mov TH,#48H                 ;load value for 0.2ms
                               Backx:  CPL P1.1                        ; compliment
                                           Setb TR1                         ;start timer
                                Here:   JNB TF1, Here                ; check timer flag
                                           CLR TR1
                                           CLR TF1
                                           SJMP Backx
8051 simple example

Example8: What is the smallest frequency we get using 11.0592 and mode 2 operation.
Smallest frequency is nothing but highest time delay. so we need to find the highest time delay produced by timer using 11.0592 crystal and 8bit mode.

The highest delay is possible when we load timer register TH with 00h so
FF -00 =FF = 255 ; adding one , 256
T= 256 x1.085us = 277.76 us

Frequency is measured on whole cycle i.e ON time and OFF time , 277.76+ 277.76 = 555.52 us total time. Frequency =1/T= 1/555.52 = 1.8Khz.
So maximum time delay we can produce using 8bit mode and 11.0592 crystal is 555.52us .

We close this section here.



mr newmen said...
This comment has been removed by the author.

Post a Comment