LED PWM 控制器

[English]

概述

LED 控制器 (LEDC) 主要用于控制 LED,也可产生 PWM 信号用于其他设备的控制。 该控制器有 6 路通道,可以产生独立的波形来驱动 RGB LED 等设备。

LED PWM 控制器可在无需 CPU 干预的情况下自动改变占空比,实现亮度和颜色渐变。

功能概览

设置 LEDC 通道分三步完成。注意,与 ESP32 不同,ESP32-C3 仅支持设置通道为低速模式。

  1. 定时器配置 指定 PWM 信号的频率和占空比分辨率。

  2. 通道配置 绑定定时器和输出 PWM 信号的 GPIO。

  3. 改变 PWM 信号 输出 PWM 信号来驱动 LED。可通过软件控制或使用硬件渐变功能来改变 LED 的亮度。

另一个可选步骤是可以在渐变终端设置一个中断。

Key Settings of LED PWM Controller's API

LED PWM 控制器 API 的关键配置

定时器配置

要设置定时器,可调用函数 ledc_timer_config(),并将包括如下配置参数的数据结构 ledc_timer_config_t 传递给该函数:

  • 速度模式(值必须为 LEDC_LOW_SPEED_MODE

  • 定时器索引 ledc_timer_t

  • PWM 信号频率

  • PWM 占空比分辨率

频率和占空比分辨率相互关联。PWM 频率越高,占空比分辨率越低,反之亦然。如果 API 不是用来改变 LED 亮度,而是用于其它目的,这种相互关系可能会很重要。更多信息详见 频率和占空比分辨率支持范围 一节。

通道配置

定时器设置好后,请配置所需的通道(ledc_channel_t 之一)。配置通道需调用函数 ledc_channel_config()

通道的配置与定时器设置类似,需向通道配置函数传递包括通道配置参数的结构体 ledc_channel_config_t

此时,通道会按照 ledc_channel_config_t 的配置开始运作,并在选定的 GPIO 上生成由定时器设置指定的频率和占空比的 PWM 信号。在通道运作过程中,可以随时通过调用函数 ledc_stop() 将其暂停。

改变 PWM 信号

通道开始运行、生成具有恒定占空比和频率的 PWM 信号之后,有几种方式可以改变该信号。驱动 LED 时,主要通过改变占空比来变化光线亮度。

以下两节介绍了如何使用软件和硬件改变占空比。如有需要,PWM 信号的频率也可更改,详见 改变 PWM 频率 一节。

注解

在 ESP32-C3 的 LED PWM 控制器中,所有的定时器和通道都只支持低速模式。对 PWM 设置的任何改变,都需要由软件显式地触发(见下文)。

使用软件改变 PWM 占空比

调用函数 ledc_set_duty() 可以设置新的占空比。之后,调用函数 ledc_update_duty() 使新配置生效。要查看当前设置的占空比,可使用 _get_ 函数 ledc_get_duty()

另外一种设置占空比和其他通道参数的方式是调用 通道配置 一节提到的函数 ledc_channel_config()

传递给函数的占空比数值范围取决于选定的 duty_resolution,应为 0(2 ** duty_resolution) - 1。例如,如选定的占空比分辨率为 10,则占空比的数值范围为 0 至 1023。此时分辨率为 ~0.1%。

使用硬件改变 PWM 占空比

LED PWM 控制器硬件可逐渐改变占空比的数值。要使用此功能,需用函数 ledc_fade_func_install() 使能渐变,之后用下列可用渐变函数之一配置:

最后用 ledc_fade_start() 开启渐变。

如不需要渐变和渐变中断,可用函数 ledc_fade_func_uninstall() 关闭。

改变 PWM 频率

LED PWM 控制器 API 有多种方式即时改变 PWM 频率:

控制 PWM 的更多方式

有一些较底层的定时器特定函数可用于更改 PWM 设置:

前两个功能可通过函数 ledc_channel_config() 在后台运行,在定时器配置后启动。

使用中断

配置 LED PWM 控制器通道时,可在 ledc_channel_config_t 中选取参数 ledc_intr_type_t ,在渐变完成时触发中断。

要注册处理程序来处理中断,可调用函数 ledc_isr_register()

频率和占空比分辨率支持范围

LED PWM 控制器主要用于驱动 LED。该控制器 PWM 占空比设置的分辨率范围较广。比如,PWM 频率为 5 kHz 时,占空比分辨率最大可为 13 位。这意味着占空比可为 0 至 100% 之间的任意值,分辨率为 ~0.012%(2 ** 13 = 8192 LED 亮度的离散电平)。然而,这些参数取决于为 LED PWM 控制器定时器计时的时钟信号,LED PWM 控制器为通道提供时钟(具体可参考 定时器配置ESP32-C3 技术参考手册 > LED PWM 计时器 (LEDC) [PDF])。

LED PWM 控制器可用于生成频率较高的信号,足以为数码相机模组等其他设备提供时钟。此时,最大频率可为 40 MHz,占空比分辨率为 1 位。也就是说,占空比固定为 50%,无法调整。

LED PWM 控制器 API 会在设定的频率和占空比分辨率超过 LED PWM 控制器硬件范围时报错。例如,试图将频率设置为 20 MHz、占空比分辨率设置为 3 位时,串行端口监视器上会报告如下错误:

E (196) ledc: requested frequency and duty resolution cannot be achieved, try reducing freq_hz or duty_resolution. div_param=128

此时,占空比分辨率或频率必须降低。比如,将占空比分辨率设置为 2 会解决这一问题,让占空比设置为 25% 的倍数,即 25%、50% 或 75%。

如设置的频率和占空比分辨率低于所支持的最低值,LED PWM 驱动器也会反映并报告,如:

E (196) ledc: requested frequency and duty resolution cannot be achieved, try increasing freq_hz or duty_resolution. div_param=128000000

占空比分辨率通常用 ledc_timer_bit_t 设置,范围是 10 至 15 位。如需较低的占空比分辨率(上至 10,下至 1),可直接输入相应数值。

应用实例

使用 LEDC 改变占空比和渐变控制的实例请参照 peripherals/ledc/ledc_fade

使用 LEDC 基本实例请参照 peripherals/ledc/ledc_basic

API 参考

Functions

esp_err_t ledc_channel_config(const ledc_channel_config_t *ledc_conf)

LEDC channel configuration Configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty resolution.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • ledc_conf: Pointer of LEDC channel configure struct

esp_err_t ledc_timer_config(const ledc_timer_config_t *timer_conf)

LEDC timer configuration Configure LEDC timer with the given source timer/frequency(Hz)/duty_resolution.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.

Parameters
  • timer_conf: Pointer of LEDC timer configure struct

esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)

