[研电赛技术支持] STM32 ADC DMA时EOC中断异常

[复制链接]
 楼主| 慢醇 发表于 2022-1-25 22:27 | 显示全部楼层 |阅读模式
AD, DM, dc, ADC, DMA
  说明,当使用DMA时EOC中断会不正常,因为DMA后,也就是DR被读取后EOC会被自动清零。但是系统仍然可以进入中断,只不过在判断ADC_IT_EOC时会失败。把DMA关闭后,EOC中断就正常了。


 楼主| 慢醇 发表于 2022-1-25 22:28 | 显示全部楼层
 楼主| 慢醇 发表于 2022-1-25 22:30 | 显示全部楼层
  1. u32 ADC12_RESULT[8]    ;


  2. void ADC1_Config(void)
  3. {undefined

  4.     GPIO_InitTypeDef GPIO_InitStructure;
  5.     ADC_InitTypeDef ADC_InitStructure;
  6.     DMA_InitTypeDef    DMA_InitStructure ;
  7.     NVIC_InitTypeDef NVIC_InitStructure;

  8.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  9.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);     
  10.     RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);

  11. /****** Configure phase c ADC channel GPIO as analog input ***
  12.     GPIO_StructInit(&GPIO_InitStructure);
  13.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  14.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  15.     GPIO_Init(GPIOA, &GPIO_InitStructure);*/

  16.     GPIO_StructInit(&GPIO_InitStructure);
  17.     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7;
  18.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  19.     GPIO_Init(GPIOA, &GPIO_InitStructure);

 楼主| 慢醇 发表于 2022-1-25 22:34 | 显示全部楼层
  1.    /* ADC1 registers reset ----------------------------------------------------*/
  2.       ADC_DeInit(ADC1);   
  3.         ADC_TempSensorVrefintCmd(ENABLE);
  4.      //ADC_Cmd(ADC1, ENABLE);
  5.       /* ADC1 configuration ------------------------------------------------------*/
  6.      ADC_StructInit(&ADC_InitStructure);
  7.       ADC_InitStructure.ADC_Mode =ADC_Mode_Independent;//ADC_Mode_RegInjecSimult;//常规和插入都同时转换模式// ADC_Mode_RegSimult;
  8.       ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  9.       ADC_InitStructure.ADC_ContinuousConvMode=ENABLE ; //;=DISABLE
  10.       ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  11.       ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  12.       ADC_InitStructure.ADC_NbrOfChannel = 6;
  13.       ADC_Init(ADC1, &ADC_InitStructure);

  14.     ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);
  15.     ADC_RegularChannelConfig(ADC1,ADC_Channel_1,2,ADC_SampleTime_239Cycles5);
  16.     ADC_RegularChannelConfig(ADC1,ADC_Channel_2,3,ADC_SampleTime_239Cycles5);
  17.     ADC_RegularChannelConfig(ADC1,ADC_Channel_3,4,ADC_SampleTime_239Cycles5);
  18.     ADC_RegularChannelConfig(ADC1,ADC_Channel_6,5,ADC_SampleTime_239Cycles5);
  19.     ADC_RegularChannelConfig(ADC1,ADC_Channel_7,6,ADC_SampleTime_239Cycles5);
  20. //    ADC_RegularChannelConfig(ADC1,ADC_Channel_16,7,ADC_SampleTime_239Cycles5);
  21. //    ADC_RegularChannelConfig(ADC1,ADC_Channel_17,8,ADC_SampleTime_239Cycles5);

  22.       //ADC_StartCalibration(ADC3);
  23.     DMA_DeInit(DMA1_Channel1);
  24.     DMA_InitStructure.DMA_PeripheralBaseAddr    = (u32)&ADC1->DR;
  25.     DMA_InitStructure.DMA_MemoryBaseAddr          = (u32)&ADC12_RESULT;
  26.     DMA_InitStructure.DMA_DIR                       = DMA_DIR_PeripheralSRC;
  27.     DMA_InitStructure.DMA_BufferSize               = 6;
  28.     DMA_InitStructure.DMA_PeripheralInc               = DMA_PeripheralInc_Disable;
  29.     DMA_InitStructure.DMA_MemoryInc                   = DMA_MemoryInc_Enable;
  30.     DMA_InitStructure.DMA_PeripheralDataSize      = DMA_PeripheralDataSize_Word;
  31.     DMA_InitStructure.DMA_MemoryDataSize          = DMA_MemoryDataSize_Word;
  32.     DMA_InitStructure.DMA_Mode                      = DMA_Mode_Circular;
  33.     DMA_InitStructure.DMA_Priority                  = DMA_Priority_High;
  34.     DMA_InitStructure.DMA_M2M                     = DMA_M2M_Disable;

  35.     DMA_Init(DMA1_Channel1,&DMA_InitStructure);           
  36.     DMA_Cmd(DMA1_Channel1,ENABLE);
  37.     /*
  38.     说明,当使用DMA时EOC中断会不正常,因为DMA后EOC会被自动清零
  39.    
  40.     */
 楼主| 慢醇 发表于 2022-1-25 23:01 | 显示全部楼层
  NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;   //0最高嵌套,中断不被打断
    NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;           //0最高响应优先.
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
/* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
     
       ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);  
  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
     
      
}
 楼主| 慢醇 发表于 2022-1-25 23:03 | 显示全部楼层
void ADC1_2_IRQHandler(void)
{   
   

      
     if(ADC_GetITStatus(ADC1, ADC_IT_EOC)!= RESET)
    {
            adcdone=1;
         ADC_ClearITPendingBit(ADC1, ADC_IT_EOC);
    }
        
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

133

主题

1381

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部