Question
Timer Arithmetic: What am I missing?
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) => 5000I 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);}