打印
[STM32F0]

STM32F051 AD多路采集DMA方式

[复制链接]
1341|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wzchenzhe|  楼主 | 2016-7-5 12:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "adc.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address                0x40012440


__IO uint16_t RegularConvData_Tab[50];
__IO uint16_t AD_CH[5];  


void ADC1_DMA_Init(void)
{
  ADC_InitTypeDef     ADC_InitStruct;
  DMA_InitTypeDef     DMA_InitStruct;
        GPIO_InitTypeDef    GPIO_InitStruct;
         NVIC_InitTypeDef NVIC_InitStructure;
         
        RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);//AD时钟不超过14MHZ
          /* ADC1 DeInit */  
  ADC_DeInit(ADC1);

        /* Enable  GPIOA clock */
          RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_GPIOC, ENABLE);
  /* ADC1 Periph clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  /* DMA1 clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);

  /* Configure PA.01  as analog input */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOB, &GPIO_InitStruct);                                // PB0,PB1输入时不用设置速率
       
        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_4|GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStruct);                                //PC0 PC4,PC5,输入时不用设置速率

/*****************DMA中断**********************************************/
   NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
  /* DMA1 Channel1 Config */
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&RegularConvData_Tab;
  DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStruct.DMA_BufferSize =50;  //DMA缓冲数组
  DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; //工作在循环方式
  DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStruct);
       
    DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
  /* DMA1 Channel1 enable */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
//   /* ADC DMA request in circular mode */
  ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
  
  /* Enable ADC_DMA */
//  ADC_DMACmd(ADC1, ENABLE);  
  
  /* Initialize ADC structure */
  ADC_StructInit(&ADC_InitStruct);
  
  /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits  */
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;                   //连续采样模式
  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
        //ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T15_TRGO;//T15定时器触发
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStruct.ADC_ScanDirection = ADC_ScanDirection_Upward;//Backward为16->0扫描 Upward位0->16扫描方式  ;
  ADC_Init(ADC1, &ADC_InitStruct);

//   /* Convert the ADC1 temperature sensor  with 55.5 Cycles as sampling time */
//   ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles);  
//   ADC_TempSensorCmd(ENABLE);
  
  /* Convert the ADC1 Vref  with 55.5 Cycles as sampling time */
  ADC_ChannelConfig(ADC1, ADC_Channel_8  , ADC_SampleTime_239_5Cycles); //选择ad8通道PB0
        ADC_ChannelConfig(ADC1, ADC_Channel_9  , ADC_SampleTime_239_5Cycles); //选择ad9通道PB1
        ADC_ChannelConfig(ADC1, ADC_Channel_10 , ADC_SampleTime_239_5Cycles); //选择ad10通道PC0
        ADC_ChannelConfig(ADC1, ADC_Channel_14 , ADC_SampleTime_239_5Cycles ); //选择ad14通道PC4
        ADC_ChannelConfig(ADC1, ADC_Channel_15 , ADC_SampleTime_239_5Cycles ); //选择ad15通道PC5
       
       
