Skip to main content
Ala
Senior
Jan 23, 2022 12:09 PM UTC
Question

1min Timer

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?

This topic has been closed for replies.

9 replies

waclawek.jan
Super User
Jan 23, 2022 1:33 PM UTC

Cube/HAL sets the TIMx_EGR.UG bit, resulting in Update interrupt.

JW

Ala
AlaAuthor
Senior
Jan 24, 2022 5:20 AM UTC

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?

Ala
AlaAuthor
Senior
Jan 24, 2022 5:42 AM UTC

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​ 

S.Ma
Principal
Jan 24, 2022 6:30 AM UTC

The programming sequencd might be wrong and cause a dummy interrupt too early, so clearing pending interrupt is a patch.

S.Ma
Principal
Jan 24, 2022 6:31 AM UTC

For minute delays, better look at RTC alarm feature...

Ala
AlaAuthor
Senior
Jan 24, 2022 8:11 AM UTC

sure, but the thing is that this particular board does not have RTC crystal or backup battery, therefore I'm confined to Timers...

waclawek.jan
Super User
Jan 24, 2022 6:51 AM UTC

Yes.

Don't use &= to clear TIMx_SR bits, use direct write.

JW

Ala
AlaAuthor
Senior
Jan 24, 2022 8:11 AM UTC

what's the difference? you mean TIM14->SR=0;?

gbm
Super User
Jan 25, 2022 8:54 AM UTC

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

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice