Search code examples
cinterruptmsp430

Interrupt with C


#include <msp430.h>

#define BUTTON   BIT3 // Port 1.3
#define REDLED   BIT0 // Port 1.0
#define GRNLED   BIT6 // Port 1.6

#define ZERO    0x08
#define ONE     0x48
#define TWO     0x09
#define THREE   0x49

int counter = 0;

int main(void) {


// Watchdog setup
WDTCTL = WDTPW + WDTHOLD; // stop watchdog (password + hold counter)

// LED initial setup
P1DIR |= REDLED + GRNLED;             // set P1.0 and P1.6 as output (1) pins
P1OUT &= ~REDLED;                      // Disable REDLED
P1OUT &= ~GRNLED;                     // Disable GRNLED

// Button setup
P1DIR &= ~BUTTON;                     // button is an input
P1OUT |= BUTTON;                      // pull-up resistor
P1REN |= BUTTON;                      // resistor enabled

P1IE |= 0x08;                           //P1.3 interrupt enable
P1IES &= ~0x08;                          //lower edge
P1IFG &=  ~0x08;                        //zero flag
while(1){


    }
}
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void){
        counter += 1;
        counter = (counter % 4);
        switch(counter){
            case 0:
                P1OUT = ZERO;
                break;
            case 1:
                P1OUT = ONE;
                break;
            case 2:
                P1OUT = TWO;
                break;
            case 3:
                P1OUT = THREE;
                break;
        }

        P1IFG &= ~0x08;
}

I can not enter the ınterrup routine .I checked interrup flag ,when I push the button flag will 1 but the leds are not change ,I think that I can not enter interrup.If I can , the led must be changed.What is the wrong ?


Solution

  • Global interrupts are disabled by default on program's startup. You need to add code that sets the global interrupt enable (GIE) bit at the end of main(). The most platform-independent (not really) way to do it is by calling __enable_interrupts() function.

    #include <msp430.h>
    #include <intrinsics.h>
    ...
    __enable_interrupts();
    

    Alternatively, set the GIE bit directly:

    __bis_status_register(GIE);
    

    To check whether interrupts are enabled (not that inside the interrupt handler they always will be disabled by default):

    if (__get_SR_register() & GIE) {
        printf("interrupts enabled\n");
    } else {
        printf("interrupts disabled\n");
    }