打印
[STM32F4]

DMA获取adc数据异常,求助

[复制链接]
5231|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
daienming|  楼主 | 2015-3-4 22:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我现在使用stm32f4上一个dma ,同时获取 adc1 上12个通道的数据,adc2 上一个通道的数据 和 adc3 上8个通道的数据,但是现在获取的数据都是0xFF,代码如下

首先配置ADC 1,2,3

void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig_ADC1;  
  __ADC1_CLK_ENABLE();

  
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8;
  hadc1.Init.Resolution = ADC_RESOLUTION8b;
  hadc1.Init.ScanConvMode = ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.NbrOfDiscConversion = 0;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 12;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = DISABLE;
  HAL_ADC_Init(&hadc1);

  sConfig_ADC1.Channel = ADC_CHANNEL_0;
  sConfig_ADC1.Rank = 1;
  sConfig_ADC1.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_1;
  sConfig_ADC1.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_2;
  sConfig_ADC1.Rank = 3;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_3;
  sConfig_ADC1.Rank = 4;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_4;
  sConfig_ADC1.Rank = 5;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_5;
  sConfig_ADC1.Rank = 6;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_6;
  sConfig_ADC1.Rank = 7;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_7;
  sConfig_ADC1.Rank = 8;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);
  
  sConfig_ADC1.Channel = ADC_CHANNEL_8;
  sConfig_ADC1.Rank = 9;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);
  
  sConfig_ADC1.Channel = ADC_CHANNEL_9;
  sConfig_ADC1.Rank = 10;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);
  
  sConfig_ADC1.Channel = ADC_CHANNEL_10;
  sConfig_ADC1.Rank = 11;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);

  sConfig_ADC1.Channel = ADC_CHANNEL_11;
  sConfig_ADC1.Rank = 12;
  HAL_ADC_ConfigChannel(&hadc1, &sConfig_ADC1);
}

void MX_ADC2_Init(void)
{
  ADC_ChannelConfTypeDef sConfig_ADC2;
  __ADC2_CLK_ENABLE();  
  
  hadc2.Instance = ADC2;
  hadc2.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8;
  hadc2.Init.Resolution = ADC_RESOLUTION8b;
  hadc2.Init.ScanConvMode = ENABLE;
  hadc2.Init.ContinuousConvMode = ENABLE;
  hadc2.Init.DiscontinuousConvMode = DISABLE;
  hadc2.Init.NbrOfDiscConversion = 0;
  hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc2.Init.NbrOfConversion = 1;
  hadc2.Init.DMAContinuousRequests = ENABLE;
  hadc2.Init.EOCSelection = EOC_SEQ_CONV;
  HAL_ADC_Init(&hadc2);  
  
  sConfig_ADC2.Channel = ADC_CHANNEL_12;
  sConfig_ADC2.Rank = 1;
  HAL_ADC_ConfigChannel(&hadc2, &sConfig_ADC2);  
}


/* ADC init function */
void MX_ADC3_Init(void)
{

  ADC_ChannelConfTypeDef sConfig_ADC3;
  __ADC3_CLK_ENABLE();
  
  hadc3.Instance = ADC3;
  hadc3.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV8;
  hadc3.Init.Resolution = ADC_RESOLUTION8b;
  hadc3.Init.ScanConvMode = ENABLE;
  hadc3.Init.ContinuousConvMode = ENABLE;
  hadc3.Init.DiscontinuousConvMode = DISABLE;
  hadc3.Init.NbrOfDiscConversion = 0;
  hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc3.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc3.Init.NbrOfConversion = 8;
  hadc3.Init.DMAContinuousRequests = ENABLE;
  hadc3.Init.EOCSelection = DISABLE;
  HAL_ADC_Init(&hadc3);

  sConfig_ADC3.Channel = ADC_CHANNEL_0;
  sConfig_ADC3.Rank = 1;
  sConfig_ADC3.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_1;
  sConfig_ADC3.Rank = 2;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_2;
  sConfig_ADC3.Rank = 3;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_3;
  sConfig_ADC3.Rank = 4;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_4;
  sConfig_ADC3.Rank = 5;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_5;
  sConfig_ADC3.Rank = 6;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

  sConfig_ADC3.Channel = ADC_CHANNEL_6;
  sConfig_ADC3.Rank = 7;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);
  
  sConfig_ADC3.Channel = ADC_CHANNEL_7;
  sConfig_ADC3.Rank = 8;
  HAL_ADC_ConfigChannel(&hadc3, &sConfig_ADC3);

}


