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

【经验分享】STM32 ADC 模拟看门狗及其应用

[复制链接]
STMCU小助手 发布时间:2022-2-23 19:05
前言  H6 T" x. ?) {$ M6 g' ]2 ^' k2 A
模拟看门狗特性允许应用程序检测输入电压是否超出用户定义的高低阈值,用户可以预先设定个模拟看门狗的上下限电压值,一旦采集到的电压超出该上下限,将会触发模拟看门狗中断。模拟看门狗一般用于检测单个的常规或注入转换通道,或同时检测所有的常规和注入通道。
; x7 @* J( [0 H2 j  e1 Q模块框图  }2 C+ L! Z: U1 X1 u/ m' z

+ q1 a/ a. V5 d PQL7CUA})XMG1WU}O`IIZSX.png
6 r% q: [- e3 h1 q+ I模拟看门狗可以预先设置 ADC 转换的高低阈值,ADC_HTR 寄存器来配置 ADC 转换的上限阈值,ADC_LTR 寄存器用来配置ADC 转换的下限阈值。( m: I7 r& H* x9 K5 _: M

8 d2 t) h1 x$ l  Y应用示例/ g6 \: X- g* T' |
ADC 配置代码1 q8 V0 l/ A4 \2 M+ u; z$ l
  1. /**" X2 ]9 L: l& f* P4 k0 N7 @

  2. 6 s) Z  G! G' q/ n% j9 x7 y2 |0 D
  3. * @brief ADC configuration. f- c$ h6 ]: Q: m8 r8 Q" G3 b( F* m
  4. * @param None
    * U+ U7 u, o" _1 M  T$ |" m) f
  5. * @retval None1 f( E& [) E  p1 c% ]/ k
  6.   */
    ; v* ~* K( @4 k3 Q  x0 z7 Z
  7.   static void ADC_Config(void): Q" D% [, K: W! u
  8.   {
    ) Y2 c# p/ j+ M. {! F; x8 T
  9.   ADC_ChannelConfTypeDef sConfig;* a3 l% W& z  n1 W3 `7 [
  10.   ADC_AnalogWDGConfTypeDef AnalogWDGConfig;
    5 o" ~/ h& j7 r/ b

  11. * @# p: ~" e7 Y# {4 d# V* Q9 w
  12. /* Configuration of ADCx init structure: ADC parameters and regular group */
    6 ?* a$ k- ~4 s$ `7 ?0 d
  13. AdcHandle.Instance = ADCx;. w2 q1 O( u1 `  [4 M* m5 c& K. R& s: R
  14. AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; /* Asynchronous
    9 \, y. f3 v( T( y) r6 ?
  15. clock mode, input ADC clock not divided */& s- U! B. n9 c
  16. AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit% h, u1 r  S& M) P5 g* |: v; F
  17. resolution for converted data */
      K9 y; o8 @' n# n8 `% n( u. D0 y
  18. AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Rightalignment for converted data */
    " [8 O& J: A% U6 U9 R
  19. AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer9 ]! y1 [* t5 U2 m: L' N; L
  20. disabled (ADC conversion on only 1 channel: channel set on rank 1) */
    # Z6 \7 k, b! X
  21. AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag
    % q0 q8 n2 }$ m3 U- E+ [
  22. picked-up to indicate conversion end */
    ) \7 |" u3 R' x, Y9 f
  23. AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed& g" k6 R+ T) i2 F( G( w( \# P
  24. conversion feature disabled */
    5 w2 }; N: Y& l' ]$ @8 H- K
  25. AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous$ _7 G, g( C6 x5 h! {
  26. mode disabled to have only 1 conversion at each conversion trig */1 M( l2 I' }2 |, A" E) A& ~3 W
  27. AdcHandle.Init.NbrOfConversion = 1; /* Parameter
    " L2 r: g  Q$ g8 J1 {" r
  28. discarded because sequencer is disabled */; K  \0 r: [  a; G! D- o& j
  29. AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter7 h1 n1 D0 d) S8 g! `8 j% d
  30. discarded because sequencer is disabled */
    9 j  H2 y; w! L$ y
  31. AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter& q5 k, O! f3 E/ X0 i6 O
  32. discarded because sequencer is disabled */
    # ?$ e* T1 R% ?0 x9 \" v
  33. AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; /* Timer 30 n% i& y. Q7 B* F0 @/ e! z: J/ u
  34. external event triggering the conversion */
    + f' n: J( }3 `
  35. AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Parameter& E5 K6 g$ i9 b2 m" f. {# i
  36. discarded because software trigger chosen */0 T0 s8 y! Y$ c5 N# Z/ i
  37. AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular/ M6 s/ E, v  @& k8 Y
  38. mode selected */
    6 T& s3 f& o1 a5 [  e8 l6 x
  39. AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is
    1 m# X% d; F9 R1 S- x. j
  40. overwritten with the last conversion result in case of overrun */
    5 F6 w: h! J' l5 ~
  41. AdcHandle.Init.OversamplingMode = DISABLE; /* No/ T) E$ g! c! s$ n( O) g  O
  42. oversampling */  v; B9 Y# F# Z  s
  43. if (HAL_ADC_Init(&AdcHandle) != HAL_OK)& Q% l  l% M& N0 c' E8 x
  44. {
    3 @/ [# E+ N+ W9 E
  45. /* ADC initialization error */
    8 H, S& E" F  @
  46. Error_Handler();' b3 r) p0 C) {
  47. }
    1 [$ b  j2 q0 I$ B1 c' R6 F# {
  48. /* Configuration of channel on ADCx regular group on sequencer rank 1 */  C1 m; E2 _, Z8 [" P! c6 W! p7 k
  49. /* Note: Considering IT occurring after each ADC conversion if ADC */
    : p) q$ w/ U% ~2 m$ `& J% D
  50. /* conversion is out of the analog watchdog window selected (ADC IT */
    ) q: w5 W  i& B5 I, B- A
  51. /* enabled), select sampling time and ADC clock with sufficient */% q) t. w  [- I) }7 Y) y, M
  52. /* duration to not create an overhead situation in IRQHandler. */- d( @! b2 R% Y: R, r
  53. sConfig.Channel = ADC_CHANNEL_5; /* Sampled channel number */; j, @8 z: B) J
  54. sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number) R9 j5 b. r7 U2 q4 S* W
  55. ADCx_CHANNEL */# y; t0 d% K& ?) Y) Y- ?/ q
  56. sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock
    ) F* e! z- B: n& q4 p- |6 V
  57. cycles unit) */
    0 s- T. [& @! m% `6 Q; Z0 G
  58. sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
      b4 A1 y# f$ C- f4 g+ G; G( K
  59. sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
    . d; r5 s% X1 [" X0 G. i2 c
  60. sConfig.Offset = 0; /* Parameter discarded because) i/ H5 A7 }- p. W5 i
  61. offset correction is disabled */# O( {' `4 L0 r
  62. 9 e4 P  S0 _! r5 X" N" ]. H
  63. if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
    ) N1 v. A4 L/ u% e- K
  64. {% p3 D* O( f" z% ?. M! J
  65. /* Channel Configuration Error */
    ' W0 @1 Y6 m- V) H% F- y3 S- y, T
  66. Error_Handler();
      H) W6 `  O9 K' ^+ i, ]
  67. }
    + C2 F0 R5 u' B
  68. 2 q) g* T" P' N" L
  69. /* Set analog watchdog thresholds in order to be between steps of DAC */7 W" ]3 s, E* ^+ {. [9 h
  70. /* voltage. */
    2 L* U$ g7 L; I
  71. /* - High threshold: between DAC steps 1/2 and 3/4 of full range: */
    6 [* f6 @2 _' `
  72. /* 5/8 of full range (4095  Vdda=3.3V): 2559 2.06V */
    ; a6 _' g0 f" [. s' E1 b
  73. /* - Low threshold: between DAC steps 0 and 1/4 of full range: */
    $ g  B; z% x4 J2 l6 S, `
  74. /* 1/8 of full range (4095  Vdda=3.3V): 512  0.41V */
    8 E. l/ E4 `/ n* P9 Q
  75. /* Analog watchdog 1 configuration */
    1 U: j( ~8 b; N2 o
  76. AnalogWDGConfig.WatchdogNumber = ADC_ANALOGWATCHDOG_1;- W3 P8 `8 F3 B2 \
  77. AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_ALL_REG;8 c  l: {9 b1 {, |6 U# t
  78. AnalogWDGConfig.Channel = ADCx_CHANNELa;
    9 y# |( e9 |1 _$ c- i' K
  79. AnalogWDGConfig.ITMode = ENABLE;0 E( b  p1 R0 O/ ]; T) t
  80. AnalogWDGConfig.HighThreshold = (RANGE_12BITS * 5/8);
    6 ]* y9 R3 h6 z+ v# b# r
  81. AnalogWDGConfig.LowThreshold = (RANGE_12BITS * 1/8);
    " Y- \) a/ {8 O# A. V* E' r
  82. if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)/ r( p; ~) R- V) ~% i
  83. {
    6 A' h  u9 |, v
  84. /* Channel Configuration Error */
    . b( b! |/ W) `/ j/ R
  85. Error_Handler();
    . w1 \! R3 [# h; [  b7 e- }
  86. }
    + I+ m' U( b3 H6 U( z
  87. / V; ]1 D5 k1 G. t& ^+ f
  88. }
复制代码

. H3 Q- E0 {3 N9 X1 l) N, H如上图所示,AnalogWDGConfig 结构体中分别使能了模拟看门狗及中断,设置了电压的上下阈值 HighThreshold 和LowThreshold。模拟参考电压为 3.3V,以上代码设置的下限电压阈值为 3.3*1/8=0.41V,上限电压阈值为 3.3*5/8=2.06V。  M! i$ |; I9 V3 L2 ^/ w
  m4 |# J% X2 L# E
模拟看门狗中断服务程序7 `% g' s0 _+ T9 r7 f
  1. /**8 r  L$ y; O" q% @/ P( S1 e

  2. 6 S% c7 z) K: }% d
  3. * @brief Analog watchdog callback in non blocking mode.
    # o  q$ d. _/ k8 T& B, Y
  4. * @param hadc: ADC handle( ]; v& ~* K1 d  [/ Y
  5. * @retval None6 ]0 }; B/ g( c# r* I# c% y' \( a. c4 }
  6.   */
    0 W& N' b# R. ^. y7 H/ ^2 H
  7.   void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
    : g/ Y. \1 ]* W3 c
  8.   {9 G1 E7 p! K. v
  9.   /* Set variable to report analog watchdog out of window status to main */
    8 A# ^  A! J6 K+ ?9 X
  10.   /* program. */3 z0 _3 y8 F% _3 _
  11.   ubAnalogWatchdogStatus = SET;
    # z; a& Q9 l# o2 J: h# A( F- @
  12.   }
复制代码
) J; g% b7 d, m% Q* Z
  当模拟看门狗检测到电压高于上限或者低于下限时将会产生看门狗中断,中断服务程序触发后,可以做出一些应对措施。这里置了 ubAnalogWatchdogStatus 标志,然后由主程序去根据标志的值去执行相应处理程序。
6 W' h+ V1 ?9 r* M9 c3 d, s
  1.   /* Infinite loop */
    5 e& Q  k6 {; \( F- O
  2.   while (1)! @0 l5 N+ _4 t( W5 P- O% Z- Q% o6 {- e$ K
  3.   {4 D/ {: ^( b6 G4 Y- o/ E8 m
  4.   /* Turn-on/off LED1 in function of ADC conversion result */" O* V0 y5 P+ L" X  m" O
  5.   /* - Turn-off if voltage is into AWD window */
    3 J/ q+ h0 A' l" ^
  6.   /* - Turn-on if voltage is out of AWD window */* w3 ~! P! E9 D3 o
  7.   /* Variable of analog watchdog status is set into analog watchdog */5 R5 s8 E3 p( G; U
  8.   /* interrupt callback */
    ! }- W; e& v' K" N0 O+ D
  9.   if (ubAnalogWatchdogStatus == RESET)
    6 C- b2 b7 O& M  k/ Z2 K
  10.   {- ]( P* R/ f3 Z' Q
  11.   BSP_LED_Off(LED1);
    / @5 M6 U. Z, T  M3 p7 h
  12.   }3 P+ A1 o. N3 q0 @& |- ~4 a# s' ~
  13.   else$ W$ ~/ J, x2 v; `/ ?" _4 i
  14.   {
    3 l$ ^( ]8 Q) t! o' \; O# \
  15.   BSP_LED_On(LED1);
    ( [1 E5 H' v. B0 K

  16. ( }0 G9 Z% C" q5 @# X
  17. /* Reset analog watchdog status for next loop iteration */
    ! [7 w5 E3 i) c% u: G  A
  18. ubAnalogWatchdogStatus = RESET;
    ' p+ L& ]1 h1 `/ \
  19. }5 q/ ?3 \5 U9 J3 u. v
  20. }
复制代码

- Q% ~7 ]* c5 E, w启动 ADC 转换代码
7 Q/ ?- z- f7 W0 u4 ~
  1. #define ADCCONVERTEDVALUES_BUFFER_SIZE 256 /* Size of array
    8 [& F- q$ v9 c! ?, L* v
  2. aADCxConvertedValues[] */; L- z* q3 q& H& O0 h6 M8 n
  3. /* Variable containing ADC conversions results */
    + D3 d1 ?( s. W1 V3 A1 \1 v( g! w7 Y" g
  4. __IO uint16_t aADCxConvertedValues[ADCCONVERTEDVALUES_BUFFER_SIZE];
    / N" J  Y& E- t' c  q9 A( c7 ]/ p
  5. /* Start ADC conversion on regular group with transfer by DMA */. _! v( q! A6 _" u6 O8 Q: ?
  6. if (HAL_ADC_Start_DMA(&AdcHandle,/ Z8 P' \1 d  x/ U
  7. (uint32_t *)aADCxConvertedValues,: [2 Y- v% q4 U8 a
  8. ADCCONVERTEDVALUES_BUFFER_SIZE8 f8 R4 ~& M; O. d; R% n+ C# z( V& {' X
  9. ) != HAL_OK)
    3 ?& J& {; P4 E) v# {
  10. {  ~: g0 P& |4 [$ B) o/ }: g
  11. /* Start Error */9 D6 s$ z% @1 g9 k
  12. Error_Handler();/ C; h! y& f0 H, |8 j0 I, b" m
  13. }
复制代码
" G2 F+ N  @. m, s4 c
使用循环模式 DMA 启动 ADC 转换,DMA 可以降低 CPU 负载。- v% d1 w1 y7 \8 d2 e3 e0 y
; ~7 P& G) U" e4 r9 h
结论
$ r& Z' G7 c# L控制系统中,需要测量严格电压、压力、温度等范围的信号,使用模拟看门狗能够快速地检测到异常状况,并做出相应的应对措施,以确保设备安全。
" S+ @1 w) `5 ^& E# s& z  J6 V6 b' g% `& Z; t
赞 收藏 评论0 发布时间:2022-2-23 19:05

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版