你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 ADC相关函数和类型

[复制链接]
STMCU小助手 发布时间:2022-3-30 11:00
01. ADC概述
STM32F4xx 系列一般都有 3 个 ADC,这些 ADC 可以独立使用,也可以使用双重/三重模式(提高采样率)。STM32F4 的 ADC 是 12 位逐次逼近型的模拟数字转换器。它有 19 个通道,可测量 16 个外部源、2 个内部源和 Vbat 通道的信号。这些通道的 A/D 转换可以单次、连续、扫描或间断模式执行。ADC 的结果可以左对齐或右对齐方式存储在 16 位数据寄存器中。 模拟看门狗特性允许应用程序检测输入电压是否超出用户定义的高/低阀值。

相关文件为 stm32f4xx_adc.h和 stm32f4xx_adc.c。

02. 相关类型
  1. /**
  2.   * @brief   ADC Init structure definition  
  3.   */
  4. typedef struct
  5. {
  6.   uint32_t ADC_Resolution;                /*!< Configures the ADC resolution dual mode.
  7.                                                This parameter can be a value of @ref ADC_resolution */                                   
  8.   FunctionalState ADC_ScanConvMode;       /*!< Specifies whether the conversion
  9.                                                is performed in Scan (multichannels)
  10.                                                or Single (one channel) mode.
  11.                                                This parameter can be set to ENABLE or DISABLE */
  12.   FunctionalState ADC_ContinuousConvMode; /*!< Specifies whether the conversion
  13.                                                is performed in Continuous or Single mode.
  14.                                                This parameter can be set to ENABLE or DISABLE. */
  15.   uint32_t ADC_ExternalTrigConvEdge;      /*!< Select the external trigger edge and
  16.                                                enable the trigger of a regular group.
  17.                                                This parameter can be a value of
  18.                                                @ref ADC_external_trigger_edge_for_regular_channels_conversion */
  19.   uint32_t ADC_ExternalTrigConv;          /*!< Select the external event used to trigger
  20.                                                the start of conversion of a regular group.
  21.                                                This parameter can be a value of
  22.                                                @ref ADC_extrenal_trigger_sources_for_regular_channels_conversion */
  23.   uint32_t ADC_DataAlign;                 /*!< Specifies whether the ADC data  alignment
  24.                                                is left or right. This parameter can be
  25.                                                a value of @ref ADC_data_align */
  26.   uint8_t  ADC_NbrOfConversion;           /*!< Specifies the number of ADC conversions
  27.                                                that will be done using the sequencer for
  28.                                                regular channel group.
  29.                                                This parameter must range from 1 to 16. */
  30. }ADC_InitTypeDef;

  31. /**
  32.   * @brief   ADC Common Init structure definition  
  33.   */
  34. typedef struct
  35. {
  36.   uint32_t ADC_Mode;                      /*!< Configures the ADC to operate in
  37.                                                independent or multi mode.
  38.                                                This parameter can be a value of @ref ADC_Common_mode */                                             
  39.   uint32_t ADC_Prescaler;                 /*!< Select the frequency of the clock
  40.                                                to the ADC. The clock is common for all the ADCs.
  41.                                                This parameter can be a value of @ref ADC_Prescaler */
  42.   uint32_t ADC_DMAAccessMode;             /*!< Configures the Direct memory access
  43.                                               mode for multi ADC mode.
  44.                                                This parameter can be a value of
  45.                                                @ref ADC_Direct_memory_access_mode_for_multi_mode */
  46.   uint32_t ADC_TwoSamplingDelay;          /*!< Configures the Delay between 2 sampling phases.
  47.                                                This parameter can be a value of
  48.                                                @ref ADC_delay_between_2_sampling_phases */

  49. }ADC_CommonInitTypeDef;


  50. /* Exported constants --------------------------------------------------------*/

  51. /** @defgroup ADC_Exported_Constants
  52.   * @{
  53.   */
  54. #define IS_ADC_ALL_PERIPH(PERIPH) (((PERIPH) == ADC1) || \
  55.                                    ((PERIPH) == ADC2) || \
  56.                                    ((PERIPH) == ADC3))  

  57. /** @defgroup ADC_Common_mode
  58.   * @{
  59.   */
  60. #define ADC_Mode_Independent                       ((uint32_t)0x00000000)      
  61. #define ADC_DualMode_RegSimult_InjecSimult         ((uint32_t)0x00000001)
  62. #define ADC_DualMode_RegSimult_AlterTrig           ((uint32_t)0x00000002)
  63. #define ADC_DualMode_InjecSimult                   ((uint32_t)0x00000005)
  64. #define ADC_DualMode_RegSimult                     ((uint32_t)0x00000006)
  65. #define ADC_DualMode_Interl                        ((uint32_t)0x00000007)
  66. #define ADC_DualMode_AlterTrig                     ((uint32_t)0x00000009)
  67. #define ADC_TripleMode_RegSimult_InjecSimult       ((uint32_t)0x00000011)
  68. #define ADC_TripleMode_RegSimult_AlterTrig         ((uint32_t)0x00000012)
  69. #define ADC_TripleMode_InjecSimult                 ((uint32_t)0x00000015)
  70. #define ADC_TripleMode_RegSimult                   ((uint32_t)0x00000016)
  71. #define ADC_TripleMode_Interl                      ((uint32_t)0x00000017)
  72. #define ADC_TripleMode_AlterTrig                   ((uint32_t)0x00000019)
  73. #define IS_ADC_MODE(MODE) (((MODE) == ADC_Mode_Independent) || \
  74.                            ((MODE) == ADC_DualMode_RegSimult_InjecSimult) || \
  75.                            ((MODE) == ADC_DualMode_RegSimult_AlterTrig) || \
  76.                            ((MODE) == ADC_DualMode_InjecSimult) || \
  77.                            ((MODE) == ADC_DualMode_RegSimult) || \
  78.                            ((MODE) == ADC_DualMode_Interl) || \
  79.                            ((MODE) == ADC_DualMode_AlterTrig) || \
  80.                            ((MODE) == ADC_TripleMode_RegSimult_InjecSimult) || \
  81.                            ((MODE) == ADC_TripleMode_RegSimult_AlterTrig) || \
  82.                            ((MODE) == ADC_TripleMode_InjecSimult) || \
  83.                            ((MODE) == ADC_TripleMode_RegSimult) || \
  84.                            ((MODE) == ADC_TripleMode_Interl) || \
  85.                            ((MODE) == ADC_TripleMode_AlterTrig))
  86. /**
  87.   * @}
  88.   */


  89. /** @defgroup ADC_Prescaler
  90.   * @{
  91.   */
  92. #define ADC_Prescaler_Div2                         ((uint32_t)0x00000000)
  93. #define ADC_Prescaler_Div4                         ((uint32_t)0x00010000)
  94. #define ADC_Prescaler_Div6                         ((uint32_t)0x00020000)
  95. #define ADC_Prescaler_Div8                         ((uint32_t)0x00030000)
  96. #define IS_ADC_PRESCALER(PRESCALER) (((PRESCALER) == ADC_Prescaler_Div2) || \
  97.                                      ((PRESCALER) == ADC_Prescaler_Div4) || \
  98.                                      ((PRESCALER) == ADC_Prescaler_Div6) || \
  99.                                      ((PRESCALER) == ADC_Prescaler_Div8))
  100. /**
  101.   * @}
  102.   */


  103. /** @defgroup ADC_Direct_memory_access_mode_for_multi_mode
  104.   * @{
  105.   */
  106. #define ADC_DMAAccessMode_Disabled      ((uint32_t)0x00000000)     /* DMA mode disabled */
  107. #define ADC_DMAAccessMode_1             ((uint32_t)0x00004000)     /* DMA mode 1 enabled (2 / 3 half-words one by one - 1 then 2 then 3)*/
  108. #define ADC_DMAAccessMode_2             ((uint32_t)0x00008000)     /* DMA mode 2 enabled (2 / 3 half-words by pairs - 2&1 then 1&3 then 3&2)*/
  109. #define ADC_DMAAccessMode_3             ((uint32_t)0x0000C000)     /* DMA mode 3 enabled (2 / 3 bytes by pairs - 2&1 then 1&3 then 3&2) */
  110. #define IS_ADC_DMA_ACCESS_MODE(MODE) (((MODE) == ADC_DMAAccessMode_Disabled) || \
  111.                                       ((MODE) == ADC_DMAAccessMode_1) || \
  112.                                       ((MODE) == ADC_DMAAccessMode_2) || \
  113.                                       ((MODE) == ADC_DMAAccessMode_3))

  114. /**
  115.   * @}
  116.   */


  117. /** @defgroup ADC_delay_between_2_sampling_phases
  118.   * @{
  119.   */
  120. #define ADC_TwoSamplingDelay_5Cycles               ((uint32_t)0x00000000)
  121. #define ADC_TwoSamplingDelay_6Cycles               ((uint32_t)0x00000100)
  122. #define ADC_TwoSamplingDelay_7Cycles               ((uint32_t)0x00000200)
  123. #define ADC_TwoSamplingDelay_8Cycles               ((uint32_t)0x00000300)
  124. #define ADC_TwoSamplingDelay_9Cycles               ((uint32_t)0x00000400)
  125. #define ADC_TwoSamplingDelay_10Cycles              ((uint32_t)0x00000500)
  126. #define ADC_TwoSamplingDelay_11Cycles              ((uint32_t)0x00000600)
  127. #define ADC_TwoSamplingDelay_12Cycles              ((uint32_t)0x00000700)
  128. #define ADC_TwoSamplingDelay_13Cycles              ((uint32_t)0x00000800)
  129. #define ADC_TwoSamplingDelay_14Cycles              ((uint32_t)0x00000900)
  130. #define ADC_TwoSamplingDelay_15Cycles              ((uint32_t)0x00000A00)
  131. #define ADC_TwoSamplingDelay_16Cycles              ((uint32_t)0x00000B00)
  132. #define ADC_TwoSamplingDelay_17Cycles              ((uint32_t)0x00000C00)
  133. #define ADC_TwoSamplingDelay_18Cycles              ((uint32_t)0x00000D00)
  134. #define ADC_TwoSamplingDelay_19Cycles              ((uint32_t)0x00000E00)
  135. #define ADC_TwoSamplingDelay_20Cycles              ((uint32_t)0x00000F00)
  136. #define IS_ADC_SAMPLING_DELAY(DELAY) (((DELAY) == ADC_TwoSamplingDelay_5Cycles) || \
  137.                                       ((DELAY) == ADC_TwoSamplingDelay_6Cycles) || \
  138.                                       ((DELAY) == ADC_TwoSamplingDelay_7Cycles) || \
  139.                                       ((DELAY) == ADC_TwoSamplingDelay_8Cycles) || \
  140.                                       ((DELAY) == ADC_TwoSamplingDelay_9Cycles) || \
  141.                                       ((DELAY) == ADC_TwoSamplingDelay_10Cycles) || \
  142.                                       ((DELAY) == ADC_TwoSamplingDelay_11Cycles) || \
  143.                                       ((DELAY) == ADC_TwoSamplingDelay_12Cycles) || \
  144.                                       ((DELAY) == ADC_TwoSamplingDelay_13Cycles) || \
  145.                                       ((DELAY) == ADC_TwoSamplingDelay_14Cycles) || \
  146.                                       ((DELAY) == ADC_TwoSamplingDelay_15Cycles) || \
  147.                                       ((DELAY) == ADC_TwoSamplingDelay_16Cycles) || \
  148.                                       ((DELAY) == ADC_TwoSamplingDelay_17Cycles) || \
  149.                                       ((DELAY) == ADC_TwoSamplingDelay_18Cycles) || \
  150.                                       ((DELAY) == ADC_TwoSamplingDelay_19Cycles) || \
  151.                                       ((DELAY) == ADC_TwoSamplingDelay_20Cycles))

  152. /**
  153.   * @}
  154.   */


  155. /** @defgroup ADC_resolution
  156.   * @{
  157.   */
  158. #define ADC_Resolution_12b                         ((uint32_t)0x00000000)
  159. #define ADC_Resolution_10b                         ((uint32_t)0x01000000)
  160. #define ADC_Resolution_8b                          ((uint32_t)0x02000000)
  161. #define ADC_Resolution_6b                          ((uint32_t)0x03000000)
  162. #define IS_ADC_RESOLUTION(RESOLUTION) (((RESOLUTION) == ADC_Resolution_12b) || \
  163.                                        ((RESOLUTION) == ADC_Resolution_10b) || \
  164.                                        ((RESOLUTION) == ADC_Resolution_8b) || \
  165.                                        ((RESOLUTION) == ADC_Resolution_6b))

  166. /**
  167.   * @}
  168.   */


  169. /** @defgroup ADC_external_trigger_edge_for_regular_channels_conversion
  170.   * @{
  171.   */
  172. #define ADC_ExternalTrigConvEdge_None          ((uint32_t)0x00000000)
  173. #define ADC_ExternalTrigConvEdge_Rising        ((uint32_t)0x10000000)
  174. #define ADC_ExternalTrigConvEdge_Falling       ((uint32_t)0x20000000)
  175. #define ADC_ExternalTrigConvEdge_RisingFalling ((uint32_t)0x30000000)
  176. #define IS_ADC_EXT_TRIG_EDGE(EDGE) (((EDGE) == ADC_ExternalTrigConvEdge_None) || \
  177.                              ((EDGE) == ADC_ExternalTrigConvEdge_Rising) || \
  178.                              ((EDGE) == ADC_ExternalTrigConvEdge_Falling) || \
  179.                              ((EDGE) == ADC_ExternalTrigConvEdge_RisingFalling))
  180. /**
  181.   * @}
  182.   */


  183. /** @defgroup ADC_extrenal_trigger_sources_for_regular_channels_conversion
  184.   * @{
  185.   */
  186. #define ADC_ExternalTrigConv_T1_CC1                ((uint32_t)0x00000000)
  187. #define ADC_ExternalTrigConv_T1_CC2                ((uint32_t)0x01000000)
  188. #define ADC_ExternalTrigConv_T1_CC3                ((uint32_t)0x02000000)
  189. #define ADC_ExternalTrigConv_T2_CC2                ((uint32_t)0x03000000)
  190. #define ADC_ExternalTrigConv_T2_CC3                ((uint32_t)0x04000000)
  191. #define ADC_ExternalTrigConv_T2_CC4                ((uint32_t)0x05000000)
  192. #define ADC_ExternalTrigConv_T2_TRGO               ((uint32_t)0x06000000)
  193. #define ADC_ExternalTrigConv_T3_CC1                ((uint32_t)0x07000000)
  194. #define ADC_ExternalTrigConv_T3_TRGO               ((uint32_t)0x08000000)
  195. #define ADC_ExternalTrigConv_T4_CC4                ((uint32_t)0x09000000)
  196. #define ADC_ExternalTrigConv_T5_CC1                ((uint32_t)0x0A000000)
  197. #define ADC_ExternalTrigConv_T5_CC2                ((uint32_t)0x0B000000)
  198. #define ADC_ExternalTrigConv_T5_CC3                ((uint32_t)0x0C000000)
  199. #define ADC_ExternalTrigConv_T8_CC1                ((uint32_t)0x0D000000)
  200. #define ADC_ExternalTrigConv_T8_TRGO               ((uint32_t)0x0E000000)
  201. #define ADC_ExternalTrigConv_Ext_IT11              ((uint32_t)0x0F000000)
  202. #define IS_ADC_EXT_TRIG(REGTRIG) (((REGTRIG) == ADC_ExternalTrigConv_T1_CC1) || \
  203.                                   ((REGTRIG) == ADC_ExternalTrigConv_T1_CC2) || \
  204.                                   ((REGTRIG) == ADC_ExternalTrigConv_T1_CC3) || \
  205.                                   ((REGTRIG) == ADC_ExternalTrigConv_T2_CC2) || \
  206.                                   ((REGTRIG) == ADC_ExternalTrigConv_T2_CC3) || \
  207.                                   ((REGTRIG) == ADC_ExternalTrigConv_T2_CC4) || \
  208.                                   ((REGTRIG) == ADC_ExternalTrigConv_T2_TRGO) || \
  209.                                   ((REGTRIG) == ADC_ExternalTrigConv_T3_CC1) || \
  210.                                   ((REGTRIG) == ADC_ExternalTrigConv_T3_TRGO) || \
  211.                                   ((REGTRIG) == ADC_ExternalTrigConv_T4_CC4) || \
  212.                                   ((REGTRIG) == ADC_ExternalTrigConv_T5_CC1) || \
  213.                                   ((REGTRIG) == ADC_ExternalTrigConv_T5_CC2) || \
  214.                                   ((REGTRIG) == ADC_ExternalTrigConv_T5_CC3) || \
  215.                                   ((REGTRIG) == ADC_ExternalTrigConv_T8_CC1) || \
  216.                                   ((REGTRIG) == ADC_ExternalTrigConv_T8_TRGO) || \
  217.                                   ((REGTRIG) == ADC_ExternalTrigConv_Ext_IT11))
  218. /**
  219.   * @}
  220.   */


  221. /** @defgroup ADC_data_align
  222.   * @{
  223.   */
  224. #define ADC_DataAlign_Right                        ((uint32_t)0x00000000)
  225. #define ADC_DataAlign_Left                         ((uint32_t)0x00000800)
  226. #define IS_ADC_DATA_ALIGN(ALIGN) (((ALIGN) == ADC_DataAlign_Right) || \
  227.                                   ((ALIGN) == ADC_DataAlign_Left))
  228. /**
  229.   * @}
  230.   */


  231. /** @defgroup ADC_channels
  232.   * @{
  233.   */
  234. #define ADC_Channel_0                               ((uint8_t)0x00)
  235. #define ADC_Channel_1                               ((uint8_t)0x01)
  236. #define ADC_Channel_2                               ((uint8_t)0x02)
  237. #define ADC_Channel_3                               ((uint8_t)0x03)
  238. #define ADC_Channel_4                               ((uint8_t)0x04)
  239. #define ADC_Channel_5                               ((uint8_t)0x05)
  240. #define ADC_Channel_6                               ((uint8_t)0x06)
  241. #define ADC_Channel_7                               ((uint8_t)0x07)
  242. #define ADC_Channel_8                               ((uint8_t)0x08)
  243. #define ADC_Channel_9                               ((uint8_t)0x09)
  244. #define ADC_Channel_10                              ((uint8_t)0x0A)
  245. #define ADC_Channel_11                              ((uint8_t)0x0B)
  246. #define ADC_Channel_12                              ((uint8_t)0x0C)
  247. #define ADC_Channel_13                              ((uint8_t)0x0D)
  248. #define ADC_Channel_14                              ((uint8_t)0x0E)
  249. #define ADC_Channel_15                              ((uint8_t)0x0F)
  250. #define ADC_Channel_16                              ((uint8_t)0x10)
  251. #define ADC_Channel_17                              ((uint8_t)0x11)
  252. #define ADC_Channel_18                              ((uint8_t)0x12)

  253. #if defined (STM32F40_41xxx) || defined(STM32F412xG) || defined(STM32F413_423xx)
  254. #define ADC_Channel_TempSensor                      ((uint8_t)ADC_Channel_16)
  255. #endif /* STM32F40_41xxx || STM32F412xG || STM32F413_423xx */

  256. #if defined (STM32F427_437xx) || defined (STM32F429_439xx) || defined (STM32F401xx) || defined (STM32F410xx) || defined (STM32F411xE)
  257. #define ADC_Channel_TempSensor                      ((uint8_t)ADC_Channel_18)
  258. #endif /* STM32F427_437xx || STM32F429_439xx || STM32F401xx || STM32F410xx || STM32F411xE */

  259. #define ADC_Channel_Vrefint                         ((uint8_t)ADC_Channel_17)
  260. #define ADC_Channel_Vbat                            ((uint8_t)ADC_Channel_18)

  261. #define IS_ADC_CHANNEL(CHANNEL) (((CHANNEL) == ADC_Channel_0) || \
  262.                                  ((CHANNEL) == ADC_Channel_1) || \
  263.                                  ((CHANNEL) == ADC_Channel_2) || \
  264.                                  ((CHANNEL) == ADC_Channel_3) || \
  265.                                  ((CHANNEL) == ADC_Channel_4) || \
  266.                                  ((CHANNEL) == ADC_Channel_5) || \
  267.                                  ((CHANNEL) == ADC_Channel_6) || \
  268.                                  ((CHANNEL) == ADC_Channel_7) || \
  269.                                  ((CHANNEL) == ADC_Channel_8) || \
  270.                                  ((CHANNEL) == ADC_Channel_9) || \
  271.                                  ((CHANNEL) == ADC_Channel_10) || \
  272.                                  ((CHANNEL) == ADC_Channel_11) || \
  273.                                  ((CHANNEL) == ADC_Channel_12) || \
  274.                                  ((CHANNEL) == ADC_Channel_13) || \
  275.                                  ((CHANNEL) == ADC_Channel_14) || \
  276.                                  ((CHANNEL) == ADC_Channel_15) || \
  277.                                  ((CHANNEL) == ADC_Channel_16) || \
  278.                                  ((CHANNEL) == ADC_Channel_17) || \
  279.                                  ((CHANNEL) == ADC_Channel_18))
  280. /**
  281.   * @}
  282.   */


  283. /** @defgroup ADC_sampling_times
  284.   * @{
  285.   */
  286. #define ADC_SampleTime_3Cycles                    ((uint8_t)0x00)
  287. #define ADC_SampleTime_15Cycles                   ((uint8_t)0x01)
  288. #define ADC_SampleTime_28Cycles                   ((uint8_t)0x02)
  289. #define ADC_SampleTime_56Cycles                   ((uint8_t)0x03)
  290. #define ADC_SampleTime_84Cycles                   ((uint8_t)0x04)
  291. #define ADC_SampleTime_112Cycles                  ((uint8_t)0x05)
  292. #define ADC_SampleTime_144Cycles                  ((uint8_t)0x06)
  293. #define ADC_SampleTime_480Cycles                  ((uint8_t)0x07)
  294. #define IS_ADC_SAMPLE_TIME(TIME) (((TIME) == ADC_SampleTime_3Cycles) || \
  295.                                   ((TIME) == ADC_SampleTime_15Cycles) || \
  296.                                   ((TIME) == ADC_SampleTime_28Cycles) || \
  297.                                   ((TIME) == ADC_SampleTime_56Cycles) || \
  298.                                   ((TIME) == ADC_SampleTime_84Cycles) || \
  299.                                   ((TIME) == ADC_SampleTime_112Cycles) || \
  300.                                   ((TIME) == ADC_SampleTime_144Cycles) || \
  301.                                   ((TIME) == ADC_SampleTime_480Cycles))
  302. /**
  303.   * @}
  304.   */


  305. /** @defgroup ADC_external_trigger_edge_for_injected_channels_conversion
  306.   * @{
  307.   */
  308. #define ADC_ExternalTrigInjecConvEdge_None          ((uint32_t)0x00000000)
  309. #define ADC_ExternalTrigInjecConvEdge_Rising        ((uint32_t)0x00100000)
  310. #define ADC_ExternalTrigInjecConvEdge_Falling       ((uint32_t)0x00200000)
  311. #define ADC_ExternalTrigInjecConvEdge_RisingFalling ((uint32_t)0x00300000)
  312. #define IS_ADC_EXT_INJEC_TRIG_EDGE(EDGE) (((EDGE) == ADC_ExternalTrigInjecConvEdge_None) || \
  313.                                           ((EDGE) == ADC_ExternalTrigInjecConvEdge_Rising) || \
  314.                                           ((EDGE) == ADC_ExternalTrigInjecConvEdge_Falling) || \
  315.                                           ((EDGE) == ADC_ExternalTrigInjecConvEdge_RisingFalling))

  316. /**
  317.   * @}
  318.   */


  319. /** @defgroup ADC_extrenal_trigger_sources_for_injected_channels_conversion
  320.   * @{
  321.   */
  322. #define ADC_ExternalTrigInjecConv_T1_CC4            ((uint32_t)0x00000000)
  323. #define ADC_ExternalTrigInjecConv_T1_TRGO           ((uint32_t)0x00010000)
  324. #define ADC_ExternalTrigInjecConv_T2_CC1            ((uint32_t)0x00020000)
  325. #define ADC_ExternalTrigInjecConv_T2_TRGO           ((uint32_t)0x00030000)
  326. #define ADC_ExternalTrigInjecConv_T3_CC2            ((uint32_t)0x00040000)
  327. #define ADC_ExternalTrigInjecConv_T3_CC4            ((uint32_t)0x00050000)
  328. #define ADC_ExternalTrigInjecConv_T4_CC1            ((uint32_t)0x00060000)
  329. #define ADC_ExternalTrigInjecConv_T4_CC2            ((uint32_t)0x00070000)
  330. #define ADC_ExternalTrigInjecConv_T4_CC3            ((uint32_t)0x00080000)
  331. #define ADC_ExternalTrigInjecConv_T4_TRGO           ((uint32_t)0x00090000)
  332. #define ADC_ExternalTrigInjecConv_T5_CC4            ((uint32_t)0x000A0000)
  333. #define ADC_ExternalTrigInjecConv_T5_TRGO           ((uint32_t)0x000B0000)
  334. #define ADC_ExternalTrigInjecConv_T8_CC2            ((uint32_t)0x000C0000)
  335. #define ADC_ExternalTrigInjecConv_T8_CC3            ((uint32_t)0x000D0000)
  336. #define ADC_ExternalTrigInjecConv_T8_CC4            ((uint32_t)0x000E0000)
  337. #define ADC_ExternalTrigInjecConv_Ext_IT15          ((uint32_t)0x000F0000)
  338. #define IS_ADC_EXT_INJEC_TRIG(INJTRIG) (((INJTRIG) == ADC_ExternalTrigInjecConv_T1_CC4) || \
  339.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T1_TRGO) || \
  340.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T2_CC1) || \
  341.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T2_TRGO) || \
  342.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T3_CC2) || \
  343.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T3_CC4) || \
  344.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T4_CC1) || \
  345.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T4_CC2) || \
  346.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T4_CC3) || \
  347.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T4_TRGO) || \
  348.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T5_CC4) || \
  349.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T5_TRGO) || \
  350.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T8_CC2) || \
  351.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T8_CC3) || \
  352.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_T8_CC4) || \
  353.                                         ((INJTRIG) == ADC_ExternalTrigInjecConv_Ext_IT15))
  354. /**
  355.   * @}
  356.   */


  357. /** @defgroup ADC_injected_channel_selection
  358.   * @{
  359.   */
  360. #define ADC_InjectedChannel_1                       ((uint8_t)0x14)
  361. #define ADC_InjectedChannel_2                       ((uint8_t)0x18)
  362. #define ADC_InjectedChannel_3                       ((uint8_t)0x1C)
  363. #define ADC_InjectedChannel_4                       ((uint8_t)0x20)
  364. #define IS_ADC_INJECTED_CHANNEL(CHANNEL) (((CHANNEL) == ADC_InjectedChannel_1) || \
  365.                                           ((CHANNEL) == ADC_InjectedChannel_2) || \
  366.                                           ((CHANNEL) == ADC_InjectedChannel_3) || \
  367.                                           ((CHANNEL) == ADC_InjectedChannel_4))
  368. /**
  369.   * @}
  370.   */


  371. /** @defgroup ADC_analog_watchdog_selection
  372.   * @{
  373.   */
  374. #define ADC_AnalogWatchdog_SingleRegEnable         ((uint32_t)0x00800200)
  375. #define ADC_AnalogWatchdog_SingleInjecEnable       ((uint32_t)0x00400200)
  376. #define ADC_AnalogWatchdog_SingleRegOrInjecEnable  ((uint32_t)0x00C00200)
  377. #define ADC_AnalogWatchdog_AllRegEnable            ((uint32_t)0x00800000)
  378. #define ADC_AnalogWatchdog_AllInjecEnable          ((uint32_t)0x00400000)
  379. #define ADC_AnalogWatchdog_AllRegAllInjecEnable    ((uint32_t)0x00C00000)
  380. #define ADC_AnalogWatchdog_None                    ((uint32_t)0x00000000)
  381. #define IS_ADC_ANALOG_WATCHDOG(WATCHDOG) (((WATCHDOG) == ADC_AnalogWatchdog_SingleRegEnable) || \
  382.                                           ((WATCHDOG) == ADC_AnalogWatchdog_SingleInjecEnable) || \
  383.                                           ((WATCHDOG) == ADC_AnalogWatchdog_SingleRegOrInjecEnable) || \
  384.                                           ((WATCHDOG) == ADC_AnalogWatchdog_AllRegEnable) || \
  385.                                           ((WATCHDOG) == ADC_AnalogWatchdog_AllInjecEnable) || \
  386.                                           ((WATCHDOG) == ADC_AnalogWatchdog_AllRegAllInjecEnable) || \
  387.                                           ((WATCHDOG) == ADC_AnalogWatchdog_None))
  388. /**
  389.   * @}
  390.   */


  391. /** @defgroup ADC_interrupts_definition
  392.   * @{
  393.   */
  394. #define ADC_IT_EOC                                 ((uint16_t)0x0205)  
  395. #define ADC_IT_AWD                                 ((uint16_t)0x0106)  
  396. #define ADC_IT_JEOC                                ((uint16_t)0x0407)  
  397. #define ADC_IT_OVR                                 ((uint16_t)0x201A)  
  398. #define IS_ADC_IT(IT) (((IT) == ADC_IT_EOC) || ((IT) == ADC_IT_AWD) || \
  399.                        ((IT) == ADC_IT_JEOC)|| ((IT) == ADC_IT_OVR))
  400. /**
  401.   * @}
  402.   */


  403. /** @defgroup ADC_flags_definition
  404.   * @{
  405.   */
  406. #define ADC_FLAG_AWD                               ((uint8_t)0x01)
  407. #define ADC_FLAG_EOC                               ((uint8_t)0x02)
  408. #define ADC_FLAG_JEOC                              ((uint8_t)0x04)
  409. #define ADC_FLAG_JSTRT                             ((uint8_t)0x08)
  410. #define ADC_FLAG_STRT                              ((uint8_t)0x10)
  411. #define ADC_FLAG_OVR                               ((uint8_t)0x20)   

  412. #define IS_ADC_CLEAR_FLAG(FLAG) ((((FLAG) & (uint8_t)0xC0) == 0x00) && ((FLAG) != 0x00))   
  413. #define IS_ADC_GET_FLAG(FLAG) (((FLAG) == ADC_FLAG_AWD) || \
  414.                                ((FLAG) == ADC_FLAG_EOC) || \
  415.                                ((FLAG) == ADC_FLAG_JEOC) || \
  416.                                ((FLAG)== ADC_FLAG_JSTRT) || \
  417.                                ((FLAG) == ADC_FLAG_STRT) || \
  418.                                ((FLAG)== ADC_FLAG_OVR))     
  419. /**
  420.   * @}
  421.   */


  422. /** @defgroup ADC_thresholds
  423.   * @{
  424.   */
  425. #define IS_ADC_THRESHOLD(THRESHOLD) ((THRESHOLD) <= 0xFFF)
  426. /**
  427.   * @}
  428.   */


  429. /** @defgroup ADC_injected_offset
  430.   * @{
  431.   */
  432. #define IS_ADC_OFFSET(OFFSET) ((OFFSET) <= 0xFFF)
  433. /**
  434.   * @}
  435.   */


  436. /** @defgroup ADC_injected_length
  437.   * @{
  438.   */
  439. #define IS_ADC_INJECTED_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x4))
  440. /**
  441.   * @}
  442.   */


  443. /** @defgroup ADC_injected_rank
  444.   * @{
  445.   */
  446. #define IS_ADC_INJECTED_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x4))
  447. /**
  448.   * @}
  449.   */


  450. /** @defgroup ADC_regular_length
  451.   * @{
  452.   */
  453. #define IS_ADC_REGULAR_LENGTH(LENGTH) (((LENGTH) >= 0x1) && ((LENGTH) <= 0x10))
  454. /**
  455.   * @}
  456.   */


  457. /** @defgroup ADC_regular_rank
  458.   * @{
  459.   */
  460. #define IS_ADC_REGULAR_RANK(RANK) (((RANK) >= 0x1) && ((RANK) <= 0x10))
  461. /**
  462.   * @}
  463.   */


  464. /** @defgroup ADC_regular_discontinuous_mode_number
  465.   * @{
  466.   */
  467. #define IS_ADC_REGULAR_DISC_NUMBER(NUMBER) (((NUMBER) >= 0x1) && ((NUMBER) <= 0x8))
  468. /**
  469.   * @}
  470.   */
