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.