Skip to content

PWM Driver

PWM Driver interface provides functionality to control frequency and duty of pulse-width modulation (PWM) signal.

To use this library

Header: #include "drv_pwm.h"

memake: MikroSDK.Driver

PWM driver controls underlying timer or dedicated hardware of a microcontroller to generate a PWM signal. User can configure signal frequency, duty cycle, output pin and channel.

Initialization

In order to initialize the PWM driver, user must call the pwm_open() function, before any other. It is required to specify signal frequency and output pin. Example initialization procedure is shown below:

pwm_t pwm;
pwm_config_t pwm_cfg;

pwm_configure_default( &pwm_cfg );
pwm_cfg.pin = PA11;
pwm_cfg.freq_hz = 1000;

pwm_open( &pwm, &pwm_cfg );

PWM control

The driver provides two functions: pwm_start() and pwm_stop() for enabling and disabling PWM signal generation, respectively. To change duty cycle, user can use the pwm_set_duty() function.

Example procedure of controlling a PWM signal:

pwm_set_freq( &pwm, pwm_cfg.freq_hz );
pwm_start( &pwm );
pwm_set_duty( &pwm, 0.5 );    // 50% duty

Reference