Wednesday, August 25, 2010

Interfacing LED to Microcontroller & LED blinking program

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.       Turn OFF LED.
4.       GO TO 2.
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

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. 
1.       ORG 0000h.
2.       loop:
3.       CLR P2.0
4.       CALL DELAY
5.       SETB P2.0
6.       CALL DELAY
7.       JMP loop 
In the above program LED is connected to P2.0. The above program can also be written as follows:
1.       ORG 0000h.
2.       loop:
3.       CPL P2.0
4.       CALL DELAY
5.       JMP loop 
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.
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 & 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 

1.       delay:
2.       mov R7,#100
3.       l1_delay:
4.       djnz r7,l1_delay
5.       ret.
 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 TECHINQUE      

1.       Delay:
2.       Mov r7,#100
3.       L2_delay:
4.       Mov r6,#200
5.       L1_delay:
6.       Djnz r6,l1_delay
7.       Djnz r7,l2_delay
8.       ret.
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.

1.       Delay:
2.       Mov r7,#50
3.       L3_delay:
4.       Mov r6,#100
5.       L2_delay:
6.       Mov r5,#200
7.       L1_delay:
8.       Djnz r5,l1_delay
9.       Djnz r6,l2_delay
10.   Djnz r7,l3_delay
11.   ret.

No comments:

Post a Comment