2021-06-17 12:34 AM
Hi,
Recently I started working on stm32F07 microcontroller. I am trying to read sensor value using ADC.
Issue: ADC value is keep on changing randomly(means its not holding value).
Below is my code. Please refer it and help me to figure out the issue.
Clock is 48Mhz.
Handler:
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if (__HAL_ADC_GET_FLAG(hadc, ADC_FLAG_EOC))
{
adcval = HAL_ADC_GetValue(hadc);
volt = 3300 * (*VREFINT_CAL_ADDR)/adcval;
// Pressure=volt*adcval/4095;
}
}
main function:
MX_GPIO_Init();
MX_ADC_Init();
/* USER CODE BEGIN 2 */
HAL_ADC_Start_IT(&hadc);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(200);
HAL_ADC_Start_IT(&hadc);
}
ADC Configuration:
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 = DISABLE;
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_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_VREFINT;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
IOC File:
Thanks & regards.
Vivek
2021-06-17 08:30 AM
Hello @VDutt.1,
First, please add the ADC calibration function to improve the conversion accuracy: HAL_ADCEx_Calibration_Start.
Another point is that I recommend you verifying the formula you are using to calculate the converted value.
Referring to the RM0360; the “Converting a supply-relative ADC measurement to an absolute voltage value�? part of the 12.9 section, if you have a known VDDA use the first formula, if not, refer to the VREFINT.
Chahinez.