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.
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,
The PxOUT registers are undefined after reset; you must initialize P1.0, too.
The for loop can be optimized away by the compiler. Use __delay_cycles() instead.
We are glad that we were able to resolve this issue, and will now proceed to close this thread.
If you have further questions related to this thread, you may click "Ask a related question" below. The newly created question will be automatically linked to this question.
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:
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++ );
In reply to Dennis Eichmann:
All content and materials on this site are provided "as is". TI and its respective suppliers and providers of content make no representations about the suitability of these materials for any purpose and disclaim all warranties and conditions with regard to these materials, including but not limited to all implied warranties and conditions of merchantability, fitness for a particular purpose, title and non-infringement of any third party intellectual property right. No license, either express or implied, by estoppel or otherwise, is granted by TI. Use of the information on this site may require a license from a third party, or a license from TI.
TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs andembedded processors, along with software, tools and the industry’s largest sales/support staff.