stm32f407ADC_DMA配置

void ADC1_Init_DMA(uint32_t mar,uint32_t size)//mar为储存器地址,size为传的数据个数
{    
    GPIO_InitTypeDef  GPIO_InitStructure;
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	ADC_InitTypeDef       ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);/*DMA2的时钟使能*/
    while(DMA_GetCmdStatus(DMA2_Stream0)!=DISABLE);/*等待DMA可以配置*/


    DMA_InitStructure.DMA_Channel = DMA_Channel_0;/*DMA通道0*/
    DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;/*外设地址*/
    DMA_InitStructure.DMA_Memory0BaseAddr    = mar;/*存取器地址*/
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;/*方向从外设到内存*/
    DMA_InitStructure.DMA_BufferSize = size;/*数据传输的数量为*/
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;/*地址不增加*/
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;/*地址增加*/
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;/*数据长度半字*/
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;/*数据长度半字*/
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;/*高优先级*/
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;/*循环模式*/
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;/*禁止FIFO*/
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;/*FIFO的值*/
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;/*单次传输*/
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;/*单次传输*/
    DMA_Init(DMA2_Stream0,&DMA_InitStructure);
    DMA_Cmd(DMA2_Stream0,ENABLE);//开启DMA传输
	
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIOA时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟

  //先初始化ADC1通道5 IO口
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//PA5 通道5
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//模拟输入
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//不带上下拉
    GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化  
 
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);	  //ADC1复位
	RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);	//复位结束	 
 
	
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//独立模式
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两个采样阶段之间的延迟5个时钟
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //DMA失能
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//预分频4分频。ADCCLK=PCLK2/4=84/4=21Mhz,ADC时钟最好不要超过36Mhz 
    ADC_CommonInit(&ADC_CommonInitStructure);//初始化
        
      ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12位模式
      ADC_InitStructure.ADC_ScanConvMode = ENABLE;//扫描模式	
      ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//连续转换
      ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//禁止触发检测,使用软件触发
      ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//右对齐	
      ADC_InitStructure.ADC_NbrOfConversion = 1;//1个转换在规则序列中 也就是只转换规则序列1 
      ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
        
      ADC_Cmd(ADC1, ENABLE);//开启AD转换器	
      ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_480Cycles );	//ADC1,ADC通道,480个周期,提高采样时间可以提高精确度			    
      
      ADC_SoftwareStartConv(ADC1);		//使能指定的ADC1的软件转换启动功能
      ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);//源数据变化时开启DMA传输
      ADC_DMACmd(ADC1,ENABLE);//使能ADC传输
}

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页