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.

CCS/MSP430G2553: interrupt function not working

Part Number: MSP430G2553

Tool/software: Code Composer Studio

Hello everyone, 

I'm new to the microcontroller so bare with me please. I'm having a problem with the interrupt function I think. I want the RED LED to blink, and when push button is pressed, switch the blinking to GREEN LED. And switch to other LED for all subsequent button pressed. I have the fallowing code but I cant find away to go back to RED led. It seems that I have the green led stuck in while loop but I wrote that the while loop should be executed when the button is pressed and I cleared the interrupt too after that. but still I cant find a way to switch back to RED LED. 

#include <msp430.h>    

/**
 * PRACTICE
 */
void main(void)
 {
 WDTCTL = WDTPW | WDTHOLD;  // stop watchdog timer
 P1DIR |= 0x41;     // configure P1.0 as output
 P1REN |= 0X08;
 P1IE  |= 0X08;
 volatile unsigned int i;
 __enable_interrupt ();
 while(~(P1IN & 0x08))
 {
     P1OUT ^= 0x40;
     P1OUT &= ~0x01;
     for(i=10000; i>0; i--);
 }
}
#pragma vector = PORT1_VECTOR
__interrupt void Port1(void)
{
    volatile unsigned int i;
    while(P1IN & 0x08)
    {
        P1OUT ^=  0x01;
        P1IFG &= ~0x08;
        P1OUT &= ~0x40;
        for(i=10000; i>0; i--);
    }
}
  • > P1REN |= 0X08;
    > P1IE  |= 0X08;

    1) The P1.3 button on the Launchpad is active low, i.e. pushing it connects it to GND and (combined with a pulllup) generates a high->low transition.

    2) P1REN configures a pullup or pulldown depending on P1OUT. P1OUT is indeterminate at Reset, so you need to explicitly set it. You want a pullup. 

    Try adding:

    > P1OUT |= 0x08;    // Pull P1.3 up, not down

    > P1IES  |=  0x08;    // Trigger on P1.3 high->low transition

    ------------------

    >    P1OUT ^= 0x40;

    This toggles the LED, probably faster than you can see (it may visibly dim). If you want to turn it on, try:

    >    P1OUT |= 0x40;   // P1.6 LED on

    ------------------

    > while(P1IN & 0x08)

    This shouldn't be in the ISR, since it locks out your logic in main(). An if() might be appropriate

    ------------------

    More generally: Trying to do the button logic both in main and the ISR will be a headache. Either (a) treat the ISR as an event, and only change LED settings there or (b) treat main() as a polling loop, and only change LED settings there.

    ------------------

    Eventually you'll encounter switch bounce (the button will appear to be pushed more than once). The Launchpad buttons don't bounce much, so I suggest you get everything else worked out first.

**Attention** This is a public forum