大学生
最后登录1970-1-1
在线时间 小时
注册时间2016-1-27
|
这两天在调407的ADC_DMA,采样出来的AD值不稳定,同样的代码在429上是稳定的,不知道什么原因,代码配置来来回回调到要吐了,还没解决,求大神解惑。上代码:1、配置PF6、7、8为AD输入
static void User_Adc_Gpio_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(USER_ADC_GPIO_PORT,&GPIO_InitStructure);
}
2、配置DMA与ADC
static void User_Adc_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);//开启两外设时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
/*--------------DMA_Channel select--------------------*/
DMA_InitStructure.DMA_Channel =DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = ((u32)ADC3+0x4c);
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_convertedvalue[0];//之前有定义:static volatile uint16_t adc_convertedvalue[10][3];//二重数组存放转换后的各通道AD值
/*----------------数据传输方向设置--------------------*/
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
/*------------------传输数据的个数-10x3=30-------------------*/
DMA_InitStructure.DMA_BufferSize = Sample_Num*USER_ADC_CHANNELS;
/*-------------------禁止外设地址递增------------------*/
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
/*--------------------存储器地址递增-------------------*/
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0,&DMA_InitStructure);
DMA_Cmd(DMA2_Stream0,ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//4分
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;(这个配置有疑问)
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion =3;
ADC_Init(USER_ADC,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC3,ADC_Channel_4,1,ADC_SampleTime_28Cycles);
ADC_RegularChannelConfig(ADC3,ADC_Channel_5,2,ADC_SampleTime_28Cycles);
ADC_RegularChannelConfig(ADC3,ADC_Channel_6,3,ADC_SampleTime_28Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC3,ENABLE);
ADC_DMACmd(ADC3,ENABLE);
ADC_Cmd(ADC3,ENABLE);
ADC_SoftwareStartConv(ADC3);
}
启动转换后,访问adc_convertedvalue[10][3](adc_convertedvalue[Sample_Num][3])读取数据:
uint16_t ReadADCAverageValue(u8 Channel)
{
uint8_t i,j=0;
uint32_t sum = 0;
uint16_t dat=0;
static uint16_t avgbuf[Sample_Num];
for(i=0;i<Sample_Num; i++)
{
avgbuf=adc_convertedvalue[Channel];
}
for(i=0;i<Sample_Num;i++)
for(j=0;j<Sample_Num-i;j++)
{
if(avgbuf[j]>avgbuf[j+1])
{
dat =avgbuf[j];
avgbuf[j]=avgbuf[j+1];
avgbuf[j+1]=dat;
}
}
for(i=1;i<Sample_Num-1;i++)
sum+=avgbuf;
return (sum/(Sample_Num-2));
}
按照上述配置转换出来的数据,在429下可以稳定下来而且误差很小,但是移到407上就懵逼了,数据各种跳,参考电压是正常的,不知道哪里出了问题!
在429上采集的数据:file:///C:\Users\Long No.1\AppData\Roaming\Tencent\Users\626115051\QQ\WinTemp\RichOle\@KK)Z2F4C_6VAA6}0J`CQ14.png
同样的电压和配置代码在407上采集的数据:
输入电压是稳定的,但是用407去采集的话,自身采集出来的AD值都会有200左右的跨度,难以费解啊!调节采样周期也差不多情况。
|
|