cancel
Showing results for 
Search instead for 
Did you mean: 

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

KKIM.6
Senior

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?

1 ACCEPTED SOLUTION

Accepted Solutions

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

2 REPLIES 2

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

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