使用STM32F4的ADC采集4路电压信号,通过DMA传输,可是程序一直不能进入中断

2019-07-20 13:26发布

[mw_shl_code=c,true]主程序
u16     ad_value[32][4];
u16     ad_qq[4];
float   ADC_V[4];


int main(void)
{
  delay_init(168);
        uart_init(115200);
  Adc_Init();
        LED_Init();
        delay_ms(10);
        LED0=0;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        MYDMA_Config(DMA2_Stream0,(u32)&ADC1->DR,(u32)&ADC_V);
        while(1);
}

void DMA2_Stream0_IRQHandler(void)
{
  if(DMA_GetITStatus(DMA2_Stream0,DMA_IT_TCIF0) != RESET)
        {
          int  sum = 0;
    u8 count,i;
          u8 N=32;
                LED0=0;
    for(i=0;i<4;i++)
    {
      for ( count=0;count<N;count++)
      {
        sum += ad_value[count];
      }
      ad_qq=sum/N;
      sum=0;
   }
         for(i=0;i<4;i++)
                {
                        ADC_V=(float)ad_qq*3.3/4026;
                  printf("%f",ADC_V);
                        delay_ms(1);
                }
                printf(" ");
                delay_ms(1);
          DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
        }
}

ADC程序
void  Adc_Init(void)
{   
  GPIO_InitTypeDef  GPIO_InitStructure;
        ADC_CommonInitTypeDef ADC_CommonInitStructure;
        ADC_InitTypeDef       ADC_InitStructure;
       
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1&#196;üGPIOAê±&#214;ó
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ê1&#196;üADC1ê±&#214;ó


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_5|GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;//&#196;£&#196;aê&#228;è&#235;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//2&#187;′&#248;é&#207;&#207;&#194;à-
  GPIO_Init(GPIOA, &GPIO_InitStructure);//3&#245;ê&#188;&#187;ˉ  

        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);          //ADC1&#184;′&#206;&#187;
        RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);        //&#184;′&#206;&#187;&#189;áê&#248;         

       
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;//&#182;àá¢&#196;£ê&#189;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//á&#189;&#184;&#246;2é&#209;ù&#189;×&#182;&#206;&#214;&#174;&#188;&#228;μ&#196;&#209;ó3ù5&#184;&#246;ê±&#214;ó
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//&#212;¤·&#214;&#198;μ4·&#214;&#198;μ&#161;£ADCCLK=PCLK2/4=84/4=21Mhz,ADCê±&#214;ó×&#238;o&#195;2&#187;òa3&#172;1y36Mhz
  ADC_CommonInit(&ADC_CommonInitStructure);//3&#245;ê&#188;&#187;ˉ
       
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;//12&#206;&#187;&#196;£ê&#189;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//&#189;&#251;&#214;1′¥·¢&#188;ì2a£&#172;ê1ó&#195;èí&#188;t′¥·¢
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//óò&#182;&#212;&#198;&#235;       
  ADC_InitStructure.ADC_NbrOfConversion = 4;
  ADC_Init(ADC1, &ADC_InitStructure);//ADC3&#245;ê&#188;&#187;ˉ
       
        ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles );        //ADC1,ADCí¨μà,480&#184;&#246;&#214;ü&#198;ú,ìá&#184;&#223;2é&#209;ùê±&#188;&#228;&#191;éò&#212;ìá&#184;&#223;&#190;&#171;è·&#182;è                       
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles );
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_480Cycles );
  ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_480Cycles );       
       
        ADC_DMARequestAfterLastTransferCmd(ADC1,ENABLE);
        ADC_DMACmd(ADC1,ENABLE);
        ADC_Cmd(ADC1, ENABLE);       
        ADC_SoftwareStartConv(ADC1);
}

DMA程序
void MYDMA_Config(DMA_Stream_TypeDef *DMA_Streamx,u32 par,u32 mar)
{

        DMA_InitTypeDef  DMA_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
       
        if((u32)DMA_Streamx>(u32)DMA2)//μ&#195;μ&#189;μ±&#199;°streamê&#199;ê&#244;óúDMA2&#187;1ê&#199;DMA1
        {
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2ê±&#214;óê1&#196;ü
               
        }else
        {
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);//DMA1ê±&#214;óê1&#196;ü
        }
  DMA_DeInit(DMA_Streamx);
       
        while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}//μè′yDMA&#191;é&#197;&#228;&#214;&#195;
       
  /* &#197;&#228;&#214;&#195; DMA Stream */
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;  //í¨μà&#209;&#161;&#212;&#241;
  DMA_InitStructure.DMA_PeripheralBaseAddr = par;//DMAíaéèμ&#216;&#214;·
  DMA_InitStructure.DMA_Memory0BaseAddr = mar;//DMA ′&#230;′¢&#198;÷0μ&#216;&#214;·
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//íaéèμ&#189;′&#230;′¢&#198;÷
  DMA_InitStructure.DMA_BufferSize = 4;//êy&#190;Y′&#171;ê&#228;á&#191;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//íaéè·&#199;&#212;&#246;á&#191;&#196;£ê&#189;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//′&#230;′¢&#198;÷&#212;&#246;á&#191;&#196;£ê&#189;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//íaéèêy&#190;Y3¤&#182;è:16&#206;&#187;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//′&#230;′¢&#198;÷êy&#190;Y3¤&#182;è:16&#206;&#187;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//&#209;-&#187;·&#196;£ê&#189;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//&#214;Dμèó&#197;&#207;è&#188;&#182;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//′&#230;′¢&#198;÷í&#187;·¢μ¥′&#206;′&#171;ê&#228;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//íaéèí&#187;·¢μ¥′&#206;′&#171;ê&#228;
  DMA_Init(DMA_Streamx, &DMA_InitStructure);//3&#245;ê&#188;&#187;ˉDMA Stream

        DMA_Cmd(DMA_Streamx, ENABLE);                      //&#191;a&#198;&#244;DMA′&#171;ê&#228;
               
  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);   
       
  DMA_ITConfig(DMA2_Stream0,DMA_IT_TC, ENABLE);
}         
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。