以下是ADC相关初始化代码,得到的电源电压不准确,有时候是3.6V,有时候是3.3V,有时候是1.6V,我是标准3.3V供电的。这个电压不准确,内部温度也就不准确了。是不是初始化有什么问题?
/*
================================================================================
描述 : ADC初始化
输入 :
输出 :
================================================================================
*/
void app_smoke_adc_init(void)
{
ADC_InitTypeDef ADC_InitStructure = {0};
__SYSCTRL_GPIOB_CLK_ENABLE();
PB02_ANALOG_ENABLE();
ADC_InitStructure.ADC_ClkDiv = ADC_Clk_Div8;
ADC_InitStructure.ADC_ConvertMode = ADC_ConvertMode_Continuous;
ADC_InitStructure.ADC_IN0.ADC_InputChannel = ADC_InputCH9;
ADC_InitStructure.ADC_IN0.ADC_SampTime = ADC_SampTime390Clk;//烟雾浓度
ADC_InitStructure.ADC_IN1.ADC_InputChannel = ADC_InputTs;
ADC_InitStructure.ADC_IN1.ADC_SampTime = ADC_SampTime390Clk;//内部温度
ADC_InitStructure.ADC_IN2.ADC_InputChannel = ADC_InputVref1P2;
ADC_InitStructure.ADC_IN2.ADC_SampTime = ADC_SampTime390Clk; //1.2V基准电压
ADC_InitStructure.ADC_SQREns = ADC_SqrEns0to2;
ADC_Init(&ADC_InitStructure);
REGBITS_SET(CW_ADC->CR, ADC_CR_BGREN_Msk);//使能BGR
ADC_SetTs(ADC_TsEnable);//使能内部温度
//ADC使能
ADC_Enable();
ADC_SoftwareStartConvCmd(ENABLE);
// g_sAppSmokeWork.adc_flag=true;
}
/*
================================================================================
描述 : ADC检测转换
输入 :
输出 :
================================================================================
*/
void app_smoke_adc_check(void)
{
// static u16 run_cnts=0;
// if(g_sAppSmokeWork.adc_flag && run_cnts++>500)
// {
u16 adc_array[3]={0};
// ADC_SoftwareStartConvCmd(DISABLE);
ADC_GetSqr0Result(&adc_array[0]);
ADC_GetSqr1Result(&adc_array[1]);
ADC_GetSqr2Result(&adc_array[2]);
// run_cnts=0;
g_sAppSmokeWork.adc_flag=false;
for(u8 i=0; i<3; i++)
{
printf("adc_%d=%d\n", i, adc_array[i]);
}
//电源电压计算
u16 volt_bgr=*((uint16_t*)0x001007D2);//基准电压值
float volt_vcc=volt_bgr*4095.f/adc_array[2]/1000;
printf("volt_bgr=%.3fV, volt_vcc=%.3fV\n", volt_bgr/1000.f, volt_vcc);
//温度计算
float temp_val=ADC_GetTs(volt_vcc, adc_array[1]);
printf("temp_val=%.1f C\n", temp_val);
// }
}
串口调试内容
[08:48:01.767] adc_0=0
[08:48:01.767] adc_1=912
[08:48:01.767] adc_2=3062
[08:48:01.767] volt_bgr=1.199V, volt_vcc=1.603V
[08:48:01.780] temp_val=-129.9 C
[08:48:01.780]
[08:48:04.153] now_rtc_time=1624237630s
[08:48:04.160]
[08:48:09.153] now_rtc_time=1624237635s
[08:48:09.159]
[08:48:12.193] adc_0=0
[08:48:12.193] adc_1=913
[08:48:12.193] adc_2=3064
[08:48:12.193] volt_bgr=1.199V, volt_vcc=1.602V
[08:48:12.193] temp_val=-129.9 C
|