cancel
Showing results for 
Search instead for 
Did you mean: 

30us delay with some constraints

avbST
Associate II

Hi

I am using Timer2 with Prescaler 83 and up counter of period 49 to be triggered after every 50us. But I am using an ADC and I want to turn it on every 330us. So after 6 interrupt triggers, I turn on the ADC but I need to generate a 30us delay before each ADC turn ON. I want this to occur infinitely in a loop. I have tried using a counter function as below

void ADC_Delay(uint16_t us)
{
  __HAL_TIM_SET_COUNTER(&htim2, 0);
  while (__HAL_TIM_GET_COUNTER(&htim2) < us);
}

 But I am seeing this delay only on the first loop and not getting the delay in subsequent loops before ADC is turned ON. I have tried various methods even using DWT count and I am still facing the same issue. Please help me solve this bug.

Thanks & Regards

Srinivas

Edited by ST moderator to be inline with the community rules especially with the code sharing. In next time please use </> button to paste your code. Please read this post: How to insert source code

6 REPLIES 6
TDK
Super User

If you want to do something every 330 us, why not set the timer period to 330 us?

Don't set the timer counter--leave it alone and measure elapsed time based on the difference between two successive readings.

If you feel a post has answered your question, please click "Accept as Solution".
avbST
Associate II

Hi @TDK 

There are other functionalities which are dependent on the timer being 50us hence I need to fix the timer interrupt at 50us. Is there any way to achieve this without using another timer and with 50us timer.

Thanks & Regards

Srinivas

 

Well resetting the count in your delay routine isn't entirely compatible with maintaining a 50us pace

Delays using timer delta are best done with a maximal count.

Probably not going to be magic answers here. Would suggest using another TIM, with the appropriate pacing, perhaps using it as a direct trigger.

With one TIM you can perhaps use the CCRx to trigger mid-cycle, ie after 6 full cycles (update), set one of the channels to the 30us count.

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

Measure the current count, and wait until 30 more counts have elapsed. Handling overflow makes it slightly more complicated but not unmanageable.

If you feel a post has answered your question, please click "Accept as Solution".
Saket_Om
ST Employee

Hello @avbST 

If you call this inside the Timer2 interrupt, you are resetting the timer counter, which can disrupt the timer’s periodic interrupt generation and cause unpredictable behavior. To avoid such issues, it is best to use Timer2 exclusively for generating regular 50 µs interrupts, and employ a separate timer, such as Timer3, for precise microsecond delays. In this setup, Timer2 should run continuously without being reset, and on every sixth interrupt (corresponding to 330 µs), you can start a one-shot 30 µs delay using Timer3. Once Timer3 completes this delay, the ADC can be triggered. This method ensures reliable timing and prevents conflicts between periodic interrupts and delay operations.

Alternatively, another solution is to use compare match events, allowing a single timer instance to handle both intervals by setting the auto-reload register (ARR) for the 330 µs period and the compare register (CMP1) for the 50 µs delay. In this approach, the compare match value would need to be recalculated dynamically each time the delay expires.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
Dor_RH
ST Employee

Hello @avbST,

You could try using a timer with a 10 µs period and count interrupts in the callback.

  • Every 5 interrupts, you get 50 µs to trigger your main event.
  • For the 30 µs ADC delay, count 3 interrupts before turning it on.

This approach ensures accurate timing without blocking delays and works reliably in a loop.

I hope my answer has helped you. When your question is answered, please select this topic as the solution that answered you, as it will help others find that answer faster.

Thanks for your contribution.

Dor_RH