AnsweredAssumed Answered

Microseconds Delay using Timer

Question asked by ben_dkhil.arwa on Dec 24, 2017
Latest reply on Dec 24, 2017 by Clive One

Hi all,

I'm trying to developpe microseconds delay based on STM32L0. I resort to use timer, which will generate an interrupt once the delay_time will be elaspsed. 

  • I have configured the Timer as follow to generate an interrupt every  10 microseconds.

                    /* Set TIM7 instance */
                     OneWireTimHandle.Instance = TIM7;
                     OneWireTimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
                     OneWireTimHandle.Init.Prescaler = 31;
                     OneWireTimHandle.Init.Period = 9;
                     /* Peripheral interrupt init */
                     HAL_NVIC_SetPriority(TIM7_IRQn, 0, 0);

  • Then, I have initiate a variable which will be incremented in each Timer interrupt: 

                   void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)


  • The delay function will be used to generate a delay time which is multiple of 10 microseconds:                                          void Delay_us(uint32_t micro)
                 TimCall =0;
            }-> To test this solution, I have included the following code to the main program and I have displayed the GPIO_LED signal on the scope.           while(1)

The GPIO_LED signal displayed on the scope is not well adjusted, the LED is blinking in every 12 or 13 microseconds instead of 10 microseconds.

Did any one know the cause of this issue? 


Best Regards.