在ESP32-S3-DevKitM-1上学习使用按键中断,无法工作的问题?

lymark
Posts: 5
Joined: Fri Dec 30, 2022 11:12 am

在ESP32-S3-DevKitM-1上学习使用按键中断,无法工作的问题?

Postby lymark » Fri Dec 30, 2022 11:32 am

硬件:ESP32-S3-DevKitM-1
IDE:Arduino IDE 2.0.3

想借助板子上的boot按钮学习使用按键外部中断控制RGB亮灭,代码附下,问题表现为开发板重启,中断无法工作。

Code: Select all

#define pushButton_pin   0
#define LED_pin   RGB_BUILTIN
 
void IRAM_ATTR toggleLED()
{
  digitalWrite(LED_pin, !digitalRead(LED_pin));
}
 
void setup()
{
  pinMode(LED_pin, OUTPUT);
  pinMode(pushButton_pin, INPUT);
  attachInterrupt(pushButton_pin, toggleLED, RISING);
}
 
void loop()
{
}
请问是不能使用该板子上的Boot按键(gpio 0)还是其他的什么问题?

ESP_LJH
Posts: 387
Joined: Tue May 18, 2021 9:21 am

Re: 在ESP32-S3-DevKitM-1上学习使用按键中断,无法工作的问题?

Postby ESP_LJH » Tue Jan 03, 2023 7:16 am

只要进入到 SPI 工作模式,用 GPIO0 应该没问题的,硬件上可以确认下是这么操作的吗?或者可以说下你具体的操作步骤,然后你说重启,重启的 log 是?

lymark
Posts: 5
Joined: Fri Dec 30, 2022 11:12 am

Re: 在ESP32-S3-DevKitM-1上学习使用按键中断,无法工作的问题?

Postby lymark » Wed Jan 04, 2023 3:03 am

当时的重启代码复制如下:

Code: Select all

abort() was called at PC 0x40378837 on core 1


Backtrace: 0x40377776:0x3fc92ac0 0x4037a591:0x3fc92ae0 0x4037fb45:0x3fc92b00 0x40378837:0x3fc92b80 0x40378949:0x3fc92bb0 0x42003741:0x3fc92bd0 0x42001987:0x3fc92c10 0x420016ff:0x3fc92c70 0x420015f2:0x3fc92d00 0x403751bd:0x3fc92d20 0x4201f2f1:0x3fc92d40 0x403751fd:0x3fc92d60 0x40375222:0x3fc92d80 0x403785a5:0x3fc92da0 0x42001a19:0x3fcec2f0




ELF file SHA256: a5afa40bd8899be3

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x4037bb43
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbec
load:0x403cc700,len:0x2920
entry 0x403c98d8
另外尝试了esp32附带的按键中断示例代码:示例->ESP32->GPIO->GPIOInterrupt 代码并修改如下:

Code: Select all

#include <Arduino.h>

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

 Button button1 = {0, 0, false};
 Button button2 = {1, 0, false};

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

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

void setup() {
    Serial.begin(115200);
    pinMode(RGB_BUILTIN, OUTPUT);
    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);
        if(button1.numberKeyPresses%2==0){neopixelWrite(RGB_BUILTIN,4,0,0);}
        if(button1.numberKeyPresses%2==1){neopixelWrite(RGB_BUILTIN,0,0,0);}
        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);
      Serial.println(lastMillis);
    }
    */
}
正常工作了!
Last edited by lymark on Wed Jan 04, 2023 9:17 am, edited 1 time in total.

Who is online

Users browsing this forum: No registered users and 6 guests