你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

DMA 获取 ADC数据异常

[复制链接]
乡下老农 提问时间:2015-3-4 22:42 /
本帖最后由 乡下老农 于 2015-3-4 23:51 编辑

我现在使用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);
    __DMA2_CLK_ENABLE();  
    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这么整齐呀
而且运行过程中修改adc的时钟,得到的值会不同,而且adc1,adc2长期溢出


收藏 评论2 发布时间:2015-3-4 22:42

举报

2个回答
MouseCat 回答时间:2015-3-5 09:10:40
一路一路的试试看
kqh1120 回答时间:2015-3-5 12:40:59
看看时序吧。。

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版