SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By txwho
#111043
I am trying to write a simple program that will do the following. On power up, LED1 (P1.0) and LED2 (P1.6) are off.
When S2 is pressed(P1.3) LED1 comes on
When S2 is relesed , LED1 remains on, and remains on until S2 is pressed again, or
until power is removed.
Then when S2 is pressed a second time, LED1 switches off, and LED2 switches on. LED2
remains on until S2 is pressed again, or until power is removed.

My target board is msp430g2231 Launchpad

Here is what I have so far. I works but is not 100%. I'm new to this so any help would be greatly appreciated. I've done this is CCE v4.
#include "msp430g2231.h"

#define red_LED BIT0
#define grn_LED BIT6
#define BTN BIT3

void delay(void);

void main(void) {
WDTCTL = WDTPW + WDTHOLD;

P1OUT = 0x00; //Turn off Red Led
P1DIR |= red_LED; // Set P1.0 as an output pin Red Led
P1DIR |= grn_LED; // Set P1.6




while(1){

while ((P1IN & BTN) == BTN); // wait for button press

P1OUT |= red_LED; // turn on Red LED

while ((P1IN & BTN) == BTN); // wait for button press

P1OUT ^= red_LED; // turn off red LED

while ((P1IN & BTN) == BTN); // wait for button press

P1OUT |= grn_LED; // turn on grn LED


while ((P1IN & BTN) == BTN); // wait for button press // waits here while button is pushed in

P1OUT ^= grn_LED; // turn off grn LED


while ((P1IN & BTN) == BTN); // wait for button press;



}
}
By gm
#111203
You are probably encountering switch bounce. Usually you need to take multiple readings of the button with delays between and only accept the button press after some number of consistent readings.

Oh, and most people use the

P1OUT &= ~red_LED;

method to reset a bit.

HTH,

gm