0
\$\begingroup\$

I am trying to work around a circuit and my requirement is as follows: - There are two inputs and two outputs. Each input controls an individual led. I am writing a code for this requirement using a Nuvoton MS51 based controller (Which itself is 8051 based controller). The software used for the same is Keil. Following is the code I have written-

#include "MS51.h"
void main (void)
{
    P01_INPUT_MODE;
    P03_INPUT_MODE; 
    P10_PUSHPULL_MODE;
    P00_PUSHPULL_MODE;
    while (1)
    {
        if (!(P0 & SET_BIT3))
        {
            while( !(P0 & SET_BIT3));
            P0 ^= 0x01;
        }
        if (!(P0 & SET_BIT1))
        {
            while (!(P0 & SET_BIT1));
            P1 ^= 0x01;
        }
    }
}

The problem I am facing is that both the switches are not working simultaneously but only in a sequence. The first switch will work, then the second (I am not able to use the first switch again). Is there anything wrong that I am doing in the code?

Solution: I have found the mistake I was doing. The mistake was of pull-up/pull-down of the switch. My switch is pulled down and the code that I had written corresponded to the case of a pull-up. Hence my code was being stuck in the while loop again and again, hence stopping the flow of code to the next line.

\$\endgroup\$
  • \$\begingroup\$ Your XORs will execute too fast to see. I suspect you want the LEDs to reflect the PB state, not toggle it. But only you know, since you've not explained it here (except through code that you say doesn't do what you want.) Just set up two variables (or two bits in a variable) to represent the current state of the LED. Clear it and turn off the LEDs. Then use your while(1) but inside just capture a snapshot of the port (both PBs) and see if either LED state differs from the PB state you just read. Adjust where different with PB1, update your internal state, same for PB2 then, and loop. \$\endgroup\$ – jonk Nov 21 '19 at 7:53
  • \$\begingroup\$ The way your code is written it looks like you want the LED to toggle on or off alternately each time its switch is turned on and off again - is that what you want? Or do you want the LED state to follow the switch state? Are they toggle switches or push buttons? \$\endgroup\$ – Bruce Abbott Nov 21 '19 at 8:20
  • \$\begingroup\$ You need to debounce your switch inputs. But you should understand that idea and rewrite your software yourself, there's plenty of existing text on the interweb covering that. \$\endgroup\$ – TonyM Nov 21 '19 at 8:23
  • \$\begingroup\$ @jonk, I think you missed the semicolon after the while. \$\endgroup\$ – Jasen Nov 21 '19 at 8:52
  • 1
    \$\begingroup\$ @Jason I wasn't writing code. Just trying to tell the OP how to get a clue. \$\endgroup\$ – jonk Nov 21 '19 at 8:53
5
\$\begingroup\$

The code may incorrectly assume buttons being active low if they actually are active high. The code can't know that, it just looks at the bit value and it may be incorrect. But that is how the code you wrote works - if either of the button bits are low, it sits in the corresponding button loop while it is low, or in other words, until it is high again without checking for the other button. If you want it to work differently, take paper and pen and design how it should work and then write it. Rarely any software is written first without thinking how it should work.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ or it could be the opposite if the switches pull in the other direction. \$\endgroup\$ – Jasen Nov 21 '19 at 8:59
  • \$\begingroup\$ Thanks, of course button being active low or active high will affect the code. \$\endgroup\$ – Justme Nov 21 '19 at 9:29

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.