Electronic – wrong with this MSP430 code

cmsp430

I am using the MSP-EXP430F5438 evaluation board, which has pushbuttons connected to P2.6 and P2.7 and LEDs connected to P1.0 and P1.1.

I want to make it such that P2.6 sets the LEDs on and P2.7 resets them. To do that, I wrote the following code:

#include  "msp430x54x.h"

int main(void)
{
          WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
          P1DIR |= BIT0 + BIT1;                 // Set P1.0 and P1.1 to output direction
          P2DIR = 0x00;                         // Set the P2 to input direction

          P1OUT = 0x00; // Blank all LEDs

          while(1)
         {
            if(P2IN & 0x40) // When P2.6 is pushed
              P1OUT = 0x03;
            if(P2IN & 0x80) // When P2.7 is pushed
              P1OUT = 0x00;
            __delay_cycles(100000);
         }
}

But it doesn't work properly; sometimes it works erratically (the LEDs turn on when P2.6 is pressed but they don't turn off when P2.7 is pressed), sometimes it doesn't work at all.

Any ideas?

Best Answer

There are no pullups on P2.6 and P2.7.

enter image description here

The MSP has internal pullup / pulldowns but you have to set them up yourself.

So as part of your initialization, add this

P2REN |= (1<<6) | (1<<7);  // turn on pullups
P2OUT |= (1<<6) | (1<<7);  // set them to pull up

Then the inputs won't float randomly when the switches aren't pressed.