PWM Driver#
PWM Driver
The PWM driver uses one or more TIMER peripherals to generate one or more PWM waveform, with configurable frequency, duty cycle, and polarity.
Multiple instances of the driver can be created and allocated to their own TIMER channel.
The duty cycle of the PWM waveform can be updated, while the PWM driver is running by calling sl_pwm_set_duty_cycle(), without causing glitches in the output waveform.
Note
If several PWM driver instances are set up to use the same TIMER instance, the PWM frequency of these instances must be the same.
PWM Example Code#
Basic example for generating PWM waveform:
#include "sl_pwm.h"
#include "em_gpio.h"
int main( void )
{
...
sl_pwm_instance_t sl_pwm_led0 = {
.timer = TIMER0,
.channel = 0,
.port = gpioPortA,
.pin = 0,
.location = 0,
};
sl_pwm_config_t pwm_led0_config = {
.frequency = 10000,
.polarity = PWM_ACTIVE_HIGH,
};
// Initialize PWM
sl_pwm_init(&sl_pwm_led0, &pwm_led0_config);
// Set duty cycle to 40%
sl_pwm_set_duty_cycle(&sl_pwm_led0, 40);
// Enable PWM output
sl_pwm_start(&sl_pwm_led0);
...
}
Modules#
Enumerations#
Functions#
Initialize PWM driver.
Deinitialize PWM driver.
Start generating PWM waveform.
Stop generating PWM waveform.
Set duty cycle for PWM waveform.
Set duty cycle for PWM waveform.
Enumeration Documentation#
sl_pwm_polarity_t#
sl_pwm_polarity_t
PWM polarity selection.
Enumerator | |
---|---|
PWM_ACTIVE_HIGH | PWM polarity active high. |
PWM_ACTIVE_LOW | PWM polarity active low. |
106
of file platform/driver/pwm/inc/sl_pwm.h
Function Documentation#
sl_pwm_init#
sl_status_t sl_pwm_init (sl_pwm_instance_t * pwm, sl_pwm_config_t * config)
Initialize PWM driver.
[in] | pwm | PWM driver instance |
[in] | config | Driver configuration |
Returns
SL_STATUS_OK if there are no errors.
145
of file platform/driver/pwm/inc/sl_pwm.h
sl_pwm_deinit#
sl_status_t sl_pwm_deinit (sl_pwm_instance_t * pwm)
Deinitialize PWM driver.
[in] | pwm | PWM driver instance |
Returns
SL_STATUS_OK if there are no errors.
157
of file platform/driver/pwm/inc/sl_pwm.h
sl_pwm_start#
void sl_pwm_start (sl_pwm_instance_t * pwm)
Start generating PWM waveform.
[in] | pwm | PWM driver instance |
166
of file platform/driver/pwm/inc/sl_pwm.h
sl_pwm_stop#
void sl_pwm_stop (sl_pwm_instance_t * pwm)
Stop generating PWM waveform.
[in] | pwm | PWM driver instance |
175
of file platform/driver/pwm/inc/sl_pwm.h
sl_pwm_set_duty_cycle#
void sl_pwm_set_duty_cycle (sl_pwm_instance_t * pwm, uint8_t percent)
Set duty cycle for PWM waveform.
[in] | pwm | PWM driver instance |
[in] | percent | Percent of the PWM period waveform is in the state defined as the active polarity in the driver configuration |
188
of file platform/driver/pwm/inc/sl_pwm.h
sl_pwm_get_duty_cycle#
uint8_t sl_pwm_get_duty_cycle (sl_pwm_instance_t * pwm)
Set duty cycle for PWM waveform.
[in] | pwm | PWM driver instance |
Returns
Percent of the PWM period waveform is in the state defined as the active polarity in the driver configuration
201
of file platform/driver/pwm/inc/sl_pwm.h