Hello, guys! I have problem with STM32L0 ADC. I measure VrefInt voltage but the behaviour of the ADC is not stable - it reports corect values(4095) alongside with incorrect values (1950)?! Would you please help me.
- Initialization:
static void MX_ADC_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = DISABLE;
hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = DISABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
HAL_ADC_Init(&hadc);
HAL_ADC_Init(&hadc);
/* if (HAL_ADC_Init(&hadc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}*/
/**Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_17;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
/* if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}*/
HAL_ADC_ConfigChannel(&hadc, &sConfig);
__HAL_RCC_ADC1_CLK_ENABLE();
}
2. Measurement:
void MeasureVDD(void)
{
#define VREFINT_CAL_ADDR 0x1FF80078
uint16_t vrefint_cal;
vrefint_cal= *((uint16_t*)VREFINT_CAL_ADDR);
uint32_t adcVal;
float vddVal;
HAL_ADC_Start(&hadc);
HAL_ADC_PollForConversion(&hadc, 50);
adcVal=HAL_ADC_GetValue(&hadc);
HAL_ADC_Stop(&hadc);
vddVal=(3.3*vrefint_cal)/adcVal;
PRINTF("ADC value:%d\r\n",adcVal);
}
