cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 generate interrupt on 0.5 microsecond using timer

hariprasad
Associate III
Posted on November 18, 2014 at 14:39

Hi

I am trying to generate timer interrupt on each 0.5microseconds. But as of now I was only able to generate timer interrupt on each 1microsecond.

below is my code

void TIM2_IRQHandler(void)

{

  if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

  {

    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

    GPIO_ToggleBits(GPIOD, GPIO_Pin_6);  

    }    

}

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 = ((SystemCoreClock / 4) / 1000000) - 1;  // 1 MHz (1us)

  TIM_TimeBaseStructure.TIM_Prescaler = 0;//84 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)

  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)

{

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    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);

    GPIO_ResetBits(GPIOD, GPIO_Pin_6);  

    INTTIM_Config();

    while(1);

}

I tried changing the TIM_Period value but still not able to obtain what I want.

How can I achieve this?

#stm32f4 #discovery #timers
2 REPLIES 2
stm322399
Senior
Posted on November 18, 2014 at 14:49

Running that fast looks like to be a design flaw.

Anyway, if you want your code to run that fast, you need to optimize you interrupt routine TIM2_IRQHandler. The question to answer is ''are 500µs enough to run that amount of code ?''

Considering your use of library functions, I would say that you have some margin to improve the situation. Replace library calls with direct registers accesses. Try to look at the generated code and try to follow the execution path. The interrupt routine only requires one status read, a test, one status register write, and the toggling needs a maximum of 2 GPIO registers accesses. I would say that it can be generated in less than 40 instructions, which will help to meet the tight timing requirement.

--

Laurent

Posted on November 18, 2014 at 15:10

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/STM32F4%20configure%20TIMER2%20for%201microsec%20interrupt&currentviews=44]Previous Thread

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..