LED Control

Overview

Instructions

Application Example

Instructions

API Reference

Instructions

Data 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 duty range is [0, (2**bit_num) - 1],

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 bit_num

LEDC channel duty depth

ledc_timer_t timer_num

The timer source of channel (0 - 3)

uint32_t freq_hz

LEDC timer frequency(Hz)

Macros

LEDC_APB_CLK_HZ (APB_CLK_FREQ)
LEDC_REF_CLK_HZ (1*1000000)

Enumerations

enum ledc_mode_t

Values:

LEDC_HIGH_SPEED_MODE = 0

LEDC high 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

enum ledc_duty_direction_t

Values:

LEDC_DUTY_DIR_DECREASE = 0

LEDC duty decrease direction

LEDC_DUTY_DIR_INCREASE = 1

LEDC duty increase direction

enum ledc_clk_src_t

Values:

LEDC_REF_TICK = 0

LEDC timer clock divided from reference tick(1Mhz)

LEDC_APB_CLK

LEDC timer clock divided from APB clock(80Mhz)

enum ledc_timer_t

Values:

LEDC_TIMER_0 = 0

LEDC source timer TIMER0

LEDC_TIMER_1

LEDC source timer TIMER1

LEDC_TIMER_2

LEDC source timer TIMER2

LEDC_TIMER_3

LEDC source timer TIMER3

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_6

LEDC channel 6

LEDC_CHANNEL_7

LEDC channel 7

enum ledc_timer_bit_t

Values:

LEDC_TIMER_10_BIT = 10

LEDC PWM depth 10Bit

LEDC_TIMER_11_BIT = 11

LEDC PWM depth 11Bit

LEDC_TIMER_12_BIT = 12

LEDC PWM depth 12Bit

LEDC_TIMER_13_BIT = 13

LEDC PWM depth 13Bit

LEDC_TIMER_14_BIT = 14

LEDC PWM depth 14Bit

LEDC_TIMER_15_BIT = 15

LEDC PWM depth 15Bit

Functions

esp_err_t ledc_channel_config(ledc_channel_config_t *ledc_conf)

LEDC channel configuration.

User this Function, configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC depth

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(ledc_timer_config_t *timer_conf)

LEDC timer configuration.

User this Function, configure LEDC timer with the given source timer/frequency(Hz)/bit_num

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 bit_num.
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.

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

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, now we only support high-speed mode. We will access low-speed mode in next version
  • 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 mode, now we only support high-speed mode. We will access low-speed mode in next version
  • channel: LEDC channel(0-7), 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)

Set LEDC 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 bit_num.
Parameters
  • speed_mode: Select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
  • 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 speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
  • timer_num: LEDC timer index(0-3), select from ledc_timer_t

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

LEDC set duty.

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

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, now we only support high-speed mode. We will access low-speed mode in next version
  • channel: LEDC channel(0-7), select from ledc_channel_t
  • duty: Set the LEDC duty, the duty range is [0, (2**bit_num) - 1]

int ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)

LEDC get duty.

Return
  • (-1) parameter error
  • Others Current LEDC duty
Parameters
  • speed_mode: Select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
  • channel: LEDC channel(0-7), select from ledc_channel_t

esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty, ledc_duty_direction_t gradule_direction, uint32_t step_num, uint32_t duty_cyle_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.

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, now we only support high-speed mode. We will access low-speed mode in next version
  • channel: LEDC channel(0-7), select from ledc_channel_t
  • duty: Set the start of the gradient duty, the duty range is [0, (2**bit_num) - 1]
  • gradule_direction: Set the direction of the gradient
  • step_num: Set the number of the gradient
  • duty_cyle_num: Set how many LEDC tick each time the gradient lasts
  • duty_scale: Set gradient change amplitude

esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void *), void *arg, )

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.

Note
Users should know that which CPU is running and then pick a INUM that is not used by system. We can find the information of INUM and interrupt level in soc.h.
Note
Note that the handler function MUST be defined with attribution of “IRAM_ATTR”.
Return
  • ESP_OK Success
  • ESP_ERR_INVALID_ARG Function pointer error.
Parameters
  • ledc_intr_num: LEDC interrupt number, check the info in soc.h, and please see the core-isa.h for more details
  • fn: Interrupt handler function.
Parameters
  • arg: Parameter for handler function

esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, 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 mode, now we only support high-speed mode. We will access low-speed mode in next version
  • timer_sel: Timer index(0-3), there are 4 timers in LEDC module
  • div_num: Timer clock divide number, the timer clock is divided from the selected clock source
  • bit_num: The count number of one period, counter range is 0 ~ ((2 ** bit_num) - 1)
  • clk_src: Select LEDC source clock.

esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_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 mode, now we only support high-speed mode. We will access low-speed mode in next version
  • timer_sel: LEDC timer index(0-3), select from ledc_timer_t

esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_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 mode, now we only support high-speed mode. We will access low-speed mode in next version
  • timer_sel: LEDC timer index(0-3), select from ledc_timer_t

esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)

pause LEDC timer resume

Return
  • ESP_ERR_INVALID_ARG Parameter error
  • ESP_OK Success
Parameters
  • speed_mode: Select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
  • timer_sel: LEDC timer index(0-3), select from ledc_timer_t

esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)

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 mode, now we only support high-speed mode. We will access low-speed mode in next version
  • channel: LEDC channel index(0-7), select from ledc_channel_t
  • timer_idx: LEDC timer index(0-3), select from ledc_timer_t