STM32F4Cube库调试多通道ADC+DMA问题

2019-07-14 15:50发布

我定义了个uint32_t ADC_Value_arr[4]={0,0,0,0};接收四个通道的ADC,单通道+DMA读取数据没有问题,多通道读取是只有ADC_Value_arr[0]有数据,并且数据特别大,达到8位,还不断变化,显然错了,不知道是何原因,实在发现不了错在哪里?贴上代码,希望有人能指出错误,非常感谢!!!
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
{
  GPIO_InitTypeDef          GPIO_InitStruct;
  static DMA_HandleTypeDef  hdma_adc;

/*##-1- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO clock */
  ADCx_CHANNEL_GPIO_CLK_ENABLE();
  /* ADC1 Periph clock enable */
  ADCx_CLK_ENABLE();
  /* Enable DMA2 clock */
  DMAx_CLK_ENABLE();

  /*##-2- Configure peripheral GPIO ##########################################*/
  /* ADC1 Channel2,3,6,7 GPIO pin configuration */
  GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN2|ADCx_CHANNEL_PIN3|ADCx_CHANNEL_PIN6|ADCx_CHANNEL_PIN7;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);

  /*##-3- Configure the DMA streams ##########################################*/
  /* Set the parameters to be configured */
  hdma_adc.Instance = ADCx_DMA_STREAM; //选择stream0

  hdma_adc.Init.Channel  = ADCx_DMA_CHANNEL;//通道0
  hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;//ÍâÉèµÀÄÚ´æ
  hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;//ÍâÉè²»Ôö
  hdma_adc.Init.MemInc = DMA_MINC_ENABLE;//内存增
  hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; //
  hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  hdma_adc.Init.Mode = DMA_CIRCULAR; //循环模式
  hdma_adc.Init.Priority = DMA_PRIORITY_HIGH;
  hdma_adc.Init.FIFOMode = DMA_FIFOMODE_DISABLE;         
  hdma_adc.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL;
  hdma_adc.Init.MemBurst = DMA_MBURST_SINGLE;
  hdma_adc.Init.PeriphBurst = DMA_PBURST_SINGLE;

  HAL_DMA_Init(&hdma_adc);

  /* Associate the initialized DMA handle to the the ADC handle */
  __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc);

  /*##-4- Configure the NVIC for DMA #########################################*/
  /* NVIC configuration for DMA transfer complete interrupt */
  HAL_NVIC_SetPriority(ADCx_DMA_IRQn, 5, 0);   
  HAL_NVIC_EnableIRQ(ADCx_DMA_IRQn);
}

void set_ADC(void)
{
        ADC_ChannelConfTypeDef sConfig;

  /* Configure the ADC peripheral */
  AdcHandle.Instance = ADCx;//ADC1

  AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;//21MHZ
  AdcHandle.Init.Resolution = ADC_RESOLUTION12b; //12位
  //AdcHandle.Init.ScanConvMode = DISABLE;
AdcHandle.Init.ScanConvMode = ENABLE;//扫描模式
  AdcHandle.Init.ContinuousConvMode = ENABLE;//连续模式
  AdcHandle.Init.DiscontinuousConvMode = DISABLE;//
  AdcHandle.Init.NbrOfDiscConversion = 0;
  AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  AdcHandle.Init.NbrOfConversion = 4;
  AdcHandle.Init.DMAContinuousRequests = ENABLE;
  AdcHandle.Init.EOCSelection = DISABLE;
  HAL_ADC_Init(&AdcHandle);

  /* Configure ADC regular channel */  
  sConfig.Channel = ADCx_CHANNEL2;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  sConfig.Offset = 0;
  HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
        
          sConfig.Channel = ADCx_CHANNEL3;
   sConfig.Rank = 2;
   sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
   sConfig.Offset = 0;
   HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
        
   sConfig.Channel = ADCx_CHANNEL6;
   sConfig.Rank = 3;
   sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
   sConfig.Offset = 0;
   HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
         
   sConfig.Channel = ADCx_CHANNEL7;
   sConfig.Rank = 4;
   sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
   sConfig.Offset = 0;
   HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);

  /* Start the conversion process and enable interrupt */  
//  HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&ADCConvertedValue, 1);
        
        HAL_ADC_Start_DMA(&AdcHandle, ADC_Value_arr, 4);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
肥肥飞飞
1楼-- · 2019-07-15 17:48
谢谢                          
youxi8745
2楼-- · 2019-07-15 17:50
 精彩回答 2  元偷偷看……
huangxie
3楼-- · 2019-07-15 18:43
另外楼主的程序还需要吧DMA中断给关掉    因为设置的是DMA连续ADC采集,这样也就是每0.428us 就生产一次DMA完成中断,太过频繁的中断会经常阻塞其他程序的执行。
wenxueshu
4楼-- · 2019-07-15 22:45
ADC的代码,就是我上边贴的代码,我的工程涉及太多文件,太大了,就不上传了……
hu_wfllllllllfl
5楼-- · 2019-07-16 04:05
楼主问题解决了,原因是什么呢?是因为BUG吗?
zhouxk
6楼-- · 2019-07-16 05:23
我的也遇到同样的问题,但是用的不是cube

一周热门 更多>