再处理gpio
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if (hadc == &hadc3)
  {
//    PF6 ------>ADC3_IN4   IR1
//    PF7 ------>ADC3_IN5      IR2
//    PF8 ------>ADC3_IN6   IR3
//    PF9 ------>ADC3_IN7   IR4
//    PF10------>ADC3_IN8   IR5
//    PF3 ------>ADC3_IN9   IR6
//    PF4 ------>ADC3_IN14   IR7
//    PF5 ------>ADC3_IN15   IR8
    GPIO_InitStruct.Pin = IR_DAT_PIN0|IR_DAT_PIN1|IR_DAT_PIN2|IR_DAT_PIN3
                          |IR_DAT_PIN4|IR_DAT_PIN5|IR_DAT_PIN6|IR_DAT_PIN7;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(IR_DAT_PORT, &GPIO_InitStruct);
   

  }
   
  
  if (hadc == &hadc2)
  {
    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
   
  }
  if (hadc == &hadc1)
  {
    /**ADC1 GPIO Configuration
    PA0-WKUP     ------> ADC1_IN0
    PA1     ------> ADC1_IN1
    PA2     ------> ADC1_IN2
    PA3     ------> ADC1_IN3
    PA4     ------> ADC1_IN4
    PA5     ------> ADC1_IN5
    PA6     ------> ADC1_IN6
    PA7     ------> ADC1_IN7
    PB0     ------> ADC1_IN8
    PB1     ------> ADC1_IN9   
    PC0     ------> ADC1_IN10
    PC1     ------> ADC1_IN11
    */
    GPIO_InitStruct.Pin = M_DAT_PIN0|M_DAT_PIN1|M_DAT_PIN2|M_DAT_PIN3|M_DAT_PIN4|
    M_DAT_PIN5|M_DAT_PIN6|M_DAT_PIN7;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
   
    GPIO_InitStruct.Pin = M_DAT_PIN8|M_DAT_PIN9;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   
    GPIO_InitStruct.Pin = M_DAT_PIN10|M_DAT_PIN11;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
   
    //ADC1 IO CONFIG FINISH
   
    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
   
    hdma_adc1.Instance = DMA2_Stream0;
    hdma_adc1.Init.Channel = DMA_CHANNEL_0;
    hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
    hdma_adc1.Init.PeriphDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_adc1.Init.Mode = DMA_CIRCULAR;
    hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
    hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;         
    hdma_adc1.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
    hdma_adc1.Init.MemBurst = DMA_MBURST_SINGLE;
    hdma_adc1.Init.PeriphBurst = DMA_PBURST_SINGLE;
    HAL_DMA_Init(&hdma_adc1);
    __HAL_LINKDMA(hadc,DMA_Handle,hdma_adc1);
  }
  
}


然后启动

    if(HAL_ADC_Start(&hadc1) != HAL_OK)
  {

    Error_Handler();
  }
      if(HAL_ADC_Start(&hadc2) != HAL_OK)
  {
    /* Start Error */    Error_Handler();
  }
  
    if(HAL_ADC_Start(&hadc3) != HAL_OK)
  {

    Error_Handler();
  }  
  
  ADC_MultiModeTypeDef   mode;
  mode.Mode = ADC_TRIPLEMODE_INTERL;
  mode.DMAAccessMode = ADC_DMAACCESSMODE_1;
  mode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_20CYCLES;
  
  
  if(HAL_ADCEx_MultiModeConfigChannel(&hadc1, &mode) != HAL_OK)
  {
    /* Channel Configuration Error */
    Error_Handler();
  }

    if(HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t*)&uhADCxConvertedValue, 40) != HAL_OK)
  {
    /* Start Error */
    Error_Handler();
  }


