2023-09-07 01:51 AM
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
2023-09-07 02:31 AM
Hello Jason,
You can simply change the PULSE_VALUE in the main.h
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.
2023-09-07 02:41 AM - edited 2023-09-07 02:45 AM
Hello @Jason_fx
TIMx channels duty cycle = (TIMx_CCR1/ (TIMx_ARR + 1))*100.
You can change the values off TIMx_CCR1 and TIMx_ARR to modify the duty cycle value.
Best regards.
II
2023-09-07 05:37 PM
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?
2023-09-07 06:56 PM
hello @Jason_fx
you can simply set "duty * counter-base-period" to "Compare Register" just like followings..
where V_TIM_Cbase is defined in configration as below.
hope this could help
regards
2023-09-07 08:19 PM
Hi MXO,
I can't understand how to join your code,
The definitions provided by your code and example code seem to be different.
2023-09-07 09:32 PM
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,
PulseVal = (uint32_t)(vfPdt * (PERIOD_VALUE + 1));
would be OK, but well.. I don't like it.
Is it OK now?
regards.
2023-09-11 08:42 PM
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.
2023-09-14 02:06 AM
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.
regards.