新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 經(jīng)典_STM32_ADC多通道采樣的例子

經(jīng)典_STM32_ADC多通道采樣的例子

作者: 時間:2016-11-28 來源:網(wǎng)絡(luò) 收藏
STM32 ADC多通道轉(zhuǎn)換
描述:用ADC連續(xù)采集11路模擬信號,并由DMA傳輸?shù)絻?nèi)存。ADC配置為掃描并且連續(xù)轉(zhuǎn)換模式,ADC的時鐘配置為12MHZ。在每次轉(zhuǎn)換結(jié)束后,由DMA循環(huán)將轉(zhuǎn)換的數(shù)據(jù)傳輸?shù)絻?nèi)存中。ADC可以連續(xù)采集N次求平均值。最后通過串口傳輸出最后轉(zhuǎn)換的結(jié)果。
程序如下:
#i nclude "stm32f10x.h" //這個頭文件包括STM32F10x所有外圍寄存器、位、內(nèi)存映射的定義
#i nclude "eval.h" //頭文件(包括串口、按鍵、LED的函數(shù)聲明)
#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轉(zhuǎn)換結(jié)果,也是DMA的目標地址
vu16 After_filter[M]; //用來存放求平均值之后的結(jié)果
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管腳是以復(fù)用的形式接到GPIO口上的,所以使用復(fù)用推挽式輸出
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 系統(tǒng)復(fù)位
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); //將外設(shè) ADC1 的全部寄存器重設(shè)為缺省值


ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在獨立模式
ADC_InitStructure.ADC_ScanConvMode =ENABLE; //模數(shù)轉(zhuǎn)換工作在掃描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模數(shù)轉(zhuǎn)換工作在連續(xù)轉(zhuǎn)換模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //外部觸發(fā)轉(zhuǎn)換關(guān)閉
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC數(shù)據(jù)右對齊
ADC_InitStructure.ADC_NbrOfChannel = M; //順序進行規(guī)則轉(zhuǎn)換的ADC通道的數(shù)目
ADC_Init(ADC1, &ADC_InitStructure); //根據(jù)ADC_InitStruct中指定的參數(shù)初始化外設(shè)ADCx的寄存器


//設(shè)置指定ADC的規(guī)則組通道,設(shè)置它們的轉(zhuǎn)化順序和采樣時間
//ADC1,ADC通道x,規(guī)則采樣順序值為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支持(要實現(xiàn)DMA功能,還需獨立配置DMA通道等參數(shù))
ADC_DMACmd(ADC1, ENABLE);

上一頁 1 2 下一頁

關(guān)鍵詞: STM32ADC多通道采

評論


技術(shù)專區(qū)

關(guān)閉