cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L031 multichannel ADC

Andy b1
Associate
Posted on June 08, 2018 at 11:47

Hi

I'm trying to read ADC 6 channel but I got very strange result 60 or 4095 not the right one...

Any help are welcome  what I'm doing wrong.  I need without DMA if possible without?

main init

static void MX_ADC_Init(void)

{

  ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

    */

  hadc.Instance = ADC1;

  hadc.Init.OversamplingMode = DISABLE;

  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;

  hadc.Init.Resolution = ADC_RESOLUTION_12B;

  hadc.Init.SamplingTime = ADC_SAMPLETIME_7CYCLES_5;

  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

  hadc.Init.ContinuousConvMode =ENABLE;

  hadc.Init.DiscontinuousConvMode = DISABLE;

  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

  hadc.Init.DMAContinuousRequests = DISABLE;

  hadc.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;

  hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

  hadc.Init.LowPowerAutoWait = DISABLE;

  hadc.Init.LowPowerFrequencyMode = DISABLE;

  hadc.Init.LowPowerAutoPowerOff = DISABLE;

  if (HAL_ADC_Init(&hadc) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_0;

  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_1;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_2;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_3;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_4;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_5;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

    /**Configure for the selected ADC regular channel to be converted.

    */

  sConfig.Channel = ADC_CHANNEL_VREFINT;

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

calling for channel 1 than 2....

uint32_t ADC_Read(uint32_t channel) {

  ADC_ChannelConfTypeDef sConfig;

  uint32_t value;

 

  /* Configure ADC regular channel */

  sConfig.Channel = channel;

 

  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK) {

    return 0;

  }

 

 

  if (HAL_ADC_Start(&hadc) != HAL_OK) {

    return 0;

  }

 

    /* Poll for end */

   //HAL_ADC_PollForConversion(&hadc, 100);

  while(HAL_ADC_PollForConversion(&hadc,0) != HAL_OK);

    HAL_Delay(100);

  /* Get the converted value of regular channel */

  value= HAL_ADC_GetValue(&hadc);

  HAL_ADC_Stop(&hadc);

  //}

 

 

  /* Return value of ADC */

  return value;

}
0 REPLIES 0