LEDC update channel parameters.

Note

Call this function to activate the LEDC updated parameters. After ledc_set_duty, we need to call this function to update the settings.

Note

ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to control one LEDC channel in different tasks at the same time. A thread-safe version of API is ledc_set_duty_and_update

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel)

Set LEDC output gpio.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • gpio_num: The LEDC output gpio

  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • ledc_channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)

LEDC stop. Disable LEDC output, and set idle level.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • idle_level: Set output idle level after LEDC stops.

esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)

LEDC set channel frequency (Hz)

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_num: LEDC timer index (0-3), select from ledc_timer_t

  • freq_hz: Set the LEDC frequency

uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)

LEDC get channel frequency (Hz)

Return

  • 0 error

  • Others Current LEDC frequency

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_num: LEDC timer index (0-3), select from ledc_timer_t

esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)

LEDC set duty and hpoint value Only after calling ledc_update_duty will the duty update.

Note

ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to control one LEDC channel in different tasks at the same time. A thread-safe version of API is ledc_set_duty_and_update

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • duty: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]

  • hpoint: Set the LEDC hpoint value(max: 0xfffff)

int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel)

LEDC get hpoint value, the counter value when the output is set high level.

Return

  • LEDC_ERR_VAL if parameter error

  • Others Current hpoint value of LEDC channel

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)

LEDC set duty This function do not change the hpoint value of this channel. if needed, please call ledc_set_duty_with_hpoint. only after calling ledc_update_duty will the duty update.

Note

ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to control one LEDC channel in different tasks at the same time. A thread-safe version of API is ledc_set_duty_and_update.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • duty: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]

uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)

