1--STM32 ADC1与ADC2 16通道DMA采集笔记(原创)

最近在搞ADC,网上还是很多资源的,
以下为参考链接:
1、对STM32 ADC单次转换模式 连续转换模式 扫描模式的理解:
https://www.cnblogs.com/zhanghankui/p/5192324.html/
2、STM32F103ADC的工作模式和触发方式的探索与理解:
http://www.stmcu.org.cn/module/forum/thread-598744-1-1.html
3、STM32 ADC单通道与多通道_DMA学习笔记:
https://blog.csdn.net/dmfylb/article/details/72802690?utm_source=blogxgwz17/
4、STM32F407ADC多通道+定时器触发+DMA模式设置:
https://www.cnblogs.com/longbiao831/p/6688006.html

STM32 采集16路ADC的有好几种方法,根据实际产品应用,附源代码。
(1)ADC+DMA+中断+滤波函数
(2)同步模式—ADC1+ADC2+DMA(无中断),这个调了好久, 
(3)采用TIM外部触发,后面再实际测试OK上传。


ADC1与ADC2+DMA ,放在AD_DATE【16】的数组里面
代码1:

//16路ADC转换
#define AD_Count 50 //AD滤波采样次数
#define N 50 //每个通道采集的次数
#define M 16 //16个通道
u16 ADCresults[M][AD_Count]; //AD采集值
extern u32 AD_Data[M]; //AD采集值最终可以调用
extern u8 flag_ADC; //DMA中断标志位
//__IO==volatile 不让编译器进行优化,即每次读取或者修改值的时候,都必须重新从内存或者寄存器中读取或者修改
__IO u16 ADC_ConvertedValue[AD_Count][M]; static void ADC1_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //可以不需要使能ADC1通道时钟与GPIOA时钟
//PA0~PA7 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2
|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5
|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOA, &GPIO_InitStructure); //PC0~PC5
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);
//PB0~PB1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void ADC1_Mode_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
ADC_DeInit(ADC1); //复位ADC1 /*初始化ADC结构体,此句必须加,不加的话多路ADC数据会交换*/
ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1工作在独立模式
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 = 16; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 //设置指定ADC的规则组通道,设置转换顺序和采样时间
//ADC1,ADC通道x,规则采样顺序为y,采样时间239周期---后续看硬件调试
  ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //PA0
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //PA1
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //PA2
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //PA3
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 ); //PA4
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 ); //PA5
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 ); //PA6
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 ); //PA7 ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_239Cycles5 ); //PB0
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 10, ADC_SampleTime_239Cycles5 ); //PB1 ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 11, ADC_SampleTime_239Cycles5 ); //PC0
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 12, ADC_SampleTime_239Cycles5 ); //PC1
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 13, ADC_SampleTime_239Cycles5 ); //PC2
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 14, ADC_SampleTime_239Cycles5 ); //PC3
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 15, ADC_SampleTime_239Cycles5 ); //PC4
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 16, ADC_SampleTime_239Cycles5 ); //PC5 //开启DMA 下面配置DMA参数
  ADC_DMACmd(ADC1, ENABLE);
//使能ADC
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //使能复位校准 while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束 ADC_StartCalibration(ADC1); //开启AD校准 while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束 ADC_SoftwareStartConvCmd(ADC1, ENABLE); //连续转换开始,ADC通过DMA方式不断的更新RAM区
}
static void ADC1_DMA_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟 DMA_DeInit(DMA1_Channel1); // DMA通道一寄存器设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 内存作为目的地
DMA_InitStructure.DMA_BufferSize = 16* AD_Count; // 缓冲数据大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //数据宽度16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 数据宽度16位
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); /*清除一次DMA中断标志*/
DMA_ClearITPendingBit(DMA1_IT_TC1);
/*使能DMA传输完成中断*/
DMA_ITConfig(DMA1_Channel1, DMA1_IT_TC1, ENABLE);
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);    DMA_Cmd(DMA1_Channel1, ENABLE);//启动DMA通道
}
void ADC1_Init(void)
{
ADC1_GPIO_Init();
ADC1_Mode_Init();
ADC1_DMA_Init();
}
void ADC_Filter(void) //滤波函数AD_Count
{
u8 i, j;
/*从DMA缓存中取出AD数据*/
for(i=0; i<16; i++)
{
for(j=0; j<AD_Count; j++)
{
ADCresults[i][j] = ADC_ConvertedValue[j][i];
}
} /*取值求和取平均*/
for(i=0; i<16; i++)
{
AD_Data[i] = 0;
for(j=0; j<AD_Count; j++)
{
AD_Data[i] += ADCresults[i][j];
}
AD_Data[i] = AD_Data[i] / AD_Count;
AD_Data[i]=(AD_Data[i] * 128 / 4096); //0~3.3转成0~127存在数组AD_Data }
}
void DMA1_Channel1_IRQHandler()
{
/*判断DMA传输完成中断*/
if(DMA_GetITStatus(DMA1_IT_TC1) != RESET)
{
flag_ADC = 1; //被main函数调用判断
}
/*清除DMA中断标志位*/
DMA_ClearITPendingBit(DMA1_IT_TC1);
}

1--STM32 ADC1与ADC2 16通道DMA采集笔记(原创)

采用同步模式,ADC1和ADC2的DMA传送,高16位位ADC2,低16位位ADC1的值。

代码2:

__IO u32 ADC_ConvertedValue[8];
float adc_buf[16]; extern u32 ADC_filter[16]; static void ADC1_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //PA0~PA7 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2
|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5
|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOA, &GPIO_InitStructure); //PC0~PC5
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);
//PB0~PB1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void ADC1_Mode_Init(void)
{
  ADC_InitTypeDef ADC_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE );    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
