扫描模式+非连续触发
uint16_t Adc_Value[5] = {0};
DMA_InitTypeDef DMA_InitStructure;
void AD_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// -------------------------------------GPIO配置---------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// ---------------------------------------ADC配置----------------------------------------
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; // DMA配置
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_8Cycles;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // 分辨率
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // 扫描
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // 非连续
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 无外部触发ADC
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO; // 触发事件:因无外部触发,无用配置
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 5; // ADC转换通道数
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 3, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 4, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 5, ADC_SampleTime_84Cycles); // 配置通道和采样周期
// --------------------------------------DMA配置----------------------------------------
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Adc_Value;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 5;
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_MemoryBurst = DMA_MemoryBurst_Single;
// DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
}
// ----------------------------------main()中循环调用------------------------------------
void AD_ValueTransfer(void){
DMA_Cmd(DMA2_Stream0, DISABLE);
ADC_DMACmd(ADC1, DISABLE);
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
ADC_DMACmd(ADC1, ENABLE);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_SoftwareStartConv(ADC1); //软件触发ADC1,此时ADC1进入采样转换状态
while(DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0) == RESET);
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
}
// ------------------------------------获取转换值--------------------------------------
uint16_t Get_Adc_Value(uint8_t i){
return Adc_Value[i];
}
扫描模式+连续触发
uint16_t Adc_Value[5] = {0};
DMA_InitTypeDef DMA_InitStructure;
void AD_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// -------------------------------------GPIO配置---------------------------------------
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// ---------------------------------------ADC配置----------------------------------------
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; // DMA配置
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_8Cycles;
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
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO; // 触发事件:因无外部触发,无用配置
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 5; // ADC转换通道数
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 3, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 4, ADC_SampleTime_84Cycles); // 配置通道和采样周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 5, ADC_SampleTime_84Cycles); // 配置通道和采样周期
// --------------------------------------DMA配置----------------------------------------
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Adc_Value;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 5;
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_MemoryBurst = DMA_MemoryBurst_Single;
// DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1); //软件触发ADC1,此时ADC1进入采样转换状态
}
// -------------------------------main()中直接获取转换值---------------------------------
uint16_t Get_Adc_Value(uint8_t i){
return Adc_Value[i];
}
标签:DMA,ENABLE,AD,多通道,InitStructure,ADC,ADC1,GPIO
From: https://blog.csdn.net/qq_39683145/article/details/140349324