AnsweredAssumed Answered

Interrupt on CEN bit setting in TIM7

Question asked by uvarov.alexander on Mar 6, 2013
Latest reply on Mar 15, 2013 by waclawek.jan
Hello everybody!
I'm using timer TIM7 on STM32F407 to generate an interrupt on overflow. It works fine except one thing. When I enable the counter at first time it rises an interrupt immediately, right after setting the CEN bit in TIM7_CR1 register. Following iterations are ok.

Does anybody know what's the problem?

Initialization code
void TIM7_Init(void)
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
  
  TIM7->CR1 |= TIM_CR1_OPM | TIM_CR1_URS; //One-pulse mode and Update generation only on Counter Overflow
  TIM7->DIER |= TIM_DIER_UIE;   // Interrupt generation on Counter Overflow
  TIM7->PSC = 1600;      //With HSI frequency 16MHz gives a 100us period
  
  NVIC_InitStruct.NVIC_IRQChannel = TIM7_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVIC_InitStruct);
}

PushButton Handler
void EXTI0_IRQHandler(void)
{
  for (volatile uint32_t cnt=200000; cnt!=0; cnt--);  //wait until bounce stops
  EXTI_ClearITPendingBit(EXTI_Line0);          //reset pending state
  GPIO_ToggleBits(GPIOD, GPIO_Pin_12); //LED

  TIM7->ARR = 10000;               //delay 1s
  TIM7->CR1 |= TIM_CR1_CEN; //start counting
}

Overflow handler
void TIM7_IRQHandler(void)
{
  TIM7->SR &= 0xFFFFFFFE;               //reset UIF flag
  GPIO_ToggleBits(GPIOD, GPIO_Pin_15);     //LED
}

Outcomes