2021-03-18 04:54 AM
2021-03-18 04:56 AM
int read_adc_data(ADC_HandleTypeDef *ptr_hadc, uint32_t channelx)
{
uint16_t vrefint_cal;
float adc_read_value = 0.0, adc_reference_voltage = 0.0;
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADC_CHANNEL_VREFINT; /* Sampled channel number */
sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number ADCx_CHANNEL */
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; /* Sampling time (number of clock cycles unit) */
sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(ptr_hadc, &sConfig) != HAL_OK)
{
Error_Handler();
return -1;
}
if (HAL_ADCEx_Calibration_Start(ptr_hadc, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED) != HAL_OK)
{
Error_Handler();
return -1;
}
if (HAL_ADC_Start(ptr_hadc) != HAL_OK)
{
Error_Handler();
return -1;
}
if (HAL_ADC_PollForConversion(ptr_hadc, 100) != HAL_OK)
{
/* End Of Conversion flag not set on time */
Error_Handler();
return -1;
}
else
{
/* ADC conversion completed */
/*##-5- Get the converted value of regular channel ########################*/
raw_value = HAL_ADC_GetValue(ptr_hadc);
}
HAL_ADC_Stop(ptr_hadc);
vrefint_cal= *((uint16_t*)VREFINT_CAL_ADDR); // read VREFINT_CAL_ADDR memory location
/*Calculating the actual V DDA voltage using the internal reference voltage*/
adc_reference_voltage = (float)(((float) 3.3* (float) vrefint_cal)/(float)raw_value);/*3.3 V x VREFINT_CAL / VREFINT_DATA*/
return 0;
}
2022-03-11 06:04 AM
I was removed the HAL_ADC_PollForConversion that working fine.