Skip to main content
KKIM.6
Senior
October 28, 2022
Solved

I want to make some delay to the microcontroller based on the number of clock cycle. What API can I use it?

  • October 28, 2022
  • 2 replies
  • 1250 views

I'm trying to control the timing of ADC for best performance.

So, I want to give exact delay based on the number of clock cycles.

However, I don't know what API should I use to give delay.

Right know, I turned on TIM16 and I know how to get timer like below.

t = __HAL_TIM_GET_COUNTER(&htim16);

I also know HAL_Delay(ms); commend but sometimes it doesn't work.

Is there any method give delay to the microcontroller based on the number of clock cycle?

This topic has been closed for replies.
Best answer by Tesla DeLorean

Well doesn't the ADC have the ability to be directly triggered by a TIM, and thus a time base / periodicity of your choice?

With the TIM, 16 or 32-bit, you can use a delta measurement technique to observe elapsed time. The time will be a function of the TIM's clocking frequency via APB clock, or other source, and the Prescaler.

The TIM should be set in maximal mode, ie ARR = 0xFFFF or 0xFFFFFFFF, and you should use equivalent sized unsigned math on the number space.

Frequency and Time have some pretty well defined relationships. If you use a Prescaler to get 1 MHz, the TIM will count micro-seconds.

Most STM32 have a DWT unit with a CYCCNT register which is 32-bit, and counts at the MCU frequency. So obviously that resolves very well.

2 replies

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
October 28, 2022

Well doesn't the ADC have the ability to be directly triggered by a TIM, and thus a time base / periodicity of your choice?

With the TIM, 16 or 32-bit, you can use a delta measurement technique to observe elapsed time. The time will be a function of the TIM's clocking frequency via APB clock, or other source, and the Prescaler.

The TIM should be set in maximal mode, ie ARR = 0xFFFF or 0xFFFFFFFF, and you should use equivalent sized unsigned math on the number space.

Frequency and Time have some pretty well defined relationships. If you use a Prescaler to get 1 MHz, the TIM will count micro-seconds.

Most STM32 have a DWT unit with a CYCCNT register which is 32-bit, and counts at the MCU frequency. So obviously that resolves very well.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
October 31, 2022

In case of very low power needs, LPTIM maybe an alternate option.