Skip to main content
Explorer
November 15, 2023
Question

stm32g030k8 ADC

  • November 15, 2023
  • 2 replies
  • 744 views

Hi I am working on multichannel ADC ,if its a single conversion I am getting correct values but when it is more than 1 i am getting same conversion values from all channels.

 

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();
	 }

	}
void ADC_Mains(void)
	{
	ADC_ChannelConfTypeDef sConfig = {0};
	sConfig.Channel = ADC_CHANNEL_5;
		 		 sConfig.Rank = ADC_REGULAR_RANK_1;
		 		 sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
		 		 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
		 		 {
		 		 Error_Handler();
		 		 }
	}
static void MX_ADC1_Init(void)
{

 /* USER CODE BEGIN ADC1_Init 0 */

 /* USER CODE END ADC1_Init 0 */

 ADC_ChannelConfTypeDef sConfig = {0};

 /* USER CODE BEGIN ADC1_Init 1 */

 /* USER CODE END ADC1_Init 1 */

 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
 */
 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();
 }

 

Can someone points out the error I made please 

    This topic has been closed for replies.

    2 replies

    ST Employee
    November 30, 2023

    Hello @meena,

    Here some guidance :

    • Do you want to use the polling mode ? You have to respect the four points below for ADC Conversion :
      • 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 because you have multichannel.
    • For ScanConvMode, ADC_SCAN_SEQ_FIXED means not fully configurable: sequencer length and each rank affection to a channel are fixed by channel HW number (channel 0 fixed on rank 0, channel 1 fixed on rank 1...) . You have to use ADC_SCAN_ENABLE if you want to configure it yourself.
    • You also need to enter the right number of channel in NbrOfConversion parameter (number of ranks that will be converted, if you use other ranks...).

    Best Regards,

    Pierre

    Visitor II
    November 30, 2023

    you need to use the ADC's scanning mode properly. The scanning mode allows you to perform conversions on multiple channels by specifying their sequence.

    You can get help from the following threads. 

    https://www.youtube.com/watch?app=desktop&v=6uiO68pdjZA

    https://www.theengineeringprojects.com/2021/11/how-to-use-adc-with-stm32.html