Electronic – How to read from multiple channels of the ADC on an STM32F407

adchal-librarystm32cubemxstm32f4-discovery

I am working on a project wherein, I need to read the Analog outputs from 4 sources and convert them to a digital value using a single ADC module on the STM32F407 microcontroller. I want to sample the ADC values every 50ms and therefore have decided to use it in polling mode and have a timer Interrupt every 50ms to trigger the ADC reading. I am using the STM32 CubeMX software to generate initializations for me.

Here are the ADC initializations:

hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.NbrOfDiscConversion = 0;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = DISABLE;

And in my main function:

while (1)
{
/* USER CODE END WHILE */
g_ADCValue = HAL_ADC_GetValue(&hadc1);
}

Now this works when I have one ADC channel enabled, but how do I read from more than 1 channel at the same time? How does the GetValue() function return the ADC value of a certain channel? Also I know I do not want to use ADC interrupts for this as I want to sample the ADC at particular intervals of time (every 50 ms), but should I be using DMA? If so, how would I do that?

Thank you very much for all your help!

Best Answer

hadc1.Init.ScanConvMode = DISABLE;

the sequencer is disabled so you cant read or even convert data coming from the other channel.
Also you have to define the rank of every channel.
for exemple :

sConfig.Rank      = 1;
     sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5 ;
     sConfig.Channel = REF_1_5_Volt_Pin;
     HAL_ADC_ConfigChannel(&hadc, &sConfig);
 sConfig.Rank = 2;
 sConfig.Channel = L1_voltage_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig);

 sConfig.Rank = 3;
 sConfig.Channel = L2_voltage_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig);

 sConfig.Rank = 4;
 sConfig.Channel = L3_voltage_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig);

 sConfig.Rank = 5;
 sConfig.Channel = L1_curent_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig);

 sConfig.Rank = 6;
 sConfig.Channel = L2_curent_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig);

 sConfig.Rank = 7
 sConfig.Channel = L3_curent_Pin;
 HAL_ADC_ConfigChannel(&hadc, &sConfig)