AnsweredAssumed Answered

TIM2 Interrupt issue

Question asked by tybandara on Dec 11, 2012
Latest reply on Dec 11, 2012 by tybandara
Hello all experts,

I configured the TIM2 in my STM32F4 discovery board such that it gives me interrupts every 50ms. The code is as follows.
*********************************
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        GPIO_ToggleBits(GPIOA, GPIO_Pin_5);
}}
*********************************
void INTTIM_Config(uint16_t numOfMilleseconds)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = (500*numOfMilleseconds) - 1;  // 1 MHz down to 1 KHz (1 ms)
  //TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
    TIM_TimeBaseStructure.TIM_Prescaler = 168-1; //168MHz Clock should be down to 1MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* TIM IT enable */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}

*******************************************

int main(void)
{
    GPIO_config();
    INTTIM_Config(50);
  while (1)
  {  
  }
}
****************************************************

- I'm checking the PA5 signal with the oscilloscope and what I expect is to toggle the signal in each 50ms interval.
- My problem is when I check this with the scope, I see that when I run TIM_Cmd(TIM2, ENABLE); command, program jumps to the interrupt routine.
- As a summary, interrupt routine is executed when I enable the time.

Can someone please help me with this issue.

Thank you for spending your valuable time.

Regards,
Thilina

Outcomes