ADC_DeInit(ADC1); //复位ADC1
   ADC_DeInit(ADC2); //复位ADC1
/*初始化ADC结构体,此句必须加,不加的话多路ADC数据会交换*/
//ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //ADC工作模式:ADC1工作在独立模式
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 = 8; //顺序进行规则转换的ADC通道的数目 //设置指定ADC的规则组通道,设置转换顺序和采样时间
//ADC1,ADC通道x,规则采样顺序为y,采样时间239周期---后续看硬件调试
   ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //PA0
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //PA1
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //PA2
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //PA3
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 ); //PA4
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 ); //PA5
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 ); //PA6
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 ); //PA7
ADC_Init(ADC1, &ADC_InitStructure); // ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //ADC工作模式:ADC1工作在独立模式
// ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模数转换工作在单通道模式
// ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式
// ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件触发
// ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐
// ADC_InitStructure.ADC_NbrOfChannel = 8; //顺序进行规则转换的ADC通道的数目 ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5 ); //PB0
ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_239Cycles5 ); //PB1 ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 3, ADC_SampleTime_239Cycles5 ); //PC0
  ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 4, ADC_SampleTime_239Cycles5 ); //PC1
ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 5, ADC_SampleTime_239Cycles5 ); //PC2
ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 6, ADC_SampleTime_239Cycles5 ); //PC3
ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 7, ADC_SampleTime_239Cycles5 ); //PC4
ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 8, ADC_SampleTime_239Cycles5 ); //PC5
ADC_Init(ADC2, &ADC_InitStructure); //开启DMA 下面配置DMA参数
ADC_DMACmd(ADC1, ENABLE);
/**** ADC1 ****/ ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //使能复位校准 while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束 ADC_StartCalibration(ADC1); //开启AD校准 while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束 /**** ADC2 ****/ ADC_Cmd(ADC2, ENABLE);
ADC_ResetCalibration(ADC2);
while(ADC_GetResetCalibrationStatus(ADC2));
ADC_StartCalibration(ADC2);
while(ADC_GetCalibrationStatus(ADC2));
/**** Enable****/
ADC_ExternalTrigConvCmd(ADC2, ENABLE); // 使能ADC2外部触发转换    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
   ADC_SoftwareStartConvCmd(ADC2, ENABLE);
}
static void ADC1_DMA_Init(void) //后面加中断
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA时钟 DMA_DeInit(DMA1_Channel1); // DMA通道一寄存器设为缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(ADC1->DR)); //外设ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 内存作为目的地
DMA_InitStructure.DMA_BufferSize = 8; // 缓冲数据大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址固定
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存寄存器递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //数据宽度4字节
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; // 数据宽度
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); // 初始化函数
   DMA_Cmd(DMA1_Channel1, ENABLE);//启动DMA通道
}
void ADC1_Init(void)
{
ADC1_GPIO_Init();
ADC1_Mode_Init();
ADC1_DMA_Init();
}
void ADC_F()
{
u8 num;
static double cell_temp[16] = {0};   adc_buf[0] =(int)(u16)ADC_ConvertedValue[0];//A1
adc_buf[1] =(int)(u16)ADC_ConvertedValue[1];//A2
adc_buf[2] =(int)(u16)ADC_ConvertedValue[2];//A3
adc_buf[3] =(int)(u16)ADC_ConvertedValue[3];//A4
adc_buf[4] =(int)(u16)ADC_ConvertedValue[4];//A4
adc_buf[5] =(int)(u16)ADC_ConvertedValue[5];//A4
adc_buf[6] =(int)(u16)ADC_ConvertedValue[6];//A4
adc_buf[7] =(int)(u16)ADC_ConvertedValue[7];//A4 adc_buf[8] =(int)(ADC_ConvertedValue[0]>>16);//A8
adc_buf[9] =(int)(ADC_ConvertedValue[1]>>16);//A8
adc_buf[10]=(int)(ADC_ConvertedValue[2]>>16);//A8
adc_buf[11]=(int)(ADC_ConvertedValue[3]>>16);//A8
adc_buf[12]=(int)(ADC_ConvertedValue[4]>>16);//A8
adc_buf[13]=(int)(ADC_ConvertedValue[5]>>16);//A8
adc_buf[14]=(int)(ADC_ConvertedValue[6]>>16);//A8
adc_buf[15]=(int)(ADC_ConvertedValue[7]>>16);//A8 for(num=0;num<16;num++)
{
ADC_filter[num]=adc_buf[num]/4096*128; }
}

1--STM32 ADC1与ADC2 16通道DMA采集笔记(原创)

上一篇:Linux下编译器的安装


下一篇:Mac下配置/使用GitHub