STM32 ADC多通道转化
描绘:用ADC接连收集11路模仿信号,并由DMA传输到内存。ADC装备为扫描而且接连转化方式,ADC的时钟装备为12MHZ。在每次转化完毕后,由DMA循环将转化的数据传输到内存中。ADC能够接连收集N次求平均值。最终经过串口传输出最终转化的成果。
程序如下:
#i nclude “stm32f10x.h” //这个头文件包含STM32F10x一切外围存放器、位、内存映射的界说
#i nclude “eval.h” //头文件(包含串口、按键、LED的函数声明)
#i nclude “SysTickDelay.h”
#i nclude “UART_INTERFACE.h”
#i nclude
#define N 50 //每通道采50次
#define M 12 //为12个通道
vu16 AD_Value[N][M]; //用来存放ADC转化成果,也是DMA的方针地址
vu16 After_filter[M]; //用来存放求平均值之后的成果
int i;
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //由于USART1管脚是以复用的方式接到GPIO口上的,所以运用复用推挽式输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA0/1/2 作为模仿通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模仿输入引脚
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB0/1 作为模仿通道输入引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模仿输入引脚
GPIO_Init(GPIOB, &GPIO_InitStructure);
//PC0/1/2/3/4/5 作为模仿通道输入引脚
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);
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit(); //RCC 体系复位
RCC_HSEConfig(RCC_HSE_ON); //敞开HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等候HSE准备好
if(HSEStartUpStatus == SUCCESS)
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
FLASH_SetLatency(FLASH_Latency_2); //Set 2 Latency cycles
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB clock = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //APB2 clock = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); //APB1 clock = HCLK/2
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); //PLLCLK = 12MHz * 6 = 72 MHz
RCC_PLLCmd(ENABLE); //Enable PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock source
while(RCC_GetSYSCLKSource() != 0x08); //Wait till PLL is used as system clock source
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO |RCC_APB2Periph_USART1, ENABLE ); //使能ADC1通道时钟,各个管脚时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC最大时刻不能超过14M
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA传输
}
}
void ADC1_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_DeInit(ADC1); //将外设 ADC1 的悉数存放器重设为缺省值
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC作业方式:ADC1和ADC2作业在独立方式
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 = M; //次序进行规矩转化的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //依据ADC_InitStruct中指定的参数初始化外设ADCx的存放器
//设置指定ADC的规矩组通道,设置它们的转化次序和采样时刻
//ADC1,ADC通道x,规矩采样次序值为y,采样时刻为239.5周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 5, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 6, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 7, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 8, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 9, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 10, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 11, ADC_SampleTime_239Cycles5 );
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 12, ADC_SampleTime_239Cycles5 );
// 敞开ADC的DMA支撑(要完成DMA功用,还需独立装备DMA通道等参数)
ADC_DMACmd(ADC1, ENABLE);