2021-08-05 06:07 AM
Hi,
I am trying to poll multiple channels of the ADC but always get the result from channel 0.
The other channels only work if I use them one at a time.
Pretty sure it is a setting and would appreciate your help.
Thank you in advance!
Using a STM32F070.
Here is my code:
static void MX_ADC_Init(void)
{
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 1 */
/* USER CODE END ADC_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
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_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_1;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_4;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
// once every second I call this function in a loop (starting with channel 0 and going up to 2)
uint16_t MX_ADC_Start(uint32_t channel)
{
ADC_ChannelConfTypeDef sConfig;
uint16_t uhADCxConvertedValue = 0;
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = channel;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
if (HAL_ADC_Start(&hadc) != HAL_OK)
{
/* Start Conversation Error */
Error_Handler();
}
if (HAL_ADC_PollForConversion(&hadc, 10) != HAL_OK)
{
/* End Of Conversion flag not set on time */
Error_Handler();
}
else
{
uhADCxConvertedValue = HAL_ADC_GetValue(&hadc);
}
return uhADCxConvertedValue;
}
//The return value is always the one from channel 0 even if I loop through all other channels as well.
2021-08-05 06:18 AM
Channels are converted on order. If you want to convert channel 1, you'll need to ensure channel 0 is not selected for conversion in the CHSELR register.
2021-08-05 07:57 AM
I thought this was done in HAL_ADC_ConfigChannel?
There the channel in binary format seems correct with 10011 ( channel 0, 1 and 4).
But this is a bitwise OR operation so the channels will not be cleared if setting another channel.
after doing this manually it worked perfectly:
hadc.Instance->CHSELR = 0;
Thank you for your help!