#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_timer.h"
#define led_pin (GPIO_NUM_2)
#define button_pin (GPIO_NUM_4)
static int led_state = 0;
volatile int16_t last_intr_time = 0;
static void IRAM_ATTR isr_handler(void* arg) {
int64_t now = esp_timer_get_time();
if (now - last_intr_time > 20000){
led_state ^= 1;
gpio_set_level(led_pin, led_state);
last_intr_time = now;
}
}
void app_main(void) {
gpio_config_t led_config = {
.pin_bit_mask = (1ULL << led_pin),
.mode = GPIO_MODE_OUTPUT
};
gpio_config(&led_config);
gpio_config_t button_config = {
.pin_bit_mask = (1ULL << button_pin),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.intr_type = GPIO_INTR_NEGEDGE
};
gpio_config(&button_config);
gpio_install_isr_service(0);
gpio_isr_handler_add(button_pin, isr_handler, NULL);
while (true) {
vTaskDelay(500/portTICK_PERIOD_MS);
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4