2025-04-23 11:39 AM
Hi everyone,
I'm using an STM32 timer in center-aligned mode, and I would like the counter to start from a value other than zero. Specifically, I want the timer to count from a custom value (e.g., 2000), up to the auto-reload value (ARR), and then count down again, repeatedly — like a normal center-aligned operation, but starting from a value other than 0.
Is there a way to set the initial value of the counter before the counting starts, so that the up/down counting occurs between a custom starting point and the ARR value?
I’m aware that I can use __HAL_TIM_SET_COUNTER() to set the counter to a custom value, but I’m not sure how it behaves in center-aligned mode or whether it interferes with the timing or alignment.
Any ideas or suggestions would be greatly appreciated!
Thanks in advance.
2025-04-23 12:10 PM - edited 2025-04-23 12:11 PM
Yes, set the counter value prior to starting the counter and it will start from there.
For example:
2025-04-26 6:06 AM
First of all, thank you very much for your helpful explanation regarding setting a custom starting value for the timer in center-aligned mode.
I updated my code under /* USER CODE BEGIN 2 */ section as follows:
However, when I observe the timer value by calling __HAL_TIM_GET_COUNTER(&htim1); inside the while(1) loop, it seems like the counter is still starting from 0, not from 999 as I expected.
Could you please help me understand why the counter is resetting to 0?
Is there something I am missing during the timer initialization or start process?
Thank you again for your support!
2025-04-26 8:51 AM
What chip are you using?
Step through the code and, when the timer is enabled via the CEN bit, ensure that CNT=999 as you have set it. Consider setting the timer update frequency to 1 Hz so you can examine the registers at human speeds to see what's going on. I didn't test it but the Reference Manual is likely correct. It may not be possible to do this with HAL. If you are particular about the very first pulse, you may need to write your own code, but if you just want TIM1 and TIM8 to be offset, as it appears, should be able to do that with HAL.