PWM driver reusable in different platforms.
- STM32L552
pwm_initialize
: used for initialize the peripheral.pwm_deinitialize
: used for de-initialize the peripheral.pwm_start
: used for start the peripheral.pwm_stop
: used for stop the peripheral.pwm_set_period
: used for set the period (sets the prescaler at 50-1 and the period at 100-1).pwm_set_duty_cycle
: used for set the duty cycle.pwm_set_frequency
: used for set the duty cycle.
- Include the header file
drv_pwm.h
- Configure the timer (
TIMx
) peripheral to operate in PWM mode withCHx
output. - Create the
pwm_t
instance. The following members of the structure must be defined:instance.handler
: handler of choice (ex. &tim1).instance.channel
: enabled PWM channel (ex. TIM_CHANNEL_3).instance.mx_init
: PWM initializing function generated by the configuration tool (ex. MX_TIM1_Init).
pwm_t hpwm =
{
.handler =&htim1,
.channel= TIM_CHANNEL_1,
.init = MX_TIM1_Init,
.clock_freq_mhz = 40
};
- Use the function
pwm_initialize
to initialize the peripheral andpwm_set_duty_cycle
to set the desired duty cycle.
Let's consider a NUCLEO - L552ZE-Q. The TIM1 timer peripheral has been configured to operate in PWM mode on channel 3 (TIM_CHANNEL_3).
pwm_initialize(&hpwm); //structure defined above
pwm_start(&hpwm);
pwm_set_period(&hpwm , 100) //the function currently sets the period at 100-1 independetly from the inseryed value
pwm_set_duty_cycle(&hpwm ,20);
pwm_set_frequency(&hpm,15000);
pwm_set_duty_cycle(&hpwm ,75);