How to interface two leds and two push button with 8051

8051c

i'm using 8051 mcu to do some tests.
i'm trying to use two input for two push buttons and two outputs for two Leds.
SW1 = push button 1
SW2 = push button 2
LED1 = led1
LED2 = led2

for example
-if i push S1 the Led1 will glow continuously.
-if i push S2 the Led2 will glow continuously.

These two command works independently.

So here what i tried:
only S1 and Led1 are working.
Please help, thank you.

#include <reg51.h>

sbit S1 = P2^0;
sbit S2 = P2^1;

sbit Led = P1^0;
sbit Led2 = P1^1;

void main()
 {
    S1 = 1;
    S2 = 1;

     while(1)  // For ever 
       {
         if(S1 == 0)  // if press Push button
          { 
            Led = 0;// ON LED ( active low )
            while(S1 == 0);// Wait here for release of push button
          }
      Led = 1;
       }

{
  if (S2 == 0)
    {
      Led2 = 0;
      while (S2 == 0);
       }
       Led2 = 1;
   }
}

Best Answer

You have un paired or extra curly brackets.

void main() {
    S1 = 1;
    S2 = 1;

    while(1) {  // For ever 
      if(S1 == 0) {  // if press Push button
         Led = 0;// ON LED ( active low )
         while(S1 == 0);// Wait here for release of push button
      }
      Led = 1;
      } // LOOPS BACK TO WHILE
    { // ***EXTRA***
    if (S2 == 0) {
      Led2 = 0;
      while (S2 == 0);
    }
    Led2 = 1;

   } // While?
} // Main?

Fixed:

void main() {
    S1 = 1;
    S2 = 1;

    while(1) {  // For ever 
      if(S1 == 0) {  // if press Push button
         Led = 0;// ON LED ( active low )
         while(S1 == 0);// Wait here for release of push button
      }

      Led = 1;

      if (S2 == 0) {
         Led2 = 0;
         while (S2 == 0);
      }

      Led2 = 1;

   } // While
} // Main
Related Topic