PWM: Pulse-Width Modulation

The driver interface is defined in the ESP8266 SDK.

Configuration Variables

ENABLE_CUSTOM_PWM
undefined

use the Espressif PWM driver

1 (default)

Use the New PWM driver, a drop-in replacement for the version provided in the Espressif SDK.

API Documentation

void pwm_init(uint32_t period, uint32_t *duty, uint32_t pwm_channel_num, uint32_t (*pin_info_list)[3])

Initialize PWM function, including GPIO selection, period and duty cycle.

Example:

    uint32 ioInfo[][3] = {
        {PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC, PWM_0_OUT_IO_NUM},
        {PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC, PWM_1_OUT_IO_NUM},
        {PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC, PWM_2_OUT_IO_NUM}
};

pwm_init(light_param.pwm_period, light_param.pwm_duty, 3, ioInfo);

Example:

    uint32 io_info[][3] = {
        {PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC, PWM_0_OUT_IO_NUM},
        {PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC, PWM_1_OUT_IO_NUM},
        {PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC, PWM_2_OUT_IO_NUM}
};

pwm_init(light_param.pwm_period, light_param.pwm_duty, 3, io_info);

Note

This API can be called only once.

Note

This API can be called only once.

Parameters:
  • period – PWM period

  • duty – duty cycle of each output

  • pwm_channel_num – PWM channel number

  • pin_info_list – Array containing an entry for each channel giving

  • period – PWM period

  • duty – duty cycle of each output

  • pwm_channel_num – PWM channel number

  • pin_info_list – Array containing an entry for each channel giving

void pwm_start(void)

Starts PWM.

This function needs to be called after PWM configuration is changed.

void pwm_set_duty(uint32_t duty, uint8_t channel)

Sets duty cycle of a PWM output.

Set the time that high-level signal will last. The range of duty depends on PWM period. Its maximum value of which can be Period * 1000 / 45.

For example, for 1-KHz PWM, the duty range is 0 ~ 22222.

Parameters:
  • duty – The time that high-level single will last, duty cycle will be (duty*45)/(period*1000)

  • channel – PWM channel, which depends on how many PWM channels are used

uint32_t pwm_get_duty(uint8_t channel)

Get duty cycle of PWM output.

Duty cycle will be (duty*45) / (period*1000).

Parameters:

channel – PWM channel, which depends on how many PWM channels are used

Return values:

uint32 – Duty cycle of PWM output

void pwm_set_period(uint32_t period)

Set PWM period.

Parameters:

period – PWM period in us. For example, 1-KHz PWM period = 1000us.

uint32_t pwm_get_period(void)

Get PWM period.

Return values:

uint32 – Return PWM period in us.

uint32_t get_pwm_version(void)

Get version information of PWM.

Return values:

uint32 – PWM version