cancel
Showing results for 
Search instead for 
Did you mean: 

multichannel adc

meena
Associate III

why i am getting same values in all channels please someone help

void Adc_Vbat(void)
	{
	 ADC_ChannelConfTypeDef sConfig = {0};
	 sConfig.Channel = ADC_CHANNEL_16;
	 sConfig.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 = 1;
		  		  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_2;
		  		  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
		  		  {
		  		    Error_Handler();
		  		  }
	}

float Mains=0,Vbat=0;

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {

	  	 Adc_Vbat();
	  		HAL_ADC_Start(&hadc1);
	  		HAL_ADC_PollForConversion(&hadc1, 1000);
	  		Vbat = HAL_ADC_GetValue(&hadc1);
	  		Vbat = (Vbat * 3.3)/ 4095;
	  		HAL_ADC_Stop(&hadc1);
	  		HAL_Delay(100);


	 ADC_Mains();
	 HAL_ADC_Start(&hadc1);
	 HAL_ADC_PollForConversion(&hadc1, 1000);
	 Mains= HAL_ADC_GetValue(&hadc1);
	 Mains = (Mains * 3.3)/ 4095;
	 HAL_ADC_Stop(&hadc1);
	 HAL_Delay(100);




    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
  RCC_OscInitStruct.PLL.PLLN = 8;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV8;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief ADC1 Initialization Function
  * @PAram None
  * @retval None
  */
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 = 2;
  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_19CYCLES_5;
  hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_39CYCLES_5;
  hadc1.Init.OversamplingMode = DISABLE;
  hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    Error_Handler();
  }

}
3 REPLIES 3
raptorhal2
Lead

You are converting only one channel at a time. Set NbrOfConversions = 1.

STTwo-32
ST Employee

Hello @meena and welcome to the ST Community.

I suggest you use an existent example of ADC Multichannel (as this example) to understand exactly what is going wrong with your code.

PS: read carefully the readme of the example.

Best Regards.

STTwo-32

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.

meena
Associate III

meena_0-1700148955847.png

I am getting  same values for all channels 

oid Batt_Vge(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 Mains_input(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();
		    }
}
void Tsense(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
			  sConfig.Channel = ADC_CHANNEL_0;
			  sConfig.Rank = ADC_REGULAR_RANK_1;
			  	sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
			    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
			    {
			      Error_Handler();
			    }
}

void I_bat(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
				  sConfig.Channel = ADC_CHANNEL_2;
				  sConfig.Rank = ADC_REGULAR_RANK_1;
				  	sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
				    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
				    {
				      Error_Handler();
				    }
}
void Open_Circuit()
{
	ADC_ChannelConfTypeDef sConfig = {0};
					  sConfig.Channel = ADC_CHANNEL_9;
					  sConfig.Rank = ADC_REGULAR_RANK_1;
					  	sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
					    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
					    {
					      Error_Handler();
					    }

}
void Batt_Monitoring(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
						  sConfig.Channel = ADC_CHANNEL_15;
						  sConfig.Rank = ADC_REGULAR_RANK_1;
						  	sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
						    if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
						    {
						      Error_Handler();
						    }
}
float Vbat=0,Mains=0,OCD=0,Temp=0,Ibat=0,Vbatt=0;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  Batt_Vge();
	  HAL_ADC_Start(&hadc1);
	  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  	Vbat = HAL_ADC_GetValue(&hadc1);
	  	Vbat = (Vbat * 3.3)/ 4095;
	  	HAL_ADC_Stop(&hadc1);
	  	HAL_Delay(10);


	  	Mains_input();
	  	HAL_ADC_Start(&hadc1);
	  		  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  		  	Mains = HAL_ADC_GetValue(&hadc1);
	  		  Mains = (Mains * 3.3)/ 4095;
	  		  	HAL_ADC_Stop(&hadc1);
	  		  	HAL_Delay(10);


	  		  Open_Circuit();
	  		  HAL_ADC_Start(&hadc1);
	  		  	  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  		  	OCD = HAL_ADC_GetValue(&hadc1);
	  		  OCD = (OCD * 3.3)/ 4095;
	  		  	  	HAL_ADC_Stop(&hadc1);
	  		  	  	HAL_Delay(10);



	  		  	Tsense();
	  		  	HAL_ADC_Start(&hadc1);
	  		  		  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  		  		Temp = HAL_ADC_GetValue(&hadc1);
	  		  		Temp = (Temp * 3.3)/ 4095;
	  		  		  	HAL_ADC_Stop(&hadc1);
	  		  		  	HAL_Delay(10);


	  		  		I_bat();
	  		  		HAL_ADC_Start(&hadc1);
	  		  			  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  		  			  	Ibat = HAL_ADC_GetValue(&hadc1);
	  		  			  	Ibat = (Ibat * 3.3)/ 4095;
	  		  			  	HAL_ADC_Stop(&hadc1);
	  		  			  	HAL_Delay(10);

	  		  			Batt_Monitoring();
	  		  		  		HAL_ADC_Start(&hadc1);
	  		  		  			  	HAL_ADC_PollForConversion(&hadc1, 1000);
	  		  		  			Vbatt = HAL_ADC_GetValue(&hadc1);
	  		  		  		Vbatt = (Vbatt * 3.3)/ 4095;
	  		  		  			  	HAL_ADC_Stop(&hadc1);
	  		  		  			  	HAL_Delay(10);


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 */
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();
  }

  /** 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 */

}