PWM¶
API Reference¶
Header File¶
Functions¶
-
esp_err_t
pwm_init
(uint32_t period, uint32_t *duties, uint8_t channel_num, const uint32_t *pin_num)¶ PWM function initialization, including GPIO, frequency and duty cycle.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- ESP_FAIL Init error
- Parameters
period
: PWM period, unit: us. e.g. For 1KHz PWM, period is 1000 us. Do not set the period below 20us.duties
: duty cycle of each channels.channel_num
: PWM channel number, maximum is 8pin_num
: GPIO number of PWM channel
-
esp_err_t
pwm_deinit
(void)¶ PWM function uninstall.
- Return
- ESP_OK Success
- ESP_FAIL Init error
-
esp_err_t
pwm_set_duty
(uint8_t channel_num, uint32_t duty)¶ Set the duty cycle of a PWM channel. Set the time that high level or low(if you invert the output of this channel) signal will last, the duty cycle cannot exceed the period.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_num
: PWM channel number the channel_num cannot exceed the value initialized by pwm_init.duty
: duty cycle
-
esp_err_t
pwm_get_duty
(uint8_t channel_num, uint32_t *duty_p)¶ Get the duty cycle of a PWM channel.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_num
: PWM channel number the channel_num cannot exceed the value initialized by pwm_init.duty_p
: pointer saves the address of the specified channel duty cycle
-
esp_err_t
pwm_set_period
(uint32_t period)¶ Set PWM period, unit: us.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
period
: PWM period, unit: us For example, for 1KHz PWM, period is 1000. Do not set the period below 20us.
-
esp_err_t
pwm_get_period
(uint32_t *period_p)¶ Get PWM period, unit: us.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
period_p
: pointer saves the address of the period
-
esp_err_t
pwm_start
(void)¶ Starts PWM.
- Note
- This function needs to be called after PWM configuration is changed.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
-
esp_err_t
pwm_stop
(uint32_t stop_level_mask)¶ Stop all PWM channel. Stop PWM and set the output of each channel to the specified level. Calling pwm_start can re-start PWM output.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
stop_level_mask
: Out put level after PWM is stoped e.g. We initialize 8 channels, if stop_level_mask = 0x0f, channel 0,1,2 and 3 will output high level, and channel 4,5,6 and 7 will output low level.
-
esp_err_t
pwm_set_duties
(uint32_t *duties)¶ Set the duty cycle of all channels.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
duties
: An array that store the duty cycle of each channel, the array elements number needs to be the same as the number of channels.
-
esp_err_t
pwm_set_phase
(uint8_t channel_num, int16_t phase)¶ Set the phase of a PWM channel.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_num
: PWM channel number the channel_num cannot exceed the value initialized by pwm_init.phase
: The phase of this PWM channel, the phase range is (-180 ~ 180).
-
esp_err_t
pwm_set_phases
(int16_t *phases)¶ Set the phase of all channels.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
phases
: An array that store the phase of each channel, the array elements number needs to be the same as the number of channels.
-
esp_err_t
pwm_get_phase
(uint8_t channel_num, uint16_t *phase_p)¶ Get the phase of a PWM channel.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_num
: PWM channel number the channel_num cannot exceed the value initialized by pwm_init.phase_p
: pointer saves the address of the specified channel phase
-
esp_err_t
pwm_set_period_duties
(uint32_t period, uint32_t *duties)¶ Set PWM period and duty of each PWM channel.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
period
: PWM period, unit: us For example, for 1KHz PWM, period is 1000.duties
: An array that store the duty cycle of each channel, the array elements number needs to be the same as the number of channels.
-
esp_err_t
pwm_set_channel_invert
(uint16_t channel_mask)¶ Set the inverting output PWM channel.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_mask
: The channel bitmask that used to invert the output e.g. We initialize 8 channels, if channel_mask = 0x0f, channels 0, 1, 2 and 3 will invert the output.
-
esp_err_t
pwm_clear_channel_invert
(uint16_t channel_mask)¶ Clear the inverting output PWM channel. This function only works for the PWM channel that is already in the inverted output states.
- Note
- After set configuration, pwm_start needs to be called to take effect.
- Return
- ESP_OK Success
- ESP_ERR_INVALID_ARG Parameter error
- Parameters
channel_mask
: The channel bitmask that need to clear e.g. The outputs of channels 0, 1, 2 and 3 are already in inverted state. If channel_mask = 0x07, the output of channel 0, 1, and 2 will return to normal, the channel 3 will keep inverting output.