This code let led work when I push the push botton and of when its not bushed. I
ID: 3707532 • Letter: T
Question
This code let led work when I push the push botton and of when its not bushed.I want to use the same code and when I push the button turn led on and stay on and when I push again it turns it of and I want it to repeat forever.
#include <msp430.h>
#define GREEN_LED 0x0080
#define CLEAR_GREEN_LED 0x007F
#define BUTTON11 0x0004
#define DEVELOPMENT 0x5A80
#define ENABLE_PINS 0xFFFE
main()
{
WDTCTL = DEVELOPMENT;
PM5CTL0 = ENABLE_PINS;
P9DIR = GREEN_LED;
P1OUT = BUTTON11;
P1REN = BUTTON11;
while(1)
{
while((BUTTON11 & P1IN) == 0)
{
P9OUT = P9OUT | GREEN_LED;
}
P9OUT = P9OUT & CLEAR_GREEN_LED;
}
}
This code let led work when I push the push botton and of when its not bushed.
I want to use the same code and when I push the button turn led on and stay on and when I push again it turns it of and I want it to repeat forever.
#include <msp430.h>
#define GREEN_LED 0x0080
#define CLEAR_GREEN_LED 0x007F
#define BUTTON11 0x0004
#define DEVELOPMENT 0x5A80
#define ENABLE_PINS 0xFFFE
main()
{
WDTCTL = DEVELOPMENT;
PM5CTL0 = ENABLE_PINS;
P9DIR = GREEN_LED;
P1OUT = BUTTON11;
P1REN = BUTTON11;
while(1)
{
while((BUTTON11 & P1IN) == 0)
{
P9OUT = P9OUT | GREEN_LED;
}
P9OUT = P9OUT & CLEAR_GREEN_LED;
}
}
This code let led work when I push the push botton and of when its not bushed.
I want to use the same code and when I push the button turn led on and stay on and when I push again it turns it of and I want it to repeat forever.
#include <msp430.h>
#define GREEN_LED 0x0080
#define CLEAR_GREEN_LED 0x007F
#define BUTTON11 0x0004
#define DEVELOPMENT 0x5A80
#define ENABLE_PINS 0xFFFE
main()
{
WDTCTL = DEVELOPMENT;
PM5CTL0 = ENABLE_PINS;
P9DIR = GREEN_LED;
P1OUT = BUTTON11;
P1REN = BUTTON11;
while(1)
{
while((BUTTON11 & P1IN) == 0)
{
P9OUT = P9OUT | GREEN_LED;
}
P9OUT = P9OUT & CLEAR_GREEN_LED;
}
}
Explanation / Answer
#include <msp430.h>
#define GREEN_LED 0x0080
#define CLEAR_GREEN_LED 0x007F
#define BUTTON11 0x0004
#define DEVELOPMENT 0x5A80
#define ENABLE_PINS 0xFFFE
main()
{
WDTCTL = DEVELOPMENT;
PM5CTL0 = ENABLE_PINS;
P9DIR = GREEN_LED;
P1OUT = BUTTON11;
P1REN = BUTTON11;
while(1)
{
while((BUTTON11 & P1IN) != 0); /// Loop until the button is not pressed
/// Once the button is pressed it will come here
if((P9OUT & GREEN_LED) == GREEN_LED) /// If LED is already on,
P9OUT = P9OUT & CLEAR_GREEN_LED; /// Then turn it off
else
P9OUT = P9OUT | GREEN_LED; /// If LED is not on, Then turn on the LED
while((BUTTON11 & P1IN) == 0); /// Loop until the button is not released
}
}
Note: The logic is to donot enter to LED state change until any button is pressed. Once button is pressed, Check the current status of the LED. If LED is off, turn it on and if it is on, turn it off. Then again donot exit the loop until the push button is released.
Ping me back if you have any issue/doubts. Thanks.