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.

Multiple Port Interrupt on same Port

Other Parts Discussed in Thread: MSP430G2553

Dear Sir,

                  Please help me to get multiple interrupt on Same Port, I have written code for Single port interrupt, And here i have inserted whatever i have written for port interrupt code.

#include <msp430g2553.h>

void main(void) {
    WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

    P1DIR |=  BIT0;                     // Set P1.0 to output direction
    P1OUT &= ~BIT0;                 // Set  LED off
    P1SEL &= ~BIT3;                    // Select Port 1 P1.3 (push button)
    P1DIR &= ~BIT3;                    // Port 1 P1.3 (push button) as input, 0 is input
    P1REN |=  BIT3;                        // Enable Port P1.3 (push button) pull-up resistor
    P1IE  |=  BIT3;                        // Port 1 Interrupt Enable P1.3 (push button)
    P1IFG &= ~BIT3;                    // Clear interrupt flag
    _BIS_SR(GIE);                           // Enable interrupts

}
                                    // Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
      __interrupt void Port_1(void) {

    P1IFG &= ~BIT3;                     // P1.3 Interrupt Flag cleared
    P1OUT ^= BIT0;                     // Toggle LED state

}

  • What do you mean with 'multiple interrupts'? More than one port pin triggering an interrupt? Or more than one interrupt form the same pin?
    Interrupts don't stack. Once an interrupt event has been detected, the IFG bit is set and remains set until cleared. If more events happen before it was cleared, you won't notice (well, some modules have an overflow detection, like the timer or the ADC)

    For multiple interrupts on different pins of a port, each pin sets its own IFG bit. Inside the ISR check the IFG bits and handle the interrupts.

  • Or he has a bouncing or IS-edge problem.

    You need to add: P1OUT |= BIT3; to make it a pull-up, also add P1IES |= BIT3; to react on a button-push (high-to-low).

  • Dear Sir,

                        I want multiple interrupt from same port but different pins, And also multiple interrupt on same pin.

    This is the code i have written and it dint works only, red led toggle and when i press P1.4 that time also Red Led glows

    #include <msp430g2553.h>

    void main(void) {
        WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

        P1DIR |=  BIT0;                     // Set P1.6 to output direction
        P1OUT &= ~BIT0;                 // Set green LED off
        P2DIR |=  BIT6;                     // Set P1.6 to output direction
        P2OUT &= ~BIT6;                 // Set green LED off
        P1SEL &= ~BIT3;                    // Select Port 1 P1.3 (push button)
        P1DIR &= ~BIT3;                    // Port 1 P1.3 (push button) as input, 0 is input
        P1REN |=  BIT3;                        // Enable Port P1.3 (push button) pull-up resistor
        P1IE  |=  BIT3;                        // Port 1 Interrupt Enable P1.3 (push button)
        P1IFG &= ~BIT3;                    // Clear interrupt flag
        P1SEL &= ~BIT4;                    // Select Port 1 P1.3 (push button)
        P1DIR &= ~BIT4;                    // Port 1 P1.3 (push button) as input, 0 is input
        P1REN |=  BIT4;                        // Enable Port P1.3 (push button) pull-up resistor
        P1IE  |=  BIT4;                        // Port 1 Interrupt Enable P1.3 (push button)
        P1IFG &= ~BIT4;                    // Clear interrupt flag
        _BIS_SR(GIE);                           // Enable interrupts

    }
                                        // Port 1 interrupt service routine
    #pragma vector=PORT1_VECTOR
          __interrupt void Port_1(void) {

        P1IFG &= ~BIT3;                     // P1.3 Interrupt Flag cleared
        P1OUT ^= BIT0;                 // Toggle LED state
        }
    #pragma vector=PORT1_VECTOR
          __interrupt void Port_2(void) {
        P1IFG &= ~BIT4;                     // P1.3 Interrupt Flag cleared
        P1OUT ^= BIT6;                     // Toggle LED state
    }

  • Each port has only one vector and onyl one ISR can be written. To distinguish between different interrupt sources for one interrupt vector, in this case the different port pins, you'll have to check the IFG bits inside the ISR.

    #pragma vector=PORT1_VECTOR
    __interrupt void port1_ISR(void) {
      if(P1IFG&BIT3)
        P1OUT^=BIT0;
      if(P1IFG&BIT4)
        P1OUT ^=BIT6;
      P1IFG=0;
    }

    On MSPs with P1 and P2 interrupt vector register, it might be better to use it:

    #pragma vector=PORT1_VECTOR
    __interrupt void port1_ISR(void) {
      while(1)
        switch(_even_in_range(P1IV,16) { // check for highest pending interrupt and clear its IFG bit
          case 0: return; // no more pending interrupts
          case 8: // P1IFG.3
            P1OUT^=BIT0;
            break;
          case 16: //P1IFG.0
            P1OUT ^=BIT6;
            break;
        }
      }
    }

    This will enter the ISR only once, even if multiple interrupts are pending, but it will only handle those interrupts where the IE bit is set and which had called the ISR anyway.

  • Dear Sir, 

    I didn't understand  second part of the code, what does "_even_in_range(P1IV,16)" mean, P1IV is not in the header file.

    #pragma vector=PORT1_VECTOR
    __interrupt void port1_ISR(void) {
      while(1)
        switch(_even_in_range(P1IV,16) { // check for highest pending interrupt and clear its IFG bit
          case 0: return; // no more pending interrupts
          case 8: // P1IFG.3
            P1OUT^=BIT0;
            break;
          case 16: //P1IFG.0
            P1OUT ^=BIT6;
            break;

    Thanks for your information.

  • That's why I wrote "on MSPs with P1 and P2 interrupt vector register" (I didn't remember what MSP you are using when writing the response).
    The G2 family MSPs don't have IV registers for the ports, only for the timers and some other modules.

**Attention** This is a public forum