| Interfacing LED to Microcontroller & LED blinking program |
Figure 1 Flashing LED ALGORITHM
We now want to flash a LED. It works by turning ON a LED & then turning it OFF & then looping back to START. However the operating speed of microcontroller is very high so the flashing frequency will also be very fast to be detected by human eye. Modified Flashing LED ALGORITHM
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. 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 DELAYLOOP TECHNIQUE
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 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 TECHNIQUEFor 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.
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 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 & 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 TECHINQUE using three RAM locations.
|
No comment posted
mXcomment 1.0.5 © 2007-2010 - visualclinic.fr
License Creative Commons - Some rights reserved
| < Prev |
|---|
















