Jump to content

Recommended Posts

Hi everybody,


I'm new here and beginner in programming both C and MSP430.


I'm trying to understand how to use the button on the TI MSP430 launchpad.


The goal is only to light on the green LED when I press the button.


The button on the board is labelled P1.3. Therefore I suppose it's connected on the bit 3 of port 1.


I wrote the following code in Energia, but the green LEDs is off, and nothing happens when I push the button.

#define RedOn P1OUT |= 0b00000001
#define GreenOn P1OUT |= 0b01000000
#define RedOff P1OUT &= 0b11111110
#define GreenOff P1OUT &= 0b10111111

int main()
  P1DIR |= 0b00000001; // 1.0 = Output, red LED
  P1DIR |= 0b01000000; // 1.6 = Output, green LED
  P1DIR &= 0b11110111; // 1.3 = Input, push button
  // Test : At first, only the red LED should be on
    if (P1IN & 0b00001000 != 0)

What is wrong with this code ? Is the bit #3 test not correct ? Isn't it the P1.3 I should use ?


Also, I prefer to not use "Arduino style" coding, with functions like digitalWrite and digitalRead.


Thanks in advance for any help.



Link to post
Share on other sites

I have not tried energia, but I think you left some important stuff out of your code.


Here try this:

#include <msp430.h> 

 * main.c

#define button BIT3  // define your port1
#define redLed BIT0
#define grnLed BIT6

int BTN = 0;  // for button read

void p1_init(void){  // set up port 1

	P1DIR |= redLed + grnLed;  // set to output
	P1DIR &= ~button;         // set as input

	P1REN |= button;         // pull up on button
	P1OUT |= button;         // set pull up on button

	P1OUT |= redLed;        // turn on red led
	P1OUT &= ~grnLed;      // green off to start


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

    DCOCTL = 0;
    BCSCTL1 = CALBC1_1MHZ;   // Run at 1 MHz


    	BTN = (P1IN & button);  // read port 1 for button press

    	if(!BTN){  // is the button pressed?

    		P1OUT ^= grnLed; // toggle the green led
    		P1OUT &= ~grnLed; // green is off

good luck!

Link to post
Share on other sites

Thank you timotet for your reply.


However, your code, though compiling correctly, doesn't work either.


Both red and green LEDs are on and nothing changes with the button.


Actually, in either your code or mine, the test is not working. The button push is never detected.


This gives me a headache !!!


Is the port really 1.3 for the button ?

Link to post
Share on other sites

Sorry timotet, but finally your code WORKS !


Another device connected to my launchpad prevented it to work correctly :sad:


As my code still doesn't work, I have to analyze yours to understand what is wrong in mine.

Link to post
Share on other sites

I got it working !


There was a missing line in the code (after line # 19) :

P1REN |= 0b00001000;

I also modified the test as follows (line # 27) :

if (P1IN & 0b00001000) 

Anyway, I need more knowledge about PxREN and other registers

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.

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...