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.

pushbutton is not working in msp430

Other Parts Discussed in Thread: MSP430G2553

Hi,

This may not be the repeat posting because i have gone through the several similar post. So, my problem is when  i am using push button, it has to glow the second LED but it is not having even i have enabled the resistor through P1REN pin. I am usd power only for the MSP430 board. Here is my code:

#include <msp430.h>
#include <msp430g2553.h>

#define LED1 BIT0
#define LED2 BIT6
#define BTN BIT3

void main(void)
{

P1DIR |= LED1+LED2;
P1OUT = 0x00;
P1REN |= BTN;

for(;;)
{
if((P1IN & BTN) == BTN)
{
P1OUT ^= LED1;
__delay_cycles(300);
}
else
{
P1OUT ^= LED2;
__delay_cycles(300);
}
} // end of for

} // end of main

When i press the pushbutton, other LED has to glow. But it is not happen. When i am observing the P1IN pin in watchlist. I am observing P1.3 is in low (usually it has to be high, because of pushup resistor), even when i am not pressed the pushbutton. P1.1,P1.2 are always are in high all the time, even i am not assign them any thing.

  • Wow I just came on here cause I have the same thing happening. I tried getting input from the button but it just illuminates the green led on 1.6. Looking forward to what someones says, I thought it may have been because it sat unprotected in the bottom of a box for a couple years along with the loose chips.

  • When you enable a bit in P1REN, the corresponding bit in P1OUT selects the direction that the resistor pulls. Since bit 3 of P1OUT is zero in your code, P1.3 is configured with a pulldown resistor instead of the pullup you want.

  • You must pull the button up. This can be done by outputting 1 on the button pin after enabling internal pull-ups.

    So, in place of 

    P1OUT=0x00

    use

    P1OUT|=BTN;

    Although implicit it is a good practice to declare pins as input too.

    You may also increase the delay to make the blink/toggle evident.

    Hope it helps

  • I have selected P1DIR of P1.3 as input and P1REN of P1.3 is selected as 1 (Resistance is connected to +Vcc). Why we need again P1OUT of P1.3 has to be configure. By the selection of P1DIR & P1REN we have configured as pull-up resistance, so by default (before i press the pushbutton) i has to come as 1. If i want to configure as pull-up resistance, what i have to do....?

  • If the outpin of P1.3 is declared as input & we are expecting to be high (since pullup configuration). Why we have to declare it as high from P1OUT. When i press the button, it has to go low. So, when i didnot press the button, by default it has to show high.

  • Hello,

    The previous postings on this matter were correct.

    You were setting your button as an input, but pulling it low before enabling the resistor. This caused your button to have a pulldown resistor enabled, instead of a pullup resistor. I corrected your code and have posted it below.

    #define LED1 BIT0
    #define LED2 BIT6
    #define BTN BIT3
    
    int main(void) {
        WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
    	
        P1DIR |= LED1+LED2;
        P1OUT = BTN;
        P1REN |= BTN;
    
        while(1) {
        	if(P1IN & BTN) {
        		P1OUT &= ~LED2;
        		P1OUT ^= LED1;
        		__delay_cycles(30000);
        	}
        	else {
        		P1OUT &= ~LED1;
        		P1OUT ^= LED2;
        		__delay_cycles(30000);
        	}
        }
    }

  • Kanchibhotla Chandra Sekhar said:
    I have selected P1DIR of P1.3 as input and P1REN of P1.3 is selected as 1 (Resistance is connected to +Vcc).

    No. By setting P1REN.3 you have enabled the resistor, but where it connects to (VCC/VSS) is selected with P3OUT.3.
    Setting P1DIR.3 will override the P1REN.3 bit on many, but not all MSPs, so the resistor is off when the pin is in output direction (saving power if the output and pullup direction are complementary)

    You can take a look at the port pin schematics in the device datasheet. It (usually) gives a good visualization of which bit controls what.

**Attention** This is a public forum