复制代码

03. 相关函数
  1. /*  Function used to set the ADC configuration to the default reset state *****/  
  2. void ADC_DeInit(void);

  3. /* Initialization and Configuration functions *********************************/
  4. void ADC_Init(ADC_TypeDef* ADCx, ADC_InitTypeDef* ADC_InitStruct);
  5. void ADC_StructInit(ADC_InitTypeDef* ADC_InitStruct);
  6. void ADC_CommonInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct);
  7. void ADC_CommonStructInit(ADC_CommonInitTypeDef* ADC_CommonInitStruct);
  8. void ADC_Cmd(ADC_TypeDef* ADCx, FunctionalState NewState);

  9. /* Analog Watchdog configuration functions ************************************/
  10. void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  11. void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold,uint16_t LowThreshold);
  12. void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);

  13. /* Temperature Sensor, Vrefint and VBAT management functions ******************/
  14. void ADC_TempSensorVrefintCmd(FunctionalState NewState);
  15. void ADC_VBATCmd(FunctionalState NewState);

  16. /* Regular Channels Configuration functions ***********************************/
  17. void ADC_RegularChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  18. void ADC_SoftwareStartConv(ADC_TypeDef* ADCx);
  19. FlagStatus ADC_GetSoftwareStartConvStatus(ADC_TypeDef* ADCx);
  20. void ADC_EOCOnEachRegularChannelCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  21. void ADC_ContinuousModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  22. void ADC_DiscModeChannelCountConfig(ADC_TypeDef* ADCx, uint8_t Number);
  23. void ADC_DiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  24. uint16_t ADC_GetConversionValue(ADC_TypeDef* ADCx);
  25. uint32_t ADC_GetMultiModeConversionValue(void);

  26. /* Regular Channels DMA Configuration functions *******************************/
  27. void ADC_DMACmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  28. void ADC_DMARequestAfterLastTransferCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  29. void ADC_MultiModeDMARequestAfterLastTransferCmd(FunctionalState NewState);

  30. /* Injected channels Configuration functions **********************************/
  31. void ADC_InjectedChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel, uint8_t Rank, uint8_t ADC_SampleTime);
  32. void ADC_InjectedSequencerLengthConfig(ADC_TypeDef* ADCx, uint8_t Length);
  33. void ADC_SetInjectedOffset(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel, uint16_t Offset);
  34. void ADC_ExternalTrigInjectedConvConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConv);
  35. void ADC_ExternalTrigInjectedConvEdgeConfig(ADC_TypeDef* ADCx, uint32_t ADC_ExternalTrigInjecConvEdge);
  36. void ADC_SoftwareStartInjectedConv(ADC_TypeDef* ADCx);
  37. FlagStatus ADC_GetSoftwareStartInjectedConvCmdStatus(ADC_TypeDef* ADCx);
  38. void ADC_AutoInjectedConvCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  39. void ADC_InjectedDiscModeCmd(ADC_TypeDef* ADCx, FunctionalState NewState);
  40. uint16_t ADC_GetInjectedConversionValue(ADC_TypeDef* ADCx, uint8_t ADC_InjectedChannel);

  41. /* Interrupts and flags management functions **********************************/
  42. void ADC_ITConfig(ADC_TypeDef* ADCx, uint16_t ADC_IT, FunctionalState NewState);
  43. FlagStatus ADC_GetFlagStatus(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  44. void ADC_ClearFlag(ADC_TypeDef* ADCx, uint8_t ADC_FLAG);
  45. ITStatus ADC_GetITStatus(ADC_TypeDef* ADCx, uint16_t ADC_IT);
  46. void ADC_ClearITPendingBit(ADC_TypeDef* ADCx, uint16_t ADC_IT);
复制代码

04. 结构体封装
  1. /**
  2.   * @brief Analog to Digital Converter  
  3.   */

  4. typedef struct
  5. {
  6.   __IO uint32_t SR;     /*!< ADC status register,                         Address offset: 0x00 */
  7.   __IO uint32_t CR1;    /*!< ADC control register 1,                      Address offset: 0x04 */      
  8.   __IO uint32_t CR2;    /*!< ADC control register 2,                      Address offset: 0x08 */
  9.   __IO uint32_t SMPR1;  /*!< ADC sample time register 1,                  Address offset: 0x0C */
  10.   __IO uint32_t SMPR2;  /*!< ADC sample time register 2,                  Address offset: 0x10 */
  11.   __IO uint32_t JOFR1;  /*!< ADC injected channel data offset register 1, Address offset: 0x14 */
  12.   __IO uint32_t JOFR2;  /*!< ADC injected channel data offset register 2, Address offset: 0x18 */
  13.   __IO uint32_t JOFR3;  /*!< ADC injected channel data offset register 3, Address offset: 0x1C */
  14.   __IO uint32_t JOFR4;  /*!< ADC injected channel data offset register 4, Address offset: 0x20 */
  15.   __IO uint32_t HTR;    /*!< ADC watchdog higher threshold register,      Address offset: 0x24 */
  16.   __IO uint32_t LTR;    /*!< ADC watchdog lower threshold register,       Address offset: 0x28 */
  17.   __IO uint32_t SQR1;   /*!< ADC regular sequence register 1,             Address offset: 0x2C */
  18.   __IO uint32_t SQR2;   /*!< ADC regular sequence register 2,             Address offset: 0x30 */
  19.   __IO uint32_t SQR3;   /*!< ADC regular sequence register 3,             Address offset: 0x34 */
  20.   __IO uint32_t JSQR;   /*!< ADC injected sequence register,              Address offset: 0x38 */
  21.   __IO uint32_t JDR1;   /*!< ADC injected data register 1,                Address offset: 0x3C */
  22.   __IO uint32_t JDR2;   /*!< ADC injected data register 2,                Address offset: 0x40 */
  23.   __IO uint32_t JDR3;   /*!< ADC injected data register 3,                Address offset: 0x44 */
  24.   __IO uint32_t JDR4;   /*!< ADC injected data register 4,                Address offset: 0x48 */
  25.   __IO uint32_t DR;     /*!< ADC regular data register,                   Address offset: 0x4C */
  26. } ADC_TypeDef;

复制代码


收藏 评论0 发布时间:2022-3-30 11:00

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版