2025-02-05 12:49 AM - last edited on 2025-02-05 01:01 AM by Andrew Neil
Hi
Can you tell me how I can calculate a value for ARR for timer 2(32bit timer) and timer3(16bit timer)
I am using STM32F407 APB1 timer clock is 12MHz prescaler is 6
2025-02-05 12:55 AM - edited 2025-02-05 01:18 AM
Hello,
For which purpose? time base?
Inspire from:
/* Get clock configuration */
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
/* Get APB1 prescaler */
uwAPB1Prescaler = clkconfig.APB1CLKDivider;
/* Compute TIM6 clock */
if (uwAPB1Prescaler == RCC_HCLK_DIV1)
{
uwTimclock = HAL_RCC_GetPCLK1Freq();
}
else
{
uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
}
/* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
/* Initialize TIM6 */
TimHandle.Instance = TIM6;
/* Initialize TIMx peripheral as follow:
+ Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
+ ClockDivision = 0
+ Counter direction = Up
*/
TimHandle.Init.Period = (1000000U / 1000U) - 1U;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.ClockDivision = 0U;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
status = HAL_TIM_Base_Init(&TimHandle);
See also the application note AN4013 "Introduction to timers for STM32 MCUs " / section 2.2 Time base generator
2025-02-05 01:02 AM - edited 2025-02-05 01:15 AM
See also the Timer section in the Reference Manual for your chip.
And: https://wiki.st.com/stm32mcu/wiki/Getting_started_with_TIM
And Application note AN4776, General-purpose timer cookbook for STM32 microcontrollers
ST Videos on timers: https://www.youtube.com/@stmicroelectronics/search?query=timer
Many 3rd-parties also have resources; eg, https://www.digikey.co.uk/en/maker/projects/getting-started-with-stm32-timers-and-timer-interrupts/d08e6493cefa486fb1e79c43c0b08cc6
2025-02-05 01:11 AM
Hello,
can you show what should be the purpose of each timers?
If you have timer clock on 12 MHz and prescaler is 6 the timer counter will be counted with frequency
12 MHz / (6+1) = 1.71 MHz. ARR register defines when timer counter will be reloaded. Frequency of reloading the timer counter is calculated as: 1.71 MHz / (ARR+1) in this case.
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.
2025-02-05 01:37 AM - edited 2025-02-05 01:56 AM
Hi
I will again refreshed my query my APB1 timer clock is 72Mhz so does it mean my timer are running on 72Mhz?
prescaler value which I said earlies it was CAN prescaler so plz ignore previous query.
second I want to create a delay like 10millisec 100millisec e.g every 10ms interrupt will generate.
for explanation you can take any 10ms ,100ms
note: at the time of Timer_Initialization I loaded ARR value with 0xFFFFFFFF and Counter value with 0x00000000
auto preload is disabled.
and at runtime time I am want to change ARR value with desired value I am using upcounter. so i want to know what will prescaler value and method to create a delay.
2025-02-05 01:52 AM
@Ash1 wrote:my APB1 timer clock is 72Mhz so does it mean my timer are running on 72Mhz?
See the Reference Manual - that will show how the timer's counter is clocked.
See also the clock configuration in CubeMX.
@Ash1 wrote:second I want to create a delay like 10millisec 100millisec e.g every 10ms interrupt will generate.
So that's a Timebase - as illustrated by @SofLit
2025-02-05 01:56 AM
Yes, your timer clock should be 72 MHz. If you can create an interrupt every 10 ms timer can be configured like this: PSC = 72 - 1 (counter frequency will be 1 MHz), ARR = 10000 - 1 (counter will be reloaded every 10 ms),
and for interrupt every 100 ms timer can be configured like this: PSC = 72 - 1 (counter frequency will be 1 MHz), ARR = 100000 - 1 (counter will be reloaded every 100 ms).
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.
2025-02-05 02:09 AM - edited 2025-02-05 02:11 AM
Hi
can you explain me this part ARR = 10000 - 1 and does this mean (PSC = 72 - 1) 71 value I have to load in prescaler register
2025-02-05 02:16 AM - edited 2025-02-05 02:25 AM
If value of ARR register (Counter period in CubeMX) is 9999 the counter will counts from 0 to 9999 (it is 10000 clock cycles in total with reload). So if the input frequency to the timer will be 1 MHz and you need reload the timer with frequency 100 Hz (period 10 ms) there should be ARR set to 9999 or 10000-1. And yes (PSC = 72 - 1) means that you should load 71 to prescaler register.
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.
2025-02-05 02:33 AM - edited 2025-02-05 03:05 AM
I am trying to run timer5 create interrupt for one minute .
I have loaded 59,999,999 in ARR register and 71 in pSC prescaler register is it correct?
I am sharing you screenshot of tim5 register at runtime but my timer is expiring at 50(approx.) second I have loaded prescaler and ARR as you said.
note: instead of one minute it is expiring before that...also I have configured CKD in (TIMx_CR1) as 00 is it correct too
Here is the screenshot - pasted into the post for easier viewing: