yanhaijian 发表于 2015-12-23 12:36:35

STM32F429的AD转换问题

本帖最后由 yanhaijian 于 2015-12-23 12:42 编辑

ADC程序如下:
void ADCInit(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef       ADC_InitStructure;
      
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
      
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStructure);

RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);         
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);      

ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInit(&ADC_CommonInitStructure);
      
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_Cmd(ADC1, ENABLE);
}


u16 Get_Adc(u8 ch)   
{
      ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_480Cycles );                  

      ADC_SoftwareStartConv(ADC1);
         
      while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
      
      return (ADC_GetConversionValue(ADC1));      
}


程序有时会死在while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ))中,大家有没有遇到这个问题。通过仿真看到ADC_SR值为0x00000030,表示发生溢出。

我的颈 发表于 2015-12-23 14:11:31

看惯了寄存器,再看库函数,有点蒙:funk:
ADC时钟?
转换采样周期?

发表于 2015-12-23 15:41:39

这里可以考虑判断完成转换的标志位,转换完成后,进行数据返回。

发表于 2015-12-23 15:41:50

这个判断是自己写的,还是例程?

yanhaijian 发表于 2015-12-23 15:42:03

我的颈 发表于 2015-12-23 14:11
看惯了寄存器,再看库函数,有点蒙
ADC时钟?
转换采样周期?

人懒了,用库方便。

yanhaijian 发表于 2015-12-23 15:43:23

安 发表于 2015-12-23 15:41
这个判断是自己写的,还是例程?

抄的原子的。

发表于 2015-12-23 15:51:49

用的是标准库还是HAL?

yanhaijian 发表于 2015-12-23 16:21:47

安 发表于 2015-12-23 15:51
用的是标准库还是HAL?

标准库。还没时间研究HAL。

发表于 2015-12-23 16:59:13

用这个标志位判断没问题,多做个处理,如果发生了溢出,清除一下溢出错误,再进行数据采集。
可能是没有及时读取完成数据导致的。

huaiqiao 发表于 2015-12-24 00:23:04

楼主这个地方,您到底是用ADC1的那个引脚呢?
页: [1] 2
查看完整版本: STM32F429的AD转换问题