Led will only remain ON even if the switch is released .
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 delay(int t); // Delay function definition.
void main()
{
led = 1; // Led turned OFF.
while(1) //Infinite loop.
{
if(sw==0) // if switch is pressed.
led=~led; // led will toggle its state as long as the switch is pressed.
delay(100); //delay function calling.
}
}
void delay(int t) // Delay function
{
int i,j; // Assigning integer i & j.
for(i=0;i<t;i++) // For loop for delay
for(j=0;j<1275;j++); // Nested For loop for delay
}
: 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 part-2 Result :
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 even if the switch is released . If you want to again turn Off the led press the switch again .
Same switch can be used to turn On and Off led.
Comments
Post a Comment