LEDC get duty.

Return

  • LEDC_ERR_DUTY if parameter error

  • Others Current LEDC duty

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t fade_direction, uint32_t step_num, uint32_t duty_cycle_num, uint32_t duty_scale)

LEDC set gradient Set LEDC gradient, After the function calls the ledc_update_duty function, the function can take effect.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • duty: Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution) - 1]

  • fade_direction: Set the direction of the gradient

  • step_num: Set the number of the gradient

  • duty_cycle_num: Set how many LEDC tick each time the gradient lasts

  • duty_scale: Set gradient change amplitude

esp_err_t ledc_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, ledc_isr_handle_t *handle, )

Register LEDC interrupt handler, the handler is an ISR. The handler will be attached to the same CPU core that this function is running on.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Function pointer error.

Parameters
  • fn: Interrupt handler function.

  • arg: User-supplied argument passed to the handler function.

  • intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.

  • handle: Pointer to return handle. If non-NULL, a handle for the interrupt will be returned here.

esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t clock_divider, uint32_t duty_resolution, ledc_clk_src_t clk_src)

Configure LEDC settings.

Return

  • (-1) Parameter error

  • Other Current LEDC duty

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_sel: Timer index (0-3), there are 4 timers in LEDC module

  • clock_divider: Timer clock divide value, the timer clock is divided from the selected clock source

  • duty_resolution: Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution)]

  • clk_src: Select LEDC source clock.

esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, ledc_timer_t timer_sel)

Reset LEDC timer.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_sel: LEDC timer index (0-3), select from ledc_timer_t

esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, ledc_timer_t timer_sel)

Pause LEDC timer counter.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_sel: LEDC timer index (0-3), select from ledc_timer_t

esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, ledc_timer_t timer_sel)

Resume LEDC timer.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • timer_sel: LEDC timer index (0-3), select from ledc_timer_t

esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_timer_t timer_sel)

Bind LEDC channel with the selected timer.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • timer_sel: LEDC timer index (0-3), select from ledc_timer_t

esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num)

Set LEDC fade function.

Note

Call ledc_fade_func_install() once before calling this function. Call ledc_fade_start() after this to start fading.

Note

ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to control one LEDC channel in different tasks at the same time. A thread-safe version of API is ledc_set_fade_step_and_start

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_FAIL Fade function init error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • target_duty: Target duty of fading [0, (2**duty_resolution) - 1]

  • scale: Controls the increase or decrease step scale.

  • cycle_num: increase or decrease the duty every cycle_num cycles

esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms)

Set LEDC fade function, with a limited time.

Note

Call ledc_fade_func_install() once before calling this function. Call ledc_fade_start() after this to start fading.

Note

ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to control one LEDC channel in different tasks at the same time. A thread-safe version of API is ledc_set_fade_step_and_start

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_FAIL Fade function init error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • target_duty: Target duty of fading [0, (2**duty_resolution) - 1]

  • max_fade_time_ms: The maximum time of the fading ( ms ).

esp_err_t ledc_fade_func_install(int intr_alloc_flags)

Install LEDC fade function. This function will occupy interrupt of LEDC module.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function already installed.

Parameters
  • intr_alloc_flags: Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.

void ledc_fade_func_uninstall(void)

Uninstall LEDC fade function.

esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode)

Start LEDC fading.

Note

Call ledc_fade_func_install() once before calling this function. Call this API right after ledc_set_fade_with_time or ledc_set_fade_with_step before to start fading.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_ERR_INVALID_ARG Parameter error.

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel number

  • fade_mode: Whether to block until fading done.

esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint)

A thread-safe API to set duty for LEDC channel and return when duty updated.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • duty: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]

  • hpoint: Set the LEDC hpoint value(max: 0xfffff)

esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t max_fade_time_ms, ledc_fade_mode_t fade_mode)

A thread-safe API to set and start LEDC fade function, with a limited time.

Note

Call ledc_fade_func_install() once, before calling this function.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_FAIL Fade function init error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • target_duty: Target duty of fading [0, (2**duty_resolution) - 1]

  • max_fade_time_ms: The maximum time of the fading ( ms ).

  • fade_mode: choose blocking or non-blocking mode

esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode)

