Designing a delay program using 8051 timers.
While designing delay programs in 8051, calculating the initial value that has to be loaded inot TH and TL registers forms a very important thing. Let us see how it is done.
Assume the processor is clocked by a 12MHz crystal.
That means, the timer clock input will be 12MHz/12 = 1MHz
That means, the time taken for the timer to make one increment = 1/1MHz = 1uS
For a time delay of “X” uS the timer has to make “X” increments.
2^16 = 65536 is the maximim number of counts possible for a 16 bit timer.
Let TH be the value value that has to be loaded to TH registed and TL be the value that has to be loaded to TL register.
Then, THTL = XXYY( Hexadecimal equivalent of (65536-X) where (65536-X) is considered in decimal.)
Load this value to the timer register.
TH=XXh TL=YYh
If the Count Value is 0xFFBE(say) we can use Timer mode 2 i.e 8 bit auto relaod and TH0/1=BE;
Gate | C/T | M1 | M0 | Gate | C/T | M1 | M0 |
---|---|---|---|---|---|---|---|
Timer 1 | Timer 0 |
G=0 ,if we control timer/counter through software and G=1 if we control through hardware
C/T =0 , if we use timer and C/T=1 , if we use counter
M1 and M0 values are taken according to the mode of operation of timers which are given in below table .
Using above values we define the value that is to be loaded in TMOD register
EXAMPLE:
If the timer1(C/T=0) is operating in mode 1(M1=0 M0=1) ,timer0 as counter0 (C/T=1) in mode2(M1=1 M0=0) and we are controlling both timers through software(G=0) then TMOD = 16H
Gate | C/T | M1 | M0 | Gate | C/T | M1 | M0 |
---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 0 | 1 | 1 | 0 |
M1 | M0 | MODE | DESCRIPTION |
---|---|---|---|
0 | 0 | Mode 0 | 13 bit timer |
0 | 1 | Mode 1 | 16 bit timer |
1 | 0 | Mode 2 | 8 bit auto reload |
1 | 1 | Mode 3 | split timr |