gpt4 book ai didi

c - 用 C 中断

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:19 24 4
gpt4 key购买 nike

#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 没有变化,我认为我无法进入中断。如果可以,则必须更换 LED。怎么了?

最佳答案

程序启动时默认禁用全局中断。您需要在 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");
}

关于c - 用 C 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43951386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com