void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStruct;
// 使能DMA2时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
DMA_StructInit(&DMA_InitStruct);
ADC_CommonStructInit(&ADC_CommonInitStructure);
ADC_StructInit(&ADC_InitStructure);
// 配置ADC通用设置
ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; // 双ADC模式,规则同步
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; // ADC时钟分频
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2; // DMA模式2
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // 双ADC采样延迟
ADC_CommonInit(&ADC_CommonInitStructure);
// 配置ADC1
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // 12位分辨率
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // 扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // 单次转换模式
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; // 上升沿触发
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T8_TRGO; // TIM8触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐
ADC_InitStructure.ADC_NbrOfConversion = 3; // 3个转换通道
ADC_Init(ADC1, &ADC_InitStructure);
// 配置ADC1的通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_56Cycles); // 通道0
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_56Cycles); // 通道1
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_56Cycles); // 通道2
// 配置ADC2
ADC_Init(ADC2, &ADC_InitStructure);
// 配置ADC2的通道
ADC_RegularChannelConfig(ADC2, ADC_Channel_7, 1, ADC_SampleTime_56Cycles); // 通道0
ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 2, ADC_SampleTime_56Cycles); // 通道1
ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 3, ADC_SampleTime_56Cycles); // 通道2
// 配置DMA2 Stream0(用于ADC1)
DMA_InitStruct.DMA_Channel = DMA_Channel_0;
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)(&ADC->CDR); // 外设地址
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)(adc_buffer); // 内存地址
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; // 传输方向
DMA_InitStruct.DMA_BufferSize = 3; // 数据长度 = 3(32 位数据)
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址不递增
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存地址递增
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; // 外设数据宽度 32 位
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; // 内存数据宽度 32 位
DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; // 循环模式
DMA_InitStruct.DMA_Priority = DMA_Priority_VeryHigh; // 高优先级
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
//DMA_InitStruct.
DMA_Init(DMA2_Stream0, &DMA_InitStruct);
// 使能DMA2 Stream0中断
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
// 使能DMA2 Stream0
DMA_Cmd(DMA2_Stream0, ENABLE);
// 使能ADC1和ADC2
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
// 8. 启动 ADC 软件触发转换(若配置为软件触发)
ADC_SoftwareStartConv(ADC1);
ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
// 配置DMA2 Stream0中断
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
while (DMA_GetCmdStatus(DMA2_Stream0) != ENABLE);
}