STM32F4x ADC 外部定时器TIM触发控制采样时间+DMA

void ADC_SR_Detect_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  /********************使能外设时钟(GPIO、ADC、DMA、TIM)**********************/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); //使能ADC3时钟
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //使能DMA2时钟
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA时钟
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟
  
  /**************************ADC GPIO初始化************************************/
  GPIO_InitStructure.GPIO_Pin = ADC_SR_PIN;        //ADC123_IN1
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;     //模拟输入
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //无上下拉
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /**************************ADC DMA NVIC配置**********************************/  
  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  /**************************ADC TIM NVIC配置******************************/  
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //定时器TIM3中断通道
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;  //抢占优先级1
  NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;        //子优先级2
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQ通道使能
  NVIC_Init(&NVIC_InitStructure);    //根据指定的参数初始化NVIC寄存器
  
  /**************************ADC DMA初始化*************************************/
  DMA_DeInit(DMA2_Stream1);
  while (DMA_GetCmdStatus(DMA2_Stream1) != DISABLE);//等待 DMA可配置
  
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;                              //DMA2通道2
  DMA_InitStructure.DMA_PeripheralBaseAddr =  (u32)&ADC3->DR;                 //外设基地址 ADC3_DR
  DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ADCSRData;                    //内存基地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;                     //DMA搬运模式 外设到内存
  DMA_InitStructure.DMA_BufferSize = 2000;                                    //缓存区大小
  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_Stream1, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream1, ENABLE);
  DMA_ITConfig(DMA2_Stream1, DMA_IT_TC, ENABLE);
  
  /**************************ADC TIM触发初始化*************************************/
  TIM_Cmd(TIM3, DISABLE);
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); //初始化定时器
  
  TIM_TimeBaseStructure.TIM_Period = 167;         //计数值  42MHz*2/1000/168=500Hz  
  TIM_TimeBaseStructure.TIM_Prescaler = 999;     //预分频器1000分频
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //时钟输入1分频
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
  TIM_ARRPreloadConfig(TIM3, ENABLE); //允许TIM3定时重载
  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);  //选择TIM3的UPDATA事件更新为触发源
  
  /***************************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_20Cycles;  //采样时间间隔
  ADC_CommonInit(&ADC_CommonInitStructure);

  /**********************ADC Init 结构体参数初始化******************************/  
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;                        //分辨率12Bit
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;                                 //开启扫描模式
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;                            //连续转换
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; //外部上升沿触发
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;        //外部触发源 TIM3_TRGO
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;                        //数据右对齐
  ADC_InitStructure.ADC_NbrOfConversion = 1;                                    //转换通道1个
  ADC_Init(ADC3, &ADC_InitStructure);
  
  
  ADC_RegularChannelConfig(ADC3,ADC_Channel_1,1,ADC_SampleTime_144Cycles); //设置指定 ADC 的规则组通道,设置它们的转化顺序和采样时间
  
  
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
  ADC_DMACmd(ADC3, ENABLE); //使能指定的ADC的DMA 
  ADC_Cmd(ADC3, ENABLE);    //使能指定的ADC
  TIM_Cmd(TIM3, ENABLE);     //使能TIM3
  //ADC_SoftwareStartConv(ADC3); //使能指定的 ADC 的软件转换启动功能

}

 

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