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.

Pushbutton interrupt code MSP430FR4133

Other Parts Discussed in Thread: MSP430FR4133, MSP-EXP430FR4133

  Hi. I am trying tot write a simple code that will turn on an LED when I push a button but something is wrong with my code and I can't figure it out.

I am using the msp430fr4133 LaunchPad.

Can you tell me where I did wrong?

Thank you.

Here is the code:

//interrupt with push button


#include <msp430.h> 





int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= BIT0; // Set P1.0 to output direction

P1OUT &= ~BIT0; // set P1.0 to 0 (LED OFF)

P1IES = 0x00;  //low to high transition

P1IE |= BIT2; // P1.2 interrupt enabled

P1IFG &= ~BIT2; // P1.2 IFG cleared

PM5CTL0 &= ~LOCKLPM5;

__bis_SR_register(LPM0_bits | GIE);          // Enter LPM0 w/ interrupt
       __no_operation();


}


// Port 1 interrupt service routine

#pragma vector = PORT1_VECTOR
__interrupt void PORT1_ISR(void)
{
P1OUT |= BIT0; // led on

}

  • Hi Vlad!

    vlad balu said:
    something is wrong with my code

    You should always describe your problem in detail - what does not work as expected?

    vlad balu said:
    // Port 1 interrupt service routine #pragma vector = PORT1_VECTOR __interrupt void PORT1_ISR(void) { P1OUT |= BIT0; // led on }

    The port interrupt flag doesn't get cleared by accessing the interrupt service routine. You have to...

    ...read the P1IV register which clears the highest pending flag

    // Port 1 interrupt service routine
    #pragma vector = PORT1_VECTOR
    __interrupt void PORT1_ISR( void )
    {
      switch( P1IV )     // Clears highest pending flag
      {
        case 6:          // P1.2
        {
          P1OUT |= BIT0; // Led on
          break;
        }
    
        default: break;
      }
    }

    or

    ...clear the flag by yourself by setting the appropriate bit in P1IFG to 0

    // Port 1 interrupt service routine
    #pragma vector = PORT1_VECTOR
    __interrupt void PORT1_ISR( void )
    {
      P1OUT |= BIT0;  // Led on
      P1IFG &= ~0x04; // Clear interrupt flag for P1.2
    }

    If you do not clear the flag, the interrupt service routine will be executed over and over again. But since you only switch your LED on and your program doesn't do anything else, this wouldn't be the problem in your case. But the program hangs after the first interrupt.

    Do you use one of the buttons on the MSP-EXP430FR4133 LaunchPad? If yes, you will need to enable a pull-up resistor for the input pin by writing

    P1REN |= 0x04; // Resistor for P1.2
    P1OUT |= 0x04; // Resistor pulls up

    And since the resistor pulls the pin up and the button causes a low level when pressed, the edge trigger must be set to high to low transition by writing

    P1IES |= 0x04; // High to low transition for P1.2

    All this configuration should be done before enabling the interrupt for the pin since setting the edge selection can cause an interrupt. So do the configuration, then clear the flag and then enable the interrupt.

    And you should google for button debouncing - a button press is never a perfect contact so one single press can cause multiple interrupts until the contacts have settled. Here in this example it isn't important because a second interrupt would also set the LED on, so no difference. But if you would toggle the LED it is possible that it is set ON-OFF-ON-OFF for example - this then looks as if nothing happened.

    Dennis

  • Thank you very much for your support. The code works perfectly now. Thank you again for your help.

**Attention** This is a public forum