Skip to main content
Explorer II
August 14, 2024
Solved

ADC Polling for multiple channel not working

  • August 14, 2024
  • 1 reply
  • 1067 views

i am trying to use multiple channel on a single ADC to measure certain voltages. what i have noticed is that let say i have selected 3 channels now in configuration of cube MX i select no of conversion to 3 and as soon as i do that Ranks get added below where i can select channel for each rank and choose that.

Code:

HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1,HAL_MAX_DELAY);
float voltage1 = HAL_ADC_GetValue(&hadc1)*(3.3/4096);
HAL_ADC_PollForConversion(&hadc1,HAL_MAX_DELAY);
float voltage2 = HAL_ADC_GetValue(&hadc1)*(3.3/4096);
HAL_ADC_PollForConversion(&hadc1,HAL_MAX_DELAY);
float voltage3 = HAL_ADC_GetValue(&hadc1)*(3.3/4096);

now when i run this code i get same output for all the conversion. the output is of the channel with rank 3. If i set the channel with rank 3 to ground i get output as 0 for all the conversion as is the channel with last rank is dominating over other. Is there something wrong in my configuration or the coding part.

PS: i am using STM32F103ZGT6 which has 12 bit ADC resolution

 

cube MX configuration:

Screenshot 2024-08-14 103128.png

    This topic has been closed for replies.
    Best answer by TDK

    Use DMA to convert multiple ADC channels. The CPU is not going to keep up with the rate of conversion in general, but especially when the sampling time is only 1.5 cycles.

    1 reply

    TDKAnswer
    Super User
    August 14, 2024

    Use DMA to convert multiple ADC channels. The CPU is not going to keep up with the rate of conversion in general, but especially when the sampling time is only 1.5 cycles.

    rahul7515Author
    Explorer II
    August 14, 2024

    is that the only way? DMA. i mean all want is to read data when i am asking it, I can change the sampling time if needed 

    Super User
    August 14, 2024

    > is that the only way?

    That is what the reference manual says. It's certainly the most straightforward way and least likely to cause issues.

    TDK_0-1723640667469.png

    Possibly you could read ADC->DR quickly enough, with a slow enough sampling rate, with interrupts disabled, you could poll for the values.

     

    Another method would be to convert a single channel and change which channel is being converted after each conversion.