1

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

jhammett
  • 23
  • 3
  • 1
    interrupts are ideally layered, thats what you want as a programmer both for development reasons and for flexibility. but that also means that while the peripheral has a gate to enable the interrupt to the next level down, there may be other gates that enable the interrupt further down toward the core, sometimes it is only one as you may be used to or two, the peripheral and the processor core, and sometimes there are even more layers and you have to enable all of them. – old_timer Dec 04 '19 at 18:51
  • likewise depending on the design you may have to clear more than one of them in a certain order – old_timer Dec 04 '19 at 18:51

1 Answers1

0

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).

CL.
  • 173,858
  • 17
  • 217
  • 259
  • So this basically means that the P1IFG bits will always be set whenever a pin changes its state no matter if the interrupt enable flag is set or not, right? – jhammett Dec 13 '19 at 19:57
  • Yes, it's completely independent from the enable flag. – CL. Dec 13 '19 at 19:57