This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to work the 89C51 with a switch

hi
I am pretty much new with the 89C51 (atmel 89C51). I am trying to turn on LED with the switch. I can turn on one LED but when i try to turn on two LED it does not work. Right now both the LED turns on with the same switch. But i want to turn on the two LED with two different switch.
my program is as follows

sbit DIPswitch1=P1^4;
sbit DIPswitch2=P1^3;
sbit greenLED1=P1^5;
sbit greenLED2=P1^6;

void main(void)
{
while(1)
{
if (DIPswitch1==1)
greenLED1=0;
else
greenLED1=1;
}
{ if (DIPswitch2==1)
greenLED2=0;
else
greenLED2=1;
}

Any help will be appreciated
thanx

  • Try this:

    sbit DIPswitch1=P1^4;
    sbit DIPswitch2=P1^3;
    sbit greenLED1=P1^5;
    sbit greenLED2=P1^6;
    
    void main(void)
    {
    while(1)
    {
     if (DIPswitch1==1)
      greenLED1=0;
     else
      greenLED1=1;
     
     if (DIPswitch2==1)
      greenLED2=0;
     else
      greenLED2=1;
    }
    

  • When posting code, please follow the instructions given when you create the post:
    "Place source code source code between &ltpre&gt and &lt/pre&gt"
    Then the layout of your code will be preserved, and your problem becomes obvious:

    sbit DIPswitch1=P1^4;
    sbit DIPswitch2=P1^3;
    sbit greenLED1=P1^5;
    sbit greenLED2=P1^6;
    
    void main(void)
    {
      while(1)
      {
        if (DIPswitch1==1)
          greenLED1=0;
        else
          greenLED1=1;
      } // This brace closes the 'while'!!
      { if (DIPswitch2==1)
          greenLED2=0;
        else
          greenLED2=1;
    }

    Thus the code which tests DIPswitch2 and controls greenLED2 is unreachable ie, it is never executed!

    Note that this could be simplified to:
      while(1)
      {
         greenLED1 = !DIPswitch1;
         greenLED2 = !DIPswitch2;
      }