Search code examples
microcontrollerinterruptmsp430

Interrupt-Flag is set although Interrupt-Enable-Flag is not set


i've written a small program for the MSP430FR6989 to toggle the LED as long as the Button is pressed.

#include <msp430.h>

/**
 * main.c
 */
int main(void)
{
    WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
    PM5CTL0 &= ~LOCKLPM5;

    // reset port 1
    P1DIR = 0x00;
    P1OUT = 0x00;

    // turn off led on startup
    P1DIR |= BIT0;
    P1OUT &= ~BIT0;

    P1DIR &= ~BIT1; // P1.1 -> input
    P1REN |= BIT1;  // P1.1 -> enable resistor
    P1OUT |= BIT1; // P1.1 -> pull-up resistor

    // enable on P1.1
    P1IES |= BIT1;
    P1IE |= BIT1;
    P1IFG = 0x00;

    __enable_interrupt();
    while(1)
    {
        __delay_cycles(1000);
    }

    return 0;
}

#pragma vector=PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
    switch (__even_in_range(P1IV, P1IV_P1IFG1))
    {
        case P1IV_P1IFG1:
            P1OUT = (P1IN & BIT1)
                ? P1OUT & ~BIT0
                : P1OUT | BIT0;
            P1IES ^= BIT1;

            break;
        default:
            break;
    }
}

Everything works as expected. BUT: When I debug the program I see that BIT0 in P1IFG is set as soon as I pressed the button for the first time. Why is this happening? I thought it would only be set if I enable the corresponding IE-Bit?

Thanks in advance


Solution

  • Section 12.2.6 of the MSP430FR58xx, MSP430FR59xx, and MSP430FR6xx Family User's Guide says:

    Each PxIFG bit is the interrupt flag for its corresponding I/O pin, and the flag is set when the selected input signal edge occurs at the pin. All PxIFG interrupt flags request an interrupt when their corresponding PxIE bit and the GIE bit are set.

    So you can always use the P1IFG bit to check if a transition on the input pin has happened. This is useful if you want to detect short pulses that might already be over when your code gets around to reading the current state of the pin.

    This is why you should clear the P1IFG register before enabling interrupts (unless you are interested in old events).