This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430F5529 Using the Push Button P1.1

Hello,

Using the TI LaunchPad, I am trying to turn on the LEDs (at P1.0 and P4.7) by pressing the push-button (at P1.1).

Here is my code. The problem is that the LEDs toggle at the same rate and I can't stop them by pressing the push-button. It seems like the if-condition is always true.

Please let me know where is my mistake.

Thanks very much,

#include <msp430.h> 
int main(void) {
unsigned int i; //delay variable
    WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    P1DIR = 0x01; //set P1.0 as output and P1.1 as intput
    P4DIR = 0x80; //set P4.7 as output
P4OUT = 0x80; //set P4.7 high
while(1)
   {
if ((P1IN & 0x02) != 0x02) //if push button is pressed
{
P1OUT ^= 0x01;
P4OUT ^= 0x80;
for (i=0; i<30000; i++);
}
   }
return 0;
}
  • The PxOUT registers are undefined after reset; you must initialize P1.0, too.

    The for loop can be optimized away by the compiler. Use __delay_cycles() instead.

  • You do not have the pull-up-resistor enabled, so your input is floating when the button is not pressed. Change your code as follows:

    #include <msp430.h>
    
    void main( void )
    {
      unsigned int i; //delay variable
      
      WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    
      P1REN |= 0x02; // Enable resistor on P1.1
      P1OUT  = 0x02; // Set resistor to pull-up, P1.0 low
      P1DIR  = 0x01; // Set P1.0 as output and P1.1 as intput
    
      P4OUT  = 0x80; // Set P4.7 high
      P4DIR  = 0x80; // Set P4.7 as output
    
      while( 1 )
      {
        if( !(P1IN & 0x02) ) // If push button is pressed
        {
          P1OUT ^= 0x01;
          P4OUT ^= 0x80;
    
          for( i=0; i<30000; i++ );
        }
      }
    }

    Dennis

  • Thank you SO much Dennis for your help. You are right, I had to enable the pull-up resistor. That makes perfect sense. Again Thank you!

**Attention** This is a public forum