0
\$\begingroup\$

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;
   }
}
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ If this helps, dont forget to upvote and accept. @MimiCracra \$\endgroup\$ – Passerby Aug 25 '15 at 20:53
0
\$\begingroup\$

the while(1) statement only encloses the S1 press handler code. count the curly braces { } and move the S2 handler code inside the set of matching curly braces that belong to the while.

while(1) // forever
{
    // all code goes here
    if(....) 
    {
    }
    if(...)
    {
    }
} // end of while(1)
// anything after this point never runs
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.