用 C 中断

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;
}

我无法进入 ınterrup 例程。我检查了中断标志,当我按下按钮时标志将 1 但 LED 没有变化,我认为我无法进入 interrup.If 我可以,LED一定是 changed.What 错了吗?

程序启动时默认禁用全局中断。您需要添加代码以在 main() 末尾设置全局中断启用 (GIE) 位。最独立于平台(不是真的)的方法是调用 __enable_interrupts() 函数。

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

或者,直接设置 GIE 位:

__bis_status_register(GIE);

检查中断是否被启用(不是在中断处理程序中它们总是默认被禁用):

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