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.

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

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

                   void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
                   {
                           TimCall++;

                   }

  • The delay function will be used to generate a delay time which is multiple of 10 microseconds:                                          void Delay_us(uint32_t micro)
            {
                 HAL_TIM_Base_Start_IT(&OneWireTimHandle);
                 while(TimCall<(micro/10));
                 TimCall =0;
                 HAL_TIM_Base_Stop(&OneWireTimHandle);
            }-> 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)
               {
                    BSP_LED_On(LED2);
                    Delay_us(10);
                    BSP_LED_Off(LED2);
                    Delay_us(10);
              }

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.

Arwa

Outcomes