cancel
Showing results for 
Search instead for 
Did you mean: 

Waiting indefinitely for an ADC conversion to complete

skon.1
Senior

Hello,

In my application I want to get the ADC voltage - waiting indefinitely until the operation completes.  Will this code work ?

while ( HAL_ADC_PollForConversion ( & hadc1 , 1000 ) == HAL_TIMEOUT );

14 REPLIES 14
prain
Senior III

No need to external while loop, HAL_ADC_PollForConversion has a while loop inside. Just use

HAL_ADC_PollForConversion ( & hadc1 , 1000 );

MCU waits at this line until EOC flag sets.

prain
Senior III

After conversion completed, use HAL_ADC_GetValue to read ADC value.

What does the "1000" value do inside the function ? Isn't it the timeout ?

I want it to wait indefinitely - not just 1000 cycles...

TDK
Guru

To wait indefinitely, use HAL_MAX_DELAY as the delay. This is #defined as 0xFFFFFFFF.

The code you linked will also work. I feel like I wrote that in a post not too long ago which I can't find.

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

The timeout value can be a uint32_t and the unit is milliseconds not cycles.

Thanks,

But either way, my goal is indefinite timeout.

So what I thought about is using:

while ( HAL_ADC_PollForConversion ( & hadc1 , 1000 ) == HAL_TIMEOUT );

Will the above code case an infinite loop until a conversion is finished ? This is what I want...

If yes, I don't really understand prain's comment:

"No need to external while loop, HAL_ADC_PollForConversion has a while loop inside"

Without the external loop - won't the internal while loop inside "HAL_ADC_PollForConversion" exit after the timeout is reached ?

your approach is correct but I can not figure out why you need to wait infinitely.​

Because I need to wait for the ADC to give me a conversion result...no matter how long it takes.

What are you suggesting to do instead ?

> What are you suggesting to do instead ?

@skon.1​ 

I answered you yesterday. See below.

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