RafaelE24
RafaelE24

Reputation: 1

MSP432 Launchpad not recognizing when only one button is pressed

I am trying to create a program on the MSP432 Launchpad which will turn on the Green LED when neither of the two onboard buttons (P1.4 and P1.1) are being pressed and turn on the Red LED if only the P1.4 button is being pressed. With the code below, the Launchpad properly turns on the green LED when no buttons are being pressed, but does not turn on the Red LED when I am only pressing the P1.4 button. Is there any issue with my code or the configuration of the pins?

#include "msp.h"
#include "clock.h"
#include "TExaS.h"
void main()
{
    Clock_Init48MHz();

     /* Configuration of MSP42 */
     P2->SEL0 &= ~0x03;             // configure P2.0 and 2.1 as GPIO
     P2->SEL1 &= ~0x03;             // configure P2.0 and 2.1 as GPIO
     P2->DIR  |= 0x03;              // configure P2.0 and P2.1 as output


     //configure buttons on P1.1 and P1.4 as GPIO pull up inputs
      P1->SEL0 &= ~0x12;
      P1->SEL1 &= ~0x12;
      P1->DIR &= ~0x12;
      P1->REN |= 0x12;
      P1->OUT |= 0x12;


     //Application
     while(1){

         Clock_Delay1ms(100);

         //if P1.4 and P1.1 are both not being pressed
         if(P1->IN & 0x12){
             P2->OUT |= 0x02;  //turn on green light
             P2->OUT &= ~0x01; //turn off red light
         }

         //if only P1.4 is pressed
         else if(P1->IN & 0x10){
             P2->OUT |= 0x01;  //turn on red light
             P2->OUT &= ~0x02; //turn off green light
         }

     }

}

Upvotes: 0

Views: 472

Answers (1)

Tom V
Tom V

Reputation: 5510

Your if statements don't agree with the comments above them.

If your switches are active low (which turning pull-ups on implies) then the first if statements tests if either button is unpressed, not both, and your else-if checks if P1.4 is unpressed irrespective of P1.1 (which can never be the case because it would already have matched the if - that is, unless the value changes between reads).

I think you might need to consider the difference between if ((x & m) == m) and if (x & m).

Upvotes: 1

Related Questions