初级会员
- 积分
- 153
- 金钱
- 153
- 注册时间
- 2016-5-22
- 在线时间
- 42 小时
|
5金钱
void Adc_Init()
{// ADC_DeInit(ADC1);
GPIO_Init(GPIOD,GPIO_Pin_0 ,GPIO_Mode_In_FL_No_IT);
CLK_SYSCLKSourceSwitchCmd(ENABLE);
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_4);
while (CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == RESET);
CLK_PeripheralClockConfig(CLK_Peripheral_ADC1,ENABLE);
ADC_Init(ADC1,ADC_ConversionMode_Single,ADC_Resolution_12Bit,ADC_Prescaler_2);
ADC_SamplingTimeConfig(ADC1,ADC_Group_SlowChannels,ADC_SamplingTime_384Cycles);
ADC_ChannelCmd(ADC1,ADC_Channel_00To07,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_08To15,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_24To27,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_16,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_17,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_18,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_19,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_20,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_21,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_23,DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channel_22,ENABLE);
// ADC_VrefintCmd(ENABLE);
ADC_Cmd(ADC1, ENABLE);
}
u16 Get_Adc(ADC_Channel_TypeDef ADC_Channels)
{
ADC_SchmittTriggerConfig(ADC1, ADC_Channels, DISABLE);
ADC_ChannelCmd(ADC1,ADC_Channels, ENABLE);
ADC_SoftwareStartConv(ADC1);
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == 0);
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(ADC_Channel_TypeDef ADC_Channels,int times)
{
u32 temp_val,temp,value_buf[100];
u8 i,j,k;
delay_init(16);
temp_val=Get_Adc(ADC_Channels);
delay_ms(2);
temp_val=0;
for(i=0;i<times;i++)
{
// temp_val+=Get_Adc(ADC_Channels);
value_buf[i]=Get_Adc(ADC_Channels);
delay_ms(2);
}
for (j=0;j<times-1;j++)
{
for(i=0;i<times-j;i++)
{
if( value_buf[i]>value_buf[i+1])
{
temp = value_buf[i];
value_buf[i] = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
for(k=25;k<times-25;k++)
{
temp_val+=value_buf[k];
} return temp_val/50;
}
void main()
{Clk_Init();
// delay_init(16);
Gpio_Init();
Adc_Init();
Iwdg_Init();
Uart_Init(115200);//高波特率抗低频干扰
// SYS_Active_Halt_Enter();
while(1)
{
GPIO_SetBits(GPIOE,GPIO_Pin_0);
u16 adcx,adcx1;
float Temp,Temp1,Ref;
char buf[2],i;
// Adc_Init();
adcx=Get_Adc_Average(ADC_Channel_22,100);
Temp=(float)adcx*(3.273/4096);
//adcx1=Get_Adc_Average(ADC_Channel_23,10);
// Temp1=(float)adcx1*(3.3/4096);
buf[0]=(u16)(Temp*10000)>>8;
buf[1]=(u16)(Temp*10000);
// CLK_PeripheralClockConfig(CLK_Peripheral_ADC1,DISABLE);
// Uart_Init(115200);
if (buf[0]!=0&buf[1]!=0)
{for (i=0;i<2;i++)
{ USART_SendData8(USART1,buf[i]);
while (USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
delay_ms(5);
}
}
}
|
|