STM32F030 HAL ADC多通道采样DMA传输

   我所选用的芯片是STM32F030C6,采用官方STM32CubeMX配置ADC多通道时只需勾选相应的ADC通道,而对于大部分说的Rank设置是不需要的,F0默认按通道数字号配置了通道顺序。

   关于ADC多通道DMA配置的顺序:Enable DMA时钟 -> 配置ADC -> 配置DMA -> 开启传输

   1、配置ADC

  ADC_ChannelConfTypeDef sConfig = {0};

  __HAL_RCC_DMA1_CLK_ENABLE();

   /* DMA interrupt init */
   /* DMA1_Channel1_IRQn interrupt configuration */
   HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 1, 0);
   HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

  hadc.Instance = ADC1;
  hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
  hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
  hadc.Init.LowPowerAutoWait = DISABLE;
  hadc.Init.LowPowerAutoPowerOff = DISABLE;
  hadc.Init.ContinuousConvMode = ENABLE;
  hadc.Init.DiscontinuousConvMode = DISABLE;
  hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc.Init.DMAContinuousRequests = ENABLE;
  hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
  if (HAL_ADC_Init(&hadc) != HAL_OK)
  {
      Error_Handler();
  }

    sConfig.Channel = ADC_CHANNEL_2;
    sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
    sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
    if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
    {
      Error_Handler();
    }

    sConfig.Channel = ADC_CHANNEL_7;
    if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
    {
      Error_Handler();
    }

2、配置DMA,关联ADC

void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

  if(adcHandle->Instance==ADC1)
  {

	__HAL_RCC_ADC1_CLK_ENABLE();
	  /**ADC GPIO Configuration
	  	  PA2     ------> ADC_IN2
	  	  PA7     ------> ADC_IN7
	  	  */
	GPIO_InitStruct.Pin = Charger_detect_Pin|Current_detect_Pin;
	GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


    hdma_adc.Instance = DMA1_Channel1;
    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_MEDIUM;
    if (HAL_DMA_Init(&hdma_adc) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc);

  }
}

3、开启传输

 HAL_ADC_Start_DMA(&hadc, (uint32_t *)ADC_Value, 30);

 

实际调试过程中主要遇到两个问题

1、因为我的ADC配置放在adc.c文件中,而DMA配置放在stm32f0xx_hal_msp.c中,但是DMA_HandleTypeDef hdma_adc;没有定义成全局变量,而在stm32f0xx_it.c中有DMA中断函数,需要用到变量hdma_adc,所以如果没有定义成全局变量,这时中断函数中调用的hdma_adc就会是未知的,这就导致我一开启ADC DMA整个程序就挂掉......所以配置过程中一定要注意。

2、另一个问题就是ADC采样数据的问题,我开启了两个通道,DMA缓冲区大小设置为30,但是实际采到的数据只有前7个有数据,后面都是0,且越前面的数据越大,到后面慢慢变小。

上网查阅,找到一篇文章解释了原因:

http://www.51hei.com/bbs/dpj-40959-1.html

将数据宽度改成半字就恢复正常了

你可能感兴趣的:(STM32,HAL)