简体   繁体   中英

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:

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. 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 . I'm wondering if there is something similar for the NodeMCU ESP32?

You could use a flag instead of disabling the interrupt. This way you also avoid the function call detachInterrupt() inside the ISR.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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