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.
Hello,
Using the TI LaunchPad, I am trying to turn on the LEDs (at P1.0 and P4.7) by pressing the push-button (at P1.1).
Here is my code. The problem is that the LEDs toggle at the same rate and I can't stop them by pressing the push-button. It seems like the if-condition is always true.
Please let me know where is my mistake.
Thanks very much,
You do not have the pull-up-resistor enabled, so your input is floating when the button is not pressed. Change your code as follows:
#include <msp430.h> void main( void ) { unsigned int i; //delay variable WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P1REN |= 0x02; // Enable resistor on P1.1 P1OUT = 0x02; // Set resistor to pull-up, P1.0 low P1DIR = 0x01; // Set P1.0 as output and P1.1 as intput P4OUT = 0x80; // Set P4.7 high P4DIR = 0x80; // Set P4.7 as output while( 1 ) { if( !(P1IN & 0x02) ) // If push button is pressed { P1OUT ^= 0x01; P4OUT ^= 0x80; for( i=0; i<30000; i++ ); } } }
Dennis
**Attention** This is a public forum