//   ADC_VrefintCmd(ENABLE);
  
  /* ADC Calibration */
  ADC_GetCalibrationFactor(ADC1);//开始ADC效验
  ADC_DMACmd(ADC1, ENABLE);     //开启ADC的DMA使能
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);          //使能指定ADC1   
// ADC_DMACmd(ADC1, ENABLE);     //开启ADC的DMA使能
  /* Wait the ADCEN falg */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));
  
  /* ADC1 regular Software Start Conv */
  ADC_StartOfConversion(ADC1);

}
/*************DMA中断*********************/
void DMA1_Channel1_IRQHandler(void)
{ //平均值10
         uint32_t  sum = 0;
   if(DMA_GetFlagStatus(DMA1_FLAG_TC1) != RESET ) //??DMA1TC1??
   {               
                                        sum= RegularConvData_Tab[0]+ RegularConvData_Tab[5]+ RegularConvData_Tab[10]+RegularConvData_Tab[15]+ RegularConvData_Tab[20]+ RegularConvData_Tab[25]+ RegularConvData_Tab[30] +RegularConvData_Tab[35]+RegularConvData_Tab[40]+ RegularConvData_Tab[45];//+ RegularConvData_Tab[50] +RegularConvData_Tab[55]+RegularConvData_Tab[60]+ RegularConvData_Tab[65]+ RegularConvData_Tab[70]+ RegularConvData_Tab[75] +RegularConvData_Tab[80]+RegularConvData_Tab[85]+ RegularConvData_Tab[90]+ RegularConvData_Tab[95];
                      AD_CH[0]=sum/10;     //AN8 PB0   AD2    rt100采集
                      sum= RegularConvData_Tab[1]+ RegularConvData_Tab[6]+ RegularConvData_Tab[11]+RegularConvData_Tab[16]+ RegularConvData_Tab[21]+ RegularConvData_Tab[26]+ RegularConvData_Tab[31] +RegularConvData_Tab[36]+RegularConvData_Tab[41]+ RegularConvData_Tab[46];//+ RegularConvData_Tab[51] +RegularConvData_Tab[56]+RegularConvData_Tab[61]+ RegularConvData_Tab[66]+ RegularConvData_Tab[71]+ RegularConvData_Tab[76] +RegularConvData_Tab[81]+RegularConvData_Tab[86]+ RegularConvData_Tab[91]+ RegularConvData_Tab[96];
                      AD_CH[1]=sum/10;     //AN9   PB1  AD3   rt100采集
                      sum= RegularConvData_Tab[2]+ RegularConvData_Tab[7]+ RegularConvData_Tab[12]+RegularConvData_Tab[17]+ RegularConvData_Tab[22]+ RegularConvData_Tab[27]+ RegularConvData_Tab[32] +RegularConvData_Tab[37]+RegularConvData_Tab[42]+ RegularConvData_Tab[47];//+ RegularConvData_Tab[52] +RegularConvData_Tab[57]+RegularConvData_Tab[62]+ RegularConvData_Tab[67]+ RegularConvData_Tab[72]+ RegularConvData_Tab[77] +RegularConvData_Tab[82]+RegularConvData_Tab[87]+ RegularConvData_Tab[92]+ RegularConvData_Tab[97];
                      AD_CH[4]=sum/10;     //AN10  PC0  AD4  0-10V采集处理
                      sum= RegularConvData_Tab[3]+ RegularConvData_Tab[8]+ RegularConvData_Tab[13]+RegularConvData_Tab[18]+ RegularConvData_Tab[23]+ RegularConvData_Tab[28]+ RegularConvData_Tab[33] +RegularConvData_Tab[38]+RegularConvData_Tab[43]+ RegularConvData_Tab[48];//+ RegularConvData_Tab[52] +RegularConvData_Tab[57]+RegularConvData_Tab[62]+ RegularConvData_Tab[67]+ RegularConvData_Tab[72]+ RegularConvData_Tab[77] +RegularConvData_Tab[82]+RegularConvData_Tab[87]+ RegularConvData_Tab[92]+ RegularConvData_Tab[97];
                      AD_CH[2]=sum/10;     //AN14  PC4  AD0  4-20ma采集处理
                      sum= RegularConvData_Tab[4]+ RegularConvData_Tab[9]+ RegularConvData_Tab[14]+RegularConvData_Tab[19]+ RegularConvData_Tab[24]+ RegularConvData_Tab[29]+ RegularConvData_Tab[34] +RegularConvData_Tab[39]+RegularConvData_Tab[44]+ RegularConvData_Tab[49];//+ RegularConvData_Tab[52] +RegularConvData_Tab[57]+RegularConvData_Tab[62]+ RegularConvData_Tab[67]+ RegularConvData_Tab[72]+ RegularConvData_Tab[77] +RegularConvData_Tab[82]+RegularConvData_Tab[87]+ RegularConvData_Tab[92]+ RegularConvData_Tab[97];
                      AD_CH[3]=sum/10;     //AN15  PC5  AD1  4-20ma采集处理
        DMA_ClearFlag(DMA1_FLAG_TC1);//清DMA中断TC1
                                      
    }
}
沙发
Jessicakjdsl| | 2016-7-5 14:35 | 只看该作者
多路采集是一个adc的,那多个adc同时采集用dma怎样实现呢?

使用特权

评论回复
板凳
mmuuss586| | 2016-7-5 20:33 | 只看该作者
谢谢分享

使用特权

评论回复
地板
ZHOU@JIE| | 2016-8-15 13:10 | 只看该作者
中断里面难道不应该先关掉DMA在计数均值吗?不关掉DMA数据不会冲掉?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

12

帖子

0

粉丝