简体   繁体   English

如何清除 NodeMCU ESP32 中的任何挂起中断?

[英]How can I clear any pending interrupts in the NodeMCU ESP32?

I have built a simple coin sensor with two copper plates that detect when a coin hits them.我已经构建了一个简单的硬币传感器,带有两个铜板,可以检测硬币何时击中它们。 Upon striking the two plates, I fire off an interrupt which looks like:敲击两个盘子后,我触发了一个中断,如下所示:

attachInterrupt( digitalPinToInterrupt(INPUT_PIN_COIN), Interrupt_CoinDeposit, FALLING );

This works fine and I am able to pick up when the coin strikes the two plates.这很好用,当硬币撞击两个盘子时,我可以捡起来。 In order to avoid the same coin being registered multiple times due to contact bounce, I detach the interrupt within the Interrupt_CoinDeposit() function as so:为了避免由于接触反弹而多次注册同一个硬币,我在Interrupt_CoinDeposit()函数中分离了中断,如下所示:

void IRAM_ATTR Interrupt_CoinDeposit()
{
    detachInterrupt(digitalPinToInterrupt(17));
    g_crOSCore.EnqueueCoin();
}

EnqueueCoin simply increases a counter and returns back to where the interrupt left off. EnqueueCoin只是增加一个计数器并返回到中断停止的地方。 After which, I check if the counter has increased, and if it does, I reattach the interrupt.之后,我检查计数器是否增加,如果增加,我重新附加中断。 However, upon reattaching the interrupt, it fires off immediately.但是,在重新附加中断时,它会立即触发。 I learnt that reattaching the interrupt completes all the pending interrupts.我了解到重新附加中断会完成所有挂起的中断。 I do not want this to happen.我不希望这种情况发生。 In the Arduino UNO R3, I believe you can solve this problem by resetting the EIFR .在 Arduino UNO R3 中,我相信您可以通过重置EIFR来解决这个问题。 I'm wondering if there is something similar for the NodeMCU ESP32?我想知道 NodeMCU ESP32 是否有类似的东西?

You could use a flag instead of disabling the interrupt.您可以使用标志而不是禁用中断。 This way you also avoid the function call detachInterrupt() inside the ISR.通过这种方式,您还可以避免在 ISR 中调用 detachInterrupt() 函数。

bool coinRegistered = false;
void IRAM_ATTR Interrupt_CoinDeposit()
{
    if (!coinRegistered) {
        coinRegistered = true;
        g_crOSCore.EnqueueCoin();
    }
}
/* ... somewhere else in the code ...*/
coinRegistered = false;

Either you can start a timer in the ISR, which resets the flag, or you reset it manually.您可以在 ISR 中启动一个计时器来重置标志,或者您可以手动重置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM