I try to detect a button click and have adapted code from gpio example :
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#define GPIO_BTN GPIO_NUM_13
#define ESP_INTR_FLAG_DEFAULT 0
volatile uint32_t nbInt = 0;
static TaskHandle_t xHandlerTask = NULL;
static void IRAM_ATTR btn_isr_handler(void *arg) {
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
nbInt++;
vTaskNotifyGiveFromISR(xHandlerTask, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR();
}
static void btn_task(void *pvParameters) {
const TickType_t xMaxExpectedBlockTime = pdMS_TO_TICKS(20);
uint32_t ulEventsToProcess;
for (;;) {
ulEventsToProcess = ulTaskNotifyTake( pdTRUE, xMaxExpectedBlockTime);
if (ulEventsToProcess != 0) {
printf("GPIO intr, val: %d, nb notif: %d\n", gpio_get_level(GPIO_BTN), ulEventsToProcess);
}
}
}
void app_main() {
gpio_config_t io_conf;
io_conf.intr_type = GPIO_PIN_INTR_NEGEDGE;
io_conf.pin_bit_mask = 1UL << GPIO_BTN;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_config(&io_conf);
xTaskCreate(btn_task, "btn_task", 2048, NULL, 10, &xHandlerTask);
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
gpio_isr_handler_add(GPIO_BTN, btn_isr_handler, (void*) GPIO_BTN);
int cnt = 0;
for (;;) {
printf("cnt: %d, nb int: %d\n", cnt++, nbInt);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
Code: Select all
cnt: 0, nb int: 0
cnt: 1, nb int: 0
cnt: 2, nb int: 0
cnt: 3, nb int: 0
GPIO intr, val: 0, nb notif: 1 <-- button pressed
GPIO intr, val: 0, nb notif: 1 <-- button released immediatly
GPIO intr, val: 1, nb notif: 9
cnt: 4, nb int: 11
cnt: 5, nb int: 11
cnt: 6, nb int: 11
cnt: 7, nb int: 11
GPIO intr, val: 0, nb notif: 1 <-- button pressed
cnt: 8, nb int: 12
cnt: 9, nb int: 12
GPIO intr, val: 0, nb notif: 1 <-- button released after few millis
GPIO intr, val: 1, nb notif: 8
cnt: 10, nb int: 21
cnt: 11, nb int: 21
I use an RC filter (R = 1KHz and C = 0.1uF) to debounce the signal, liake this schematic (Rp1 of 10k is replaced by pull-up of ~45k).
It gives this signal : It seems very clean.
I can't find what is my mistake.
If someone can help me ...
Regards