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.

MSP430 Interrupt problem

Other Parts Discussed in Thread: MSP430G2231

Hi.

The code below just fades some LEDs in a sequence. The "start" of the LED action is given by the pushbutton (S2) on the launchpad. Also, the "stop" of the LED fading/blinking should be done with a push of the same button. The "start" is just fine. The "stop" on the other hand acts weird. For example it won't turn off the LEDs until the whole while(1) loop is complete and also, sometimes when I press the button, one of the LEDs will stay ON no matter what part of the program is executed. Any ideas?

What I am looking for is something like this: push button --> LEDs blink&fade; push button again --> all LEDs OFF.

Thanks.

This is the code I am currently running (I apologize for the length, but I wanted you to see if there is something wrong with what I'm doing) :

#include <msp430g2231.h>

#define LED_OUT P1OUT
#define LED_DIR P1DIR
#define P0 BIT0 // white led
#define P1 BIT1 // Red led
#define P2 BIT2 // white led
#define BUTTON BIT3 // button

void Delay_us(unsigned int us);
unsigned int blink = 0;

void main(void)
{
    WDTCTL = WDTPW + WDTHOLD;    // Set WDT to stop
    LED_DIR |= (P0 + P1); // Set P1.0 and P1.6 to output direction
    LED_OUT &= ~(P0 + P1); // Set the LEDs off
    P1IE |= BUTTON;

    __enable_interrupt();

    LED_DIR |= P2;
    LED_OUT &= ~P2;
    int j, k, i, m, n;
    i=0; // used to vary the "speed" of the LEDs
    n=0; // not currently used
    while(1)
    {
            if(blink>0)
            {
                while(1)
                {
                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P0;
                                Delay_us(j);
                                P1OUT ^=P0;
                            }
                        }

                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 6; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P0;
                                Delay_us(255-j);
                                P1OUT ^=P0;
                            }
                        }
                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 2; k++)
                            {
                            Delay_us(255-j);
                            P1OUT ^=P0;
                            Delay_us(j);
                            P1OUT ^=P0;
                            }
                        }
                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 2; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P0;
                                Delay_us(255-j);
                                P1OUT ^=P0;
                            }
                        }
                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 3; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P0;
                                Delay_us(j);
                                P1OUT ^=P0;
                            }
                        }
                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 3; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P0;
                                Delay_us(255-j);
                                P1OUT ^=P0;
                            }
                        }
                        __delay_cycles(100000);

                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P1;
                                P1OUT ^=P0;
                                Delay_us(j);
                                P1OUT ^=P1;
                                P1OUT ^=P0;
                            }
                        }

                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P1;
                                P1OUT ^=P0;
                                Delay_us(255-j);
                                P1OUT ^=P1;
                                P1OUT ^=P0;
                            }
                        }
                        __delay_cycles(100000);

                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P2;
                                P1OUT ^=P1;
                                Delay_us(j);
                                P1OUT ^=P2;
                                P1OUT ^=P1;
                            }
                        }

                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P2;
                                P1OUT ^=P1;
                                Delay_us(255-j);
                                P1OUT ^=P2;
                                P1OUT ^=P1;
                            }
                        }
                        __delay_cycles(100000);
                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 12; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P1;
                                Delay_us(j);
                                P1OUT ^=P1;
                            }
                        }

                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 6; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P1;
                                Delay_us(255-j);
                                P1OUT ^=P1;
                            }
                        }
                        for(j = 0; j < 255; j++)
                        {    // fades LED out
                            for(k = i; k < 3; k++)
                            {
                                Delay_us(255-j);
                                P1OUT ^=P1;
                                Delay_us(j);
                                P1OUT ^=P1;
                            }
                        }

                        for(j = 0; j < 255; j++)
                        {    // fades LED in
                            for(k = i; k < 3; k++)
                            {
                                Delay_us(j);
                                P1OUT ^=P1;
                                Delay_us(255-j);
                                P1OUT ^=P1;
                            }
                        }
                        __delay_cycles(100000);
            i=i+2;
            if (i>11)
            {
                i=0;
            }
            n=n+1;
            }
        }
    }
}
void Delay_us(unsigned int us) // delay function
{

while(us--){

__delay_cycles(1);

}
}
// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
blink ^= 0x01;
P1IFG &= ~BUTTON; // P1.3 IFG cleared
LED_OUT &= ~(P0 + P1 + P2); // Clear the LEDs so they start in OFF state

}

  • Your code will wait for button press to start fading. However, then it simply runs its way until the whoel sequence is done.
    To have the 'blink' doing anything, you'd have to check it inside each and every loop and break out of it if blink is set.

    In the current code, the first button push starts the fade, then the next button push turns all LEDs off, just to have them set on again immediately by the ongoing main code.
    So the push that shall turn the fde off will only be a barely noticeable flicker in the current course of the fading.

    Also, unless oyu use a bounce-free pushbutton, your port ISR will be triggered multiple times due to button bouncing, starting and stopping teh fade multiple times (if stopping would work).

    Also, if blink is 1 for the first time, the main code enters an endless loop and won't stop at all even if blink is 0 again.

    Suggestions:

    1) declare blnk as volatile. If you don't, the main code may not notice that it 'magically' changes its value by the itnerrupt

    2) check for blink in each and every loop (or at least at strategical points) and break out of the loops if cleared by the interrupt

    3) Apply a debouncing mechanism to the pushbutton interrupt. E.g. after a port itnerrupt, disable the port interrupt and start a timer delay. Ath the delay end, re-enable the port interrupt.

    4) use the PWM hardware to do the LED fading. To fade in, juwt increase the duty cycle, and decrease it when fading out. TO stop it, simply deactivate the PWM.

**Attention** This is a public forum