AnsweredAssumed Answered

STM32F4 configure TIMER2 for 1microsec interrupt

Question asked by arbitrer on Nov 10, 2014
Latest reply on Nov 18, 2014 by Clive One
Hi

I want to generate a timer interrupt after every 1micoseconds. I am using TIMER2 for this purpose.So I took a code from one site and modified for my purpose.
But the issue is I cant get an interrupt of 1microsecond.To ensure this I toggled a gpio in the ISR and verified the frequency on DSO and it was found to be 806Khz.

Is there anything that I'm doing wrong in the code?

void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  {
        count++;
      TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        flag_for_state_machine=1;
        if(count%2 == 0)
            GPIO_ResetBits(GPIOD, GPIO_Pin_12);
        else
            GPIO_SetBits(GPIOD, GPIO_Pin_12);
  }
}

void configure_d1_d2_d3(void)
{
        /* Enable the GPIO_LED Clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void INTTIM_Config(void)
{
  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 = 1;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  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)
{    
        configure_d1_d2_d3();
        INTTIM_Config();
        while(1);        
}

Outcomes