Led will only remain ON as long as you have pressed the switch.
Code :
include<reg51.h> // Include 8051 library.
sbit led = P1^0; // Set the port where the led is connected.
sbit sw = P1^1; // Set the port where the switch is connected.
void main() // Main function starts
{
led = 1; // Led turned OFF.
if(sw==0) // if switch is pressed.
led = 0; // Led turns ON.
else //else switch is released .
led =1; // Led turns OFF.
}
: Circuit Diagram
Led is connected in sinking mode. Sinking mode is when cathode of LED is connected to Port of 8051 and anode is connected to Vcc.
A ballast resistor is added in series with led to limit the flow of current through the led and prevent excess current that can blow the led.
A switch is connected to Port(P1.1) such as when ever it excites it will provide a ground (low).
Switch controlled led :
Initially we make the port(P1.0) high for the led to turn off.
As soon as the switch excites it will turn on the led connected to Port(P1.0) as it will provide ground to the port .
The led will stay ON as long as the switch is pressed, once released the led will turn off again and can be turned on again by pressing the switch.
Comments
Post a Comment