Skip to main content
NJP.1
Associate III
September 1, 2021
Question

Hi I am working on stm32f051c8tx in adc part , below is the code I am using, the issue is, there is a variation of 30 millivolt from the multimeter to the voltage read by the controller.

  • September 1, 2021
  • 2 replies
  • 814 views

static void MX_ADC_Init(void)

{

 /* USER CODE BEGIN ADC_Init 0 */

 /* USER CODE END ADC_Init 0 */

 ADC_ChannelConfTypeDef sConfig = {0};

 /* USER CODE BEGIN ADC_Init 1 */

 /* USER CODE END ADC_Init 1 */

 /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 

 */

 hadc.Instance = ADC1;

 hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;

 hadc.Init.Resolution = ADC_RESOLUTION_12B;

 hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

 hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

 hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

 hadc.Init.LowPowerAutoWait = DISABLE;

 hadc.Init.LowPowerAutoPowerOff = DISABLE;

 hadc.Init.ContinuousConvMode = ENABLE;

 hadc.Init.DiscontinuousConvMode = DISABLE;

 hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;

 hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

 hadc.Init.DMAContinuousRequests = DISABLE;

 hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;

 if (HAL_ADC_Init(&hadc) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure for the selected ADC regular channel to be converted. 

 */

 sConfig.Channel = ADC_CHANNEL_1;

 sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

 sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

 if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN ADC_Init 2 */

 /* USER CODE END ADC_Init 2 */

}

HAL_ADC_PollForConversion(adc, 500);

raw_value= HAL_ADC_GetValue(adc);

voltage=(raw_value*3.37/4095;

HAL_ADC_Stop(adc);

This topic has been closed for replies.

2 replies

waclawek.jan
Super User
September 1, 2021

Try to increase sampling time. If this helped, the impedance of your signal source is too high for given sampling time.

JW

NJP.1
NJP.1Author
Associate III
September 2, 2021

thankyou i will try this and reply here soon.

TDK
Super User
September 1, 2021

> raw_value*3.37/4095

Did your multimeter read 3.370V? when measuring the rail?

"If you feel a post has answered your question, please click ""Accept as Solution""."
NJP.1
NJP.1Author
Associate III
September 2, 2021

thankyou yes it shows 3.7v in the multimeter.