|
void ADCInit(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* ADC Configuration -------------------------------------------------------*/
RCC_HSICmd(ENABLE); // Enable HSI Clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable the AHB peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); // Enable the APB2 peripheral clock
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//精度为12位
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//是否连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//外部触发转换
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_96Cycles ); // ADC1 regular channel20 configuration
//ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE); //中断使能
//ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);
ADC_TempSensorVrefintCmd(ENABLE);//温度传感器
//ADC_DelaySelectionConfig(ADC1,ADC_DelayLength_Freeze);
ADC_Cmd(ADC1, ENABLE); //Enable ADC1
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) // Wait until ADC1 ON status 设置完成
{
}
ADC_SoftwareStartConv(ADC1);
}
//采集电压值//
uint16_t Get_Battery_voltage(void)
{
__IO uint16_t ADCdata = 0; //转换完的结果
while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)// Wait until ADC Channel 20 end of conversion
{
}
ADCdata = ADC_GetConversionValue(ADC1); // Read ADC conversion result
if(ADCdata>4000||ADCdata<3000)
wrong1++;
wrong3++;
//ADC12_Off();
//ADC12_DeInit();//复位
// while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
// {
// }
return ADCdata;
}
以上是我的初始化函数,和读取函数~请各位帮忙看看 |
|