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.

Interrupt with push button on P2.1 not working.

Hi

I bought a MSP430f5529LP launchpad and tried to test onboard components by writing a simple code to read push button on P2.1 as interrupt but it does not seem to work.

Is there any other register I need to enable or any other intrinsic function to be used to enable interrupt?

#include <msp430.h>

#define DELAY() \
        i = 10000;    \
        do i--;        \
        while(i != 0)
/*
 * main.c
 */
 int main(void) {
    WDTCTL = WDTPW | WDTHOLD;    // Stop watchdog timer

    P1DIR |= BIT0;
    P4DIR |= BIT7;

    P2DIR &= ~BIT1;
    P2REN |= BIT1;
    P2OUT |= BIT1;
    P2IE |= BIT1;
    P2IFG &= ~BIT1;

    __enable_interrupt();    //Enable all interrupts.
    //_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt

    while(1)
    {
        unsigned volatile long i;
        P1OUT ^= BIT0; //toggle P1.0
        DELAY();
    }
}
#pragma vector = PORT1_VECTOR
__interrupt void port_1(void)
{
        P4OUT |= BIT7;    //Glow P4.7 on interrupt.
        P2IFG &= ~BIT1;
}

Now in the above code when push button on P2.1 is pressed the LED on P4.7 should glow, but it does not.

Kindly advise.

  • Sachin, you are configuring everything related to your input button for port 2, but your interrupt vector is for port 1. Start at this point and go on testing.

    Dennis
  • Solved it.
    Just needed to change as follows :

    #pragma vector = PORT2_VECTOR

    BUT still when i tried to toggle P4.7 in ISR instead of just glowing the response is very slow, i.e when i keep pressing the button quickly sometimes the LED toggle and sometimes it does not.
  • Sachin,

    This can be caused by the bouncing of your button. When you press or release it, the contacts will jump up and down for a short time. A single human button press isn't a single microcontroller button press. You probably have this bouncing effect which can cause the following scenario:

    LED off -> button press -> LED on -> button bounce -> LED off -> button bounce ->LED on -> button bounce -> LED off -> button settled

    So it seems as if your LED remains off, but in reality it has toggled more than one time and the button just stops bouncing when the LED has the same state like before the button press. Have you got an oscilloscope? Then hook it at your button an have a look at it's bouncing and also look at the LED if it toggles a few times.

    Dennis

**Attention** This is a public forum