The original code appeared to be missing some edges, so I've stripped it down to the bare minimum and wired up the ESP32 to 1kHz square wave generator to try to rule out as many external factors as possible.
Here's the waveform recorded on my scope: The yellow trace is the 1kHz square wave from the signal generator
The blue trace is from an ESP32 pin configured as an output which should toggle on each edge of the square wave.
For some reason I'm missing 1-3 edges in what appears to be a very regular pattern.
Here's the code (Yes - I know I've programmed the GPIO input and output ports in different ways...)
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "freertos/semphr.h"
#include "esp_timer.h"
#include "esp_log.h"
// Define GPIO pins for the encoder
#define ENCODER_PIN_A GPIO_NUM_5
#define OUTPUT_PIN_A GPIO_NUM_7
#define DEBOUNCE_DELAY 10 // 100 us debounce delay
volatile uint32_t lastDebounceTime = 0;
volatile int outputValue=0;
void IRAM_ATTR handleEncoder(void* pvArg) {
uint32_t currentTime = esp_timer_get_time(); // Get current time in us
if ((currentTime - lastDebounceTime) > DEBOUNCE_DELAY) {
if (outputValue++ >1) outputValue=0;
gpio_set_level(OUTPUT_PIN_A,outputValue);
lastDebounceTime = currentTime;
}
}
void app_main() {
[Codebox=c file=Untitled.c][/Codebox]// Configure GPIO
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_POSEDGE;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pin_bit_mask = (1ULL << ENCODER_PIN_A) ;
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
gpio_reset_pin(OUTPUT_PIN_A);
gpio_set_direction(OUTPUT_PIN_A,GPIO_MODE_OUTPUT);
// Install ISR service
gpio_install_isr_service(0);
gpio_isr_handler_add(ENCODER_PIN_A, handleEncoder, (void*) ENCODER_PIN_A);
while (1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Any suggestions on what I'm doing wrong or how to fix this very much appreciated
Thanks