野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13666|回复: 2

DMA中断+ADC+TIM进行ADC采样出现问题

[复制链接]
发表于 2019-8-8 18:32:18 | 显示全部楼层 |阅读模式
单独用ADC+TIM+DMA没有问题,可以采集,但是加上DMA中断后就不行了 串口一直输出的是一个定值  比如0X0FFF 求大神指点迷津  代码如下
static void Rheostat_ADC_GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_AHB1PeriphClockCmd(RHEOSTAT_ADC_GPIO_CLK, ENABLE);
               
        GPIO_InitStructure.GPIO_Pin = RHEOSTAT_ADC_GPIO_PIN;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;            
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //2»éÏà-2»ÏÂà-
        GPIO_Init(RHEOSTAT_ADC_GPIO_PORT, &GPIO_InitStructure);                       
}
static void Rheostat_ADC_Mode_Config(void)
{
        DMA_InitTypeDef DMA_InitStructure;
        ADC_InitTypeDef ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
        NVIC_InitTypeDef NVIC_InitStruct;
        RCC_APB2PeriphClockCmd(RHEOSTAT_ADC_CLK , ENABLE);
  RCC_AHB1PeriphClockCmd(RHEOSTAT_ADC_DMA_CLK, ENABLE);

        DMA_InitStructure.DMA_PeripheralBaseAddr = RHEOSTAT_ADC_DR_ADDR;       

        DMA_InitStructure.DMA_Memory0BaseAddr = (u32)&ADC_ConvertedValue;  

        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;       

        DMA_InitStructure.DMA_BufferSize = 1;       

        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;

        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;       

        DMA_InitStructure.DMA_Mode =DMA_Mode_Circular;//DMA_Mode_Normal;// DMA_Mode_Circular;

        DMA_InitStructure.DMA_Priority = DMA_Priority_High;

  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;  

  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;  

  DMA_InitStructure.DMA_Channel = RHEOSTAT_ADC_DMA_CHANNEL;

        DMA_Init(RHEOSTAT_ADC_DMA_STREAM, &DMA_InitStructure);

        NVIC_InitStruct.NVIC_IRQChannel=DMA2_Stream0_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
        NVIC_Init(&NVIC_InitStruct);
  DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TC);                             //Çå3yÖD¶Ï±êÖ¾   
        DMA_ITConfig(RHEOSTAT_ADC_DMA_STREAM,DMA_IT_TC,ENABLE);  //ê1Äü·¢ËíÖD¶Ï
        DMA_Cmd(DMA2_Stream0, ENABLE);
       

  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;

  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;

  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;

  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;  
  ADC_CommonInit(&ADC_CommonInitStructure);
       
        ADC_StructInit(&ADC_InitStructure);
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

  ADC_InitStructure.ADC_ScanConvMode = DISABLE;

  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;

  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;

  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

  ADC_InitStructure.ADC_NbrOfConversion = 1;                                    
  ADC_Init(RHEOSTAT_ADC, &ADC_InitStructure);
  //---------------------------------------------------------------------------
       

  ADC_RegularChannelConfig(RHEOSTAT_ADC, RHEOSTAT_ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);

  ADC_DMARequestAfterLastTransferCmd(RHEOSTAT_ADC, ENABLE);

  ADC_DMACmd(RHEOSTAT_ADC, ENABLE);

  ADC_Cmd(RHEOSTAT_ADC, ENABLE);  
  ADC_SoftwareStartConv(RHEOSTAT_ADC);
}
void MY_Time(u16 sample)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;

  RCC_APB1PeriphClockCmd(GENERAL_TIM_CLK, ENABLE);

  TIM_TimeBaseStructure.TIM_Period =  (10000-1)/sample;      
       
  TIM_TimeBaseStructure.TIM_Prescaler = 9000-1;       

  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
       

        TIM_TimeBaseInit(GENERAL_TIM, &TIM_TimeBaseStructure);
       
        TIM_OCStructInit(&TIM_OCInitStructure);                       //éèÖÃè±ê¡Öμ,Õaò»2½×îoüóéÏ·àÖ1·Åμ½′®¿ú3õê¼»ˉoó3öÎêìa
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;       

  TIM_OCInitStructure.TIM_Pulse = 90000000/sample/2;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC4Init(GENERAL_TIM, &TIM_OCInitStructure);       
       

        TIM_OC4PreloadConfig(GENERAL_TIM, TIM_OCPreload_Enable);

        TIM_Cmd(GENERAL_TIM, ENABLE);               
        TIM_CtrlPWMOutputs(GENERAL_TIM, ENABLE);
}
/**
  * @brief  ADC13õê¼»ˉ
  * @param  ÎT
  * @retval ÎT
  */
void Rheostat_Init(void)
{
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        Rheostat_ADC_GPIO_Config();
        Rheostat_ADC_Mode_Config();
}
void DMA2_Stream0_IRQHandler()
{
         if(DMA_GetITStatus(RHEOSTAT_ADC_DMA_STREAM,DMA_IT_TCIF0)) //ÅD¶ÏêÇ·ñÎaDMA1·¢Ëííê3éÖD¶Ï;
  {
    DMA_ClearITPendingBit( DMA2_Stream0 ,DMA_IT_TCIF0);
                DMA_Cmd(RHEOSTAT_ADC_DMA_STREAM, ENABLE);        //1رÕDMA;
                ADC_Flag=1;
  }
}

int main(void)
{

    /*3õê¼»ˉUSART1*/
    Debug_USART_Config();
             Rheostat_Init();
          MY_Time(20000);
    while (1)
    {
      ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*(float)3.3; // ¶áè¡×a»»μÄADÖμ

      printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue);
      printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal);

      Delay(0xffffee);  
    }
}

static void Delay(__IO uint32_t nCount)         //¼òμ¥μÄÑóê±oˉêy
{
        for(; nCount != 0; nCount--);
}

回复

使用道具 举报

发表于 2019-8-8 22:50:28 | 显示全部楼层
ADC_Flag有什么用,这里的DMA中断是想做什么呢
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-9 01:46:09 | 显示全部楼层
WishWish 发表于 2019-8-8 22:50
ADC_Flag有什么用,这里的DMA中断是想做什么呢

关闭DMA传输  对数据做处理
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-23 10:48 , Processed in 0.034664 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表