STM32F429单片机HAL库ADC配置问题

2019-03-23 15:44发布

本帖最后由 shipeng 于 2018-7-19 13:33 编辑

最近在使用STM32F429单片机的ADC功能,需要用到7个通道使用扫描方式工作。于是打开了DMA,初始化代码是用STM32Cube工具生成的(并参考了网上一个例程配置了DMA)。现在的问题是AD的转换结果比实际输入电压低很多(输入3.3V时AD只有700左右,12位AD应该是4095左右)而且还很不稳定一直在变变化幅度最大有100个AD。不知作何解恳请大家帮我看下初始化有没有问题:
/* ADC1 init function */
static void MX_ADC1_Init(void)
{

  ADC_ChannelConfTypeDef sConfig;

    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV8;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.ScanConvMode = ENABLE;
  hadc1.Init.ContinuousConvMode = ENABLE;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 7;
  hadc1.Init.DMAContinuousRequests = ENABLE;
  hadc1.Init.EOCSelection = DISABLE;//ADC_EOC_SEQ_CONV;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_0;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_1;
  sConfig.Rank = 2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_2;
  sConfig.Rank = 3;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_3;
  sConfig.Rank = 4;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_9;
  sConfig.Rank = 5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_14;
  sConfig.Rank = 6;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
    */
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
  sConfig.Rank = 7;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

以上都是STM32Cube自动生成的,下面是参考网上的例程添加的代码:

u16 adv_buf[7] = {0,0,0,0,0,0,0};

void MY_ADC_DMA_Init(void)
{
        HAL_ADC_Start(&hadc1);
        __HAL_RCC_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_PDATAALIGN_HALFWORD;  
    hdma_adc1.Init.MemDataAlignment=DMA_PDATAALIGN_HALFWORD;
    hdma_adc1.Init.Mode=DMA_CIRCULAR;                                                                        
    hdma_adc1.Init.Priority=DMA_PRIORITY_MEDIUM;         
    hdma_adc1.Init.FIFOMode=DMA_FIFOMODE_DISABLE;              
    hdma_adc1.Init.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL;     
    hdma_adc1.Init.MemBurst=DMA_MBURST_SINGLE;               
    hdma_adc1.Init.PeriphBurst=DMA_PBURST_SINGLE;            
    __HAL_LINKDMA(&hadc1,DMA_Handle,hdma_adc1);     
    /* Configure DMA Stream source address */
    hdma_adc1.Instance->PAR  = (u32)&ADC1->DR;
    hdma_adc1.Instance->M0AR = (u32)adv_buf;
    hdma_adc1.Instance->NDTR =7;
    HAL_DMA_Init(&hdma_adc1);
                HAL_DMA_Start (&hdma_adc1,(u32)&ADC1->DR,(u32)&adv_buf,7);//
               
        HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adv_buf, 7);
}

         main函数:
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
        MX_DMA_Init();
  //MX_IWDG_Init();
  MX_RTC_Init();
  MX_SPI1_Init();
  MX_UART8_Init();
  MX_UART7_Init();
  MX_I2C1_Init();
  MX_TIM1_Init();
  MX_TIM8_Init();
  MX_USB_OTG_FS_USB_Init();
  MX_ADC1_Init();
  MX_UART5_Init();
  MX_SPI3_Init();
  MX_TIM3_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
        RTC_Set_WakeUp(RTC_WAKEUPCLOCK_CK_SPRE_16BITS,0);
        delay_init(120);
        Lcd_Init();
        MainPageDisp();
        MY_ADC_DMA_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
                HAL_IWDG_Refresh(&hiwdg);
  /* USER CODE END WHILE */
                if (tim_refresh!=0)
                {
                        tim_refresh=0;
                        DispDate();
                        DispTime();
                        LCD_ShowNum(70,364,adv_buf[5],5);
                }
  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}

此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
1条回答
shipeng
1楼-- · 2019-03-24 00:47
/ 问题原因找到了,是IO口配置有问题。都怪我是半路出家初次创建工程的时候没有配置完善ADC,做到半路又修改STM32Cube工程配置生成另一个工程后再把新工程的代码拷出,由于拷贝不完善导致GPIO配置未更新所以出现了上面啼笑皆非的闹剧

一周热门 更多>