2025-01-23 02:24 AM
Hello.
I need 9 channel ADC data on STM32F030 MCU.
I want to read ADC CH0, ADC CH1.... ADC CH8 in order. (PA0, PA1, .... PA7, PB0)
It will be a simple application. I don't need DMA.
I could not change the channel in Regular Conversion. I tried to read two channels to try. I always read the data of channel 0. I have a code like this.
For testing I wanted to read PA0 and PA1 data.
/*Channel 0 (PA0) Selecting. */
ADC_CH_Cfg.Channel = ADC_CHANNEL_0; //Channel Select
ADC_CH_Cfg.Rank = ADC_RANK_CHANNEL_NUMBER;
ADC_CH_Cfg.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &ADC_CH_Cfg) != HAL_OK) //Kanal Ayarlaniyor
{
Error_Handler();
}
if (HAL_ADC_Start(&hadc) != HAL_OK) //ADC Start
{
Error_Handler();
}
if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) != HAL_OK)//Donusumun tamamlanmasini bekle
{
Error_Handler();
}
ADC_Data[0]=HAL_ADC_GetValue(&hadc); //Get ADC value
HAL_ADC_Stop(&hadc);
HAL_Delay(100);
/*Channel 1 (PA1) Selecting. */
ADC_CH_Cfg.Channel = ADC_CHANNEL_1; //Channel Select
ADC_CH_Cfg.Rank = ADC_RANK_CHANNEL_NUMBER;
ADC_CH_Cfg.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &ADC_CH_Cfg) != HAL_OK)
{
Error_Handler();
}
if (HAL_ADC_Start(&hadc) != HAL_OK) //ADC Start
{
Error_Handler();
}
if (HAL_ADC_PollForConversion(&hadc, HAL_MAX_DELAY) != HAL_OK)//Donusumun tamamlanmasini bekle
{
Error_Handler();
}
ADC_Data[1]=HAL_ADC_GetValue(&hadc); //Get ADC value
HAL_ADC_Stop(&hadc);
HAL_Delay(100);
My ADC settings are as follows.
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();
}
What could be the problem? ADC always outputs Channel 0 data.
2025-02-03 08:29 AM
Hello,
the easiest way how to switch between reading each ADC channels is to use direct register control as it is in this example:
/* read analog value from PA0 */
MODIFY_REG(ADC1->CHSELR,0xFFFFFF,ADC_CHANNEL_0);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
uint8_t res = HAL_ADC_GetValue(&hadc1);
printf("resA0:%d\n\r", res);
HAL_ADC_Stop(&hadc1);
/* read analog value from PA1 */
MODIFY_REG(ADC1->CHSELR,0xFFFFFF,ADC_CHANNEL_1);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
res = HAL_ADC_GetValue(&hadc1);
printf("resA1:%d\n\r", res);
HAL_ADC_Stop(&hadc1);
where is used the same ADC configuration as in your code
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.