STM32F4(PVD) GitHub 仓库:http://github.com/XinLiGH/STM32F4xx_PVD_Example5 V' B, q; I! |( K; ~
PS:博文不再更新,后续更新会在 GitHub 仓库进行。
/ l, _6 r# K& [$ ^1 y
# P, L. m6 {% C) U, F8 e: W* Q- O7 m) }7 a 在实际的产品需求中,常常需要产品在断电时保存一些参数或做一些断电保护,这就需要 MCU 能够检测到断电的过程,STM32 片上有电源电压检测器(PVD)给工程师的开发提供了方便,可以比较简单的检测到产品断电的过程。
% Z9 y, u4 q0 d, G8 M4 E _7 f+ `9 j0 c
?: m, M0 y6 v4 g) B9 O1,开发环境
8 a- v% n6 q# j
/ e* z: L$ S% ?. X$ h) q6 F6 ^ 1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
6 \3 M! o. |' ?' A- T 2,编译器:ARMCC V5.06* }' Q x2 j& D2 E
3,IDE:Keil uVision55 M* Z" r! h# u. I
4,操作系统:Windows 10 专业版
# M8 g6 O2 F& G$ {* O
8 i6 D: D2 v V: x2,程序源码" b+ q+ Q( b8 y- }, \# h; }
PVD.h 文件" W% M- z' y5 `
- /**
% b! N: l4 B+ A& y# ~& Y' g - ******************************************************************************
% G# v! o3 Y2 u+ T+ A1 t, v - * @file PVD.h- S6 F1 X, ~' l2 Z2 w
- * @author XinLi- h% {" E0 }6 n. W0 d
- * @version v1.05 w2 B# H5 W, c7 _
- * @date 24-October-2017
6 T2 Q; V0 j, w4 i, `# B& ^ - * @brief Header file for PVD.c module.! `! R- O2 r. O. e9 w' b" j
- ******************************************************************************
, m& ]% h8 ], c - * @attention
% `. T' r& W7 o8 D0 k9 Z - *
) R: w) D( u' \8 Z4 U - * <h2><center>Copyright © 2017 XinLi</center></h2>! N! | a8 e8 M0 W; n. s( ?. u
- *
) ]+ d* C s% o1 f( Q3 ]- v - * This program is free software: you can redistribute it and/or modify% I |# o5 N0 E: q. J
- * it under the terms of the GNU General Public License as published by
) w! g1 B2 p) ^- S+ J- o - * the Free Software Foundation, either version 3 of the License, or& B8 x+ \5 _0 @
- * (at your option) any later version.* R8 F# m& ^) F/ {
- *
5 K! ^ R) f$ a' [ |+ q, z2 B - * This program is distributed in the hope that it will be useful,
6 u% B8 w b- h- V' m1 U- e - * but WITHOUT ANY WARRANTY; without even the implied warranty of
/ A/ _. I1 X3 S$ V - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
( F E9 s+ l% {7 v5 a - * GNU General Public License for more details." z! t- |5 h2 T% n. P5 \
- *
5 Y5 Z B2 U6 s% |! e, { - * You should have received a copy of the GNU General Public License
; ~) S5 I' c; q. k/ U: I2 k - * along with this program. If not, see <http://www.gnu.org/licenses/>., _ t5 S, _. |8 m5 c. K# E
- *$ O3 X$ P7 \5 u5 G
- ******************************************************************************
* Y- o. |: I. {. ? - */1 E) R, {- l( d6 W+ x5 n
- ' f s+ G1 O0 l" W
- #ifndef __PVD_H8 N# V6 ], ], U+ I; K
- #define __PVD_H! f0 _- t% b; S3 M" J3 d
- 3 R1 m0 l- z/ q7 T2 a9 w
- #ifdef __cplusplus- o% j& ^3 Y1 B+ \
- extern "C" {1 m; ^7 v: a/ S2 W0 B( ^: r& ]( c
- #endif
; H0 `% H/ l3 ?
) G8 D" K) _% R, ~5 t u4 m& ~$ K! p3 T- /* Header includes -----------------------------------------------------------*/
1 O' y% ~* k! c, c. Y - #include "stm32f4xx.h"
u: j( I% [0 A3 F% c) C0 ^
$ h# q7 `3 a8 k3 A/ g- /* Macro definitions ---------------------------------------------------------*/: J( W2 t2 v7 a0 t
- #define PVD_IRQ_PreemptionPriority (0)
: d- @* p2 T3 \ - #define PVD_IRQ_SubPriority (0)
$ ]: w" j. B. w/ J) n* Q2 k Q
( d( _4 s) H: p- /* Type definitions ----------------------------------------------------------*/4 X8 r: e$ k- z) C' D: V$ ~
- typedef enum: q- ]& [5 m% u) C2 @% ^/ x4 b& T2 d
- {
/ k, ?- S" u! ?- @4 w- k. I2 v4 T - PVD_Level_2V0 = PWR_PVDLevel_0,1 t- G6 j% _" H+ _' i3 l+ o
- PVD_Level_2V1 = PWR_PVDLevel_1,
1 _, R: p' c7 T8 r - PVD_Level_2V3 = PWR_PVDLevel_2,
, w& q o; J2 b) U( M' y* t! k6 T - PVD_Level_2V5 = PWR_PVDLevel_3,
" k% l& l! b: j3 _* c, O* M" x: Q - PVD_Level_2V6 = PWR_PVDLevel_4,
5 l6 Y% A! p5 ^( g - PVD_Level_2V7 = PWR_PVDLevel_5,2 H/ y: y0 j8 s+ h; a$ ^
- PVD_Level_2V8 = PWR_PVDLevel_6,
7 W: J3 Y# o! @% K - PVD_Level_2V9 = PWR_PVDLevel_7
, \9 x& t Y0 _4 f/ @0 h - }PVD_Level;
8 [4 x4 a1 l- E3 O: P& b! D2 [1 m7 E - ) p- m B) x/ e+ Z
- typedef enum$ j" B9 g- E% P
- {
% ^3 g3 X* A' U - PVD_Output_High = 0,
" M" V8 ]; M. }3 z3 a1 L - PVD_Output_Low = 1
3 X" j( D" z4 ?7 c - }PVD_Output;
9 y4 p" c, P3 k n' |9 v9 @7 F
" W' T( a# s: K0 m7 x1 y3 ?- typedef void (*PVD_Callback)(PVD_Output output);
/ b1 Y0 m( G4 p$ Q8 v- t6 T0 b' R+ O( R
& z7 D: x1 d" [ A; Q3 w* ]- /* Variable declarations -----------------------------------------------------*/
) R6 W7 x% V6 ] - /* Variable definitions ------------------------------------------------------*/
& h& z0 m$ w* H% P6 U - /* Function declarations -----------------------------------------------------*/
3 ~- ]% k8 ^; Y% }' K - void PVD_Init(PVD_Level level, PVD_Callback function);1 M7 @% ~( U' h2 G6 W
- void PVD_DeInit(void);
9 v) W, k; M4 L! K) a* ^$ Y. L - + w0 L) N/ H0 |% ?0 U) r
- void PVD_SetLevel(PVD_Level level);
/ M1 W3 W9 j# D8 u: A3 g - PVD_Level PVD_GetLevel(void);
% p. U* [0 x: U# v+ v& R5 A - 8 O$ t0 l' ?. Q4 @- q5 L
- void PVD_SetCallback(PVD_Callback function);
4 u8 B6 ^! A$ k: Q* \ - PVD_Callback PVD_GetCallback(void); \. z5 s+ W" c6 d
6 z6 N4 J: v1 ]3 h; Z- PVD_Output PVD_GetOutput(void);' D( q3 [( g* o# z4 m# s
4 I/ o& X9 _: y2 k1 _! h3 }0 }4 U- /* Function definitions ------------------------------------------------------*/- c+ d$ |* b( a2 H R2 D, |$ @
: o6 ^; K/ m- m) R$ W1 F- #ifdef __cplusplus
- T& K) ?% W" n p - } f/ w" f$ r( C+ l5 h! V+ p
- #endif- X- I {2 h- [- B# v' P7 l3 \
$ B1 g6 G0 D8 P) R p, {- #endif /* __PVD_H */
7 }# n8 B) {% W% j0 E k
复制代码
/ p; F1 G- {' g1 ~ PVD.c 文件
! s! j' t& B7 T& X6 Z( ~- /**
o# M; k5 ~3 {6 O* w - ******************************************************************************
7 C3 H# b- T$ F5 [" R - * @file PVD.c
1 [' m9 K3 b4 Q7 B0 ^ - * @author XinLi) v' i/ j' g# F; } e# \
- * @version v1.0' ?# }% v' s& ^" g; c0 k
- * @date 24-October-2017/ Y7 [1 W5 ]3 T7 H6 ^/ `% i
- * @brief Power voltage detector driver. r1 y& G/ } ~& ]8 ]' P
- ******************************************************************************% [ k8 k; o8 D1 Z1 u% ?, {( ?
- * @attention
) P8 G5 [; K$ ^, u# \& }. b; T4 x - *
7 s, K# |, I5 x- s. V - * <h2><center>Copyright © 2017 XinLi</center></h2>
5 a; B; i" h& l& c; Y8 v; T - *
! c1 T; [3 ~7 @( l$ t* z- Y - * This program is free software: you can redistribute it and/or modify
+ E7 s$ M. U" @! K, T - * it under the terms of the GNU General Public License as published by
) o5 b; f7 }/ H( `4 d$ g; E - * the Free Software Foundation, either version 3 of the License, or
" Y A4 @# L; h" \8 V5 M - * (at your option) any later version.4 x9 n9 Q* ]6 a8 ^/ z5 Q+ i8 ^
- *# v- V$ }8 K, r
- * This program is distributed in the hope that it will be useful,, L0 X: x7 r. c) b, K
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
0 L! v% q$ @2 h! s* ^. k - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! _" } G2 Y/ C4 [# v - * GNU General Public License for more details.% S& [0 S {. Y, ^# R5 r
- *6 L S1 ?' S P6 [( x/ k3 C
- * You should have received a copy of the GNU General Public License8 g9 x! R9 C9 R& X
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
% U* u) O3 k0 u- ^ k* m8 Z {, \, k - *: X ]. s4 y0 z* k% Y7 x2 W
- ******************************************************************************
2 B% n) X6 z4 C7 e - *// c, P1 a1 U9 ^6 I
- ' O, I4 V9 F3 C
- /* Header includes -----------------------------------------------------------*/- X2 u/ ~* w' B7 z7 g7 U
- #include "PVD.h"" c t; s+ |6 l. Z2 v* f
- #include <string.h>
7 F2 B3 M9 z' ^1 G3 Q! n - `& |5 B9 Q+ M- t
- /* Macro definitions ---------------------------------------------------------*/& }3 g) w* N c
- /* Type definitions ----------------------------------------------------------*/
% r0 m+ b6 D5 r5 c O - /* Variable declarations -----------------------------------------------------*// V' k& z8 n: H# x# N, M
- /* Variable definitions ------------------------------------------------------*/
7 |% i8 t* X2 [ - static __IO PVD_Callback callback = NULL;+ C6 s2 \' ~# B) q O* V
- 6 Z$ n; I, z4 ]0 r$ J% R8 _' a% w; L1 |5 s
- /* Function declarations -----------------------------------------------------*// Y+ Y; q& S# ]
- /* Function definitions ------------------------------------------------------*/
$ Z7 ?+ J" x7 y" S n4 C" G - / G4 n- o7 b& C2 z
- /**
3 }0 Z; X1 b, E3 y0 q - * @brief Power voltage detector initialize.
6 J0 i2 O' F& W$ k3 J/ ]' | ~ - * @param [in] level: Power voltage detector level.
7 @4 ~1 J* Z1 g' g2 D. y7 m# Y - * @param [in] function: Power voltage detector callback.* [7 T$ l( S2 _0 ]* S
- * @return None.
6 E7 ]/ j( T0 P' o; D - */7 t) C4 H4 ^# J: G# d! g9 Z
- void PVD_Init(PVD_Level level, PVD_Callback function); \0 o5 v4 d' j) z- l
- {
" R& a4 Q9 n. q' Y - EXTI_InitTypeDef EXTI_InitStructure = {0};/ ?1 `# T Q" w! `7 ]
- NVIC_InitTypeDef NVIC_InitStructure = {0};
, V0 U, J% j: ~" d9 D -
" Q% R, U; T5 m* f9 | - RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
3 m; y$ q. S' T, r5 S- Y$ c - $ K3 [7 ~, @9 n1 w3 e! `
- EXTI_InitStructure.EXTI_Line = EXTI_Line16;; n4 k' q+ j- f# Z/ S
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
* S/ O5 X, x, Z) S) } - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;, z3 X9 \- J; N( l7 a
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;% [- ]2 C Q8 R3 g; x4 g$ M; o a
- EXTI_Init(&EXTI_InitStructure);8 l# V; r) b& R* y( u
-
$ S! C2 m+ I0 y( t! K1 T8 S9 k - NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;1 R7 [) ^* D( I$ v" @. A2 H
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;
! ]% ^3 w) x2 O9 b: n* U - NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;' Y* ^1 k: K, R: G0 k4 t
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
* w- {. q i) ]# p5 K) a% T - NVIC_Init(&NVIC_InitStructure);8 S |+ g" l3 Z( o( |& z! Z
-
% W; U7 H6 y9 c* T, m0 g - PWR_PVDLevelConfig(level);0 X7 y6 D- E/ r% h! N: {
- PWR_PVDCmd(ENABLE);
' ~+ i8 ~' K# s9 N7 q -
- z I1 a* B0 v! I, z1 Y+ ? - callback = function;: ?9 ^; z. I4 E9 S
- }( i, v* S0 g0 H7 x
3 l6 S# M! H3 A/ |& v, Z- /**: i1 m( V3 u( }8 J
- * @brief Power voltage detector deinitializes.
7 y8 y: Z+ `2 h. y - * @param None.
: z% j! ^$ V$ I& }! H% S - * @return None.# J, ~/ X/ h* d' `9 |# B
- */; M6 S0 J; s' h' k
- void PVD_DeInit(void): [) r2 q2 j" c# U/ q$ c9 _7 S
- {0 D: B$ x6 L/ n+ _! P" O( l
- EXTI_InitTypeDef EXTI_InitStructure = {0};
# c+ z1 h( {2 V0 c i% B - NVIC_InitTypeDef NVIC_InitStructure = {0};0 h. z( {' A- V; R* v
-
- \( w9 B% \9 A9 F - EXTI_InitStructure.EXTI_Line = EXTI_Line16;( M/ u$ y% a3 {! w; ~8 ]
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;: q5 @* I" Y4 N! {( P
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
& \! ?8 V- ]: h2 B% B - EXTI_InitStructure.EXTI_LineCmd = DISABLE;+ _* A1 N- D1 p5 T
- EXTI_Init(&EXTI_InitStructure);
8 w1 p0 B! V! Q# v% a- W - % C7 b! \6 Z' j- W
- NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
0 U5 i8 e' k9 @ {; a- M - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;( K0 u( O+ v z' k- l
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;
5 q4 L5 B" d! P9 _" s$ j j - NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;3 t r' O9 J8 J
- NVIC_Init(&NVIC_InitStructure);% O9 ^* X$ ^( Q) _( l
-
7 w) [3 V% z( V! G - PWR_PVDCmd(DISABLE);
4 g& K& _8 x$ K4 d' g' X( c- d -
9 F% R) p% A! m4 A7 T - callback = NULL;% d _+ F$ a+ d% L/ g' J7 ?" x
- }
, x5 J2 [5 v$ [$ H$ m/ |6 e: L6 a - 7 t: k# k. h3 \4 ~5 c8 ?
- /**
' A4 q1 Y4 f( E( }9 U2 G - * @brief Set power voltage detector level.: {: P$ \3 w3 R( Q# m
- * @param [in] level: Power voltage detector level.
2 z9 c* d/ Y6 ], F5 j - * @return None.$ k3 g" v8 {! q: L8 h
- */# a) d; [0 c, z
- void PVD_SetLevel(PVD_Level level)
' e4 {3 e3 L2 A2 C( O - {$ f( w7 H4 L+ E) k- x
- PWR_PVDLevelConfig(level);. w; s7 w) d4 O; M2 P
- }+ D( v) f8 O' t; l9 ]
- / n/ [7 b* L7 q( T1 E
- /**) X) f$ w ^( X- c$ K1 Y u8 F
- * @brief Get power voltage detector level.$ u1 D0 v- w W3 X x" i
- * @param None.
4 I3 L9 u& y% N3 W - * @return Power voltage detector level.
& o) @ N' i t- h4 g# a9 F$ r - */+ T! ?% E0 _! C9 E! b( M
- PVD_Level PVD_GetLevel(void)( J0 D3 e; @3 a* v+ I
- {( F' [1 n" ^+ `; k
- uint32_t tmpreg = PWR->CR;. i! K" Y. F, d+ x
-
+ _* ?# N* s& L6 Y4 j _8 F5 y - return (PVD_Level)(tmpreg & 0xE0);
. Y5 P9 Q) G/ k5 o& b" @ - }
; y+ t9 s1 `- O' f B/ |
, O, w" T1 F& H* U, L- /**# A0 ]4 i) t* _6 l' j$ N
- * @brief Set power voltage detector callback.* }8 J& {" M- `8 v
- * @param [in] function: Power voltage detector callback.
8 b! E6 `4 t( C' @ - * @return None.# G3 } c1 T4 A' Q+ i% f
- */& \* r' a" b! p% M% t2 O7 T; e" M3 h
- void PVD_SetCallback(PVD_Callback function)
5 r! H) A1 L; ~ - {" k" ] t N' C9 i, G- @+ V5 D2 F% i
- callback = function;
& A! D' v. x$ N - }: | h( S; k6 e& p2 C$ g3 g
# m6 c; ~9 T1 f( n" m- /**
$ ?& @3 w5 v( J$ S3 y" D5 e6 r - * @brief Get power voltage detector callback.( ~2 m' z& K A& u
- * @param None., D0 N7 o5 O6 b3 n
- * @return Power voltage detector callback.
9 ^/ }2 u/ A0 T+ v - */
( n6 Z' v/ O6 A7 l1 @. \1 Q - PVD_Callback PVD_GetCallback(void)5 @0 i+ G( A# i8 @" D Y
- {, d$ W% C0 p6 w. ^0 x; Q. T- f! z
- return callback;: [ ~. r# O/ L7 \3 c `% ?. r4 P8 v
- }% H* k7 c' e/ J% g. @/ t, [5 f* M
- / u9 [. n2 N( X
- /**6 q3 O6 b& l. `; z5 U0 v0 m
- * @brief Get power voltage detector output.3 w4 m" o7 @3 B2 H% D X
- * @param None.% l* L4 d9 e+ [- x
- * @return Power voltage detector output.' f! I( i3 B8 ^8 i5 T
- */
. z3 m, a& y+ j: s$ { - PVD_Output PVD_GetOutput(void)
: a- f1 c( s; v: s# ~& _1 T - {* Q+ D* A* Y" Z# W2 \: G2 B# s* I
- uint32_t tmpreg = PWR->CSR;0 c% x" L. Y: F2 C2 v# b& c
-
5 K- V5 d* H2 d/ `: L1 i - return (PVD_Output)((tmpreg >> 2) & 0x01);
4 V# C; z9 ]/ Q0 d. N - }
7 H7 x% ]% m5 J% l' F: v
2 T& Y1 f! g, [/ R3 E7 O8 i- /**: E5 m4 X; d* A& w+ w
- * @brief This function handles the PVD Output interrupt request.$ Y5 P8 D) A& v5 v8 }# ~
- * @param None.
: c7 j4 ^% j! y3 _0 p" ~/ @ - * @return None.
+ M: ^( [7 E( P s - */" D/ `, v! `9 r6 e
- void PVD_IRQHandler(void)& J5 w/ y/ a' ~+ ]( f
- {1 l7 h% h* ]" h; s
- if(EXTI_GetITStatus(EXTI_Line16) != RESET)
4 O( h- s/ P; H3 q: U( _ - {
3 z/ K8 @" |$ I: [ - EXTI_ClearITPendingBit(EXTI_Line16);
; W+ S ]: d3 C7 {# } -
% }8 V8 b+ I) q8 }+ N# |: [; o3 z - if(callback != NULL)
9 E! g) @: [5 W/ h, [ - {
( U; G) Y& Y% l2 f - callback(PVD_GetOutput());
: @* Y" Z w3 z+ s) A - }
: R) E/ Y1 E4 n/ Q3 b) y: l0 i - }
) i6 b* h. @1 F - }
: e( E ~( ]% c1 O# }- T: _
复制代码 , c3 z, J. d7 ?
! G# B/ Q3 [$ l( v
main.c 文件
* L/ u: z. v( s6 P) M( B) t. K- /** m# j2 j1 t% S( R! R# f( ^6 z, {/ D9 c% p
- ******************************************************************************) o0 z( M2 a/ Y e8 z1 f9 F
- * @file main.c( s" H$ D% ^) W2 U4 Z
- * @author XinLi
' p" U( ^9 v- X p8 G - * @version v1.0
7 L8 S4 H: c. o - * @date 24-October-20170 x$ }' j6 @ \# G9 O& h
- * @brief Main program body.
: X- n3 N1 R, x. u9 l - ******************************************************************************
$ C2 U6 m' k0 h- N+ y9 D7 A - * @attention0 y& ~) y+ b8 u0 O
- *; U8 o' [" a6 n1 z& w7 _& [
- * <h2><center>Copyright © 2017 XinLi</center></h2># G, s2 t( G- T6 Y, W# X- ~
- *
6 m- W- }8 K: a1 \2 l) [$ I( A - * This program is free software: you can redistribute it and/or modify0 T) X) W$ Q- E+ N* A% y* b
- * it under the terms of the GNU General Public License as published by
6 V/ x v% j; G! H8 Q9 v8 \ - * the Free Software Foundation, either version 3 of the License, or
) `/ f; M1 a, _$ t* A - * (at your option) any later version.
# A0 O. c; b/ e8 y$ w6 D6 J! @+ ^ - *
, l4 ~ T \* Z8 t" Z) y7 f - * This program is distributed in the hope that it will be useful,
6 P4 K5 M6 R. i5 ] - * but WITHOUT ANY WARRANTY; without even the implied warranty of5 s0 n5 [: Z V7 L3 w3 \
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3 E/ M x% o% s2 x1 y* t# E2 _ - * GNU General Public License for more details.
0 y, w0 z9 q' s! |/ t - *
# b/ Z/ [) C9 c- V l" ]- o - * You should have received a copy of the GNU General Public License& U, f8 r# n- l/ a8 e' y% R
- * along with this program. If not, see <http://www.gnu.org/licenses/>.3 v H( y9 X/ [. `. W( l2 B# O' h
- *" }! i0 w1 R# \/ U C8 P: F7 G
- ******************************************************************************
+ I: T. Y" X I1 H2 ?8 _ - */
- q/ U4 Z. ^5 k - : O3 _8 d+ L5 V, D
- /* Header includes -----------------------------------------------------------*/9 {- T+ ^7 l# m! u: u# \) n4 T
- #include "main.h"
8 g3 L; @. C( v- D! R; g - #include "PVD.h"
$ i, Q9 y0 Z: a/ f5 c1 U+ C& X - #include "LED.h"" [ l* m/ g9 I1 M& ^
* l+ m$ T9 \; V6 B% n/ C( A- /* Macro definitions ---------------------------------------------------------*/# }% y6 w$ k5 q) r8 U% t3 m
- /* Type definitions ----------------------------------------------------------*/4 w X( V% M; l. u; P0 W! f. |
- /* Variable declarations -----------------------------------------------------*/5 K$ A/ b! `9 e: v) z \
- /* Variable definitions ------------------------------------------------------*/1 a/ q: S0 [6 p
- /* Function declarations -----------------------------------------------------*/
1 k D+ i8 Z/ Q - static void PowerDownProtect(PVD_Output output);4 n+ G/ b* O/ m @
% P6 b" {7 y, N- /* Function definitions ------------------------------------------------------*/
( l, A2 D/ p$ g5 v5 R - 4 m/ J; m j9 F5 P) `' l$ R
- /**3 K) ~' [. g' f& x3 F
- * @brief Main program.
& n K c$ s, Y+ y6 z" F - * @param None." M2 J0 d- `/ y; Q6 [) r
- * @return None.
9 O$ l/ M1 d$ o- c9 e - */
' a' _) U. _+ o( w1 u+ ] - int main(void)2 B0 G8 C& \) T8 u& a6 c
- {" S W* H6 R2 ?0 S) W8 ^5 h' U! \
- LED_SetStatus(LED_Pin1, LED_On);- \5 E9 A& v& M# m1 c
- LED_SetStatus(LED_Pin2, LED_Off);* I7 |% R* A t T. {
- 7 }- k1 r0 b, P/ W) X8 D
- PVD_Init(PVD_Level_2V5, PowerDownProtect);
; e. g6 N/ t* {5 K0 F -
9 O. b& Y. [6 W2 S+ |2 v - for(;;)9 t9 C6 ^3 M. A) q& ~1 D1 Q
- {+ j9 }0 V9 q- q1 p0 u: B% w
- * I' ]' U7 ?5 d4 }; q, W# U
- }
( g; a/ }. N! s - } ~7 C/ r1 r/ z; F. ~' z( [
( n$ q! G4 @0 T' _- C- /**
9 U! M' j( M+ g1 B1 o* \" i3 [: t - * @brief Power voltage detector callback.
2 `& z. J; U) G* w& ?4 S- } - * @param [in] output: Power voltage detector output.
F. F- G7 Q: q( D - * @return None.
% n. _3 }- r+ U0 G/ L - */
3 W. k) S. \: p* R' o8 { B - static void PowerDownProtect(PVD_Output output)
8 Z* v+ {: J$ h7 X; ^ - {* M6 H& r3 A2 j5 L' T
- if(output == PVD_Output_High)" U$ z* \+ \+ @* y7 l& Y
- {
. k, A" p; g3 z( C$ x( k& F! Z. q3 z - LED_SetStatus(LED_Pin1, LED_On);
, U3 R4 x7 J- A& L0 m - LED_SetStatus(LED_Pin2, LED_Off);
9 N6 u7 ~4 I3 L" ~5 [+ h# x. F - }7 P5 b% N+ r _. B& d" a
- else
# e2 Q; h- i; m- p' e - {. R" t9 N4 n- u9 M" M6 ]& _
- LED_SetStatus(LED_Pin1, LED_Off);/ p( {" J3 i9 D2 i
- LED_SetStatus(LED_Pin2, LED_On);5 g- Y4 D. Q! Q/ o/ P
- }$ D& n5 h# M5 G: y O
- }. y' d$ b, u- e6 @0 x7 B
复制代码 ' Q7 B7 @+ ~( k& {% J6 n9 y
; e# C6 b2 q+ K1 E* P0 |# g
% j+ U# v9 D5 j7 s4 [6 U8 _2 l1 j0 F# g; U, N7 `
: w# G8 Z1 h+ ?! \0 l- }
|