cancel
Showing results for 
Search instead for 
Did you mean: 

ADC

meena
Associate III

the code generated for my channels if i initialize this channels and

meena_0-1700148308920.png

 

 

 

 hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_SEQ_FIXED;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.LowPowerAutoPowerOff = DISABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.DMAContinuousRequests = DISABLE;
  hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
  hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_39CYCLES_5;
  hadc1.Init.OversamplingMode = DISABLE;
  hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_9;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_15;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure Regular Channel
  */
  sConfig.Channel = ADC_CHANNEL_16;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */

}

 

 

i am getting same value for all the adc value

1 ACCEPTED SOLUTION

Accepted Solutions
Pierre_Paris
ST Employee

Hello @meena,

Here some additional information :

  • HAL_ADC_ConfigChannel() configure a channel to be assigned to ADC group regular. That means you are configuring the channel every time you want to start conversions, that's a waste of time. You should configure once and if you want to update parameters on the fly without resetting the ADC, please use ADC_ChannelConfTypeDef().
  • You want to use the polling mode, you respected well the four points below :
    • Activate ADC peripheral and start conversions using function HAL_ADC_Start()
    • Wait for ADC conversion completion using function HAL_PollForConversion()
    • Retrieve conversion results using function HAL_ADC_GetValue()
    • Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop()
  • But in the polling mode, you are polling the EOC (End Of Conversion) single flag configured with EOCSelection. You should set this parameter to ADC_EOC_SEQ_CONV and poll this flag to indicate the end of sequence conversions. If I understood your application, the best option for you is starting once the ADC, polling the right flag and then stop the ADC. 
  • "ScanConvMode = ADC_SCAN_SEQ_FIXED" set to not fully configurable : number of ranks in the scan sequence is defined by number of channels set in the sequence, rank of each channel is fixed by channel HW number (channel 0 fixed on rank 0...). But you are affecting each rank to a channel as in the fully configurable mode (eg: channel 9 to rank 5). You may have to change that.

Best Regards,

Pierre

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.

View solution in original post

4 REPLIES 4
Pierre_Paris
ST Employee

Hello @meena,

Thank you for your question!

Here some remark :

  • Do you know there are example code existing ? You can get software here (you can go under the root STM32Cube_FW_G0_V1.6.0\Projects\NUCLEOG071RB\Examples\ADC\ADC_MultiChannelSingleConversion).
  • Are you sure for NbrOfConversion = 1 ? You declared 6 regular channels right ?
  • For the overrun case, my advise is to use ADC_OVR_DATA_OVERWRITTEN.
  • Do you want to use the DMA feature with the ADC ? 
     
  • I don't see any calibration before to start the ADC. You need to.

Best Regards,

Pierre

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.

Thanks for the reply.This is what like I configured The no.of conversion made to 6 also getting same values 

void ADC_Vbat(void)
	{
	ADC_ChannelConfTypeDef sConfig = {0};

	sConfig.Channel = ADC_CHANNEL_16;
	 sConfig.Rank = ADC_REGULAR_RANK_1;
	sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
	  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  HAL_ADC_Start(&hadc1);
	  	  	  		HAL_ADC_PollForConversion(&hadc1, 1000);
	  	  	  		Vbat = HAL_ADC_GetValue(&hadc1);
	  	  	  		//Vbat = (Vbat * 3.3)/ 4095;
	  	  	  		HAL_ADC_Stop(&hadc1);



	}
void ADC_BattIsense(void)
	{
	ADC_ChannelConfTypeDef sConfig = {0};
	 sConfig.Channel = ADC_CHANNEL_3;
	  sConfig.Rank = ADC_REGULAR_RANK_2;
	  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
	  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  HAL_ADC_Start(&hadc1);
	 	  	  	  		HAL_ADC_PollForConversion(&hadc1, 1000);
	 	  	  	  		BattI = HAL_ADC_GetValue(&hadc1);
	 	  	  	  		HAL_ADC_Stop(&hadc1);


	}
void ADC_ledIsense(void)
	{
	ADC_ChannelConfTypeDef sConfig = {0};
		 sConfig.Channel = ADC_CHANNEL_2;
		 sConfig.Rank = ADC_REGULAR_RANK_3;
			sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
		  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
		  {
		    Error_Handler();
		  }
		  HAL_ADC_Start(&hadc1);
		 	  	  	  		HAL_ADC_PollForConversion(&hadc1, 1000);
		 	  	  	  		LedI = HAL_ADC_GetValue(&hadc1);
		 	  	  	  		HAL_ADC_Stop(&hadc1);
	}
void ADC_Temp(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
	sConfig.Channel = ADC_CHANNEL_0;
	  sConfig.Rank = ADC_REGULAR_RANK_4;
	  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
	  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  HAL_ADC_Start(&hadc1);
	  		 	  	  	  		HAL_ADC_PollForConversion(&hadc1, 1000);
	  		 	  	  	  		Temp = HAL_ADC_GetValue(&hadc1);
	  		 	  	  	  		HAL_ADC_Stop(&hadc1);
}
void Open_Circuit(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
	 sConfig.Channel = ADC_CHANNEL_9;
	  sConfig.Rank = ADC_REGULAR_RANK_5;
	  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;
	  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  HAL_ADC_Start(&hadc1);
	 	  		 	  	  	  		HAL_ADC_PollForConversion(&hadc1, 1000);
	 	  		 	  	  	  		Opv = HAL_ADC_GetValue(&hadc1);
	 	  		 	  	  	  		HAL_ADC_Stop(&hadc1);
}

 

Pierre_Paris
ST Employee

Hello @meena,

Here some additional information :

  • HAL_ADC_ConfigChannel() configure a channel to be assigned to ADC group regular. That means you are configuring the channel every time you want to start conversions, that's a waste of time. You should configure once and if you want to update parameters on the fly without resetting the ADC, please use ADC_ChannelConfTypeDef().
  • You want to use the polling mode, you respected well the four points below :
    • Activate ADC peripheral and start conversions using function HAL_ADC_Start()
    • Wait for ADC conversion completion using function HAL_PollForConversion()
    • Retrieve conversion results using function HAL_ADC_GetValue()
    • Stop conversion and disable the ADC peripheral using function HAL_ADC_Stop()
  • But in the polling mode, you are polling the EOC (End Of Conversion) single flag configured with EOCSelection. You should set this parameter to ADC_EOC_SEQ_CONV and poll this flag to indicate the end of sequence conversions. If I understood your application, the best option for you is starting once the ADC, polling the right flag and then stop the ADC. 
  • "ScanConvMode = ADC_SCAN_SEQ_FIXED" set to not fully configurable : number of ranks in the scan sequence is defined by number of channels set in the sequence, rank of each channel is fixed by channel HW number (channel 0 fixed on rank 0...). But you are affecting each rank to a channel as in the fully configurable mode (eg: channel 9 to rank 5). You may have to change that.

Best Regards,

Pierre

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.

Hello,

I use 4 channels of ADC1 in my program. But, when I see the result by HAL_ADC_GetValue() function,

the results of channel0~channel3 of ADC1 are all same.

Could you tell me the reason of this phenomenon?

Thank you!