A thread-safe API to set and start LEDC fade function.

Note

Call ledc_fade_func_install() once before calling this function.

Note

If a fade operation is running in progress on that channel, the driver would not allow it to be stopped. Other duty operations will have to wait until the fade operation has finished.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_FAIL Fade function init error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • target_duty: Target duty of fading [0, (2**duty_resolution) - 1]

  • scale: Controls the increase or decrease step scale.

  • cycle_num: increase or decrease the duty every cycle_num cycles

  • fade_mode: choose blocking or non-blocking mode

esp_err_t ledc_cb_register(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_cbs_t *cbs, void *user_arg)

LEDC callback registration function.

Note

The callback is called from an ISR, it must never attempt to block, and any FreeRTOS API called must be ISR capable.

Return

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_OK Success

  • ESP_ERR_INVALID_STATE Fade function not installed.

  • ESP_FAIL Fade function init error

Parameters
  • speed_mode: Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.

  • channel: LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t

  • cbs: Group of LEDC callback functions

  • user_arg: user registered data for the callback function

Structures

struct ledc_cb_param_t

LEDC callback parameter.

Public Members

ledc_cb_event_t event

Event name

uint32_t speed_mode

Speed mode of the LEDC channel group

uint32_t channel

LEDC channel (0 - LEDC_CHANNEL_MAX-1)

uint32_t duty

LEDC current duty of the channel, the range of duty is [0, (2**duty_resolution) - 1]

struct ledc_cbs_t

Group of supported LEDC callbacks.

Note

The callbacks are all running under ISR environment

Public Members

ledc_cb_t fade_cb

LEDC fade_end callback function

Macros

LEDC_APB_CLK_HZ
LEDC_REF_CLK_HZ
LEDC_ERR_DUTY
LEDC_ERR_VAL

Type Definitions

typedef intr_handle_t ledc_isr_handle_t
typedef bool (*ledc_cb_t)(const ledc_cb_param_t *param, void *user_arg)

Type of LEDC event callback.

Parameters
  • param: LEDC callback parameter

  • user_arg: User registered data

Enumerations

enum ledc_cb_event_t

LEDC callback event type.

Values:

LEDC_FADE_END_EVT

LEDC fade end event

Structures

struct ledc_channel_config_t

Configuration parameters of LEDC channel for ledc_channel_config function.

Public Members

int gpio_num

the LEDC output gpio_num, if you want to use gpio16, gpio_num = 16

ledc_mode_t speed_mode

LEDC speed speed_mode, high-speed mode or low-speed mode

ledc_channel_t channel

LEDC channel (0 - 7)

ledc_intr_type_t intr_type

configure interrupt, Fade interrupt enable or Fade interrupt disable

ledc_timer_t timer_sel

Select the timer source of channel (0 - 3)

uint32_t duty

LEDC channel duty, the range of duty setting is [0, (2**duty_resolution)]

int hpoint

LEDC channel hpoint value, the max value is 0xfffff

unsigned int output_invert : 1

Enable (1) or disable (0) gpio output invert

struct ledc_channel_config_t::[anonymous] flags

LEDC flags

struct ledc_timer_config_t

Configuration parameters of LEDC Timer timer for ledc_timer_config function.

Public Members

ledc_mode_t speed_mode

LEDC speed speed_mode, high-speed mode or low-speed mode

ledc_timer_bit_t duty_resolution

LEDC channel duty resolution

ledc_timer_bit_t bit_num

Deprecated in ESP-IDF 3.0. This is an alias to ‘duty_resolution’ for backward compatibility with ESP-IDF 2.1

ledc_timer_t timer_num

The timer source of channel (0 - 3)

uint32_t freq_hz

LEDC timer frequency (Hz)

ledc_clk_cfg_t clk_cfg

Configure LEDC source clock. For low speed channels and high speed channels, you can specify the source clock using LEDC_USE_REF_TICK, LEDC_USE_APB_CLK or LEDC_AUTO_CLK. For low speed channels, you can also specify the source clock using LEDC_USE_RTC8M_CLK, in this case, all low speed channel’s source clock must be RTC8M_CLK

Enumerations

enum ledc_mode_t

Values:

LEDC_LOW_SPEED_MODE

