cancel
Showing results for 
Search instead for 
Did you mean: 

TIM2 Interrupt issue

tybandara
Associate II
Posted on December 11, 2012 at 09:30

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

#adc-and-tim2-interrupt. #discovery-board
1 REPLY 1
tybandara
Associate II
Posted on December 11, 2012 at 11:41

Ok. I found the solution. I cleared the timer interrupt flag before enabling interrupt and it's working.

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);              //Added this

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);