2022-01-23 04:09 AM
hey there
here I try to demonstrate a 1minute Timer using TIM14. I'm using STM32F030k6t6 which has 44236.8MHz clock for timers.
below is my TIM14 config:
htim14.Instance = TIM14;
htim14.Init.Prescaler = 60000;
htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
htim14.Init.Period = 44236;
htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
{
Error_Handler();
}
and then after MX_TIM14_Init(); I start it with
if(HAL_TIM_Base_Start_IT(&htim14)!=HAL_OK)
{
Error_Handler();
}
but the thing is just as I start the program, interrupt happens and I dont know why? just as I start my program, 1minute is not passed, then why this happens?
2022-01-23 05:33 AM
Cube/HAL sets the TIMx_EGR.UG bit, resulting in Update interrupt.
JW
2022-01-23 09:20 PM
now I try to understand whats going on... but anyways, how can I fix it? I mean I read the referenced page you Taged, but really what is the code which I should use?
2022-01-23 09:42 PM
eventually I used this command after MX_TIM14_Init() and before starting TIM14
TIM14->SR &= 0xFFFFFFFE;
and it worked! was this the hint you ment?@Community member
2022-01-23 10:30 PM
The programming sequencd might be wrong and cause a dummy interrupt too early, so clearing pending interrupt is a patch.
2022-01-23 10:31 PM
For minute delays, better look at RTC alarm feature...
2022-01-23 10:51 PM
Yes.
Don't use &= to clear TIMx_SR bits, use direct write.
JW
2022-01-24 12:11 AM
sure, but the thing is that this particular board does not have RTC crystal or backup battery, therefore I'm confined to Timers...
2022-01-24 12:11 AM
what's the difference? you mean TIM14->SR=0;?
2022-01-25 12:54 AM
TIMx->SR = ~TIM_SR_UIF
means: clear UIF flag
TIMx->SR &= ~TIM_SR_UIF
means: clear UIF flag and the flags for all other timer interrupts triggered during the execution of this statement, effectively missing these interrupts