Search code examples
microcontrollerstm32adchal

How to read multichannels ADC Value?


I can read ADC value from ADC1_IN1 channel with this code ADCValue = HAL_ADC_GetValue(&hadc1); but ADC1 port (you can see on picture) has many channels. How can I read values from ADC1_IN2 ? How can I describe channel 2 in code ?

ADC1 port


Solution

  • You can set ADC Channel by HAL_ADC_ConfigChannel function :

    ADC_ChannelConfTypeDef sConfig;
    sConfig.Channel = ADC_CHANNEL_2; // ADC Channel
    sConfig.Rank = 1; //Rank (1-16) Rank: The rank in the regular group sequencer.
    sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES; //ADC Sampling Times
    sConfig.Offset = 0; // Reserved
    
    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
        //Error
    }
    

    Description of STM32F4xx HAL drivers