为什么我可以使用一个全局bool变量,其值取决于中断标志?

发布于 2025-01-21 05:40:59 字数 286 浏览 2 评论 0原文

我试图等待中断以继续执行代码,类似的事情:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2025-01-28 05:40:59

您需要更改:

bool flag = false;

to:

volatile bool flag = false;

原因是,如果没有volatile允许编译器假定标志已读取一次后永远不会更改,但是您希望它重复读取标志。

You need to change:

bool flag = false;

to:

volatile bool flag = false;

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.

Hello爱情风 2025-01-28 05:40:59

唔。我不知道那个特定的编译器,而是对微芯片编译器的快速检查说的话立即向我跳了出来。

您有一个名为Interrupt处理程序的功能,但是您没有将其标识为Decorator。我建议您在第5.8.1节中查看本指南“编写中断服务例程”,其中说“使用__interrupt()指定符编写每个ISR原型”。

因此,您的日常工作看起来至少是这样:

void __interrupt(high_priority) interrupt_handler (uintptr_t context) {
    flag = true;
}

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:

void __interrupt(high_priority) interrupt_handler (uintptr_t context) {
    flag = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文