LEDC low speed speed_mode

LEDC_SPEED_MODE_MAX

LEDC speed limit

enum ledc_intr_type_t

Values:

LEDC_INTR_DISABLE = 0

Disable LEDC interrupt

LEDC_INTR_FADE_END

Enable LEDC interrupt

LEDC_INTR_MAX
enum ledc_duty_direction_t

Values:

LEDC_DUTY_DIR_DECREASE = 0

LEDC duty decrease direction

LEDC_DUTY_DIR_INCREASE = 1

LEDC duty increase direction

LEDC_DUTY_DIR_MAX
enum ledc_slow_clk_sel_t

Values:

LEDC_SLOW_CLK_RTC8M = 0

LEDC low speed timer clock source is 8MHz RTC clock

LEDC_SLOW_CLK_APB

LEDC low speed timer clock source is 80MHz APB clock

LEDC_SLOW_CLK_XTAL

LEDC low speed timer clock source XTAL clock

enum ledc_clk_cfg_t

Values:

LEDC_AUTO_CLK = 0

The driver will automatically select the source clock(REF_TICK or APB) based on the giving resolution and duty parameter when init the timer

LEDC_USE_REF_TICK

LEDC timer select REF_TICK clock as source clock

LEDC_USE_APB_CLK

LEDC timer select APB clock as source clock

LEDC_USE_RTC8M_CLK

LEDC timer select RTC8M_CLK as source clock. Only for low speed channels and this parameter must be the same for all low speed channels

LEDC_USE_XTAL_CLK

LEDC timer select XTAL clock as source clock

enum ledc_clk_src_t

Values:

LEDC_REF_TICK = LEDC_USE_REF_TICK

LEDC timer clock divided from reference tick (1Mhz)

LEDC_APB_CLK = LEDC_USE_APB_CLK

LEDC timer clock divided from APB clock (80Mhz)

enum ledc_timer_t

Values:

LEDC_TIMER_0 = 0

LEDC timer 0

LEDC_TIMER_1

LEDC timer 1

LEDC_TIMER_2

LEDC timer 2

LEDC_TIMER_3

LEDC timer 3

LEDC_TIMER_MAX
enum ledc_channel_t

Values:

LEDC_CHANNEL_0 = 0

LEDC channel 0

LEDC_CHANNEL_1

LEDC channel 1

LEDC_CHANNEL_2

LEDC channel 2

LEDC_CHANNEL_3

LEDC channel 3

LEDC_CHANNEL_4

LEDC channel 4

LEDC_CHANNEL_5

LEDC channel 5

LEDC_CHANNEL_MAX
enum ledc_timer_bit_t

Values:

LEDC_TIMER_1_BIT = 1

LEDC PWM duty resolution of 1 bits

LEDC_TIMER_2_BIT

LEDC PWM duty resolution of 2 bits

LEDC_TIMER_3_BIT

LEDC PWM duty resolution of 3 bits

LEDC_TIMER_4_BIT

LEDC PWM duty resolution of 4 bits

LEDC_TIMER_5_BIT

LEDC PWM duty resolution of 5 bits

LEDC_TIMER_6_BIT

LEDC PWM duty resolution of 6 bits

LEDC_TIMER_7_BIT

LEDC PWM duty resolution of 7 bits

LEDC_TIMER_8_BIT

LEDC PWM duty resolution of 8 bits

LEDC_TIMER_9_BIT

LEDC PWM duty resolution of 9 bits

LEDC_TIMER_10_BIT

LEDC PWM duty resolution of 10 bits

LEDC_TIMER_11_BIT

LEDC PWM duty resolution of 11 bits

LEDC_TIMER_12_BIT

LEDC PWM duty resolution of 12 bits

LEDC_TIMER_13_BIT

LEDC PWM duty resolution of 13 bits

LEDC_TIMER_14_BIT

LEDC PWM duty resolution of 14 bits

LEDC_TIMER_BIT_MAX
enum ledc_fade_mode_t

Values:

LEDC_FADE_NO_WAIT = 0

LEDC fade function will return immediately

LEDC_FADE_WAIT_DONE

LEDC fade function will block until fading to the target duty

LEDC_FADE_MAX