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.

Turn on LED with button Press?

I am still new to the Launchpad and so far I can get a LED to blink, so now I want to control that LED by having it turn on or off( don’t care at this point :p)  when I press a button(switch 2).  However the button always seems to show as being pressed. I have Googled tons and found some examples but none of them seem to work, It seems I have the newer version of the Launchpad and I need a pullup or down resistor.  I read the datasheet and I have tried adding the code for the pull up resistor but I don’t think I did it right. I have included the code below I hope someone can point me in the right direction or point out my errors

Thanks in Advance. 
Chris 

#include  <msp430g2553.h> // For the M430G2553 chip,

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;               // Stop watchdog timer

        P1REN |= 0x08;							//Turn on PullUp on????? How do you turn it off?

        P1DIR &= ~BIT3;                         // Push Port 1 P1.3 (push button) as input
        //P1OUT &= 0x08;                         // Pull down resistor???
        P1DIR |= BIT6;                          // Set P1.6 (LED) to output direction
        P1SEL &= ~(BIT3 | BIT6);                // Select Port 1 P1.3 (push button)
        
        P1OUT &= ~BIT6;                         // Set the LED off

        int value = P1IN & BIT3;
        while( 1 ) {
                if( value == 0)         // Push button down when bit 3 == 0
                        P1OUT |= BIT6;          // Set LED on when button down
                else
                        P1OUT &= ~BIT6;         // Set LED off when button off
        }
}
  • Hi,

    did you notice, that the button on the launchpad is connected to P1.2? You are using P1.3 and BIT3 instead - or are you attaching your own button? Apart from that, you only check the button once outside the main loop, so you will most likely miss the event if the button is not hold when your board is powered up. At first, put the check in side, at second read a bit about debouncing that you will need later.

  • Chris Hammond1 said:
            P1REN |= 0x08;                          //Turn on PullUp on????? How do you turn it off?


    The |= operator sets al bits in the lvalue that are set in the operand, leaving all others untouched. This activates the pullup(or pulldown - the direction is set on P1OUT) on P1.3.

    To deactivate it, the bit must be cleared by the opposite operation:
    P1REN &= ~ 0x08;
    (which will first invert 0x08 to 0xf7 and then perform an AND instruction, clearing all bits in P1REN that are not set in the operand).

    Simple binary logic.

    Chris Hammond1 said:
            int value = P1IN & BIT3;

    This must be moved into the loop, or value will never change even if P1IN changes.

    Alternatively, do the following:

    #define value (P1IN&BIT3)

    Then the token 'value' will be replaced with '(P1IN&BIT3)' whereever you use it.

**Attention** This is a public forum