Skip to main content
Explorer
February 5, 2025
Solved

Calculate ARR value for Timer TIM2, TIM3 in STM32F407

  • February 5, 2025
  • 3 replies
  • 3936 views

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

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

    Read The Fine Document - the Reference Manual, GPIO section. The only register needed for simple output setup is MODER; you don't need to touch the others.

     

    Regarding the timer setup: why should you calculate the values? Leave it to the compiler. For simple timebase setup:

    #define TIMCLK_FREQ 84000000u

    #define TIM_PERIOD_us 1000u

    TIMx->PSC = TIMCLK_FREQ / 1000000u - 1;

    TIMx->ARR = TIM_PERIOD_us - 1;

     

    3 replies

    Technical Moderator
    February 5, 2025

    Hello,

    For which purpose? time base?

    Inspire from:

    https://github.com/STMicroelectronics/stm32f4xx-hal-driver/blob/4ae1c83b8aa574042fee644688d406b2429a95d6/Src/stm32f4xx_hal_timebase_tim_template.c

     

     

     /* 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

     

    Super User
    February 5, 2025
    ST Employee
    February 5, 2025

    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.

    Ash1Author
    Explorer
    February 5, 2025

    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.

    Super User
    February 5, 2025

    @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 @mƎALLEm