Featured

MAX30100 Pulse Oximeter and Heart Beat Sensor

MAX30100 Pulse Oximeter and Heart Beat Sensor

MAX30100 Pulse Oximeter and Heart Beat SensorThis is a Pulse Oximeter and Heart rate Sensor module b..

Rs.2,300.00

AD8232 ECG Sensor Module

AD8232 ECG Sensor Module

AD8232 ECG Sensor ModuleECG also known as Electrocardiogram is a noninvasive medical procedure to me..

Rs.1,950.00

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.
led_interfacing
Fig.1 Inferfacing LED to Microcontroller

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.

ORG 0000h

loop:

CLR P2.0

CALL DELAY

SETB P2.0

CALL DELAY

JMP loop

In the above program LED is connected to P2.0 of 89s52 Microcontroller.

The above program can also be written as follows:

ORG 0000h

loop:

CPL P2.0

CALL DELAY

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 TECHNIQUE

delay:

mov R7,#100

l1_delay:

djnz r7,l1_delay

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 TECHNIQUE

Delay:

Mov r7,#100

L2_delay:

Mov r6,#200

L1_delay:

Djnz r6,l1_delay

Djnz r7,l2_delay

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 TECHNIQUE using three RAM locations.

Delay:

Mov r7,#50

L3_delay:

Mov r6,#100

L2_delay:

Mov r5,#200

L1_delay:

Djnz r5,l1_delay

Djnz r6,l2_delay

Djnz r7,l3_delay

ret

 

.

Written by Amol Shah

Amol Shah

Founder of DNA Technology an Electronic Engineer by choice. Started working on this website as an Hobby and now its a full time venture. Very passionate about Electronics and like to learn new stuff. Want to make DNA Technology one of the best Online Store for Electronics Components in India.
Follow Me Twitter | Facebook | Google Plus | Instagram