u16 Get_val(u8 ch)
{
u16 DataValue; //又是英文注释、、啊哈
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5);
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
/* Test if the ADC1 EOC flag is set or not */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
//FlagStatus Status;
//Status = ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC);
//while(!Status);---->这样做实现不了、请注意
/*Returns the ADC1 Master data value of the last converted channel*/
DataValue = ADC_GetConversionValue(ADC1);
return DataValue;
}
u16 ADC_Get_aveg(u8 ch,u8 n)
{
u32 ad_sum = 0;
u8 i;
for(i=0;i<n;i++)
{
ad_sum += Get_val(ch);
delay_ms(5);
}
return (ad_sum / n);
}
adcx=ADC_Get_aveg(ADC_Channel_1,10);//获取AD数值(0~4095)
temp=(float)adcx*(3.3/4096);//获取相应的电压值
|