cancel
Showing results for 
Search instead for 
Did you mean: 

Microseconds Delay using Timer

bendkhilarwa
Associate III
Posted on December 24, 2017 at 20:31

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

#stm32-timer #timer-delay #tim-counter #timer
3 REPLIES 3
Posted on December 24, 2017 at 22:33

Such an interrupt rate is undesirable, just use the TIM count to observe elapsed time in microseconds or CPU ticks.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bendkhilarwa
Associate III
Posted on December 24, 2017 at 23:07

Thanks for your quick response.

I think that when removing the Tim interrupt, it will be mandatory to use a blocking while instruction.

Have you please an other idea to enssure a non-blocking microseconds delay? 

Posted on December 24, 2017 at 23:22

And how is that any different from spinning in a loop like you are now? Except you aren't burning cycles entering/exiting interrupt context, and loading instructions that do very little useful work.

At 32 MHz, you have 320 cycles to use every 10us, look very carefully at what you are doing, especially how much gets burned doing software division, and wading in/out of the HAL abstraction.

Clock the TIM at 32 MHz, and watch TIM->CNT advance 320 ticks. Shave off a few ticks to account for entry/exit, tune/benchmark by toggling a GPIO and scoping.

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