个人工作备忘用,有问题欢迎指出,仅供参考哈!
采集外部模拟信号,需要开启对应IO口,在这里我们要将IO设为模拟输入,记得不能将IO口复用
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_ADC_InitStruct;
GPIO_ADC_InitStruct.GPIO_Mode=GPIO_Mode_AN;
//GPIO_ADC_InitStruct.GPIO_OType=GPIO_OType_OD;
GPIO_ADC_InitStruct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_ADC_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_ADC_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_ADC_InitStruct);
实现DMA自动转运数据,DMA配置如下:
这里将外设数据转运到内存,记得配置数据转运方向。
数据大小根据需求调整即可,切记数据存储Buff的数据类型要统一,否则会出现数据跳变或特别大。
DMA_InitTypeDef DMA_VC_InitStructure;
DMA_VC_InitStructure.DMA_BufferSize=2;
DMA_VC_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_VC_InitStructure.DMA_MemoryBaseAddr=(uint32_t)vc_ADC_Arr;
DMA_VC_InitStructure.DMA_PeripheralBaseAddr=(uint32_t)(&(ADC1->DR));
DMA_VC_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_VC_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;//16bit
DMA_VC_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_VC_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_VC_InitStructure.DMA_Mode=DMA_Mode_Circular;
DMA_VC_InitStructure.DMA_Priority=DMA_Priority_High;
DMA_VC_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_Init(DMA1_Channel1,&DMA_VC_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
ADC_DeInit(ADC1);
ADC_ClockModeConfig(ADC1,ADC_ClockMode_SynClkDiv4);
ADC_InitTypeDef ADC_VC_InitStruct;
ADC_StructInit(&ADC_VC_InitStruct);
ADC_VC_InitStruct.ADC_Resolution=ADC_Resolution_12b;
ADC_VC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
// ADC_VC_InitStruct.ADC_ExternalTrigConv=DISABLE;
ADC_VC_InitStruct.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;
ADC_VC_InitStruct.ADC_ContinuousConvMode=ENABLE;
ADC_VC_InitStruct.ADC_ScanDirection=ADC_ScanDirection_Upward;
ADC_Init(ADC1,&ADC_VC_InitStruct);
ADC_ChannelConfig(ADC1,ADC_Channel_6,ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1,ADC_Channel_7,ADC_SampleTime_239_5Cycles);
ADC_GetCalibrationFactor(ADC1);
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
//ADC_ContinuousModeCmd(ADC1,ENABLE);
ADC_StartOfConversion(ADC1); //开启所选通道的转换