2017-06-28 03:14 PM
Hi. I use Keil Compiler and HAL lib. I configure TIM4 as normal timer (time base counter) and implement Update event with HAL_TIM_PeriodElapsedCallback function. My initialization code below;
__HAL_RCC_TIM4_CLK_ENABLE();
TIM4_Handler.Instance=TIM4; TIM4_Handler.Init.Prescaler=16000; // 16 MHZ/16000 = 1000 Hz -> 1 ms inc/dec period TIM4_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; TIM4_Handler.Init.Period=1000; // 1msec x 1000 = 1 sec overflow inerrupt TIM4_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1; TIM4_Handler.Init.RepetitionCounter=0; HAL_TIM_Base_Init(&TIM4_Handler);When I run code like above, UEV interrupt occure in 1 sec. This is normal
But when I change clock division suc as TIM_CLOCKDIVISION_DIV4, this period is not affected. There is an UEV interrupt in 1 sec.
What is the problem? (Note that my APB1/2 clock is 16MHZ)
2017-06-29 02:49 AM
hi,
It works as expected!
''Clock division'' has nothing to do the clock prescaler.(okay, terminology is quite confusing!)
The CKD(clock division) bits and PSC bits(prescaler bits) have different meanings,
The former is about dead time and filter settings, it has nothing to do with prescaling as the name suggests.
Check the datasheet!
Zt
2017-06-29 03:13 AM
by the way,
your prescaler should be 16000-1
2017-06-29 11:57 AM
thank you Mr. LIU
2018-03-04 07:02 AM
Thanks! I was also confused!
But the prescaler value of 16000 is right, it is the clock divider, so 16000000/16000 = 1 KHz freq = 1 ms.The Period should be XX - 1, like 1000 - 1 = 999 for 1 sec.
Cheers
2018-03-04 09:23 AM
Both Prescaler and Period values are expressed as N-1
Update Rate = TIMCLK / (P * Q)
Where
TIM4_Handler.Init.Prescaler = Q - 1;
TIM4_Handler.Init.Period = P - 1;
2018-03-04 09:33 AM
But the prescaler value of 16000 is right, it is the clock divider, so 16000000/16000 = 1 KHz freq = 1 ms.
No.
JW
2018-03-04 10:51 AM
Good, now it is clear, thanks for the update!