LED PWM 控制器¶
概述¶
LED PWM 控制器主要用于控制 LED,也可产生 PWM 信号用于其他设备的控制。该控制器有 8 路高速通道和 8 路低速通道,可以产生独立的波形来驱动 RGB LED 设备等。
LED PWM 控制器的高速通道和低速通道均支持硬件渐变功能,可在无需 CPU 干预的情况下自动改变 PWM 信号的占空比,也可由软件改变 PWM 信号的占空比,实现亮度和颜色渐变。此外,低速通道在 Sleep 模式下仍可运行。
功能概览¶
要让指定 LED PWM 控制器 高速模式或低速模式 通道运行,需进行如下配置:
- 配置定时器 指定 PWM 信号的频率和占空比分辨率。
- 配置通道 绑定定时器和输出 PWM 信号的 GPIO。
- 改变 PWM 信号 输出 PWM 信号来驱动 LED。可通过软件控制或使用硬件渐变功能来改变 LED 的亮度。
配置定时器¶
要设置定时器,可调用函数 ledc_timer_config()
,并将包括如下配置参数的数据结构 ledc_timer_config_t
传递给该函数:
- 定时器索引
ledc_timer_t
- 速度模式
ledc_mode_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 频率 一节。
使用软件改变 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 频率:
- 通过调用函数
ledc_set_freq()
设置频率。可用函数ledc_get_freq()
查看当前频率。- 通过调用函数
ledc_bind_channel_timer()
改变频率和占空比分辨率,将其他定时器与通道相连。- 通过调用函数
ledc_channel_config()
改变通道的定时器。
使用中断¶
配置 LED PWM 控制器通道时,可在 ledc_channel_config_t
中选取参数 ledc_intr_type_t
,在渐变完成时触发中断。
要注册处理程序来处理中断,可调用函数 ledc_isr_register()
。
LED PWM 控制器高速和低速模式¶
LED PWM 控制器有 8 个可用定时器和 16 路通道,其中有一半专以高速模式运行,另一半则以低速模式运行。要选取高速或低速定时器或通道,可借助所调用函数中的参数 ledc_mode_t
。
高速模式的优点是可平稳地改变定时器设置。也就是说,高速模式下如定时器设置改变,此变更会自动应用于定时器的下一次溢出中断。而更新低速定时器时,设置变更应由软件显式触发。LED PWM 驱动器在后台更改设置,比如在调用函数 ledc_timer_config()
或 ledc_timer_set()
时。
更多关于速度模式的详细信息请参阅 ESP32 技术参考手册 (PDF)。注意,该手册中提到的支持 SLOW_CLOCK
暂不适用于 LED PWM 驱动器。
频率和占空比分辨率支持范围¶
LED PWM 控制器主要用于驱动 LED。该控制器 PWM 占空比设置的分辨率范围较广。比如,PWM 频率为 5 kHz 时,占空比分辨率最大可为 13 位。这意味着占空比可为 0 至 100% 之间的任意值,分辨率为 ~0.012%(2 ** 13 = 8192 LED 亮度的离散电平)。
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),可直接输入相应数值。
应用实例¶
LED PWM 改变占空比和渐变控制的实例请参照 peripherals/ledc。
API 参考¶
Header File¶
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 speed_mode, high-speed mode and low-speed mode,channel
: LEDC channel (0-7), 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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), select from ledc_channel_tidle_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 speed_mode, high-speed mode and low-speed modetimer_num
: LEDC timer index (0-3), select from ledc_timer_tfreq_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 speed_mode, high-speed mode and low-speed modetimer_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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), select from ledc_channel_tduty
: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), 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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), select from ledc_channel_tduty
: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
-
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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), 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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), select from ledc_channel_tduty
: Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)]fade_direction
: Set the direction of the gradientstep_num
: Set the number of the gradientduty_cycle_num
: Set how many LEDC tick each time the gradient lastsduty_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 speed_mode, high-speed mode and low-speed modetimer_sel
: Timer index (0-3), there are 4 timers in LEDC moduleclock_divider
: Timer clock divide value, the timer clock is divided from the selected clock sourceduty_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 speed_mode, high-speed mode and low-speed modetimer_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 speed_mode, high-speed mode and low-speed modetimer_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 speed_mode, high-speed mode and low-speed modetimer_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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel index (0-7), select from ledc_channel_ttimer_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 speed_mode, high-speed mode and low-speed mode,channel
: LEDC channel index (0-7), select from ledc_channel_ttarget_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 speed_mode, high-speed mode and low-speed mode,channel
: LEDC channel index (0-7), select from ledc_channel_ttarget_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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel numberfade_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 speed_mode, high-speed mode and low-speed modechannel
: LEDC channel (0-7), select from ledc_channel_tduty
: Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]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 speed_mode, high-speed mode and low-speed mode,channel
: LEDC channel index (0-7), select from ledc_channel_ttarget_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 speed_mode, high-speed mode and low-speed mode,channel
: LEDC channel index (0-7), select from ledc_channel_ttarget_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 cyclesfade_mode
: choose blocking or non-blocking mode
Type Definitions¶
-
typedef intr_handle_t
ledc_isr_handle_t
¶