为什么我可以使用一个全局bool变量,其值取决于中断标志?
我试图等待中断以继续执行代码,类似的事情:
bool flag = false;
void interrupt_handler (uintptr_t context)
{
flag = true;
}
void main()
{
CallbackRegister(event, interrupt_handler,0);
while(!flag);
}
但是即使发生中断,它也总是在段落中。有人知道为什么吗? 我目前正在使用SAMD21J17微控制器的MPLABX。
I'm trying to wait for an interrupt to continue with the execution of the code, something like this:
bool flag = false;
void interrupt_handler (uintptr_t context)
{
flag = true;
}
void main()
{
CallbackRegister(event, interrupt_handler,0);
while(!flag);
}
But it always stays in the while loop even when the interrupt occurs. Does anyone know why?
I'm currently using MPLABX with a SAMD21J17 microcontroller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要更改:
to:
原因是,如果没有
volatile
允许编译器假定标志已读取一次后永远不会更改,但是您希望它重复读取标志。You need to change:
to:
The reason is that without
volatile
the compiler is allowed to assume that the flag never changes after it has been read once, but you want it to read the flag repeatedly.唔。我不知道那个特定的编译器,而是对微芯片编译器的快速检查说的话立即向我跳了出来。
您有一个名为Interrupt处理程序的功能,但是您没有将其标识为Decorator。我建议您在第5.8.1节中查看本指南“编写中断服务例程”,其中说“使用__interrupt()指定符编写每个ISR原型”。
因此,您的日常工作看起来至少是这样:
Hmm. I don't know that particular compiler but a quick check of the microchip compiler user guide says something which jumped out to me immediately.
You have a function named interrupt handler, but you don't have it identified with decorator. I suggest you look at this guide in section 5.8.1 "Writing an Interrupt Service Routine" where it says "Write each ISR prototype using the __interrupt() specifier".
So your routine would look like this at a minimum: