不啰嗦,直接上程序
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Common config
*/
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.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.NbrOfDiscConversion = 1;
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.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_VBAT; //ADC_CHANNEL_TEMPSENSOR ADC_CHANNEL_VBAT ADC_CHANNEL_VREFINT
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
float GetVbat11(void)
{
volatile uint32_t ad;
float v;
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED);
MyDelay(10000);
HAL_ADC_Start(&hadc1);
MyDelay(10000);
ad = HAL_ADC_GetValue(&hadc1);
MyDelay(10000);
ad = HAL_ADC_GetValue(&hadc1);
//HAL_Delay(10);
HAL_ADC_Stop(&hadc1);
v = ad / 4096.0 * 3 * 3 * 1000;
return v;
}
采集出来的电压值 和实际不符,我电池电压在3.5的时候,输出才有3.15左右 看手册 看了好几遍了 也没看出哪里还有问题 |