Skip to main content
Hicss.2033
Associate III
Sep 7, 2020 3:05 PM UTC
Solved

I'm using a STM32F429I discovery with HAL drivers. Is there a sample code showing how to change the duty cycle of a timer working in PWM output mode at run-time? Thanks, Piero

..

This topic has been closed for replies.
TDK
TDKBest answer
Sep 7, 2020 4:53 PM UTC

You can just change the CCRx register directly. I don't think HAL has a function for doing only this. Pretty sure the LL library does.

For 50% duty:

TIMx->CCRx = (TIMx->ARR + 1) / 2;

For 25%/75% duty:

TIMx->CCRx = (TIMx->ARR + 1) / 4;

"If you feel a post has answered your question, please click ""Accept as Solution""."

4 replies

TDK
TDKBest answer
Sep 7, 2020 4:53 PM UTC

You can just change the CCRx register directly. I don't think HAL has a function for doing only this. Pretty sure the LL library does.

For 50% duty:

TIMx->CCRx = (TIMx->ARR + 1) / 2;

For 25%/75% duty:

TIMx->CCRx = (TIMx->ARR + 1) / 4;

"If you feel a post has answered your question, please click ""Accept as Solution""."
Hicss.2033
Associate III
Sep 8, 2020 3:59 PM UTC

I've find the solution in the following reply. Thanks for your support.

waclawek.jan
Super User
Sep 7, 2020 5:04 PM UTC
Hicss.2033
Associate III
Sep 8, 2020 3:59 PM UTC

thanks for your answer, it works!!