You caught my mistake. Thanks Mr.Chips I appreciate you. My current program is able to turn ON/OFF two LED's using two switches.Where is the GND connection on the second switch?
#include <REG51.h>
sbit Switch1 = P0^1; //switch connected to P0.1
sbit Switch2 = P0^2; //switch connected to P0.2
sbit LED1 = P2^1; //LED connected to p2.1
sbit LED2 = P2^2; //LED connected to p2.2
#define ON 1
#define OFF 0
#define input 1
void main (void)
{
Switch1 = input; // make P0.1 and P0.2 an input!!
Switch2 = input;
LED1 = OFF;
LED2 = OFF;
while(1)
{
while(Switch1 == ON) //If switch1 is ON
{
LED1 = ON; // Turn ON LED1
}
while(Switch2 == ON) //If switch2 is ON
{
LED2 = ON; // Turn ON LED2
}
}
}
I wrote code for following tableThere are four possible combinations of Switch1 and Switch2.
Draw a truth table of all combinations and the desired outcome of LED1 and LED2.
All you need is one additional line of code for the case when both Switch1 and Switch2 are On.
S1 S2 LED1 LED2
Off Off off off
On Off On Off
Off On off On
On On On On
#include <REG51.h>
sbit Switch1 = P0^1; //switch connected to P0.1
sbit Switch2 = P0^2; //switch connected to P0.2
sbit LED1 = P2^1; //LED connected to p2.1
sbit LED2 = P2^2; //LED connected to p2.2
#define ON 1
#define OFF 0
#define input 1
void main (void)
{
Switch1 = input; // make P0.1 and P0.2 an input!!
Switch2 = input;
LED1 = OFF;
LED2 = OFF;
while(1)
{
if(Switch1 == ON && Switch2 == ON) //If both is ON
{
LED1 = LED2 = ON; // Turn ON LED1
}
else if(Switch1 == ON && Switch2 == OFF) //If switch1 is ON
{
LED1 = ON; // Turn ON LED1
}
else if(Switch1 == OFF && Switch2 == ON) //If switch2 is ON
{
LED2 = ON; // Turn ON LED2
}
else
{
LED1 = LED2 = OFF; // Turn OFF Both
}
}
}
while(1)
{
P2 = ~( P0 >>1);
}
My calculation prove me wrongThat doesn't even need processing!!!
Read the switches and shift right, then output onto the led'sC:while(1) { P2 = ~( P0 >>1); }
by Jake Hertz
by Jake Hertz
by Aaron Carman