1--STM32 ADC1與ADC2 16通道DMA採集筆記(原創)

最近在搞ADC,網上仍是不少資源的,
如下爲參考連接:
一、對STM32 ADC單次轉換模式 連續轉換模式 掃描模式的理解:
https://www.cnblogs.com/zhanghankui/p/5192324.html/
二、STM32F103ADC的工做模式和觸發方式的探索與理解:
http://www.stmcu.org.cn/module/forum/thread-598744-1-1.html
三、STM32 ADC單通道與多通道_DMA學習筆記:
https://blog.csdn.net/dmfylb/article/details/72802690?utm_source=blogxgwz17/
四、STM32F407ADC多通道+定時器觸發+DMA模式設置:
https://www.cnblogs.com/longbiao831/p/6688006.htmlhtml

STM32 採集16路ADC的有好幾種方法,根據實際產品應用,附源代碼。
(1)ADC+DMA+中斷+濾波函數
(2)同步模式—ADC1+ADC2+DMA(無中斷),這個調了很久, 
(3)採用TIM外部觸發,後面再實際測試OK上傳。數組


ADC1與ADC2+DMA ,放在AD_DATE【16】的數組裏面
代碼1:緩存

//16路ADC轉換 #define AD_Count 50 //AD濾波採樣次數 #define N 50 //每一個通道採集的次數 #define M 16 //16個通道 u16 ADCresults[M][AD_Count]; //AD採集值 extern u32 AD_Data[M]; //AD採集值最終能夠調用 extern u8 flag_ADC; //DMA中斷標誌位 //__IO==volatile 不讓編譯器進行優化,即每次讀取或者修改值的時候,都必須從新從內存或者寄存器中讀取或者修改 __IO u16 ADC_ConvertedValue[AD_Count][M]; static void ADC1_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE ); //能夠不須要使能ADC1通道時鐘與GPIOA時鐘 //PA0~PA7 做爲模擬通道輸入引腳 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5 |GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模擬輸入引腳 GPIO_Init(GPIOA, &GPIO_InitStructure); //PC0~PC5 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); //PB0~PB1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); } static void ADC1_Mode_Init(void) { ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE ); RCC_ADCCLKConfig(RCC_PCLK2_Div6); //設置ADC分頻因子6 72M/6=12,ADC最大時間不能超過14M ADC_DeInit(ADC1); //復位ADC1 /*初始化ADC結構體,此句必須加,不加的話多路ADC數據會交換*/ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工做模式:ADC1工做在獨立模式 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 = 16; //順序進行規則轉換的ADC通道的數目 ADC_Init(ADC1, &ADC_InitStructure); //根據ADC_InitStruct中指定的參數初始化外設ADCx的寄存器 //設置指定ADC的規則組通道,設置轉換順序和採樣時間 //ADC1,ADC通道x,規則採樣順序爲y,採樣時間239週期---後續看硬件調試   ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //PA0 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //PA1 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //PA2 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //PA3 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 ); //PA4 ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 ); //PA5 ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 ); //PA6 ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 ); //PA7 ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_239Cycles5 ); //PB0 ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 10, ADC_SampleTime_239Cycles5 ); //PB1 ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 11, ADC_SampleTime_239Cycles5 ); //PC0 ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 12, ADC_SampleTime_239Cycles5 ); //PC1 ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 13, ADC_SampleTime_239Cycles5 ); //PC2 ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 14, ADC_SampleTime_239Cycles5 ); //PC3 ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 15, ADC_SampleTime_239Cycles5 ); //PC4 ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 16, ADC_SampleTime_239Cycles5 ); //PC5 //開啓DMA 下面配置DMA參數   ADC_DMACmd(ADC1, ENABLE); //使能ADC ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //使能復位校準 while(ADC_GetResetCalibrationStatus(ADC1)); //等待復位校準結束 ADC_StartCalibration(ADC1); //開啓AD校準 while(ADC_GetCalibrationStatus(ADC1)); //等待校準結束 ADC_SoftwareStartConvCmd(ADC1, ENABLE); //連續轉換開始,ADC經過DMA方式不斷的更新RAM區 } static void ADC1_DMA_Init(void) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA時鐘 DMA_DeInit(DMA1_Channel1); // DMA通道一寄存器設爲缺省值 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //外設ADC基地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // DMA內存基地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 內存做爲目的地 DMA_InitStructure.DMA_BufferSize = 16* AD_Count; // 緩衝數據大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外設地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 內存寄存器遞增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //數據寬度16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 數據寬度16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 循環緩衝模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /*清除一次DMA中斷標誌*/ DMA_ClearITPendingBit(DMA1_IT_TC1); /*使能DMA傳輸完成中斷*/ DMA_ITConfig(DMA1_Channel1, DMA1_IT_TC1, ENABLE); //中斷優先級NVIC設置 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);    DMA_Cmd(DMA1_Channel1, ENABLE);//啓動DMA通道 } void ADC1_Init(void) { ADC1_GPIO_Init(); ADC1_Mode_Init(); ADC1_DMA_Init(); } void ADC_Filter(void) //濾波函數AD_Count { u8 i, j; /*從DMA緩存中取出AD數據*/ for(i=0; i<16; i++) { for(j=0; j<AD_Count; j++) { ADCresults[i][j] = ADC_ConvertedValue[j][i]; } } /*取值求和取平均*/ for(i=0; i<16; i++) { AD_Data[i] = 0; for(j=0; j<AD_Count; j++) { AD_Data[i] += ADCresults[i][j]; } AD_Data[i] = AD_Data[i] / AD_Count; AD_Data[i]=(AD_Data[i] * 128 / 4096); //0~3.3轉成0~127存在數組AD_Data } } void DMA1_Channel1_IRQHandler() { /*判斷DMA傳輸完成中斷*/ if(DMA_GetITStatus(DMA1_IT_TC1) != RESET) { flag_ADC = 1; //被main函數調用判斷 } /*清除DMA中斷標誌位*/ DMA_ClearITPendingBit(DMA1_IT_TC1); }

