1.34 STM32H7xx ADC 轮训ADC

1、CubeMX配置如下:

在这里插入图片描述
在这里插入图片描述
如上图:PA5,ADC1,CH19

1、ADC_Settings及ADC_InitTypeDef:

如图:
在这里插入图片描述

- [ ] Clock Prescaler:用于ADC的时钟分频设置,PLL2异步时钟,1~256

ADC_CLOCK_ASYNC_DIV1
ADC_CLOCK_ASYNC_DIV2
ADC_CLOCK_ASYNC_DIV4
ADC_CLOCK_ASYNC_DIV6
ADC_CLOCK_ASYNC_DIV8
ADC_CLOCK_ASYNC_DIV10
ADC_CLOCK_ASYNC_DIV12
ADC_CLOCK_ASYNC_DIV16
ADC_CLOCK_ASYNC_DIV32
ADC_CLOCK_ASYNC_DIV64
ADC_CLOCK_ASYNC_DIV128
ADC_CLOCK_ASYNC_DIV256

- [ ] Resolution:ADC分辨率设置,8位到16位

ADC_RESOLUTION_16B
ADC_RESOLUTION_14B
ADC_RESOLUTION_12B
ADC_RESOLUTION_10B
ADC_RESOLUTION_8B

- [ ] Scan Conversion Mode:扫描模式

  用于使能或者禁止ADC的扫描模式,即多通道转换。此参数配合成员DiscontinuousConvMode可以将主转换序列分成多个子系列进行逐步转换。

  • ADC_SCAN_DISABLE 禁止扫描模式,表示单通道转换,成员NbrOfConversion和InjectedNbrOfConversion不起作用,等效为1,即仅进行一个通道转换
  • ADC_SCAN_ENABLE 使能扫描模式,表示支持多通道转换,成员NbrOfConversion和InjectedNbrOfConversion分别用于规则通道和注入通道转换序列数。

- [ ] Continuous Conversion Mode:ENABLE

用于配置使用单次转换还是连续转换;
【1】、在单次转换模式下,ADC 执行一次转换。完成所选通道的转换之后:然后,ADC 停止。
【2】 、在连续转换模式中,当前面ADC转换一结束马上就启动另一次转换。

- [ ] End Of Conversion Selection:

用于中断或者查询模式时,转换结束标志EOC (End Of Conversion)的选择。参数可以单通道转换结束或者序列转换结束
【1】、 ADC_EOC_SINGLE_CONV 单通道转换结束
【2】 、ADC_EOC_SEQ_CONV 序列转换结束

//	while(!__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOC));//单通转换结束标志位
	while(!__HAL_ADC_GET_FLAG(hadc,ADC_FLAG_EOS));//序列转换结束标志位

- [ ] Overun behaviou

用于配置ADC转换数据未及时读取,造成溢出时的处理,可以选择继续保留上次转换的数据,也可以选择新转换的数据覆盖

- [ ] Left Bit Shit

用于设置ADC转换结果的左移位数,可设如下:
ADC_LEFTBITSHIFT_NONE
ADC_LEFTBITSHIFT_1
ADC_LEFTBITSHIFT_2
ADC_LEFTBITSHIFT_3
ADC_LEFTBITSHIFT_4
ADC_LEFTBITSHIFT_5
ADC_LEFTBITSHIFT_6
ADC_LEFTBITSHIFT_7
ADC_LEFTBITSHIFT_8
ADC_LEFTBITSHIFT_9
ADC_LEFTBITSHIFT_10
ADC_LEFTBITSHIFT_11
ADC_LEFTBITSHIFT_12
ADC_LEFTBITSHIFT_13
ADC_LEFTBITSHIFT_14
ADC_LEFTBITSHIFT_15

- [ ] Conversion Data Management Mode

用于ADC采集数据的管理,可以存到ADC的DR寄存器,传输给DFSDM,又或者通过DMA的单次或者循环模式传输数据到指定地址
ADC_CONVERSIONDATA_DR
ADC_CONVERSIONDATA_DFSDM
ADC_CONVERSIONDATA_DMA_ONESHOT
ADC_CONVERSIONDATA_DMA_CIRCULAR

