Skip to main content
Graduate II
July 25, 2023
Question

Reading adc channels for temp e vref gives same value

  • July 25, 2023
  • 1 reply
  • 1447 views

Hi amazing comminity.. 

I have configured the adc for reading the internal temp and vref in the following way ..

 

 

 

 

void MX_ADC_temp_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.ScanConvMode = ADC_SCAN_DISABLE;
	 hadc1.Init.ContinuousConvMode = ENABLE;
	 hadc1.Init.DiscontinuousConvMode = DISABLE;
	 hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
	 hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
	 hadc1.Init.NbrOfConversion = 1;

	 if (HAL_ADC_Init(&hadc1) != HAL_OK)
	 {
	 Error_Handler();
	 }

	 // Configure ADC channel for temperature sensor
	 sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
	 sConfig.Rank = ADC_REGULAR_RANK_1;
	 sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;

	 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	 {
	 Error_Handler();
	 }

	 // Configure ADC channel for internal voltage reference (VREF)
	 sConfig.Channel = ADC_CHANNEL_VREFINT;
	 sConfig.Rank = ADC_REGULAR_RANK_2;
	 sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;

	 if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	 {
	 Error_Handler();
	 }

}

 

 

 

 Then i execute the following code for reading the 2 channels.. 

 

 

 

 

MX_ADC_temp_Init();

		/*start of adc*/
		HAL_ADC_Start(&hadc1);
		HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
		raw_temp_value_mcu = HAL_ADC_GetValue(&hadc1);
		HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
		raw_vref_mcu = HAL_ADC_GetValue(&hadc1);
		/*final computation of mcu temp*/
		mcu_temperature = mcu_get_temperature(raw_temp_value_mcu, raw_vref_mcu);

 

 

 

 It seems when i look to the raw adc values that is always the ssame value (raw_temp_value_mcu) and raw_vref_mcu are very similar as it is always reading the same channel . 

 

What could be wrong here ? 

 

Thanks a lot for your support @Tesla DeLorean 

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    August 4, 2023

    Hi @SGasp.1 ,

    Calling HAL_ADC_PollForConversion will clear ADC flag EOS (ADC group regular end of sequence conversion).
    So it needs to be called only one time after the last HAL_ADC_GetValue.

    You can also use the DMA with ADC. A ready to use example is available in https://github.com/STMicroelectronics/STM32CubeL4/tree/master/Projects/NUCLEO-L496ZG/Examples/ADC/ADC_Sequencer. In this example, 3 channels are converted: one external channel and two internal channels (VrefInt and temperature sensors).

    You didn't mention the used hardware, but you may find a similar example for your device in the relevant STM32Cube package.

    -Amel