What is the purpose of HAL_ADC_PollForConversion in STM32 HAL ADC code?

So I've been experimenting with the STM32 (F103RB) for a few weeks, but I'm unclear about the purpose of the HAL_ADC_PollForConversion function. It seems to have no effect on the ADC readings in my code. Here’s an example: The 1st (with PollForConversion):
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

uint32_t value = HAL_ADC_GetValue(&hadc1);
float voltage = 3.3f * value / 4096.0f;

printf("ADC = %lu (%.3f V)\n", value, voltage); // send the value via UART
HAL_Delay(250);
}
while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

uint32_t value = HAL_ADC_GetValue(&hadc1);
float voltage = 3.3f * value / 4096.0f;

printf("ADC = %lu (%.3f V)\n", value, voltage); // send the value via UART
HAL_Delay(250);
}
Then the 2nd (without PollForConversion):
while (1)
{
HAL_ADC_Start(&hadc1);
//HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

uint32_t value = HAL_ADC_GetValue(&hadc1);
float voltage = 3.3f * value / 4096.0f;

printf("ADC = %lu (%.3f V)\n", value, voltage); // send the value via UART
HAL_Delay(250);
}
while (1)
{
HAL_ADC_Start(&hadc1);
//HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);

uint32_t value = HAL_ADC_GetValue(&hadc1);
float voltage = 3.3f * value / 4096.0f;

printf("ADC = %lu (%.3f V)\n", value, voltage); // send the value via UART
HAL_Delay(250);
}
From what I can see, whether I include PollForConversion or not, the UART readings appear the same (although I’m not entirely sure if they truly are). Continuous mode is disabled. Could someone clarify what I might be missing?
1 Reply
Dtynin
Dtynin5mo ago
@Sterling the HAL_ADC_PollForConversion function makes sure the ADC conversion is finished before you grab the value. Without it, you might get an incomplete reading, which can lead to errors. Even though your readings seem okay without it, that's likely because of the delay in your loop. Using PollForConversion just guarantees you're always getting accurate results.
Want results from more Discord servers?
Add your server