STM32用DMA實現(xiàn)多路ADC通道數(shù)據(jù)采集
找了不少例子參考,ADC和DMA的設(shè)置了解了個大概,并直接利用開發(fā)板來做一些實驗來驗證相關(guān)的操作,保證自己對各部分設(shè)置的理解。
本文引用地址:http://m.butianyuan.cn/article/201611/318275.htm我這里用了3路的ADC通道,1路外部變阻器輸入,另外兩路是內(nèi)部的溫度采樣和Vrefint,這樣就能組成連續(xù)的采樣,來測試多通道ADC自動掃描了,ADC分規(guī)則轉(zhuǎn)換和注入轉(zhuǎn)換,其實規(guī)則轉(zhuǎn)換就是按照既定的設(shè)定來順序轉(zhuǎn)換,而注入轉(zhuǎn)換就是可以在這順序隊列中插隊一樣,能夠提前轉(zhuǎn)換了。
初始化設(shè)置:
1 //PC0 FOR ANAGLE SAMPLE2 static void Protect_ClkInit(void)3 {4 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1RCC_APB2Periph_GPIOC,ENABLE);5 RCC_ADCCLKConfig(RCC_PCLK2_Div6);6 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);7 }8 9 static void Protect_GPIOInit(void)10 {11 GPIO_InitTypeDef GPIO_InitStructure;12 13 /*GPIO PhaseA_H 初始化*/14 GPIO_InitStructure.GPIO_Pin = ADC_PIN;15 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;16 GPIO_Init(TIMER_GPIO_AH_PORT, &GPIO_InitStructure);17 }18 19 static void Protect_AdcInit(void)20 {21 ADC_InitTypeDef ADC_InitStructure;22 23 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;24 ADC_InitStructure.ADC_ScanConvMode = ENABLE;25 ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;26 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;27 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;28 ADC_InitStructure.ADC_NbrOfChannel = 3;29 ADC_Init(ADC1, &ADC_InitStructure);30 31 ADC_TempSensorVrefintCmd(ENABLE);32 ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_7Cycles5);33 ADC_RegularChannelConfig(ADC1,ADC_Channel_TempSensor,2,ADC_SampleTime_7Cycles5);34 ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,3,ADC_SampleTime_7Cycles5);35 36 ADC_DMACmd(ADC1, ENABLE);37 /* Enable ADC1 external trigger */38 ADC_ExternalTrigConvCmd(ADC1, DISABLE);39 40 ADC_Cmd(ADC1, ENABLE);41 42 ADC_ResetCalibration(ADC1);43 44 while(ADC_GetResetCalibrationStatus(ADC1));45 46 ADC_StartCalibration(ADC1);47 48 while(ADC_GetCalibrationStatus(ADC1));49 50 }51 52 static void Protect_DMAInit(void)53 {54 55 DMA_InitTypeDef DMA_InitStructure;56 57 /* DMA1 channel1 configuration -*/58 DMA_DeInit(DMA1_Channel1);59 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) & (ADC1->DR);60 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;61 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;62 DMA_InitStructure.DMA_BufferSize = 3;63 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;64 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;65 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;66 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;67 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;68 DMA_InitStructure.DMA_Priority = DMA_Priority_High;69 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;70 DMA_Init(DMA1_Channel1, &DMA_InitStructure);71 72 /* Enable DMA1 channel1 */73 DMA_Cmd(DMA1_Channel1, ENABLE);74 }
設(shè)置好后測試,發(fā)現(xiàn)一個比較嚴重的問題,我在調(diào)變阻器的時候,發(fā)現(xiàn)會影響其他2路采樣的數(shù)據(jù),且數(shù)據(jù)變化比較大,我就先測試ADC的參考電壓即Vref+和Vref-,沒發(fā)現(xiàn)變化,那采樣的初始化是否會有問題,在網(wǎng)上找了不少的資料,都沒表明我的設(shè)置有問題,不過還是發(fā)現(xiàn)了一個不同,就是ADC_sampling_time的時間不同。那么我就把它設(shè)置成:
1 ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_239Cycles5);2 ADC_RegularChannelConfig(ADC1,ADC_Channel_TempSensor,2,ADC_SampleTime_239Cycles5);3 ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,3,ADC_SampleTime_239Cycles5);
即最大的采樣時間,結(jié)果發(fā)現(xiàn)確實是這個問題,后來又試了下其他幾個采樣時間,最短也要ADC_SampleTime_71Cycles5,不然數(shù)據(jù)都會被影響,大概采樣周期不能太短, 不然DMA數(shù)據(jù)傳輸可能會被影響。
評論