前言 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
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
- /**" X2 ]9 L: l& f* P4 k0 N7 @
6 s) Z G! G' q/ n% j9 x7 y2 |0 D- * @brief ADC configuration. f- c$ h6 ]: Q: m8 r8 Q" G3 b( F* m
- * @param None
* U+ U7 u, o" _1 M T$ |" m) f - * @retval None1 f( E& [) E p1 c% ]/ k
- */
; v* ~* K( @4 k3 Q x0 z7 Z - static void ADC_Config(void): Q" D% [, K: W! u
- {
) Y2 c# p/ j+ M. {! F; x8 T - ADC_ChannelConfTypeDef sConfig;* a3 l% W& z n1 W3 `7 [
- ADC_AnalogWDGConfTypeDef AnalogWDGConfig;
5 o" ~/ h& j7 r/ b
* @# p: ~" e7 Y# {4 d# V* Q9 w- /* Configuration of ADCx init structure: ADC parameters and regular group */
6 ?* a$ k- ~4 s$ `7 ?0 d - AdcHandle.Instance = ADCx;. w2 q1 O( u1 ` [4 M* m5 c& K. R& s: R
- AdcHandle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; /* Asynchronous
9 \, y. f3 v( T( y) r6 ? - clock mode, input ADC clock not divided */& s- U! B. n9 c
- AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit% h, u1 r S& M) P5 g* |: v; F
- resolution for converted data */
K9 y; o8 @' n# n8 `% n( u. D0 y - AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Rightalignment for converted data */
" [8 O& J: A% U6 U9 R - AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer9 ]! y1 [* t5 U2 m: L' N; L
- disabled (ADC conversion on only 1 channel: channel set on rank 1) */
# Z6 \7 k, b! X - AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag
% q0 q8 n2 }$ m3 U- E+ [ - picked-up to indicate conversion end */
) \7 |" u3 R' x, Y9 f - AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed& g" k6 R+ T) i2 F( G( w( \# P
- conversion feature disabled */
5 w2 }; N: Y& l' ]$ @8 H- K - AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous$ _7 G, g( C6 x5 h! {
- mode disabled to have only 1 conversion at each conversion trig */1 M( l2 I' }2 |, A" E) A& ~3 W
- AdcHandle.Init.NbrOfConversion = 1; /* Parameter
" L2 r: g Q$ g8 J1 {" r - discarded because sequencer is disabled */; K \0 r: [ a; G! D- o& j
- AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter7 h1 n1 D0 d) S8 g! `8 j% d
- discarded because sequencer is disabled */
9 j H2 y; w! L$ y - AdcHandle.Init.NbrOfDiscConversion = 1; /* Parameter& q5 k, O! f3 E/ X0 i6 O
- discarded because sequencer is disabled */
# ?$ e* T1 R% ?0 x9 \" v - AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO; /* Timer 30 n% i& y. Q7 B* F0 @/ e! z: J/ u
- external event triggering the conversion */
+ f' n: J( }3 ` - AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; /* Parameter& E5 K6 g$ i9 b2 m" f. {# i
- discarded because software trigger chosen */0 T0 s8 y! Y$ c5 N# Z/ i
- AdcHandle.Init.DMAContinuousRequests = ENABLE; /* DMA circular/ M6 s/ E, v @& k8 Y
- mode selected */
6 T& s3 f& o1 a5 [ e8 l6 x - AdcHandle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is
1 m# X% d; F9 R1 S- x. j - overwritten with the last conversion result in case of overrun */
5 F6 w: h! J' l5 ~ - AdcHandle.Init.OversamplingMode = DISABLE; /* No/ T) E$ g! c! s$ n( O) g O
- oversampling */ v; B9 Y# F# Z s
- if (HAL_ADC_Init(&AdcHandle) != HAL_OK)& Q% l l% M& N0 c' E8 x
- {
3 @/ [# E+ N+ W9 E - /* ADC initialization error */
8 H, S& E" F @ - Error_Handler();' b3 r) p0 C) {
- }
1 [$ b j2 q0 I$ B1 c' R6 F# { - /* Configuration of channel on ADCx regular group on sequencer rank 1 */ C1 m; E2 _, Z8 [" P! c6 W! p7 k
- /* Note: Considering IT occurring after each ADC conversion if ADC */
: p) q$ w/ U% ~2 m$ `& J% D - /* conversion is out of the analog watchdog window selected (ADC IT */
) q: w5 W i& B5 I, B- A - /* enabled), select sampling time and ADC clock with sufficient */% q) t. w [- I) }7 Y) y, M
- /* duration to not create an overhead situation in IRQHandler. */- d( @! b2 R% Y: R, r
- sConfig.Channel = ADC_CHANNEL_5; /* Sampled channel number */; j, @8 z: B) J
- sConfig.Rank = ADC_REGULAR_RANK_1; /* Rank of sampled channel number) R9 j5 b. r7 U2 q4 S* W
- ADCx_CHANNEL */# y; t0 d% K& ?) Y) Y- ?/ q
- sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5; /* Sampling time (number of clock
) F* e! z- B: n& q4 p- |6 V - cycles unit) */
0 s- T. [& @! m% `6 Q; Z0 G - sConfig.SingleDiff = ADC_SINGLE_ENDED; /* Single-ended input channel */
b4 A1 y# f$ C- f4 g+ G; G( K - sConfig.OffsetNumber = ADC_OFFSET_NONE; /* No offset subtraction */
. d; r5 s% X1 [" X0 G. i2 c - sConfig.Offset = 0; /* Parameter discarded because) i/ H5 A7 }- p. W5 i
- offset correction is disabled */# O( {' `4 L0 r
- 9 e4 P S0 _! r5 X" N" ]. H
- if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
) N1 v. A4 L/ u% e- K - {% p3 D* O( f" z% ?. M! J
- /* Channel Configuration Error */
' W0 @1 Y6 m- V) H% F- y3 S- y, T - Error_Handler();
H) W6 ` O9 K' ^+ i, ] - }
+ C2 F0 R5 u' B - 2 q) g* T" P' N" L
- /* Set analog watchdog thresholds in order to be between steps of DAC */7 W" ]3 s, E* ^+ {. [9 h
- /* voltage. */
2 L* U$ g7 L; I - /* - High threshold: between DAC steps 1/2 and 3/4 of full range: */
6 [* f6 @2 _' ` - /* 5/8 of full range (4095 Vdda=3.3V): 2559 2.06V */
; a6 _' g0 f" [. s' E1 b - /* - Low threshold: between DAC steps 0 and 1/4 of full range: */
$ g B; z% x4 J2 l6 S, ` - /* 1/8 of full range (4095 Vdda=3.3V): 512 0.41V */
8 E. l/ E4 `/ n* P9 Q - /* Analog watchdog 1 configuration */
1 U: j( ~8 b; N2 o - AnalogWDGConfig.WatchdogNumber = ADC_ANALOGWATCHDOG_1;- W3 P8 `8 F3 B2 \
- AnalogWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_ALL_REG;8 c l: {9 b1 {, |6 U# t
- AnalogWDGConfig.Channel = ADCx_CHANNELa;
9 y# |( e9 |1 _$ c- i' K - AnalogWDGConfig.ITMode = ENABLE;0 E( b p1 R0 O/ ]; T) t
- AnalogWDGConfig.HighThreshold = (RANGE_12BITS * 5/8);
6 ]* y9 R3 h6 z+ v# b# r - AnalogWDGConfig.LowThreshold = (RANGE_12BITS * 1/8);
" Y- \) a/ {8 O# A. V* E' r - if (HAL_ADC_AnalogWDGConfig(&AdcHandle, &AnalogWDGConfig) != HAL_OK)/ r( p; ~) R- V) ~% i
- {
6 A' h u9 |, v - /* Channel Configuration Error */
. b( b! |/ W) `/ j/ R - Error_Handler();
. w1 \! R3 [# h; [ b7 e- } - }
+ I+ m' U( b3 H6 U( z - / V; ]1 D5 k1 G. t& ^+ f
- }
复制代码
. 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
- /**8 r L$ y; O" q% @/ P( S1 e
6 S% c7 z) K: }% d- * @brief Analog watchdog callback in non blocking mode.
# o q$ d. _/ k8 T& B, Y - * @param hadc: ADC handle( ]; v& ~* K1 d [/ Y
- * @retval None6 ]0 }; B/ g( c# r* I# c% y' \( a. c4 }
- */
0 W& N' b# R. ^. y7 H/ ^2 H - void HAL_ADC_LevelOutOfWindowCallback(ADC_HandleTypeDef* hadc)
: g/ Y. \1 ]* W3 c - {9 G1 E7 p! K. v
- /* Set variable to report analog watchdog out of window status to main */
8 A# ^ A! J6 K+ ?9 X - /* program. */3 z0 _3 y8 F% _3 _
- ubAnalogWatchdogStatus = SET;
# z; a& Q9 l# o2 J: h# A( F- @ - }
复制代码 ) J; g% b7 d, m% Q* Z
当模拟看门狗检测到电压高于上限或者低于下限时将会产生看门狗中断,中断服务程序触发后,可以做出一些应对措施。这里置了 ubAnalogWatchdogStatus 标志,然后由主程序去根据标志的值去执行相应处理程序。
6 W' h+ V1 ?9 r* M9 c3 d, s- /* Infinite loop */
5 e& Q k6 {; \( F- O - while (1)! @0 l5 N+ _4 t( W5 P- O% Z- Q% o6 {- e$ K
- {4 D/ {: ^( b6 G4 Y- o/ E8 m
- /* Turn-on/off LED1 in function of ADC conversion result */" O* V0 y5 P+ L" X m" O
- /* - Turn-off if voltage is into AWD window */
3 J/ q+ h0 A' l" ^ - /* - Turn-on if voltage is out of AWD window */* w3 ~! P! E9 D3 o
- /* Variable of analog watchdog status is set into analog watchdog */5 R5 s8 E3 p( G; U
- /* interrupt callback */
! }- W; e& v' K" N0 O+ D - if (ubAnalogWatchdogStatus == RESET)
6 C- b2 b7 O& M k/ Z2 K - {- ]( P* R/ f3 Z' Q
- BSP_LED_Off(LED1);
/ @5 M6 U. Z, T M3 p7 h - }3 P+ A1 o. N3 q0 @& |- ~4 a# s' ~
- else$ W$ ~/ J, x2 v; `/ ?" _4 i
- {
3 l$ ^( ]8 Q) t! o' \; O# \ - BSP_LED_On(LED1);
( [1 E5 H' v. B0 K
( }0 G9 Z% C" q5 @# X- /* Reset analog watchdog status for next loop iteration */
! [7 w5 E3 i) c% u: G A - ubAnalogWatchdogStatus = RESET;
' p+ L& ]1 h1 `/ \ - }5 q/ ?3 \5 U9 J3 u. v
- }
复制代码
- Q% ~7 ]* c5 E, w启动 ADC 转换代码
7 Q/ ?- z- f7 W0 u4 ~- #define ADCCONVERTEDVALUES_BUFFER_SIZE 256 /* Size of array
8 [& F- q$ v9 c! ?, L* v - aADCxConvertedValues[] */; L- z* q3 q& H& O0 h6 M8 n
- /* Variable containing ADC conversions results */
+ D3 d1 ?( s. W1 V3 A1 \1 v( g! w7 Y" g - __IO uint16_t aADCxConvertedValues[ADCCONVERTEDVALUES_BUFFER_SIZE];
/ N" J Y& E- t' c q9 A( c7 ]/ p - /* Start ADC conversion on regular group with transfer by DMA */. _! v( q! A6 _" u6 O8 Q: ?
- if (HAL_ADC_Start_DMA(&AdcHandle,/ Z8 P' \1 d x/ U
- (uint32_t *)aADCxConvertedValues,: [2 Y- v% q4 U8 a
- ADCCONVERTEDVALUES_BUFFER_SIZE8 f8 R4 ~& M; O. d; R% n+ C# z( V& {' X
- ) != HAL_OK)
3 ?& J& {; P4 E) v# { - { ~: g0 P& |4 [$ B) o/ }: g
- /* Start Error */9 D6 s$ z% @1 g9 k
- Error_Handler();/ C; h! y& f0 H, |8 j0 I, b" m
- }
复制代码 " 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
|