STM32多通道ADC的DMA方式采集
硬件:STM32F103C8T6 软件:KEIL MDK 5.23 固件库:STM32F1标准外设库。 调试软件:XCOM串口调试助手
stm32有多达16个通道,常用的采样方法有两种,一是分时采集每个通道的数据,用查询或者中断的方式采集完一个通道的数据,将通道设置为下一个,依次采集,这种方法思路简单,但是效率不高,适合初学者。二是利用DMA功能采集多个规则通道的数据,。
本文使用代码主要基于野火bsp库修改。
bsp_adc.h
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
#define Vref 3300.0
#define ADC_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_CLK RCC_APB2Periph_ADC1
#define ADC_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_GPIO_CLK1 RCC_APB2Periph_GPIOA
#define ADC_PORT1 GPIOA
#define ADC_GPIO_CLK2 RCC_APB2Periph_GPIOB
#define ADC_PORT2 GPIOB
#define ADC_GPIO_CLK3 RCC_APB2Periph_GPIOC
#define ADC_PORT3 GPIOC
#define NOFCHANEL 16
#define ADC_PIN1 GPIO_Pin_0
#define ADC_CHANNEL1 ADC_Channel_0
#define ADC_PIN2 GPIO_Pin_1
#define ADC_CHANNEL2 ADC_Channel_1
#define ADC_PIN3 GPIO_Pin_2
#define ADC_CHANNEL3 ADC_Channel_2
#define ADC_PIN4 GPIO_Pin_3
#define ADC_CHANNEL4 ADC_Channel_3
#define ADC_PIN5 GPIO_Pin_4
#define ADC_CHANNEL5 ADC_Channel_4
#define ADC_PIN6 GPIO_Pin_5
#define ADC_CHANNEL6 ADC_Channel_5
#define ADC_PIN7 GPIO_Pin_6
#define ADC_CHANNEL7 ADC_Channel_6
#define ADC_PIN8 GPIO_Pin_7
#define ADC_CHANNEL8 ADC_Channel_7
#define ADC_PIN9 GPIO_Pin_0
#define ADC_CHANNEL9 ADC_Channel_8
#define ADC_PIN10 GPIO_Pin_1
#define ADC_CHANNEL10 ADC_Channel_9
#define ADC_PIN11 GPIO_Pin_0
#define ADC_CHANNEL11 ADC_Channel_10
#define ADC_PIN12 GPIO_Pin_1
#define ADC_CHANNEL12 ADC_Channel_11
#define ADC_PIN13 GPIO_Pin_2
#define ADC_CHANNEL13 ADC_Channel_12
#define ADC_PIN14 GPIO_Pin_3
#define ADC_CHANNEL14 ADC_Channel_13
#define ADC_PIN15 GPIO_Pin_4
#define ADC_CHANNEL15 ADC_Channel_14
#define ADC_PIN16 GPIO_Pin_5
#define ADC_CHANNEL16 ADC_Channel_15
#define ADC_x ADC1
#define ADC_DMA_CHANNEL DMA1_Channel1
#define ADC_DMA_CLK RCC_AHBPeriph_DMA1
void ADCx_Init(void);
void filter(int in[5],int N);
void get_adc(void);
#endif
bsp_adc.c
#include "bsp_adc.h"
#include <stdio.h>
#include <math.h>
#include "usart.h"
extern int adc[NOFCHANEL];
int out[5];
__IO uint16_t ADC_ConvertedValue[NOFCHANEL]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
static void ADCx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_GPIO_APBxClock_FUN (ADC_GPIO_CLK1, ENABLE );
GPIO_InitStructure.GPIO_Pin = ADC_PIN1|
ADC_PIN2|
ADC_PIN3|
ADC_PIN4|
ADC_PIN5|
ADC_PIN6|
ADC_PIN7|
ADC_PIN8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_PORT1, &GPIO_InitStructure);
ADC_GPIO_APBxClock_FUN (ADC_GPIO_CLK2, ENABLE );
GPIO_InitStructure.GPIO_Pin = ADC_PIN9|
ADC_PIN10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_PORT2, &GPIO_InitStructure);
ADC_GPIO_APBxClock_FUN (ADC_GPIO_CLK3, ENABLE );
GPIO_InitStructure.GPIO_Pin = ADC_PIN11|
ADC_PIN12|
ADC_PIN13|
ADC_PIN14|
ADC_PIN15|
ADC_PIN16;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_PORT3, &GPIO_InitStructure);
}
static void ADCx_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_AHBPeriphClockCmd(ADC_DMA_CLK, ENABLE);
ADC_APBxClock_FUN (ADC_CLK, ENABLE);
DMA_DeInit(ADC_DMA_CHANNEL);
DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) ( & ( ADC_x->DR ) );
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = NOFCHANEL;
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(ADC_DMA_CHANNEL, &DMA_InitStructure);
DMA_Cmd(ADC_DMA_CHANNEL , ENABLE);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
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_InitStructure.ADC_NbrOfChannel = NOFCHANEL;
ADC_Init(ADC_x, &ADC_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL1 , 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL2 , 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL3 , 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL4 , 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL5 , 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL6 , 6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL7 , 7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL8 , 8, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL9 , 9, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL10, 10, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL11, 11, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL12, 12, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL13, 13, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL14, 14, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL15, 15, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_CHANNEL16, 16, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC_x, ENABLE);
ADC_Cmd(ADC_x, ENABLE);
ADC_ResetCalibration(ADC_x);
while(ADC_GetResetCalibrationStatus(ADC_x));
ADC_StartCalibration(ADC_x);
while(ADC_GetCalibrationStatus(ADC_x));
ADC_SoftwareStartConvCmd(ADC_x, ENABLE);
}
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
}
void filter(int in[5],int N)
{
int i;
if(N < 5)
{
for(i=0; i<=N-1; i++)
out[i]= in[i];
}
else
{
out[0]= (69.0* in[0]+ 4.0* in[1]- 6.0* in[2]+ 4.0* in[3]- in[4])/ 70.0;
out[1]= (2.0* in[0]+ 27.0* in[1]+ 12.0* in[2]- 8.0* in[3]+ 2.0* in[4])/ 35.0;
for(i= 2; i<= N- 3; i++)
{
out[i]= (-3.0* (in[i- 2]+ in[i+ 2])+ 12.0* (in[i- 1]+ in[i+ 1])+17.0 *in[i] )/ 35.0;
}
out[N-2]= (2.0* in[N - 5] - 8.0 * in[N - 4] + 12.0 * in[N - 3] + 27.0 * in[N - 2] + 2.0 * in[N - 1]) / 35.0;
out[N-1]= (-in[N- 5]+ 4.0* in[N- 4]- 6.0* in[N- 3]+ 4.0* in[N - 2] + 69.0 * in[N - 1]) / 70.0;
}
}
void get_adc(void)
{
int i,j,k,a[NOFCHANEL][5],c[5];
for(j=0; j<5; j++){
for(i=0; i<5; i++){
for(k=0; k<NOFCHANEL; k++){
adc[k] =(float) ADC_ConvertedValue[k]/4096*Vref;
a[k][i]=adc[k];
}
}
for(k=0;k<NOFCHANEL;k++){
for(i=0;i<5;i++){
c[i]=a[k][i];
}
filter(c,5);
adc[k]= (out[0]+out[1]+out[2]+out[3]+out[4])/5;
a[k][j]=adc[k];
}
}
for(k=0;k<NOFCHANEL;k++){
for(i=0;i<5;i++){
c[i]=a[k][i];
}
filter(c,5);
adc[k]= (out[0]+out[1]+out[2]+out[3]+out[4])/5;
}
}
main.c
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "bsp_adc.h"
#define Vref 3300.0
int adc[NOFCHANEL];
int main(void)
{
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
ADCx_Init();
while(1)
{
get_adc();
printf("\r\n-----\r\n
A%d A%d A%d A%d\r\n
A%d A%d A%d A%d\r\n
A%d A%d A%d A%d\r\n
A%d A%d A%d A%d\r\n
\r\n-----\r\n",
adc[0],adc[1],adc[2],adc[3],
adc[4],adc[5],adc[6],adc[7],
adc[8],adc[9],adc[10],adc[11],
adc[12],adc[13],adc[14],adc[15]
);
delay_ms(1000);
}
}
参考文章
[1] tpos_YU. stm32的DMA+ADC多通道数据采集 [2] 发呆健将. STM32 多通道ADC采集详解(DMA模式和非DMA模式) [3] 夜风~. STM32——多通道ADC的DMA方式采集方法
|