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.

MSP430F5529LP LaunchPad push button's not working

Other Parts Discussed in Thread: MSP430F5529

Hi


I bought a MSP430F5529 launchpad and tried testing by writing a basic example for the push buttons connected on port P1.1 and glow LED's on P1.0 and P4.7 but it seems the push buttons does not affect the  LEDs.


Here is the code below,

#include <msp430.h>

void main()

{

WDTCTL = WDTPW  + WDTHOLD;

P1DIR |= BIT0;

P1REN |= BIT1;

P4DIR |= BIT7;

P1OUT = P4OUT = 0X00;

while(1)

{

if(P1IN & BIT1)

P1OUT  |= BIT0;


else

P4OUT |= BIT7;

}

return 0;

}

So when i press the button on P1.1 it does not enter the if part.

Kindly advise.

Thank you.

  • Sachin,

    you have enabled the resistor on P1.1, but if it is a pull-up or pull-down is controlled by the P1OUT register. You do not set P1.1 in P1OUT, so it is a pull-down resistor. Your button will pull the input down, too. So it is low all the time, regardless of your button press. You need a pull-up resistor.

    #include <msp430.h>
    
    void main( void )
    {
      WDTCTL = WDTPW | WDTHOLD;
    
      P1REN = BIT1;
      P1OUT = BIT1;
      P1DIR = BIT0;
      P4OUT = 0x00;
      P4DIR = BIT7;
      
      while( 1 )
      {
        if( !(P1IN & BIT1) ) // Is P1.1 low? Button pressed
        {
          P4OUT &= ~BIT7;    // P4.7 low
          P1OUT |=  BIT0;    // P1.0 high
        }
        else                 // P1.1 is high - button not pressed
        {
          P1OUT &= ~BIT0;    // P1.0 low
          P4OUT |=  BIT7;    // P4.7 high
        }
      }
    }

    I have inserted the two lines for the LEDs to switch number one off when number two lights up and vice versa. Otherwise they would just stay on after they were set to high once.

    Dennis

  • Hi Dennis

    Thanks for your quick response.

    The code worked fine, but I still didn't understand it quite well could you elaborate a little more on it?

  • Then ask a question :-) Which part(s) do you not understand?
  • I didn't understand this statement

    "You do not set P1.1 in P1OUT, so it is a pull-down resistor"

    Does it mean that pull up should be enabled by PxOUT everytime?
  • A digital input pin always needs a defined potential, otherwise it is catching up crap from the environment which can result in switching between high and low all the time surprisingly. If you connect a button between this pin and GND and you push the button, it connects the pin to GND which is a defined potential for the pin - it is low in that case. But if you release the button, this pin is hanging "in the air" again because an open button is like nothing connected to the pin at all. It is exactly the same scenario as mentioned above. So the button needs a defined potential here, too. That is why you use pull-up or pull-down resistors. Since your button switches to GND you need a pull-up one. If it would switch to Vcc, you would need a pull-down one. This pull-up resistor now sets the input to Vcc when the button is not pressed. Why a resistor? When you would connect the pin directly to Vcc, then you would short out your supply rail when the button is pressed.

    You can now connect an external resistor between the input pin and Vcc or you use the internal ones of the MSP. The MSP gives you both options - you can program the resistor to pull the pin down or up. This is done by the PxREN register. You have already used it yourself.

    Let's take the button on P1.1 again which shorts the pin down to GND when pressed. We need a pull-up resistor, so we set

    P1REN  = BIT1;
    // Or
    P1REN |= BIT1; 

    The difference between both is that you are writing to the whole port with the first statement while only manipulating BIT1 with the second one. But for this example it results in the same behaviour.

    Now the resistor is enabled in general. But the P1OUT register defines if it is pulling the pin down or up. When BIT1 in P1OUT is low, then the resistor pulls down. If the bit is set the resistor pulls high. But P1OUT is all zero after startup, so you have to set this bit by writing

    P1OUT = BIT1;

    Since I am using "=" here again, I'm writing to the complete port again, that means P1OUT is now 0000 0010. In this case it would be the same again when using "|=" because P1OUT is 0000 0000 after startup and we now just ORING 0000 0010 to this 0000 0000 in P1OUT which also results in 0000 0010. So you see that the resistor is always configured via PxREN and PxOUT. But if you would need a pull-down resistor you just would not need to do anything in P1OUT because it is zero already.

    Dennis

  • Hi Dennis

    Understood
    Thanks a lot.
  • No problem at all!

**Attention** This is a public forum