STM32 ADC 同步规则模式(两通道)

操作系统 2016-10-14

感觉直接贴代码会好点。。。。。。

1 #include "adc.h"
  2 
  3 float AD1_Value[N];
  4 float AD2_Value[N];
  5 volatile u32 ADCConvertedValue[N];//ADת»»»º³åÇø,Õ¼ÓÃRAM 0.8KB
  6 
  7 
  8 void  Adc_Init(void)
  9 {     
 10     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 11     ADC_InitTypeDef ADC_InitStructure; 
 12     GPIO_InitTypeDef GPIO_InitStructure;
 13     DMA_InitTypeDef DMA_InitStructure;
 14     NVIC_InitTypeDef NVIC_InitStructure;
 15     ////////////////////////////////////  Enable Clock   ///////////////////////////////////////////////////////////////////////
 16     RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC the Max Time Is Lager Than 14M
 17     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 
 18     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1    , ENABLE );      //ʹÄÜADC1ͨµÀʱÖÓ
 19     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC2    , ENABLE );      //ʹÄÜADC2ͨµÀʱÖÓ
 20 //    RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC the Max Time Is Lager Than 14M        
 21     
 22     //PA0.PA1.PA4.PA5 ×÷ΪģÄâͨµÀÊäÈëÒý½Å                         
 23     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1;
 24     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //Ä£ÄâÊäÈëÒý½Å
 25     GPIO_Init(GPIOA, &GPIO_InitStructure);    
 26     
 27 ////////////////////////////////////////Initialize DMA /////////////////////////////////////////////////////////////////////    
 28     DMA_DeInit(DMA1_Channel1); //Reset DMA_Channel_1's Register To Default Value
 29     DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA Device to ADC's Base Address
 30     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue; //DMA Memory Base Address    
 31 //    DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue; //DMA Memory Base Address
 32     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //Memory be use to Data Chansminsion's Destination
 33     DMA_InitStructure.DMA_BufferSize = N; //DMA Channel's DMA Buffer Size
 34     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //Outside Address's Register Is not Change 
 35     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Buffer Address's Register Is Increasing
 36     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //Data size is 16 Bit
 37     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //Data Size Is 16 Bit 
 38     DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //Work in Cycle Cache Mode
 39 
 40     DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA Channel X Have The Higest Priority
 41     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA Channel X do not set up to Chansmission in Memory to Memory
 42     DMA_Init(DMA1_Channel1, &DMA_InitStructure); //According to DMA_InitStruct to Initialize the DMA Channel    
 43     DMA_Cmd(DMA1_Channel1,ENABLE);                               //ʹÄÜDMA1ͨµÀ1
 44 
 45 
 46 ////////////////////////////////////////Initialize NVIC /////////////////////////////////////////////////////////////////////    
 47     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
 48     NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
 49     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
 50     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
 51     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 52     NVIC_Init(&NVIC_InitStructure);
 53     DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);            //ʹÄÜDMA CH1ÖÐ¶Ï  
 64 ////////////////////////////////////////Initialize ADC /////////////////////////////////////////////////////////////////////    
 65 
 66     ADC_DeInit(ADC1);  //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
 67     ADC_DeInit(ADC2);  //¸´Î» ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
 68     
 69     ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;    //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
 70     ADC_InitStructure.ADC_ScanConvMode = ENABLE;    //ADC Works in Scan Model
 71     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;    //ADC works in Continious Changing Model
 72     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;    //ADC is triggered by sofeware instead of Outside
 73     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;    //ADC Data Right Align
 74     ADC_InitStructure.ADC_NbrOfChannel = 1;    //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
 75     ADC_Init(ADC1, &ADC_InitStructure);    //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷   
 76     ADC_RegularChannelConfig(ADC1, ADC_Channel_1,  1, ADC_SampleTime_1Cycles5 );
 77     ADC_DMACmd(ADC1,ENABLE);                         //ʹÄÜADC1 DMA  
 84     ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;    //ADC¹¤×÷ģʽ:ADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
 85     ADC_InitStructure.ADC_ScanConvMode = ENABLE;    //ADC Works in Scan Model
 86     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;    //ADC works in Continious Changing Model
 87     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;    //ADC is triggered by sofeware instead of Outside
 88     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;    //ADC Data Right Align
 89     ADC_InitStructure.ADC_NbrOfChannel = 1;    //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
 90     ADC_RegularChannelConfig(ADC2, ADC_Channel_2,  1, ADC_SampleTime_1Cycles5 );
 91     ADC_Init(ADC2, &ADC_InitStructure);    //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷       
 92     ADC_ExternalTrigConvCmd(ADC2, ENABLE); 
 93     
 94     
 95     ADC_DMACmd(ADC1, ENABLE);  
 96     ADC_Cmd(ADC1, ENABLE);    //ʹÄÜÖ¸¶¨µÄADC1 98     
 99     ADC_ResetCalibration(ADC1);    //ʹÄܸ´Î»Ð£×¼  
100     while(ADC_GetResetCalibrationStatus(ADC1));    //µÈ´ý¸´Î»Ð£×¼½áÊø
101     ADC_StartCalibration(ADC1);     //¿ªÆôADУ׼
102     while(ADC_GetCalibrationStatus(ADC1));     //µÈ´ýУ׼½áÊø
103     
104     ADC_Cmd(ADC2, ENABLE);
105     ADC_ResetCalibration(ADC2);    //ʹÄܸ´Î»Ð£×¼  
106     while(ADC_GetResetCalibrationStatus(ADC2));    //µÈ´ý¸´Î»Ð£×¼½áÊø
107     ADC_StartCalibration(ADC2);     //¿ªÆôADУ׼
108     while(ADC_GetCalibrationStatus(ADC2));     //µÈ´ýУ׼½áÊø
109 
110 //    delay_ms(10);
111     ADC_SoftwareStartConvCmd(ADC1, ENABLE);        //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ
112 }    
113 
114 
115 void DMA1_Channel1_IRQHandler()
116 {  
117     u16 i;
118     u16 temp1,temp2;
119   DMA_ClearFlag(DMA1_FLAG_TC1);    //清除DMA传输完成中断
120     DMA_Cmd(DMA1_Channel1,DISABLE);    
121     for(i = 0; i < N ; i++)
122     {
123         temp1 = (ADCConvertedValue[i]<<16)>>16;
124         temp2 = ADCConvertedValue[i] >> 16;        
125         AD1_Value[i] =((float)temp1)*(3.3/4096); 
126         AD2_Value[i] =((float)temp2)*(3.3/4096); 
127         Get_tem_buf(i);
128     }
129     DMA_Cmd(DMA1_Channel1,ENABLE);   
131     LED0 = ~LED0;
132 }

有些注释直接从Keil5里面粘出来到这里就不支持了。。。。。。。好尴尬。。。。下次码代码注释还是全英算了、、、

哈哈。。有什么问题可以一起来探讨、、、不知道为啥分类不到嵌入式那里只好点Linux那里了。。

相关推荐