2015-12-11 6:00 AM
Hello
I'm having a hard time getting multiple ADC's working on a STM32F I have 3 analog voltages I would like to read with the MCU. But not continuous. I would like to take 1 sample at a time and at different intervals. I use the HAL library for this project and it seems very hard to select a channel and take a reading. I use CubeMX for the initialisation and this is what it returns:void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC;
hadc.Init.Resolution = ADC_RESOLUTION12b;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.EOCSelection = EOC_SINGLE_CONV;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = ENABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.Overrun = OVR_DATA_PRESERVED;
HAL_ADC_Init(&hadc);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_7;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_8;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_VREFINT;
HAL_ADC_ConfigChannel(&hadc, &sConfig);
}
Then in the main function I use this to retreive a sample:
/* USER CODE BEGIN 3 */
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 1000);
adcSample = HAL_ADC_GetValue(&hadc);
HAL_ADC_Stop(&hadc);
}
/* USER CODE END 3 */
But this always returns the value of adc_channel_1 on PA0
What is the correct way to retrieve a sample from another channel?
My sincerely,
#adc #hal #f0
2016-02-16 11:11 AM
I'm still not able to do single conversion on single channel. Is there a simple way to do adc conversion without the use of interrupts. Just polling between channels?
2016-11-03 3:17 PM
I'm having a similar problem. I thought that if I declared a ADC_ChannelConfTypeDef variable and called the HAL_ADC_ConfigChannel() function before trying to start the ADC and get a value, that might help, but I've not had any luck so far.
Talked to my friend and he said use DMA. However, it seems silly that HAL can't change channels.We’re moving the ST Community to a new platform to give you a better and more reliable community experience.