STM32F ADC 多通道 非DMA

STM32F的ADC,有多种分类,如单次/连续,间断/不间断,规则组/注入组等。
1、当设为连续模式时,即continuous convert mode = enable。间断模式(discontinuous convert mode)无效。连续模式,用于描述转换序列(sequencer)在整个序列(序列,即包括若干个通道)转换完成后,是否再次从头开始扫描。
2、当设为连续模式时,即continuous convert mode = disable。间断模式(discontinuous convert mode)有效。间断模式用于描述转换序列是否分成若干小的序列(小序列的大小根据(number of discontinuous conversion)。如果间断模式使能,每个小序列转换完成后,便会停止,等待下一个转换开始指令。每个小序列,在读取数据前,均需启动转换,因为每次转换完成后,都停止了。
下面是代码中,对连续模式和间断模式的描述。
uint32_t ContinuousConvMode; /*!< Specifies whether the conversion is performed in single mode (one conversion) or continuous mode for regular group,
after the selected trigger occurred (software start or external trigger).
This parameter can be set to ENABLE or DISABLE. */

uint32_t DiscontinuousConvMode; /*!< Specifies whether the conversions sequence of regular group is performed in Complete-sequence/Discontinuous-sequence (main sequence subdivided in successive parts).
Discontinuous mode is used only if sequencer is enabled (parameter ‘ScanConvMode’). If sequencer is disabled, this parameter is discarded.
Discontinuous mode can be enabled only if continuous mode is disabled. If continuous mode is enabled, this parameter setting is discarded.
This parameter can be set to ENABLE or DISABLE. */

下面是对于Poll方法下,多通道采集的配置。
STM32F ADC 多通道 非DMA_第1张图片

代码如下

static void MX_ADC1_Init(void)
{

  ADC_ChannelConfTypeDef sConfig;

    /**Common config 
    */
  hadc1.Instance = ADC1;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.DiscontinuousConvMode = ENABLE;
  hadc1.Init.NbrOfDiscConversion = 1;
  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.NbrOfConversion = 2;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = 1;
  sConfig.SamplingTime = ADC_SAMPLETIME_55CYCLES_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_9;
  sConfig.Rank = 2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

注:此处配置为,每个小序列为1,即每个通道转换完就停止。如果大于1,转换完读取到的数据是最后一个通道的值。

你可能感兴趣的:(sofeware,note)