描述:用ADC連續(xù)采集11路模擬信號(hào),并由DMA傳輸?shù)絻?nèi)存。ADC配置為掃描并且連續(xù)轉(zhuǎn)換模式,ADC的時(shí)鐘配置為12MHZ。在每次轉(zhuǎn)換結(jié)束后,由DMA循環(huán)將轉(zhuǎn)換的數(shù)據(jù)傳輸?shù)絻?nèi)存中。ADC可以連續(xù)采集N次求平均值。最后通過串口傳輸出最后轉(zhuǎn)換的結(jié)果。
程序如下:
#i nclude “stm32f10x.h” //這個(gè)頭文件包括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個(gè)通道
vu16 AD_Value[N][M]; //用來存放ADC轉(zhuǎn)換結(jié)果,也是DMA的目標(biāo)地址
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; //因?yàn)閁SART1管腳是以復(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準(zhǔn)備好
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通道時(shí)鐘,各個(gè)管腳時(shí)鐘
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC最大時(shí)間不能超過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工作在獨(dú)立模式
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ù)右對(duì)齊
ADC_InitStructure.ADC_NbrOfChannel = M; //順序進(jìn)行規(guī)則轉(zhuǎn)換的ADC通道的數(shù)目
ADC_Init(ADC1, &ADC_InitStructure); //根據(jù)ADC_InitStruct中指定的參數(shù)初始化外設(shè)ADCx的寄存器
//設(shè)置指定ADC的規(guī)則組通道,設(shè)置它們的轉(zhuǎn)化順序和采樣時(shí)間
//ADC1,ADC通道x,規(guī)則采樣順序值為y,采樣時(shí)間為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支持(要實(shí)現(xiàn)DMA功能,還需獨(dú)立配置DMA通道等參數(shù))
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
ADC_ResetCalibration(ADC1); //復(fù)位指定的ADC1的校準(zhǔn)寄存器
while(ADC_GetResetCalibrationStatu
本文引用地址: http://www.21ic.com/app/mcu/201810/778451.htm
s(ADC1)); //獲取ADC1復(fù)位校準(zhǔn)寄存器的狀態(tài),設(shè)置狀態(tài)則等待
ADC_StartCalibration(ADC1); //開始指定ADC1的校準(zhǔn)狀態(tài)
while(ADC_GetCalibrationStatus(ADC1)); //獲取指定ADC1的校準(zhǔn)程序,設(shè)置狀態(tài)則等待
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1); //將DMA的通道1寄存器重設(shè)為缺省值
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1-》DR; //DMA外設(shè)ADC基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; //DMA內(nèi)存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //內(nèi)存作為數(shù)據(jù)傳輸?shù)哪康牡?/p>
DMA_InitStructure.DMA_BufferSize = N*M; //DMA通道的DMA緩存的大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設(shè)地址寄存器不變
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //內(nèi)存地址寄存器遞增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //數(shù)據(jù)寬度為16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //數(shù)據(jù)寬度為16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循環(huán)緩存模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x擁有高優(yōu)先級(jí)
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x沒有設(shè)置為內(nèi)存到內(nèi)存?zhèn)鬏?/p>
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //根據(jù)DMA_InitStruct中指定的參數(shù)初始化DMA的通道
}
//配置所有外設(shè)
void Init_All_Periph(void)
{
RCC_Configuration();
GPIO_Configuration();
ADC1_Configuration();
DMA_Configuration();
//USART1_Configuration();
USART_Configuration(9600);
}
u16 GetVolt(u16 advalue)
{
return (u16)(advalue * 330 / 4096); //求的結(jié)果擴(kuò)大了100倍,方便下面求出小數(shù)
}
void filter(void)
{
int sum = 0;
u8 count;
for(i=0;i《12;i++)
{
for ( count=0;count
{
sum += AD_Value[count][i];
}
After_filter[i]=sum/N;
sum=0;
}
}
int main(void)
{
u16 value[M];
init_All_Periph();
SysTick_Initaize();
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE); //啟動(dòng)DMA通道
while(1)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//等待傳輸完成否則第一位數(shù)據(jù)容易丟失
filter();
for(i=0;i《12;i++)
{
value[i]= GetVolt(After_filter[i]);
printf(“value[%d]:\t%d.%dv\n”,i,value[i]/100,value[i]0) ;
delay_ms(100);
}
}
}
總結(jié)
該程序中的兩個(gè)宏定義,M和N,分別代表有多少個(gè)通道,每個(gè)通道轉(zhuǎn)換多少次,可以修改其值。
曾出現(xiàn)的問題:配置時(shí)鐘時(shí)要知道外部晶振是多少,以便準(zhǔn)確配置時(shí)鐘。將轉(zhuǎn)換值由二進(jìn)制轉(zhuǎn)換為十進(jìn)制時(shí),要先擴(kuò)大100倍,方便顯示小數(shù)。最后串口輸出時(shí)在 printf語(yǔ)句之前加這句代碼,防止輸出的第一位數(shù)據(jù)丟失:while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
評(píng)論
查看更多