Question
STM32G051 ADC Calibration issue
Hi I am using the STM23G051C8T6 for the internal RTC and an external battery on Vbat. I noticed if I perform the Calibration using the following line
HAL_ADCEx_Calibration_Start(&hadc1);
my initial ADC reading will be lower then actual reading. For example will read 2.7 instead of 2.9.
If I don't perform the Calibration I read the 2.9 correctly from the 1st measurement however subsequence measurements read higher. My ADC Config below.
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_DIV4;
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_12CYCLES_5;
hadc1.Init.SamplingTimeCommon2 = ADC_SAMPLETIME_12CYCLES_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 */
}
And my ADC Measurment code:
ADC->CCR |= ADC_CCR_VBATEN; // Set Divide by 3 Bridge to Measure Voltage on Vbat
HAL_ADC_Start(&hadc1); // Start ADC conversion
// HAL_Delay(100);
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK) // Wait for conversion to complete
{
adcValue = HAL_ADC_GetValue(&hadc1); // Read ADC value
// Calculate voltage from ADC value
battery_voltage_mv = ((float)adcValue / 4096.0f) * 3.04f * 3000;
}
// HAL_Delay(100);
ADC->CCR &= ~ADC_CCR_VBATEN; // Clear Divide by 3 Bridge to Vbat to reduce battery drain
HAL_ADC_Stop(&hadc1);
