用C中断

时间:2017-05-13 09:20:11

标签: c interrupt msp430

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

我无法进入nternterrup例程。我检查了中断标志,当我按下按钮标志将为1但是LED没有改变,我认为我不能进入中断。如果可以,必须更换LED。有什么不对?

1 个答案:

答案 0 :(得分:3)

默认情况下,在程序启动时禁用全局中断。您需要添加在GIE末尾设置全局中断启用(main())位的代码。最独立于平台(非实际)的方法是调用__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");
}