Skip to main content
hbarta2
Associate III
March 10, 2015
Question

Timer Arithmetic: What am I missing?

  • March 10, 2015
  • 3 replies
  • 812 views
Posted on March 10, 2015 at 16:53

I want a timer to generate events at 2KHz. From the manual I see that TIM6 uses TIMxCLK for its input. TIMxCLK is the processor clock, right? My H/W is a STM32F428I-DISCO board using a STM32CubeMX generated project with unmodified clock yielding a 168 MHz clock. I have fiddled with TIM6 Prescaler and Counter Period to get a clock that counts at twice the frequency as the SysTick (as reported by HAL_GetTick().) The resulting values are Prescaler=4 and Counter Period=8400. I thought the following calculation would yield the timer frequency:

(SysTick /(Prescaler * Counter Period) or (168000000/(4*8400) => 5000

I would have thought that TIM6 would count at 5KHz and not 2KHz. 

What do I have wrong? I'm happy that I have the right results but not happy that I don't understand the numbers.

Thanks!

Edit: Here's the timer init code:

/* TIM6 init function */

void MX_TIM6_Init(void)

{

  TIM_MasterConfigTypeDef sMasterConfig;

  htim6.Instance = TIM6;

  htim6.Init.Prescaler = 4;

  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim6.Init.Period = 8400;

  HAL_TIM_Base_Init(&htim6);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig);

}

    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    March 10, 2015
    Posted on March 10, 2015 at 17:09

    TIM clocks are not the processor clock. They are APB clocks, nominally x2 except for the DIV1 case. See Clock Tree Diagram

    On your general F4 TIM's on APB1 clock at 84 MHz, and on APB2 at 168 MHz

    Period and Prescaler are N-1 values, where the counter has N states, ie 0..N-1

    84000000 / (5 * 8401) = 1999.76193

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hbarta2
    hbarta2Author
    Associate III
    March 10, 2015
    Posted on March 10, 2015 at 17:14

    Thank you. I thought that one of the clocks on the Clock Configuration page might be part of this but was unable to figure out the connection.

    Tesla DeLorean
    Guru
    March 10, 2015
    Posted on March 10, 2015 at 17:14

    2 KHz from 84 MHz

    htim6.Init.Prescaler = 5 - 1;

    htim6.Init.Period = 8400 - 1;

    5 KHz from 84 MHz

    htim6.Init.Prescaler = 4 - 1;

    htim6.Init.Period = 4200 - 1;

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..