之前用DMA一直出现AD采集错误 最后找了好多论坛 各种修改终于解决了 程序共享一下
void ADC1x_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
//PA0/1/2 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOA, &GPIO_InitStructure);
// //PB0/1 作为模拟通道输入引脚
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
// GPIO_Init(GPIOB, &GPIO_InitStructure);
//
// //PC0/1/2/3/4/5 作为模拟通道输入引脚
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
// GPIO_Init(GPIOC, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE ); //使能ADC1通道时钟,各个管脚时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&AD_Value; //DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //内存作为数据传输的目的地
DMA_InitStructure.DMA_BufferSize = 14*2; //DMA通道的DMA缓存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //数据宽度为32位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //数据宽度为32位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //禁止内存互相访问
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //初始化DMA的通道
DMA_Cmd(DMA1_Channel1, ENABLE); //启动DMA通道
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //工作在独立模式
ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模数转换工作在扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模数转换工作在连续转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部触发转换关闭
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 2; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //初始化外设ADCx的寄存器
//设置指定ADC的规则组通道,设置它们的转化顺序和采样时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_1Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_1Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_1Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 5, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 6, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 7, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 8, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 9, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 10, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 11, ADC_SampleTime_7Cycles5 );
// ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 12, ADC_SampleTime_7Cycles5 );
ADC_DMACmd(ADC1, ENABLE); // 开启ADC的DMA支持
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
ADC_ResetCalibration(ADC1); //复位指定的ADC1的校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1)); //获取ADC1复位校准寄存器的状态,设置状态则等待
ADC_StartCalibration(ADC1); //开始指定ADC1的校准状态
while(ADC_GetCalibrationStatus(ADC1)); //获取指定ADC1的校准程序,设置状态则等待
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void filter(void)
{
int count ;
uint32_t sum = 0;
for ( count=0;count<10;count++)
sum += AD_Value[count][0];
After_filter[0]=(uint32_t)sum/10;
}
一周热门 更多>