Skip to main content
Hicss.2033
Associate III
September 7, 2020
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

  • September 7, 2020
  • 2 replies
  • 839 views

..

This topic has been closed for replies.
Best answer by TDK

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;

2 replies

TDK
TDKBest answer
Super User
September 7, 2020

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
September 8, 2020

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

waclawek.jan
Super User
September 7, 2020
Hicss.2033
Associate III
September 8, 2020

thanks for your answer, it works!!