Jump to content
43oh

Button not working on TI Launchpad (beginner)


Recommended Posts

I'm learning how to use the button on the TI Launchpad with MSP430G2553 and the button doesn't seem to work with this code (or any other codes involving using buttons) with my 2 Launchpad boards. I'm not sure if there's something wrong my the IDE or my computer. Here's the code I'm trying with. Do tell me if it works for you.

#include <msp430.h> 

/*
 * Test when a button is pressed with polling
 */

#define GREEN BIT6
#define RED BIT0
void flash(unsigned int);

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	P1OUT = 0;
	P1DIR |= RED + GREEN;	//output at P1.0 and P1.6
	P1REN |= BIT3;			//button at P1.3 active low
	//I/O to input at initialization
	while(1) {
		if(P1IN & BIT3 == 1) {
			//button is pressed
			flash(GREEN);
		} else {
			//flash red LED when button isn't pressed
			flash(RED);
		}
	}
	return 0;
}

void flash(unsigned int bit)
{
	unsigned int i = 0;	//int only goes up to 32000 ish so the for loop will become an infinite loop. Needs to be unsigned
	P1OUT ^= bit;
	for(i = 0; i < 60000; i++);
}

Link to post
Share on other sites

You need to set BIT3 on P1OUT (P1OUT |= BIT3).  This connects the pin to a pull-up resistor (your code has it connected to a pull-down).  That way - it'll register as a "1" value until the button is pushed - connecting it to GND - making it a "0" value.  Your logic for P1IN will need to flip (or it'll just operate inverse to the comments).  

Early Launchpads (V1.4 - you probably have a V1.5) had the pin connected through an external pull-up, but that meant people couldn't use the pin for anything else.  

Link to post
Share on other sites

Are you using code composer?  You can right-click on the breakpoint and choose Breakpoint Properties.  You have the option to have it halt operation, or another option is to have it refresh a view.  I believe you have to set that up while debugging (not sure why it's only available at run time).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...