Odd LEDC behavior
Posted: Sun Jul 04, 2021 12:01 am
Hi y'all! I have a bit of an odd issue (to me at least) related to the built in clock generator. I'm trying to produce a square wave with a pulse length of ~50ns across a range of frequencies (100Hz-4kHz). I found some code relating to LEDC usage and have this:
I've connected my ESP32-WROOM-32D to an oscilloscope and this code generates the 50ns, 305Hz signal pretty much perfectly (little bit of under/overshoot but that's to be expected), however if I try to change the frequency to anything greater than 305Hz (whether it be 306Hz, 1000Hz, ect) the ESP32 produces no output. I've tried changing the number of bits for the timer and the corresponding number in the "int var_duty" line, and a different range of frequencies can be produced but no matter what duty resolution I try, the full range doesn't display. If anyone has any clue as to why this is happening, some help would be greatly appreciated!
Code: Select all
#include "driver/ledc.h"
#include "driver/periph_ctrl.h"
#include "esp_system.h"
#include "sdkconfig.h"
#include <stdio.h>
#include <math.h>
#define var_gpio_num 15
#define var_freq_hz 305
#define var_pulse_length 0.00000005
#define var_duty_resolution LEDC_TIMER_18_BIT
void app_main()
{
periph_module_enable(PERIPH_LEDC_MODULE);
int var_duty = var_freq_hz*var_pulse_length*262144;
ledc_timer_config_t ledc_timer = {
.duty_resolution = var_duty_resolution,
.freq_hz = var_freq_hz,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
};
ledc_timer_config(&ledc_timer);
ledc_channel_config_t channel_config = {
.channel = LEDC_CHANNEL_0,
.duty = var_duty,
.gpio_num = var_gpio_num,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.timer_sel = LEDC_TIMER_0
};
ledc_channel_config(&channel_config);
}