STM32F4x ADC配置 独立模式多通道采集+DMA

 

void ADC_Volt_Detect_Config()
{
  GPIO_InitTypeDef GPIO_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;

  /********************使能外设时钟(GPIO、ADC、DMA、TIM)**********************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能 GPIOA时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能 APB2 外设ADC1时钟
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //使能 APB2 外设DMA2时钟
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  //使能 TIM3 时钟
  
  /**************************ADC DMA初始化*************************************/
  DMA_DeInit(DMA2_Stream0);
  while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE);   //等待 DMA可配置
  
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;                                //开启DMA2通道0
  DMA_InitStructure.DMA_PeripheralBaseAddr =  (u32)&ADC1->DR;                   //DMA对应的外设基地址 ADC1 DR
  DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ADCVoltData;                    //内存存储基地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;                       //DMA的转换模式为SRC模式,由外设搬移到内存
  DMA_InitStructure.DMA_BufferSize = 3000;                                      //DMA缓存大小 3000
  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;           //DMA搬移数据尺寸,HalfWord就是为16位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;                               //循环转换模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;                           //DMA优先级高
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;                         //打开FIFO模式
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;                 //DMA fifo阈值级别FULL
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;                   //DMA内存单次传输
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;           //DMA外设单次传输
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  
  /**************************ADC GPIO初始化************************************/
  GPIO_InitStructure.GPIO_Pin = ADC_5V_PIN|ADC_12V_PIN|ADC_24V_PIN;      //ADC123_IN10/12/11
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;                           //模拟输入
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;                       //无上下拉
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  /**************************ADC NVIC配置**************************************/
  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /**************************DMA2_Stream0 enable ******************************/
  DMA_Cmd(DMA2_Stream0, ENABLE);
  DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
  
  /***************************ADC Common Init**********************************/  
  ADC_DeInit();
    
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                      //ADC独立模式
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;                   //选择时钟频率 2分频
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;       //禁止 DMA 直接访问模式
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;  //采样时间间隔
  ADC_CommonInit(&ADC_CommonInitStructure);
  
  /**********************ADC Init 结构体参数初始化******************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;                        //分辨率12Bit
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;                                  //开启扫描模式
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;                            //连续转换
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;   //禁止外部边沿触发
  //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;       //外部触发源
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                        //数据右对齐
  ADC_InitStructure.ADC_NbrOfConversion = 3;                                    //转换通道3个
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /*******************配置 ADC 通道转换顺序和采样时间周期************************/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_480Cycles); //ADCVoltData[i][0] 此项为5V电压数据  采样时间144cycles
  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_480Cycles); //ADCVoltData[i][1] 此项为24V电压数据 采样时间144cycles
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_480Cycles); //ADCVoltData[i][2] 此项为12V电压数据 采样时间144cycles

  ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);                             //使能 DMA 请求 after last transfer (Single-ADC mode)
  ADC_DMACmd(ADC1, ENABLE);                                                    //使能ADC1 DMA
  ADC_Cmd(ADC1, ENABLE);                                                       //使能ADC1
  ADC_SoftwareStartConv(ADC1);                                                 //开始ADC1 转换,软件触发
}

 

你可能感兴趣的:(嵌入式/单片机编程)