Code:
{
volatile long int i=0;
// Stop WDT
WDTCTL = WDTPW + WDTHOLD;
// Configure input/output pins
// Setup P1.0 as an output port (for LED1)
P1SEL = P1SEL & ~BIT0;
P1DIR = P1DIR | BIT0;
// Setup P1.1 as an output port (for LED2)
P1SEL = P1SEL & ~BIT1;
P1DIR = P1DIR | BIT1;
// Setup P2.6 as an input port (for SW1)
P2SEL = P2SEL & ~BIT6;
P2DIR = P2DIR & ~BIT6;
// Enable the internal pull resistor and set as a pullup
P2REN = P2REN | BIT6;
P2OUT = P2OUT | BIT6;
// Set-up the interrupt for P1.6 (SW1)
// P2.6 Hi/lo edge (falling edge)
P2IES |= BIT6;
// P2.6 IFG cleared
P2IFG &= ~BIT6;
// P2.6 Interrupt Enable
P2IE |= BIT6;
// Enable interrupts globally
_EINT();
// Polling loop with software delay
for(;;) // this is equivalent to while(1)
{
// Toggle LED 2
P1OUT ^= 0x02;
// Spin round for n cycles
for(i=0;i<30000;i++){;}
}
}
// Port 2 Interrupt Service Routine (ISR)
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
// P1.0 = toggle LED 1
P1OUT ^= BIT0;
// clear P2.6 IFG
P2IFG &= ~BIT6;
}
I'm very new to programming and my task is to modify this code so that when Switch 1 is released, LED 1 will remain on and when it's pressed, LED 1 will turn off.
The way the code is right now, LED 1 will turn on and off every time I click switch 1 and LED 2 will blink constantly on a loop.
Can someone tell me if this is correct what I've done to complete my task? I added:
Code:
void main(void)
{
volatile long int i=0;
// Stop WDT
WDTCTL = WDTPW + WDTHOLD;
// Configure input/output pins
// Setup P1.0 as an output port (for LED1)
P1SEL = P1SEL & ~BIT0;
P1DIR = P1DIR | BIT0;
// Setup P1.1 as an output port (for LED2)
P1SEL = P1SEL & ~BIT1;
P1DIR = P1DIR | BIT1;
// Setup P2.6 as an input port (for SW1)
P2SEL = P2SEL & ~BIT6;
P2DIR = P2DIR & ~BIT6;
// Enable the internal pull resistor and set as a pullup
P2REN = P2REN | BIT6;
P2OUT = P2OUT | BIT6;
// Set-up the interrupt for P1.6 (SW1)
// P2.6 Hi/lo edge (falling edge)
P2IES |= BIT6;
// P2.6 IFG cleared
P2IFG &= ~BIT6;
// P2.6 Interrupt Enable
P2IE |= BIT6;
// Enable interrupts globally
_EINT();
// Polling loop with software delay
for(;;) // this is equivalent to while(1)
{
// Toggle LED 2
P1OUT ^= 0x02;
// Spin round for n cycles
for(i=0;i<30000;i++){;}
}
}
// Port 2 Interrupt Service Routine (ISR)
#pragma vector=PORT2_VECTOR
__interrupt void Port_2(void)
{
P2IES ^=BIT6;
if(P2IN & BIT6)
{
// Turn on the LED
P1OUT |= BIT0;
}
else
{
// Turn off the LED
P1OUT &= ~BIT0;
} // clear P2.6 IFG
P2IFG &= ~BIT6;
}
Will this code work so that when LED 1 is released, it will stay off and when it's pressed, it will stay on. Also, will LED 2 continue to keep blinking on the loop as this happens?