cancel
Showing results for 
Search instead for 
Did you mean: 

How to change NUCLEO-G431KB PWM duty cycle

Jason_fx
Associate II

I use the PWM output example code of STM32CubeIDE,

how do I modify the duty cycle of the PWM, I can't find a way to modify it

8 REPLIES 8
Stassen.C
ST Employee

Hello Jason,

You can simply change the PULSE_VALUE in the main.h

StassenC_0-1694078991632.png

Regards,
Stassen

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Issamos
Lead II

Hello @Jason_fx 

TIMx channels duty cycle = (TIMx_CCR1/ (TIMx_ARR + 1))*100.

top.PNG

You can change the values off TIMx_CCR1 and TIMx_ARR to modify the duty cycle value.

Best regards.

II

Hi Stassen, 

Thanks for your reply.

But I have a question,

I find this code but I can't understand how to change the PULSE_VALUE,

example code is output PWM has a 90% duty cycle, if change 50% duty cycle how to set the PULSE_VALUE?

hello @Jason_fx 

you can simply set "duty * counter-base-period" to "Compare Register" just like followings..

mxo_1-1694137721511.png

 where V_TIM_Cbase is defined in configration as below.

mxo_0-1694137571774.png

hope this could help

regards

Hi MXO,

I can't understand how to join your code,
The definitions provided by your code and example code seem to be different.

hello, @Jason_fx

off course join the codes into your main() or some applicable place by yourself.

and, if the Counter Period in your project is for example "999" (=1000-1), I mean V_TIM_Cbase = 1000,

then it should be refered in the code which I show you.

if your sample code is as below,   

mxo_0-1694147074065.png

  PulseVal = (uint32_t)(vfPdt * (PERIOD_VALUE + 1));

would be OK, but well.. I don't like it.

Is it OK now?

regards.

Hi @mxo 

I can't find the ___HAL_TIM_SET_COMPARE function on the PWMOutput sample code

I tried changing #define PULSE1_VALUE (uint32_t)(1000 / 2) to #define PULSE1_VALUE (uint32_t)(50000 / 1) and it outputs 76% but I don't understand code work logic.

Jason_fx_1-1694489992051.png

Jason_fx_0-1694489969167.png

 

hello @Jason_fx 

>I can't find the ___HAL_TIM_SET_COMPARE function on the PWMOutput sample code

you can find in stm32??xx_hal_tim.h. (?? = depends on your project, I guess)

 

One important thing that duty must be 0 ~ 1 (or can say 0 ~ 100%).

is your sample project 'TIM_PWMOutput', right?

#define PULSE1_VALUE (uint32_t)(50000 / 1)  .. means over 100% duty.

since the base period is 1000 in that sample project, pulse value should be 0 ~ 1000, or result is unexpected.

TIM_PWMOutput project only shows set duty (=pulse value) at configuration time.

better to find some other samples to understand function, then this can be enough to talk, I think.

mxo_0-1694682176867.png

regards.