cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate ARR value for Timer TIM2, TIM3 in STM32F407

Ash1
Associate III

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

17 REPLIES 17
SofLit
ST Employee

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

 

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.
PS:
1 - This is NOT an online support (https://ols.st.com) but a collaborative space.
2 - Please be polite in your reply. Otherwise, it will be reported as inappropriate and you will be permanently blacklisted from my help.
Hl_st
ST Employee

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.

Ash1
Associate III

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.


@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 

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.

Ash1
Associate III

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

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.

Ash1
Associate III

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:

AndrewNeil_0-1738752305058.png