STM32F407 ADC 采样,AD值跳动大于 50个字
配置如下:
void ADC1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//初始化GPIOA的Pin_0为模拟量输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit();
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //精度为12位
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //扫描转换模式失能,单通道不用
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//ENABLE; //连续转换不使能
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //不用外部触发,软件触发转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐,低字节对齐
ADC_InitStructure.ADC_NbrOfConversion = 1; //规定了顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);//ADC,通道,1转换,所选通道的采样周期
ADC_Cmd(ADC1, ENABLE); //使能
// ADC_SoftwareStartConv(ADC1); //软件启动转换
}
unsigned short GetADCValue(void) //读AD结果
{ ADC_SoftwareStartConv(ADC1); //软件启动转换
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);
} |