There are two methods for delay calculations which are: 1. LOOP Technique. 2. Using Timer. |
|
1. LOOP TECHNIQUE |
|
This is very simple software based delay technique. In this method we load a number in a RAM location and decrement it till it becomes Zero. |
|
Algorithm for Loop Technique |
|
1. Start. 2. Load a number in a RAM location. e.g. R0 3. Decrement RAM Location. 4. Is RAM = 00? If NO GO TO 3. 5. STOP. |
|
As you can see in the algorithm a number is loaded in a RAM location. It is then decremented and then if the content of the RAM location is not equal to zero a jump is made to the decrementing instruction.In 8051 a single instruction "DJNZ" is specifically designed for this kind of programs. It stands for Decrement and Jump if Not Zero. This instruction takes care or STEP 3 and STEP 4 of the above algorithm. |
|
Program for LOOP TECHINQUE |
|
In above program number 100 is loaded in R7 so the LOOP (djnz instruction) is executed 100 times. To increase the delay we will have to load a larger number. The largest delay can be achieved by loading R7 with 255 i.e. 0FFh. |
|
LOOP WITHIN LOOP TECHNIQUE |
|
For longer delays we use this technique. In previous case only a single RAM location was used here the number of RAM locations depends on the number of LOOPS used. Here we discuss delays using two RAM locations. |
|
1. Start. 2. Load R7. 3. Load R6. 4. Decrement R6. 5. Is R6=0 if NO go to 4. 6. Decrement R7 7. Is R7=0 if NO go to 3. 8. Stop. The above algorithm contains two loops the INNER LOOP i.e. STEP 4 and 5. The OUTER LOOP consist of steps 3, 4, 5, 6 and 7. |
|
Program for LOOP WITHIN LOOP TECHINQUE |
|
Here the inner loop (l1_delay: djnz r6,l1_delay) takes 200 iterations before R6 becomes 0. When this happens the loop is exited and then R7 is decremented and if R7 is not equal to 0 then R6 is again loaded with 200. and again the inner loop is executed. This continues till R7=0 i.e. the inner loop is executed 100 times before the before the controller can exit from this subroutine. The delay generated can be controlled by changing the values that are loaded in R6 and R7. |
|
Program for LOOP WITHIN LOOP TECHINQUE using three RAM locations |
|
|
|
2. USING TIMER |
|
Many times we require precise internal time delays between two actions this can be accomplished using software techniques like Loop Technique but these delays keep the processor occupied because of which other important functions cannot be done. To relieve the processor of this burden we can use TIMERS provided by the controller. 8051 has two internal timers T0 and T1 (8052 has 3 timers T0, T1 and T2) these timers can be controlled individually. |
|
The timers count increments by every MACHINE CYCLE. A single Machine Cycle consists of 12 crystal pulses. i.e. 1 MACHINE CYCLE = Crystal Time/12. Crystal Time = 1/Crystal Frequency. So if we use 12MHz crystal then the timer will increment by 1 in every 1µS. The Timer always COUNTS UP, doesn’t matter in which mode it is being used the timer value is always incremented and overflows when the count goes from 0ffffh to 0000h. |
|
Algorithm for Delay using Timer |
|
1. Initialize Timer.
2. Start Timer. 3. Wait for Timer to overflow. 4. Stop |
|
In the above algorithm you have seen how to generate delay using timer. Now let us write a small program for generating delay. Suppose we have to generate 0.05 seconds delay. So now we have to calculate the Value of TH0 and TL0. | |
Count for 0.05 Sec = 0.05/1µs = 50000 |
|
So we require Timer 0 to count 50000 (0C350h) internal clock pulses to generate 0.05 sec delay. Since the timer count's up, so it will be necessary to put the (ffffh - C350h = 3CAF) in timer and count’s Up until it over flows. TF0 is set when the timer overflows. |
|
Program for Timer 0 Initialization. |
|
The above subroutine initializes TIMER 0 as 16 bit timer and loads TH0 and TL0 with values for 0.05 sec overflow duration. |
|
Program for Delay using Timer |
|
In the above program the first line initializes Timer 0. Then we Start the timer and wait for the timer to over flow. The instruction (JNB TF0,$) means "Jump if TF0 not set, back to the same instruction." The '$' operand means the address of the current instruction. Thus as long as the timer has not overflowed and the TF0 bit has not been set the program will keep executing this same instruction. After 0.05 second Timer 0 will overflow, set the TF0 bit, and program execution will then break out of the loop. Then we clear the TF0 flag and Stop the Timer. In the above program we have waited till the timer has overflowed. Instead we can use Interrupts. If we use interrupts the controller will interrupt after the timer has overflowed i.e. after 0.05 seconds. |
.