Reuse LEDC timer/channel [solved]
Posted: Wed Nov 30, 2022 11:44 am
I'm trying to reuse an LEDC timer/channel on a different pin, but when I do so, the "old" pin is still connected to the channel. How do I make a channel only work on a new pin?
In the example below, I use the LEDC_TIMER_0 and LEDC_CHANNEL_0. I first configure it to be on pin 32.
I let the run_pwm function run, and then turn it off. So far everything works as expected.
However, when I now call the function with a different pin (33), then the old pin (32) also lights up. How can I disconnect pin 32 from LEDC_TIMER_0/LEDC_CHANNEL_0 ?
In the example below, I use the LEDC_TIMER_0 and LEDC_CHANNEL_0. I first configure it to be on pin 32.
I let the run_pwm function run, and then turn it off. So far everything works as expected.
However, when I now call the function with a different pin (33), then the old pin (32) also lights up. How can I disconnect pin 32 from LEDC_TIMER_0/LEDC_CHANNEL_0 ?
Code: Select all
#include <stdio.h>
#include <driver/ledc.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
void run_pwm(int gpio_num) {
ledc_timer_t timer = LEDC_TIMER_0;
ledc_timer_config_t timer_conf = {
.speed_mode = LEDC_HIGH_SPEED_MODE,
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 5000,
.timer_num = timer,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&timer_conf);
ledc_channel_t channel = LEDC_CHANNEL_0;
ledc_channel_config_t channel_conf = {
.gpio_num = gpio_num,
.speed_mode = LEDC_HIGH_SPEED_MODE,
.channel = channel,
.intr_type = LEDC_INTR_DISABLE,
.timer_sel = timer,
.duty = 0,
.hpoint = 0,
};
ledc_channel_config(&channel_conf);
ledc_set_duty(LEDC_HIGH_SPEED_MODE, channel, 4095);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, channel);
vTaskDelay(1000 / portTICK_PERIOD_MS);
ledc_stop(LEDC_HIGH_SPEED_MODE, channel, 0);
ledc_timer_rst(LEDC_HIGH_SPEED_MODE, timer);
}
void app_main(void)
{
run_pwm(32);
vTaskDelay(1000 / portTICK_PERIOD_MS);
run_pwm(33);
}