採用同步模式,ADC1和ADC2的DMA傳送,高16位位ADC2,低16位位ADC1的值。app

代碼2:函數

__IO u32 ADC_ConvertedValue[8]; float adc_buf[16]; extern u32 ADC_filter[16]; static void ADC1_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //PA0~PA7 做爲模擬通道輸入引腳 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2 |GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5 |GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模擬輸入引腳 GPIO_Init(GPIOA, &GPIO_InitStructure); //PC0~PC5 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); //PB0~PB1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); } static void ADC1_Mode_Init(void) {   ADC_InitTypeDef ADC_InitStructure;   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE );    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //設置ADC分頻因子6 72M/6=12,ADC最大時間不能超過14M ADC_DeInit(ADC1); //復位ADC1    ADC_DeInit(ADC2); //復位ADC1 /*初始化ADC結構體,此句必須加,不加的話多路ADC數據會交換*/ //ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //ADC工做模式:ADC1工做在獨立模式 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 = 8; //順序進行規則轉換的ADC通道的數目 //設置指定ADC的規則組通道,設置轉換順序和採樣時間 //ADC1,ADC通道x,規則採樣順序爲y,採樣時間239週期---後續看硬件調試    ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //PA0 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //PA1 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //PA2 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //PA3 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 ); //PA4 ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 ); //PA5 ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 ); //PA6 ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 ); //PA7 ADC_Init(ADC1, &ADC_InitStructure); // ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; //ADC工做模式:ADC1工做在獨立模式 // ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模數轉換工做在單通道模式 // ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模數轉換工做在單次轉換模式 // ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //轉換由軟件觸發 // ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC數據右對齊 // ADC_InitStructure.ADC_NbrOfChannel = 8; //順序進行規則轉換的ADC通道的數目 ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5 ); //PB0 ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 2, ADC_SampleTime_239Cycles5 ); //PB1 ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 3, ADC_SampleTime_239Cycles5 ); //PC0   ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 4, ADC_SampleTime_239Cycles5 ); //PC1 ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 5, ADC_SampleTime_239Cycles5 ); //PC2 ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 6, ADC_SampleTime_239Cycles5 ); //PC3 ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 7, ADC_SampleTime_239Cycles5 ); //PC4 ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 8, ADC_SampleTime_239Cycles5 ); //PC5 ADC_Init(ADC2, &ADC_InitStructure); //開啓DMA 下面配置DMA參數 ADC_DMACmd(ADC1, ENABLE); /**** ADC1 ****/ ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_ResetCalibration(ADC1); //使能復位校準 while(ADC_GetResetCalibrationStatus(ADC1)); //等待復位校準結束 ADC_StartCalibration(ADC1); //開啓AD校準 while(ADC_GetCalibrationStatus(ADC1)); //等待校準結束 /**** ADC2 ****/ ADC_Cmd(ADC2, ENABLE); ADC_ResetCalibration(ADC2); while(ADC_GetResetCalibrationStatus(ADC2)); ADC_StartCalibration(ADC2); while(ADC_GetCalibrationStatus(ADC2)); /**** Enable****/ ADC_ExternalTrigConvCmd(ADC2, ENABLE); // 使能ADC2外部觸發轉換    ADC_SoftwareStartConvCmd(ADC1, ENABLE);    ADC_SoftwareStartConvCmd(ADC2, ENABLE); } static void ADC1_DMA_Init(void) //後面加中斷 { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA時鐘 DMA_DeInit(DMA1_Channel1); // DMA通道一寄存器設爲缺省值 DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(ADC1->DR)); //外設ADC基地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // DMA內存基地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 內存做爲目的地 DMA_InitStructure.DMA_BufferSize = 8; // 緩衝數據大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外設地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; // 內存寄存器遞增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //數據寬度4字節 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; // 數據寬度 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 循環緩衝模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // DMA_Init(DMA1_Channel1, &DMA_InitStructure); // 初始化函數    DMA_Cmd(DMA1_Channel1, ENABLE);//啓動DMA通道 } void ADC1_Init(void) { ADC1_GPIO_Init(); ADC1_Mode_Init(); ADC1_DMA_Init(); } void ADC_F() { u8 num; static double cell_temp[16] = {0};   adc_buf[0] =(int)(u16)ADC_ConvertedValue[0];//A1 adc_buf[1] =(int)(u16)ADC_ConvertedValue[1];//A2 adc_buf[2] =(int)(u16)ADC_ConvertedValue[2];//A3 adc_buf[3] =(int)(u16)ADC_ConvertedValue[3];//A4 adc_buf[4] =(int)(u16)ADC_ConvertedValue[4];//A4 adc_buf[5] =(int)(u16)ADC_ConvertedValue[5];//A4 adc_buf[6] =(int)(u16)ADC_ConvertedValue[6];//A4 adc_buf[7] =(int)(u16)ADC_ConvertedValue[7];//A4 adc_buf[8] =(int)(ADC_ConvertedValue[0]>>16);//A8 adc_buf[9] =(int)(ADC_ConvertedValue[1]>>16);//A8 adc_buf[10]=(int)(ADC_ConvertedValue[2]>>16);//A8 adc_buf[11]=(int)(ADC_ConvertedValue[3]>>16);//A8 adc_buf[12]=(int)(ADC_ConvertedValue[4]>>16);//A8 adc_buf[13]=(int)(ADC_ConvertedValue[5]>>16);//A8 adc_buf[14]=(int)(ADC_ConvertedValue[6]>>16);//A8 adc_buf[15]=(int)(ADC_ConvertedValue[7]>>16);//A8 for(num=0;num<16;num++) { ADC_filter[num]=adc_buf[num]/4096*128; } }
相關文章
相關標籤/搜索