我已经建立了一个使用名为Thunderbird12的16位微控制器的项目,类似于9s12/Freescale 68HC12系列。它做了一些事情,但主要是打开一个外部水泵。一切正常,除了我需要实现一个中断。我希望能够通过按钮使用中断来停止电机。
我已经设置了按钮,该按钮在按下时将端口P的引脚0设置为高电平。使用C语言,我已经初始化了硬件并编写了代码,但是没有调用标志。请参阅下面的代码。
// Interrupt function
int interruptFlag;
void interrupt 56 WaterPumpRoutine() {
if ((PIFP & 0x01) == 0x01) { // check if pin 0 of port p is high (when button is pressed)
interruptFlag = 1; // set the flag to 1
}
// Main
void main() {
DDRP = 0x00; // set port P as input
PIEP = PIEP | 0x01; // enable interrupts on port P, pin 0
PERP = PERP | 0x01; // enable pull-up/down on port P, pin 0
if ( interruptFlag == 1)
PORTB = (PORTB & 0x00) // Here I'm turning off all the pins in Port B, which includes the pump.
}如果我将这段代码PORTB = (PORTB & 0x00)放在WaterPumpRoutine()函数中,它可以很好地工作,但我需要能够在任何地方调用该标志。我不确定我错过了什么。任何帮助都将不胜感激!
发布于 2019-05-22 10:59:08
假设中断调用运行良好..
int interruptFlag;更改为volatile int interruptFlag;。这是为了避免编译器优化main中的if条件。main中,你需要在某些条件下重置interruptFlag。这是依赖于程序的。也许你可以这样做。if ( interruptFlag == 1) { PORTB = (PORTB & 0x00) //这里我将关闭端口B中的所有引脚,其中包括泵。interruptFlag = 0;}
while(1) { if(interruptFlag == 1) { ... } }
发布于 2019-05-22 13:58:17
如果你仍然不能触发中断,在其他人建议之后,你应该检查你是否启用了全局中断或特定部分的中断( GPIO ),因为在大多数uc中,将GPIO设置为生成中断不会启用中断
https://stackoverflow.com/questions/56248554
复制相似问题