用于使能或者禁止低功耗自动延迟等待模式。仅当用户调用函数HAL_ADC_GetValue()获取规则通道数据或者调用函数HAL_ADCEx_InjectedGetValue()获取注入通道数据后才会开启下一次ADC转换。

- [ ] Low Power Auto Wait

  • 此功能可自动将ADC触发频率调整为读取数据速度,即完全由用户决定的自适应模式。这样可以避免溢出,适用于低频应用。
  • 此模式仅可用于ADC的查询模式,不可用于中断或者DMA模式,即调用函数HAL_ADC_Start_IT()或者HAL_ADC_Start_DMA(),因为这种模式会立即清除EOC转换结束标志,从而释放中断请求向量序列。
  • 使用查询模式的调用方法:
    - 调用函数HAL_ADC_Start启动转换。
    - 调用函数HAL_ADC_PollForConversion等待转换结束,然后调用函数HAL_ADC_GetValue可以获取当前的转换值,并且会自动启动下次转换。
    - 如果是注入通道,就是调用函数HAL_ADCExInjected_Start()启动转换,调用函数HAL_ADCEx_InjectedGetValue()获取转换数据。

对应结构体如下 ADC_InitTypeDef:

typedef struct
{
   
   
  uint32_t ClockPrescaler;        /*!< Select ADC clock source (synchronous clock derived from APB clock or asynchronous clock derived from system clock or PLL (Refer to reference manual for list of clocks available)) and clock prescaler.
                                       This parameter can be a value of @ref ADC_HAL_EC_COMMON_CLOCK_SOURCE.
                                       Note: The ADC clock configuration is common to all ADC instances.
                                       Note: In case of usage of channels on injected group, ADC frequency should be lower than AHB clock frequency /4 for resolution 12 or 10 bits,
                                             AHB clock frequency /3 for resolution 8 bits, AHB clock frequency /2 for resolution 6 bits.
                                       Note: In case of synchronous clock mode based on HCLK/1, the configuration must be enabled only
                                             if the system clock has a 50% duty clock cycle (APB prescaler configured inside RCC
                                             must be bypassed and PCLK clock must have 50% duty cycle). Refer to reference manual for details.
                                       Note: In case of usage of asynchronous clock, the selected clock must be preliminarily enabled at RCC top level.
                                       Note: This parameter can be modified only if all ADC instances are disabled. */

  uint32_t Resolution;            /*!< Configure the ADC resolution.
                                       This parameter can be a value of @ref ADC_HAL_EC_RESOLUTION */

#if defined(ADC_VER_V5_V90)
  uint32_t DataAlign;             /*!< Specify ADC data alignment in conversion data register (right or left).
                                       Refer to reference manual for alignments formats versus resolutions.
                                       This parameter can be a value of @ref ADC_HAL_EC_DATA_ALIGN
                                       This parameter is reserved for ADC3 on devices STM32H72xx and STM32H73xx*/
#endif

  uint32_t ScanConvMode;          /*!< Configure the sequencer of ADC groups regular and injected.
                                       This parameter can be associated to parameter 'DiscontinuousConvMode' to have main sequence subdivided in successive parts.
                                       If disabled: Conversion is performed in single mode (one channel converted, the one defined in rank 1).
                                                    Parameters 'NbrOfConversion' and 'InjectedNbrOfConversion' are discarded (equivalent to set to 1).
                                       If enabled:  Conversions are performed in sequence mode (multiple ranks defined by 'NbrOfConversion' or 'InjectedNbrOfConversion' and rank of each channel in sequencer).
                                                    Scan direction is upward: from rank 1 to rank 'n'.
                                       This parameter can be a value of @ref ADC_Scan_mode */

  uint32_t EOCSelection;          /*!< Specify which EOC (End Of Conversion) flag is used for conversion by polling and interruption: end of unitary conversion or end of sequence conversions.
                                       This parameter can be a value of @ref ADC_EOCSelection. */

  FunctionalState LowPowerAutoWait; /*!< Select the dynamic low power Auto Delay: new conversion start only when the previous
                                       conversion (for ADC group regular) or previous sequence (for ADC group injected) has been retrieved by user software,
                                       using function HAL_ADC_GetValue() or HAL_ADCEx_InjectedGetValue().
                                       This feature automatically adapts the frequency of ADC conversions triggers to the speed of the system that reads the data. Moreover, this avoids risk of overrun
                                       for low frequency applications.
                                       This parameter can be set to ENABLE or DISABLE.
                                       Note: It is not recommended to use with interruption or DMA (HAL_ADC_Start_IT(), HAL_ADC_Start_DMA()) since these modes have to clear immediately the EOC flag (by CPU to free the IRQ pending event or by DMA).
                                                       Auto wait will work but fort a very short time, discarding its intended benefit (except specific case of high load of CPU or DMA transfers which can justify usage of auto wait).
                                                       Do use with polling: 1. Start conversion with HAL_ADC_Start(), 2. Later on, when ADC conversion data is needed:
                                                       and use HAL_ADC_GetValue() to retrieve conversion result and trig another conversion (in case of usage of injected group, 
                                                       use the equivalent functions HAL_ADCExInjected_Start(), HAL_ADCEx_InjectedGetValue(), ...). */

  FunctionalState ContinuousConvMode; /*!< Specify whether the conversion is performed in single mode (one conversion) or continuous mode for ADC group regular,
                                       after the first ADC conversion start trigger occurred (software start or external trigger).
                                       This parameter can be set to ENABLE or DISABLE. */

  uint32_t NbrOfConversion;       /*!< Specify the number of ranks that will be converted within the regular group sequencer.
                                       To use the regular group sequencer and convert several ranks, parameter 'ScanConvMode' must be enabled.
                                       This parameter must be a number between Min_Data = 1 and Max_Data = 16.
                                       Note: This parameter must be modified when no conversion is on going on regular group (ADC disabled, or ADC enabled without
                                       continuous mode or external trigger that could launch a conversion). */

  FunctionalState DiscontinuousConvMode; /*!< Specify whether the conversions sequence of ADC group regular 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. */

  uint32_t NbrOfDiscConversion;   /*!< Specifies the number of discontinuous conversions in which the main sequence of ADC group regular (parameter NbrOfConversion) will be subdivided.
                                       If parameter 'DiscontinuousConvMode' is disabled, this parameter is discarded.
                                       This parameter must be a number between Min_Data = 1 and Max_Data = 8. */

  uint32_t ExternalTrigConv;      /*!< Select the external event source used to trigger ADC group regular conversion start.
                                       If set to ADC_SOFTWARE_START, external triggers are disabled and software trigger is used instead.
                                       This parameter can be a value of @ref ADC_regular_external_trigger_source.
                                       Caution: external trigger source is common to all ADC instances. */

  uint32_t ExternalTrigConvEdge;  /*!< Select the external event edge used to trigger ADC group regular conversion start.
                                       If trigger source is set to ADC_SOFTWARE_START, this parameter is discarded.
                                       This parameter can be a value of @ref ADC_regular_external_trigger_edge */

  uint32_t ConversionDataManagement; /*!< Specifies whether the Data conversion data is managed: using the DMA (oneshot or circular), or stored in the DR register or transferred to DFSDM register.
                                       Note: In continuous mode, DMA must be configured in circular mode. Otherwise an overrun will be triggered when DMA buffer maximum pointer is reached.
                                       This parameter can be a value of @ref ADC_ConversionDataManagement.
                                       Note: This parameter must be modified when no conversion is on going on both regular and injected groups
                                       (ADC disabled, or ADC enabled without continuous mode or external trigger that could launch a conversion).*/
#if defined(ADC_VER_V5_V90)
  /*Note: On devices STM32H72xx and STM32H73xx, this parameter is specific to ADC3 only. */

  uint32_t SamplingMode;          /*!< Select the sampling mode to be used for ADC group regular conversion.
                                       This para
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值