0
\$\begingroup\$

I'm trying to read multiple ADC value one after the other using the continuous scan method. I have fallowed quite a few tutorial and example, but nothing seems to work for me. I am using the STM32L100C-Discovery board. Here is my code, I'll be grateful if someone can tell me what I am doing wrong. Everything seems to work fine, but when I try to evaluate the voltage, I only get wrong values... If I put 3.3V on PA0, adc1 gets me a value of 1500 so around 1.4 V which makes no sense. The value on adc1, adc2,... are all quite different as well as they are floating pin.

int main(void)
{

HAL_Init();

MX_ADC_Init();
int adc0, adc1, adc2, adc3, adc4, adc5, adc6;
while (1)
{
        HAL_ADC_Start(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc1 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc2 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc3 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc4 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc5 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_PollForConversion(&hadc, 40);
        adc6 = HAL_ADC_GetValue(&hadc);
        HAL_ADC_Stop(&hadc);
}

And the ADC_init is :

static void MX_ADC_Init(void)
{

  ADC_ChannelConfTypeDef sConfig = {0};
  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_ENABLE;
  hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.NbrOfConversion = 6;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.DMAContinuousRequests = DISABLE;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
    Error_Handler();
  }

  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

And so forth until ADC_CHANNEL_5 and ADC_REGULAR_RANK_6.

\$\endgroup\$
  • \$\begingroup\$ How is the ADC VREF connected? \$\endgroup\$ – analogsystemsrf Apr 5 at 2:31
  • \$\begingroup\$ Oh, I did not connect Vref... I did not need it to read voltage for one ADC, is it necessary if I read multiple ADC? \$\endgroup\$ – Nathaniel Brochu Apr 5 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.