espressif / esp-idf

Espressif IoT Development Framework. Official development framework for Espressif SoCs.
Apache License 2.0
13.67k stars 7.29k forks source link

Configuring two ledc channels (IDFGH-9748) #11081

Open onurdof opened 1 year ago

onurdof commented 1 year ago

Answers checklist.

IDF version.

5.1

Operating System used.

Linux

How did you build your project?

VS Code IDE

If you are using Windows, please specify command line type.

None

What is the expected behavior?

I expected to control two ledc channels with different pwm signals .

What is the actual behavior?

While one channel is working, the other channel is not .

Steps to reproduce.

  1. Step
  2. Step
  3. Step ...

Build or installation Logs.

No response

More Information.

Hello, ı want to configure two gpıos with different pwm values . When ı try to use two different ledc_channel, one channel working fine but the other channel not. Working channel is the step channel . Here is the my code

include "freertos/FreeRTOS.h"

include "freertos/task.h"

include "driver/mcpwm.h"

include "driver/ledc.h"

include "esp_log.h"

define STEP_MOTOR_GPIO_EN 21

define STEP_MOTOR_GPIO_DIR 22

define STEP_MOTOR_GPIO_STEP 19 //ledc

define FAN_GPIO 18 //ledc

define LEDC_TIMER_STEP LEDC_TIMER_0

define LEDC_MODE_STEP LEDC_HIGH_SPEED_MODE

define LEDC_CHANNEL_STEP LEDC_CHANNEL_1

define LEDC_DUTY_RES_STEP LEDC_TIMER_12_BIT // Set duty resolution to 13 bits

define LEDC_DUTY_STEP (2048) // Set duty to 50%. ((2 * 12) - 1) 50% = 4095

define LEDC_FREQUENCY_STEP (5000) // Frequency in Hertz. Set frequency at 5 kHz

define LEDC_TIMER_FAN LEDC_TIMER_1

define LEDC_MODE_FAN LEDC_HIGH_SPEED_MODE

define LEDC_CHANNEL_FAN LEDC_CHANNEL_6

define LEDC_DUTY_RES_FAN LEDC_TIMER_8_BIT // Set duty resolution to 13 bits

define LEDC_DUTY_FAN (0) // İnitially set 0 we change between 0 - 255

define LEDC_FREQUENCY_FAN (5000) // Frequency in Hertz. Set frequency at 5 kHz

static const char *TAG = "STEPPER MOTOR"; static void ledc_init(void); static void step_gpio_init(void);

void app_main() { ledc_init(); step_gpio_init(); gpio_set_level(STEP_MOTOR_GPIO_EN,0); gpio_set_level(STEP_MOTOR_GPIO_DIR,0); / Step / ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE_STEP, LEDC_CHANNEL_STEP, LEDC_DUTY_STEP)); // Update duty to apply the new value ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE_STEP, LEDC_CHANNEL_STEP));

/* Fan */
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE_FAN, LEDC_CHANNEL_FAN, 50));
// Update duty to apply the new value
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE_FAN, LEDC_CHANNEL_FAN));

}

static void ledc_init(void) { ledc_timer_config_t step_ledc_timer = { .speed_mode = LEDC_MODE_STEP, .timer_num = LEDC_TIMER_STEP, .duty_resolution = LEDC_DUTY_RES_STEP, .freq_hz = LEDC_FREQUENCY_STEP, // Set output frequency at 5 kHz .clk_cfg = LEDC_AUTO_CLK }; ESP_ERROR_CHECK(ledc_timer_config(&step_ledc_timer));

ledc_channel_config_t step_ledc_channel = {
    .speed_mode     = LEDC_MODE_STEP,
    .channel        = LEDC_CHANNEL_STEP,
    .timer_sel      = LEDC_TIMER_STEP,
    .intr_type      = LEDC_INTR_DISABLE,
    .gpio_num       = STEP_MOTOR_GPIO_STEP,
    .duty           = 0, // Set duty to 0%
    .hpoint         = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&step_ledc_channel));

/* FAN */
ledc_timer_config_t fan_ledc_timer = {
    .speed_mode       = LEDC_MODE_FAN,
    .timer_num        = LEDC_TIMER_FAN,
    .duty_resolution  = LEDC_DUTY_RES_FAN,
    .freq_hz          = LEDC_FREQUENCY_FAN,  // Set output frequency at 5 kHz
    .clk_cfg          = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&fan_ledc_timer));

ledc_channel_config_t fan_ledc_channel = {
    .speed_mode     = LEDC_MODE_FAN,
    .channel        = LEDC_CHANNEL_FAN,
    .timer_sel      = LEDC_TIMER_FAN,
    .intr_type      = LEDC_INTR_DISABLE,
    .gpio_num       = FAN_GPIO,
    .duty           = 0, // Set duty to 0%
    .hpoint         = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&fan_ledc_channel));

}

static void step_gpio_init(void) { ESP_LOGI(TAG, "Initialize EN + DIR GPIO"); gpio_config_t step_config = { .mode = GPIO_MODE_OUTPUT, .intr_type = GPIO_INTR_DISABLE, .pin_bit_mask = 1ULL << STEP_MOTOR_GPIO_DIR | 1ULL << STEP_MOTOR_GPIO_EN, }; ESP_ERROR_CHECK(gpio_config(&step_config)); }

songruo commented 1 year ago

Hi @onurdof, I have tried your code on the current master. I see two PWM signals outputting correctly from the pins with a logic analyzer. If your conclusion of one PWM is not working is drawn from the working status of the motor/fan, then maybe you can check the wiring or hardware.

fenilGhoghari commented 1 year ago

Hi, @onurdof can you please share your whole code with app_main function also so i can test...