尽管未设置中断启用标志,但已设置中断标志

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

我已经为 MSP430FR6989 编写了一个小程序,只要按下按钮就会切换 LED。

#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;
    }
}

一切正常。 但是:当我调试程序时,我看到 P1IFG 中的 BIT0 在我第一次按下按钮时就被设置了。 为什么会这样?我以为只有启用相应的IE-Bit才会设置?

提前致谢

MSP430FR58xx, MSP430FR59xx, and MSP430FR6xx Family User's Guide 的第 12.2.6 节说:

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.

因此您始终可以使用 P1IFG 位来检查输入引脚上是否发生了转换。如果您想检测当您的代码开始读取引脚当前状态时可能已经结束的短脉冲,这将很有用。

这就是为什么您应该在启用中断之前清除 P1IFG 寄存器(除非您对旧事件感兴趣)。