void ADC1_Config(void){ ADC_InitTypeDef ADC_InitStruct; GPIO_InitTypeDef GPIO_InitStruct; /* Enable GPIOA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//使能GPIOA时钟 /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//使能ADC1时钟 /* Configure PA.0 as analog input */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStruct); //设置PA0为模拟输入,PA0对应的是通道0 ADC总共19个通道,16个外部通道,一个温度,一个电压,还有一个自己内部的Vbat通道; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStruct); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; //12位的分辨率 ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; //循环采样,意思就是在整个程序可以被进行多次转换,单次转换的话整个程序生命周期只能被触发一次 ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;软件触发 ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; 右对齐 ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStruct); ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_55_5Cycles); 设置采样通道,一定要把通道号和引脚的串口号对应起来 /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); //adc校准 ADC_Cmd(ADC1, ENABLE); 使能adc /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); 开始ADC转换}ADCData[Num]=ADC_GetConversionValue(ADC1) 在实际程序中可以用这个函数进行采样