STM32F4 adc+dma多通道採集
阿新 • • 發佈:2018-12-11
這裡使用STM32F407實現adc的多通道採集,使用的感測器為灰度感測器與紅外感測器,在面放出程式碼,具體的解釋在註釋中都寫的很詳細。
感測器的IO口配置
void DMA_GPIO_config() { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能GPIOA時鐘 //先初始化ADC1通道IO口(灰度) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | //灰度感測器暫時保留2個介面 GPIO_Pin_2 | GPIO_Pin_3 | //紅外感測器PA埠 GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //模擬輸入 GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化 //先初始化ADC1通道IO口(紅外) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ; //紅外感測器PA埠 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_Init(GPIOC, &GPIO_InitStructure); }
adc初始化配置
void DMA_ADC_config() { ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1時鐘 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE); //ADC1復位 RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE); //復位結束 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //獨立模式 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_10Cycles; //兩個取樣階段之間的延遲x個時鐘 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; //DMA使能(DMA傳輸下要設定使能) ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //預分頻4分頻。ADCCLK=PCLK2/4=84/4=21Mhz,ADC時鐘最好不要超過36Mhz ADC_CommonInit(&ADC_CommonInitStructure); //初始化 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12位模式 ADC_InitStructure.ADC_ScanConvMode =ENABLE; //掃描(開啟DMA傳輸要設定掃描) ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //開啟連續轉換(開啟DMA傳輸要設定連續轉換) ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //禁止觸發檢測,使用軟體觸發 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右對齊 ADC_InitStructure.ADC_NbrOfConversion = 12; //有幾個通道傳輸就寫幾 (DMA傳輸下要設定為通道數) ADC_Init(ADC1, &ADC_InitStructure); //ADC初始化 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_144Cycles); //res[0] ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_144Cycles); //res[1] ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_144Cycles); //res[2] ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_144Cycles); //res[3] ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_144Cycles); //res[4] ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_144Cycles); //res[5] ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_144Cycles); //res[6] ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_144Cycles); //res[7] ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 9, ADC_SampleTime_144Cycles); //res[8]-PC0 ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 10, ADC_SampleTime_144Cycles); //res[9]-PC1 ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 11, ADC_SampleTime_144Cycles); //res[8]-PC2 ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 12, ADC_SampleTime_144Cycles); //res[9]-PC3 ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); //開啟AD轉換器 }
DMA各通道配置
//DMAx的各通道配置 //這裡的傳輸形式是固定的,這點要根據不同的情況來修改 //從儲存器->外設模式/8位資料寬度/儲存器增量模式 //DMA_Streamx:DMA資料流,DMA1_Stream0~7/DMA2_Stream0~7 //chx:DMA通道選擇,@ref DMA_channel DMA_Channel_0~DMA_Channel_7 //par:外設地址 //mar:儲存器地址 //ndtr:資料傳輸量 void DMA_config(DMA_Stream_TypeDef *DMA_Streamx, u32 chx, u32 par, u32 mar, u16 ndtr) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; if((u32)DMA_Streamx>(u32)DMA2) //得到當前stream是屬於DMA2還是DMA1 { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE); //DMA2時鐘使能 }else { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); //DMA1時鐘使能 } DMA_DeInit(DMA_Streamx); while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){} //等待DMA可配置 /* 配置 DMA Stream */ DMA_InitStructure.DMA_Channel = chx; //通道選擇 DMA_InitStructure.DMA_PeripheralBaseAddr = par; //DMA外設地址 DMA_InitStructure.DMA_Memory0BaseAddr = mar; //DMA 儲存器0地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //儲存器到外設模式 DMA_InitStructure.DMA_BufferSize = ndtr; //資料傳輸量 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外設非增量模式 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //儲存器增量模式 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外設資料長度:8位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //儲存器資料長度:8位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //使用普通模式 DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //中等優先順序 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; //儲存器突發單次傳輸 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; //外設突發單次傳輸 DMA_Init(DMA_Streamx, &DMA_InitStructure); //初始化DMA Stream DMA_ClearFlag(DMA2_Stream0,DMA_IT_TC); DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE); NVIC_InitStructure.NVIC_IRQChannel=DMA2_Stream0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //搶佔優先順序 NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x01; //響應優先順序 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init(&NVIC_InitStructure); while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE){} DMA_Cmd(DMA2_Stream0, ENABLE); }
整體的配置函式
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx, u32 chx, u32 par, u32 mar, u16 ndtr)
{
DMA_GPIO_config();
DMA_ADC_config();
DMA_config(DMA_Streamx, chx, par, mar, ndtr);
}
將採集到的adc值放入陣列中儲存
#define PER 12 //外設數量
#define NUM 10 //採集次數
u16 res[PER];
u16 fir[NUM][PER];
extern u16 res[PER];
extern u16 fir[NUM][PER];
void filter(void)
{
register u16 sum=0;
u8 count=0,i=0,j=0;
for(;i<PER;i++)
{
while(j<NUM)
{//865
if(fir[j][i]<0){}
else
{
sum+=fir[j][i];
count++;
}
j++;
}
res[i]=sum/count;
sum=0;count=0;j=0;
}
}
DMA傳輸開啟函式
//開啟一次DMA傳輸
//DMA_Streamx:DMA資料流,DMA1_Stream0~7/DMA2_Stream0~7
//ndtr:資料傳輸量
void DMA2_Stream0_IRQHandler(void)
{
if (DMA_GetFlagStatus(DMA2_Stream0, DMA_IT_TCIF0) == SET)
{
filter();
DMA_ClearFlag(DMA2_Stream0, DMA_IT_TCIF0);
}
}
最後在主函式中呼叫以下兩句即可開啟傳輸
MYDMA_Config(DMA2_Stream0, DMA_Channel_0, (u32)&ADC1->DR, (u32)fir, NUM*PER);
ADC_SoftwareStartConv(ADC1); //開啟DMA