Arduino ESP32:测试GPIO中断功能

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino ESP32:测试GPIO中断功能相关的知识,希望对你有一定的参考价值。

Arduino ESP32:测试GPIO中断功能

ESP32:测试GPIO中断功能

实例代码

/*
 测试GPIO中断功能
  接线说明: 按键1接23,按键2接18
*/

#include <Arduino.h>

struct Button {
    const uint8_t PIN;
    uint32_t numberKeyPresses;
    bool pressed;
};

Button button1 = {23, 0, false};
Button button2 = {18, 0, false};

void IRAM_ATTR isr(void* arg) {
    Button* s = static_cast<Button*>(arg);
    s->numberKeyPresses += 1;
    s->pressed = true;
}

void IRAM_ATTR isr() {
    button2.numberKeyPresses += 1;
    button2.pressed = true;
}

void setup() {
    Serial.begin(115200);
    pinMode(button1.PIN, INPUT_PULLUP);
    attachInterruptArg(button1.PIN, isr, &button1, FALLING);
    pinMode(button2.PIN, INPUT_PULLUP);
    attachInterrupt(button2.PIN, isr, FALLING);
}

void loop() {
    if (button1.pressed) {
        Serial.printf("Button 1 has been pressed %u times\\n", button1.numberKeyPresses);
        button1.pressed = false;
    }
    if (button2.pressed) {
        Serial.printf("Button 2 has been pressed %u times\\n", button2.numberKeyPresses);
        button2.pressed = false;
    }
    static uint32_t lastMillis = 0;
    if (millis() - lastMillis > 10000) {
      lastMillis = millis();
      detachInterrupt(button1.PIN);
    }
}

以上是关于Arduino ESP32:测试GPIO中断功能的主要内容,如果未能解决你的问题,请参考以下文章

Arduino ESP32 WIFI &蓝牙模式触控按键切换

Arduino ESP32自带触摸传感器功能测试

IoTESP32 Arduino GPIO 使用简析

Arduino ESP32 看门狗定时器

Arduino ESP32 读写查看SD信息示例

Arduino ESP32 读取Micro sd卡容量信息示例