Occasionally reading channel 0 when expecting channel 1 of ADC sequence. Why?
I have a fairly simple STM32F051R8 system reading an ADC sequence with two channels. I configured the system using CubeMX (v5.1.0, linux) with continuous and discontinuous modes disabled. I am polling the ADC in the simplest way imaginable.
At times (5-10 seconds), I get the value for the first channel when reading the second. I have never seen the opposite (but it could be happening without me catching it). To test this, I am placing different DC values on each channel and logging the read values.
I have tried various modes (continuous, discontinuous), EOC vs. EOS, without too much success. I found that the most stable sampling was performed this way (for polling at least).
Any ideas why the ADC is not behaving as expected? What am I doing wrong with this simple ADC polling method?
Thanks for the help!
Here is my ADC config generated from CubeMX:
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Cofigure for the selected ADC regular channel to be converted. */
sConfig.Channel = ADC_CHANNEL_11;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {
Error_Handler();
}My sampling code is:
HAL_ADC_Start(pAdcInst);
for (idx = 0; idx < 2; idx++)
{
status |= HAL_ADC_PollForConversion(pAdcInst, 100);
g_adc_values[idx] = HAL_ADC_GetValue(pAdcInst);
}
HAL_ADC_Stop(pAdcInst);