如何使用STM32F7从ADC获取数据

时间:2018-02-03 21:27:29

标签: stm32 adc stm32f7

所以我想在我的stm32f7发现中读出adc3的多个频道。我已经能够读出一个频道并设置多个频道,但我无法弄清楚如何读出每个频道。我想通过中断读取它们,所以我将adc设置为:

  hadc3.Instance          = ADC3;

  hadc3.Init.ClockPrescaler        = ADC_CLOCKPRESCALER_PCLK_DIV4;
  hadc3.Init.Resolution            = ADC_RESOLUTION_12B;
  hadc3.Init.ScanConvMode          = ENABLE;                       /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
  hadc3.Init.ContinuousConvMode    = ENABLE;                       /* Continuous mode enabled to have continuous conversion  */
  hadc3.Init.DiscontinuousConvMode = DISABLE;                       /* Parameter discarded because sequencer is disabled */
  hadc3.Init.NbrOfDiscConversion   = 0;
  hadc3.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE;        /* Conversion start trigged at each external event */
  hadc3.Init.ExternalTrigConv      = ADC_EXTERNALTRIGCONV_T1_CC1;
  hadc3.Init.DataAlign             = ADC_DATAALIGN_RIGHT;
  hadc3.Init.NbrOfConversion       = 2;
  hadc3.Init.DMAContinuousRequests = DISABLE;
  hadc3.Init.EOCSelection          = DISABLE;



  if (HAL_ADC_Init(&hadc3) != HAL_OK)
  {
    /* ADC initialization Error */
    Error_Handler();
  }

  /*##-2- Configure ADC regular channel ######################################*/
  sConfig.Channel      = ADC_CHANNEL_8;
  sConfig.Rank         = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Offset       = 0;

  if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  {
    /* Channel Configuration Error */
    Error_Handler();
  }
    /*##-2- Configure ADC regular channel ######################################*/
  sConfig.Channel      = ADC_CHANNEL_0;
  sConfig.Rank         = 2;
  sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  sConfig.Offset       = 0;

  if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
  {
    /* Channel Configuration Error */
    Error_Handler();
  }

  /*##-3- Start the conversion process #######################################*/
  if(HAL_ADC_Start_IT(&hadc3) != HAL_OK)
  {
    /* Start Conversation Error */
    Error_Handler();
  }

然后我有一个回调,它会在转换结束时进行,这里我想读取数据,但我不知道如何读出每个频道。

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{

    /* Get the converted value of regular channel */
    ADC3ConvertedValue[0] = HAL_ADC_GetValue(AdcHandle);
    ADC3ConvertedValue[1] = HAL_ADC_GetValue(AdcHandle);
    char disp[50];
    sprintf(disp, "%d%%", ADC3ConvertedValue[0]);
    BSP_LCD_DisplayStringAtLine(1, (uint8_t*) disp);
    char disp1[50];
    sprintf(disp1, "%d%%", ADC3ConvertedValue[1]);
    BSP_LCD_DisplayStringAtLine(2, (uint8_t*) disp1);
}

任何人都可以帮我阅读这篇文章。我不想使用DMA,因为它与LCD冲突。

0 个答案:

没有答案