利用STM32F030C8T6的PA0和PA1引脚来进行两通道ADC采集,通过DMA方式。直接上代码。
1、ADC的GPIO引脚配置
void ADC_GPIO_Config(void) { ?? ?GPIO_InitTypeDef ?GPIO_InitStructure; ?? ? ?? ?/* GPIOC Periph clock enable */ ?? ?RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
?? ?GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; ?? ?GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;?? ?//模拟输入 ?? ?GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;? ?? ?? ?GPIO_Init(GPIOA, &GPIO_InitStructure);?? ?? }
2、ADC对应的是DMA1的通道1:DMA1_Channel1
void DMA_Config(void) { ? ? DMA_InitTypeDef ? DMA_InitStructure; ? ? /* DMA1 clock enable */ ? ? RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); ? ?? ? ? /* DMA1 Channel1 Config */ ? ? DMA_DeInit(DMA1_Channel1); ? ? DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; ? ? DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; ? ? DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; ? ? DMA_InitStructure.DMA_BufferSize = 10; ? ? 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_M2M = DMA_M2M_Disable; ? ? DMA_Init(DMA1_Channel1, &DMA_InitStructure); ? ? /* DMA1 Channel1 enable */ ? ? DMA_Cmd(DMA1_Channel1, ENABLE); }
3、ADC初始化配置
void ?ADC_Mode_Config(void) { ? ? ?? ?ADC_InitTypeDef ADC_InitStructure; ?? ? ?? ?RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);//配置ADC的时钟频率 ?? ?/* ADC1 Periph clock enable */ ?? ?RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ?? ? ?? ?/* Initialize ADC structure */ ?? ?ADC_StructInit(&ADC_InitStructure);
?? ?ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;?? ?//12位模式 ?? ?ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ?? ?ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;? ?? ?? ?ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;? ? ?? ?ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;?? ?//右对齐?? ? ?? ?ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
? ? ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); ?? ? ?? ?ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles);? ?? ? ?? ?/* ADC Calibration */ ?? ?ADC_GetCalibrationFactor(ADC1);
?? ?/* ADC DMA request in circular mode */ ?? ?ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); ?? ?ADC_DMACmd(ADC1,ENABLE); ?? ?ADC_Cmd(ADC1,ENABLE);?? ?//开启AD转换器?? ? ?? ? ?? ?/* Wait the ADRDY flag */ ?? ?while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY));?
?? ?/* ADC1 regular Software Start Conv */? ?? ?ADC_StartOfConversion(ADC1);
}
|