STM32F407 ADC采样丢点

2019-07-20 22:07发布



用了ADC1的2通道和ADC2的3通道,注入和规则都同时的模式,然后DMA采用双缓存模式传输回ADC的CDR寄存器,高16位是ADC2转换后的值,低16位是ADC1转换后的值。ADC的采样率是21Khz,采的是2khz的三角波,然后采集回来ADC1的波形如下,如果采用单个ADC,然后DAM还是双缓存,采集的结果是对的。所以问问为什么?

配置代码如下;
/* ½á11ìåéùÃ÷*/
        GPIO_InitTypeDef GPIO_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        ADC_InitTypeDef  ADC_InitStructure;
        DMA_InitTypeDef  DMA_InitStructure;
       
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1ÄüADC1ê±Öó
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //ê1ÄüADC1ê±Öó
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);//ê1ÄüDMA2ê±Öó
       
  //Ïè3õê¼»ˉADC1í¨μà2 IO¿ú
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//PA2 í¨μà2
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//Ä£Äaêäèë
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//2»′øéÏÏÂà-
  GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉ  

        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);          //ADC1¸′λ
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);        //¸′λ½áêø         
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,ENABLE);          //ADC2¸′λ
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC2,DISABLE);        //¸′λ½áêø         

        /*ADC common config*/
  ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult_InjecSimult;//ADC_DualMode_RegSimult_InjecSimult;   //
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//ὸö2éÑù½×¶ÎÖ®¼äμÄÑó3ù5¸öê±Öó
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//Ô¤·ÖÆμ4·ÖÆμ¡£ADCCLK=PCLK2/4=84/4=21Mhz,ADCê±Öó×îoÃ2»òa3¬1y36Mhz
  ADC_CommonInit(&ADC_CommonInitStructure);//3õê¼»ˉ
       
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12λÄ£ê½
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//·Çé¨Ãèģ꽠       μ¥í¨μàê±1رÕ£¬¶àí¨μà걿aÆô
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//á¬Dø×a»»
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//½ûÖ1′¥·¢¼ì2a£¬ê1óÃèí¼t′¥·¢
  //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò¶ÔÆë       
  ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸ö×a»»Ôú1æÔòDòáDÖD
  ADC_Init(ADC1, &ADC_InitStructure);//ADC3õê¼»ˉ
       
        ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12λÄ£ê½
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//·Çé¨Ãèģ꽠       μ¥í¨μàê±1رÕ£¬¶àí¨μà걿aÆô
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//á¬Dø×a»»
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//½ûÖ1′¥·¢¼ì2a£¬ê1óÃèí¼t′¥·¢
  //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò¶ÔÆë       
  ADC_InitStructure.ADC_NbrOfConversion = 1;//1¸ö×a»»Ôú1æÔòDòáDÖD  
  ADC_Init(ADC2, &ADC_InitStructure);//ADC3õê¼»ˉ
       
        //éèÖÃÖ¸¶¨ADCμÄ1æÔò×éí¨μ࣬ò»¸öDòáD£¬2éÑùê±¼ä
        ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_480Cycles );        //ADC1,ADCí¨μà,480¸öÖüÆú,ìá¸ß2éÑùê±¼ä¿éòÔìá¸ß¾«è·¶è               
        ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 1, ADC_SampleTime_480Cycles );        //ADC2,í¨μà3,480¸öÖüÆú,ìá¸ß2éÑùê±¼ä¿éòÔìá¸ß¾«è·¶è               
       
        /* Enable DMA request after last transfer (Multi-ADC mode) */
  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);
       
        ADC_Cmd(ADC1, ENABLE);//¿aÆôAD×a»»Æ÷       
        ADC_Cmd(ADC2, ENABLE);//¿aÆôAD×a»»Æ÷
       
       
        DMA_DeInit(DMA2_Stream0);//¸′λ
        while (DMA_GetCmdStatus(DMA2_Stream0) != DISABLE){}//μè′yDMA¿éÅäÖÃ
       

        /* ÅäÖÃ DMA Stream */
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;  //í¨μàÑ¡Ôñ0
  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC->CDR;//DMAíaéèμØÖ·
  DMA_InitStructure.DMA_Memory0BaseAddr =(u32)&adc_buffa;//DMA ′æ′¢Æ÷0μØÖ·
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//íaéèμ½′æ′¢Æ÷ģ꽠      0
  DMA_InitStructure.DMA_BufferSize = 20;//êy¾Y′«êäá¿
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·ÇÔöá¿Ä£ê½ 0
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′æ′¢Æ÷Ôöá¿Ä£ê½ 0
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//íaéèêy¾Y3¤¶è:16λ  0
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//′æ′¢Æ÷êy¾Y3¤¶è:16λ    0
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// ê1óÃÑ-»·Ä£ê½  0
  DMA_InitStructure.DMA_Priority = DMA_Priority_High ;//¸ßμèóÅÏ輶   0
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;//DMA_FIFOMode_Disable;      0   
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;//DMA_FIFOThreshold_HalfFull;  0
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//′æ′¢Æ÷í»·¢μ¥′Î′«êä
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//íaéèí»·¢μ¥′Î′«êä 0
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);//3õê¼»ˉDMA Stream
       
                /*open dualbuffer mode*/
        DMA_DoubleBufferModeConfig(DMA2_Stream0, (uint32_t)adc_buffb, DMA_Memory_0);  
  DMA_DoubleBufferModeCmd(DMA2_Stream0, ENABLE);
       
        DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);   //clear int bit
  DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);   //enable int
       
        ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE); //Ô′êy¾Y±ä»ˉ걿aÆôDMA′«êä
        ADC_DMACmd(ADC1,ENABLE);//ê1ÄüADCμÄDMA′«êä
        ADC_DMACmd(ADC2,ENABLE);//ê1ÄüADCμÄDMA′«êä
        ADC_SoftwareStartConv(ADC1);//  ¿aÆôADC1μÄ×a»»
        ADC_SoftwareStartConv(ADC2);//  ¿aÆôADC2μÄ×a»»
        DMA_Cmd(DMA2_Stream0, ENABLE);                      //¿aÆôDMA′«êä


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。