void T_Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
ADC_DeInit(ADC1);
ADC_Struc
tinit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1,ADC_Channel_16, ADC_SampleTime_239_5Cycles );
ADC_TempSensorCmd(ENABLE);
ADC_GetCalibrationFactor(ADC1);
ADC_Cmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_ADEN)==RESET);
ADC_StartOfConversion(ADC1);
}
uint16_t T_Get_Adc() //读取采样值
{
ADC_ChannelConfig(ADC1,ADC_Channel_16, ADC_SampleTime_239_5Cycles );
ADC_StartOfConversion(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
return ADC_GetConversionValue(ADC1);
}
uint16_t T_Get_Adc_Average(uint8_t times)//读取多次采样值求平均
{
uint32_t temp_val=0;
uint8_t t;
for(t=0;t<times;t++)
{
temp_val+=T_Get_Adc();
delay_ms(5);
}
return temp_val/times;
}
int main(void)
{
uint32_t adcx=0,temp_a=0;
float temp;
T_Adc_Init();
while(1)
{
adcx=T_Get_Adc_Average(10);
temp=((float)adcx*(3.3/4096));
temp=(1.43-temp)/0.0043+25;
temp_a=temp*1000;// 扩大温度值的1000倍
}
}
一周热门 更多>