Figure 1 shows how to interface the LED to microcontroller. As you can see the Anode is connected through a resistor to Vcc & the Cathode is connected to the Microcontroller pin. So when the Port Pin is HIGH the LED is OFF & when the Port Pin is LOW the LED is turned ON. | ||
|
||
Flashing LED ALGORITHM |
||
|
||
|
||
|
||
1. Start. 2. Turn ON LED. 3. Wait for some time (delay). 4. Turn OFF LED. 5. Wait for some time (delay). 6. Go To 2. |
||
You can see in the modified algorithm that after turning ON the LED the controller waits for the delay period & then turns OFF the led & again waits for the delay period & then goes back to the start. |
||
In the above program LED is connected to P2.0 of 89s52 Microcontroller. |
||
The above program can also be written as follows: |
||
|
||
The only drawback of the second program is that the LED's ON time will be equal to LED's OFF time. Whereas in the first program if different delay routines are called the LED's ON time can be different than that of LED's OFF time. |
||
GENERATING DELAY |
||
LOOP TECHNIQUE |
||
1. Start.
|
||
As you can see in the algorithm a number is loaded in a RAM location. It is then decremented & 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 & Jump if Not Zero. This instruction takes care or STEP 3 & STEP 4 of the above algorithm. |
||
Program for LOOP TECHNIQUE |
||
|
||
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 & 5. The OUTER LOOP consist of steps 3, 4, 5, 6 & 7. |
||
|
||
Program for LOOP WITHIN LOOP TECHNIQUE |
||
Here the inner loop (l1_delay: djnz r6,l1_delay) takes 200 iterations before R6 becomes 0. When this happens the loop is exited & then R7 is decremented & if R7 is not equal to 0 then R6 is again loaded with 200. & 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 & R7. |
||
Program for LOOP WITHIN LOOP TECHNIQUE using three RAM locations. |
||
|
.