cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G4 ADC MULTICHANNEL DMA

RaulPosada
Associate II

I'm trying to read ADC2 IN1(PA0 connected to GND) and ADC2 IN2 (PA1 connected to 3.3V).

 

static void MX_ADC2_Init(void)
{

  /* USER CODE BEGIN ADC2_Init 0 */

  /* USER CODE END ADC2_Init 0 */

  ADC_ChannelConfTypeDef sConfig = {0};

  /* USER CODE BEGIN ADC2_Init 1 */

  /* USER CODE END ADC2_Init 1 */

  /** Common config
  */
  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc2.Init.Resolution = ADC_RESOLUTION_12B;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.GainCompensation = 0;
  hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc2.Init.LowPowerAutoWait = DISABLE;
  hadc2.Init.ContinuousConvMode = ENABLE;
  hadc2.Init.NbrOfConversion = 2;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc2.Init.DMAContinuousRequests = ENABLE;
  hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  hadc2.Init.OversamplingMode = DISABLE;
  if (HAL_ADC_Init(&hadc2) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */

  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC2_Init 2 */

  /* USER CODE END ADC2_Init 2 */

}

 

1 ACCEPTED SOLUTION

Accepted Solutions
RaulPosada
Associate II

I'v solved this issue by changing the variable ADC2_RES_BUFFER[2] to uint16_t. Worked perfectly.

View solution in original post

3 REPLIES 3
SofLit
ST Employee

Hello,

You need to describe the problem. Sharing a photo cannot give much clear image on what is going on...

Thank you.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

I was expecting you to describe the problem because when i'm trying to read a single channel using this method it works perfectly but after the IN2 was added the conversion does not succeed. Since PA0 is connected to GND the ADC2_RES_BUFFER[0] should be 0 or near it, but as you can see on the screenshot it shows a value near to 264x10^6. And as you can see if the Conversion != OK the code gets stuck in the first conversion, the ADC2_RES_BUFFER[1] does not have his value update and the code does not get to clearing the flagTim3 variable.

If you could please tell me more about the details you need to help me solve this. Thank you for your answer and time.

RaulPosada
Associate II

I'v solved this issue by changing the variable ADC2_RES_BUFFER[2] to uint16_t. Worked perfectly.