2019 8 10 STM32F407ADC1M采样频率相关设置

て烟熏妆下的殇ゞ 提交于 2019-11-27 16:17:23
  GPIO_InitTypeDef  GPIO_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef       ADC_InitStructure;
    
    ADC_DeInit();
  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 = DISABLE;//·ÇɨÃèģʽ    
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//¹Ø±ÕÁ¬Ðøת»»
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;//´¥·¢¼ì²â,ÉÏÉýÑØ´¥·¢
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T5_CC1;        //TIM5_CC1´¥·¢
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ÓÒ¶ÔÆë    
  ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸öת»»ÔÚ¹æÔòÐòÁÐÖÐ Ò²¾ÍÊÇֻת»»¹æÔòÐòÁÐ1 
  ADC_Init(ADC1, &ADC_InitStructure);//ADC³õʼ»¯
    
    ADC1->CR2|=0x0200;  //ADCDMA循环传输模式,必须把DDS位置1
    ADC_DMACmd(ADC1,ENABLE);
    
    //400KHz²ÉÑùƵÂÊ£¬2.5us=£¨12+²ÉÑùʱ¼ä£©/21M£¬²ÉÑùʱ¼ä<40.5¸öÖÜÆÚ
    //1MHZ采样频率,1us = (12+采样时间)/21M  采样时间<9个周期
    ADC_Cmd(ADC1, ENABLE);//¿ªÆôADת»»Æ÷    
    ADC_RegularChannelConfig(ADC1, 5, 1, ADC_SampleTime_3Cycles );    //ADC1,ADCͨµÀ,28¸öÖÜÆÚ
    

DMA相关配置,启动了DMA中断

    DMA_InitTypeDef  DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;  //NVIC¿âº¯Êý½á¹¹Ìå
    
    if((u32)DMA_Streamx>(u32)DMA2)//µÃµ½µ±Ç°streamÊÇÊôÓÚDMA2»¹ÊÇDMA1
    {
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ʱÖÓʹÄÜ 
        
    }else 
    {
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ʱÖÓʹÄÜ 
    }
  DMA_DeInit(DMA_Streamx);
    
    while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//µÈ´ýDMA¿ÉÅäÖà 
    
  /* ÅäÖÃ DMA Stream */
  DMA_InitStructure.DMA_Channel = chx;  //ͨµÀÑ¡Ôñ
  DMA_InitStructure.DMA_PeripheralBaseAddr = par;                                    //DMAÍâÉèµØÖ·
  DMA_InitStructure.DMA_Memory0BaseAddr = mar;                                        //DMA ´æ´¢Æ÷0µØÖ·
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;                    //´æ´¢Æ÷µ½ÍâÉèģʽ
  DMA_InitStructure.DMA_BufferSize = ndtr;                                                //Êý¾Ý´«ÊäÁ¿ 
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//ÍâÉè·ÇÔöÁ¿Ä£Ê½
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;                    //´æ´¢Æ÷ÔöÁ¿Ä£Ê½
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//ÍâÉèÊý¾Ý³¤¶È:8λ
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word ;//´æ´¢Æ÷Êý¾Ý³¤¶È:8λ
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;                                        // ʹÓÃÆÕͨģʽ 
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;                                //ÖеÈÓÅÏȼ¶
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;                    //´æ´¢Æ÷Í»·¢µ¥´Î´«Êä
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;    //ÍâÉèÍ»·¢µ¥´Î´«Êä
  DMA_Init(DMA_Streamx, &DMA_InitStructure);                                                    //³õʼ»¯DMA Stream
        
    //DMA NVIC ÅäÖÃ
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;    //DMAÖжÏ
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //ÏÈÕ¼ÓÅÏȼ¶2¼¶
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;         //´ÓÓÅÏȼ¶1¼¶
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            //IRQͨµÀ±»Ê¹ÄÜ
    NVIC_Init(&NVIC_InitStructure);  //¸ù¾ÝNVIC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèNVIC¼Ä´æÆ÷    
    
    DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE);               //¿ªÆôDMA2_Stream0´«ÊäÍê³ÉÖжÏÔ´
    //DMA2_Stream0->CR|= (1<<5);

 

开始一次采样(有同步信号效果更佳)

//        DMA_Cmd(DMA2_Stream0, ENABLE);                      //¿ªÆôDMA´«Êä 
        DMA2_Stream0->CR |= 1; 
//        ADC_Cmd(ADC1, ENABLE);//¿ªÆôADת»»Æ÷    
        ADC1->CR2 |= 1;

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!