cancel
Showing results for 
Search instead for 
Did you mean: 

1min Timer

Ala
Senior

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?

9 REPLIES 9

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

JW

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
Senior

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

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

S.Ma
Principal

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

Yes.

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

JW

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

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

gbm
Lead II

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