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.

Rookie Question About Double Interrupt. (2 side joystick)

Other Parts Discussed in Thread: MSP430F5436A

Hello,

I recently started to learn about the msp430 (msp-exp430f5438 with a msp430f5436A),

I wish to have 2 different interrupt rutines, one will start when i press the right side of the joystick, and another rutine will start when i press the left button.

The naive way i though of, is to check with 2 "if" commands, which interrupt flag was raised, and then write my rutine inside each "if" command.

Any better ways?

  • What's your actual problem? Are you trying to prevent both flag triggered at approximately same time?

    If you are using the same port register (e.g. P1 for both), a way to prevent it is to clear all flag while executing the a flag. Also you might want to give priority for a certain flag.

    Something like :

    #pragma vector=PORT1_VECTOR
    __interrupt void port1_interrupt(void)  {
    
    if (P1IFG & 0x01)  {
    
    //do something
    
    P1IFG &= 0x00;
    
    }
    
    else if (P1IFG & 0x02)  {
    
    //do something
    
    P1IFG &= 0x00;
    
    }
    
    
    }

    Another way is to use two different IO register for interrupt channel, e.g. P1 for Left Button and P2 for Right Button. This way, both interrupt routines will act independently. If both buttons are pressed at the same time, both interrupt vectors will be triggered based on their vector priority level (P1 vector is called first, then P2 vector is called)

  • So let's say i want to do it the 2nd way you suggested, how will i implement it?

    lets assume the joystick buttons are located at port 2.1 and port 2.2, how can i define 2 different interrupts?

  • You got it wrong.

    What I meant is that you place left button at Port 1 register (e.g. P1.0) and right button at Port 2 register (e.g. P2.0).

    Then you simply need to configure both P1.0 and P2.0 to have interrupt vectors (P1IE and P2IE are set at their respective IO pin for the buttons), and declare the interrupt service routine for both register.

    Something like :

    void main() {
        P1DIR &= BIT0;
        P1IE |= BIT0;
        P1IES &= 0;   //Depends on your button
        P1REN |= BIT0 //Pull-up resistor, Optional
    
        P2DIR &= BIT0;
        P2IE |= BIT0;
        P2IES &= 0;
        P2REN |= BIT0 //Same usage as P1 settings
    
        while(1);
    }
    
    #pragma vector=PORT1_Vector
    __interrupt void port1_int(void) {
        //do something for left button command
        P1IFG &= 0x00;
    }
    
    #pragma vector=PORT2_Vector
    __interrupt void port2_int(void) {
        //do something for right button command
        P2IFG &= 0x00;
    }
  • Since all the joystick buttons are connected to different P2 pins, they are capable of only generating P2 interrupt, and can only be handled by one ISR (the ISR for P2 interrupt.)

    However, P2IV will tell you which of the P2 pins is causing the current P2 interrupt. Thus your single P2 ISR can handle all the the interrupts the joystick buttons generated. This is not much different from what you wanted to do.

**Attention** This is a public forum