最终在这里获取上数据
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
这里直接断点看结果

00000000 00000000 00000000 ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000000
}       

为啥会这样???
我的ref脚 3.3v已经ok ,其他脚上的电压应该是七零八落不会全是ff这么整齐呀
沙发
airwill| | 2015-3-5 07:14 | 只看该作者
首先这种情况, 第一感觉的是地址弄错了.
大体看了一下你的代码, 好象没有看到你的 DMA 外设地址的设置

使用特权

评论回复
板凳
daienming|  楼主 | 2015-3-5 08:56 | 只看该作者
airwill 发表于 2015-3-5 07:14
首先这种情况, 第一感觉的是地址弄错了.
大体看了一下你的代码, 好象没有看到你的 DMA 外设地址的设置 ...

HAL_StatusTypeDef HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length)
{
  uint16_t counter = 0;
  
  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.ContinuousConvMode));
  assert_param(IS_ADC_EXT_TRIG_EDGE(hadc->Init.ExternalTrigConvEdge));
  assert_param(IS_FUNCTIONAL_STATE(hadc->Init.DMAContinuousRequests));
  
  /* Process locked */
  __HAL_LOCK(hadc);
  
  /* Enable ADC overrun interrupt */
  __HAL_ADC_ENABLE_IT(hadc, ADC_IT_OVR);
  
  if (hadc->Init.DMAContinuousRequests != DISABLE)
  {
    /* Enable the selected ADC DMA request after last transfer */
    ADC->CCR |= ADC_CCR_DDS;
  }
  else
  {
    /* Disable the selected ADC EOC rising on each regular channel conversion */
    ADC->CCR &= ~ADC_CCR_DDS;
  }
  
  /* Set the DMA transfer complete callback */
  hadc->DMA_Handle->XferCpltCallback = ADC_MultiModeDMAConvCplt;
  
  /* Set the DMA half transfer complete callback */
  hadc->DMA_Handle->XferHalfCpltCallback = ADC_MultiModeDMAHalfConvCplt;
     
  /* Set the DMA error callback */
  hadc->DMA_Handle->XferErrorCallback = ADC_MultiModeDMAError ;
  
  /* Enable the DMA Stream */
  HAL_DMA_Start_IT(hadc->DMA_Handle, (uint32_t)&ADC->CDR, (uint32_t)pData, Length);


//////这里调用dma 的驱动,把adc-cdr地址和data的地址传进去了。


  /* Change ADC state */
  hadc->State = HAL_ADC_STATE_BUSY_REG;
  
  /* Check if ADC peripheral is disabled in order to enable it and wait during
     Tstab time the ADC's stabilization */
  if((hadc->Instance->CR2 & ADC_CR2_ADON) != ADC_CR2_ADON)
  {  
    /* Enable the Peripheral */
    __HAL_ADC_ENABLE(hadc);
   
    /* Delay inserted to wait during Tstab time the ADC's stabilazation */
    for(; counter <= 540; counter++)
    {
      __NOP();
    }
  }
  
  /* if no external trigger present enable software conversion of regular channels */
  if (hadc->Init.ExternalTrigConvEdge == ADC_EXTERNALTRIGCONVEDGE_NONE)
  {
    /* Enable the selected ADC software conversion for regular group */
    hadc->Instance->CR2 |= (uint32_t)ADC_CR2_SWSTART;
  }
  
  /* Process unlocked */
  __HAL_UNLOCK(hadc);
  
  /* Return function status */
  return HAL_OK;
}


外设地址应该已经有了

使用特权

评论回复
地板
左右逢源| | 2015-4-5 20:13 | 只看该作者
请问这个问题解决了么,我也遇同样问题了,出来数据是FFF,然后就是ADSTART位无法置位

使用特权

评论回复
5
小浣熊| | 2015-4-6 21:17 | 只看该作者
这个代码太长了没时间看

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

6

帖子

0

粉丝