Skip to main content
Explorer
September 18, 2023
Question

STM32G071CB Battery voltage monitoring issue

  • September 18, 2023
  • 2 replies
  • 969 views

 

HI.

 I would like to use the battery voltage monitoring feature.
I am reading the ADC by connecting a DC source and adjusting the value from 2.6V to 3.6V. We tested 10 boards and they all had different ADC values.
The biggest difference was over 100.

PCLK is set to 64Mhz.
The ADC setting values ​​were as follows.

/* ADC code */

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_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.LowPowerAutoPowerOff = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 1;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  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_3CYCLES_5;
  hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_1CYCLE_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_VBAT;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLINGTIME_COMMON_1;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN ADC1_Init 2 */

  /* USER CODE END ADC1_Init 2 */
}

There are two questions I would like to ask.
1. Is the setting method correct?
2. I thought it was wrong and looked for an example of battery voltage monitoring, but couldn't find one. Can you please share?


thanks.

 

    This topic has been closed for replies.

    2 replies

    Super User
    September 18, 2023

    >   hadc1.Init.SamplingTimeCommon1 = ADC_SAMPLETIME_3CYCLES_5;

    Increase your sampling time to maximum to improve accuracy on DC signals.

    Also, the ADC must be calibrated before being used. The reference manual has more details on this.

    TDK_0-1695001264838.png

     

    Graduate II
    September 18, 2023

    I had suggest 3 things to increase ADC Precision.
    but you need to change sampling time, ADC_SAMPLETIME_3CYCLES_5 - it's too low which it should be over 19.5 cycles.

    1. before start running ADC you should be run HAL_ADCEx_Calibration_Start to Calibrate it.
    2. you should calculate millivolt per ADC bits from Vref which reference voltage is about 1,212 millivolt because you never know what exactly VDDA so you need to calculate it from where you really know and you can use this constant value to calculate thee VBat voltage.
    and the last one
    3. you can use over sampling to reduce a ripple from your supply on VDDA or use some digital filter technique to reduce effect from them.

    but even you do all of them, you 'll got a stable value but it will difference from measure by multimeter about 30-40 mV so if you want more accurate, you should add linear regression to calibrate it with linear equation which the error can be down to less than 10mV compare to your multimeter.