STM32F4(PVD) GitHub 仓库:http://github.com/XinLiGH/STM32F4xx_PVD_Example0 y+ ]# K+ W+ c/ A
PS:博文不再更新,后续更新会在 GitHub 仓库进行。2 m# g# |! i+ b+ q( X% `
& T* [. D; }. C7 C
在实际的产品需求中,常常需要产品在断电时保存一些参数或做一些断电保护,这就需要 MCU 能够检测到断电的过程,STM32 片上有电源电压检测器(PVD)给工程师的开发提供了方便,可以比较简单的检测到产品断电的过程。6 T6 w! Y' o+ R/ f6 b. c
9 q# |. V2 ^2 e2 b4 |8 U b( a( m7 M
1,开发环境: l& ~$ A5 D, A! e9 e8 \8 w4 L
" Y1 Q! N! r. r% u
1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
( ]8 [# w* b# Z; P0 ~. v, t 2,编译器:ARMCC V5.06/ a- Y) r: S$ W: ]
3,IDE:Keil uVision5
+ D4 {: ^& ~* Q: ^ 4,操作系统:Windows 10 专业版
* m) Y# ~% k* w" _# B9 N# T
! q3 G; t# H# u# `- b$ J! l! T2,程序源码
/ f1 ? W( K3 h* v+ ^) M PVD.h 文件* g: _. k5 o" C% ~$ q3 y8 Y
- /**- _7 F8 p% |9 g. D% q d
- ******************************************************************************3 a3 \. [9 |) A0 s2 s. u
- * @file PVD.h& R0 T" B3 O3 t8 Q
- * @author XinLi
0 B6 q. L$ x& L! g/ Z9 L& |& x - * @version v1.05 R& F9 r6 S) C
- * @date 24-October-20179 \$ A2 Y2 h8 b" K
- * @brief Header file for PVD.c module.! ^- l* N4 y! u) Q m6 G1 I
- ******************************************************************************
) c$ {" T j5 Z& t0 h c% T* T - * @attention
5 T9 I8 `) u& w - *7 S8 Y$ a1 @8 R/ D$ u4 z
- * <h2><center>Copyright © 2017 XinLi</center></h2>9 e& w7 Z4 q/ ]( C W" q" I
- *
- i ^; `0 E. q" P7 o5 J# v - * This program is free software: you can redistribute it and/or modify8 o" l+ ~, l q! A" X5 q% P! E2 H! n
- * it under the terms of the GNU General Public License as published by9 C* F0 e5 d% `4 u2 j! c
- * the Free Software Foundation, either version 3 of the License, or" |* s2 p p( ]! A: c
- * (at your option) any later version., u8 q& O" m1 d( M, n5 U$ O. E% [6 T
- *9 J* p. k8 C; v Q
- * This program is distributed in the hope that it will be useful,8 j' i* C7 ^, `: I8 b2 E$ E3 W/ j
- * but WITHOUT ANY WARRANTY; without even the implied warranty of7 o& p$ V! @0 ]6 L
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3 e; a5 T1 p) n) B - * GNU General Public License for more details.
) l" O4 {4 f# o5 U; E - *
! T; U- Z3 L" A% |5 } - * You should have received a copy of the GNU General Public License" O) C3 |5 Y7 ]" g6 i& w
- * along with this program. If not, see <http://www.gnu.org/licenses/>.& L" y7 }: g5 c# U( H
- *
. V( i; ^$ g# a - ******************************************************************************& F9 C4 G5 |0 n" | d
- */
' C% @+ s/ `3 Q$ C" D' a, Z, B
1 X0 R0 l9 v+ `# u: z( [2 K- #ifndef __PVD_H& c' y% a; h' |
- #define __PVD_H
% X( J, T& Q6 _" i0 m! P- \
! E. i, |% B. c+ H$ N/ \* ?- #ifdef __cplusplus
1 z+ \. @1 l' j! Z) J; n - extern "C" {( O$ e6 w4 @1 u. {$ X) {5 A" Y
- #endif7 ~( k- O+ u: y. m& `! h. X+ A
- 1 M: W; R: M& G% I( ~$ q6 E/ D& V& O
- /* Header includes -----------------------------------------------------------*/9 }2 ^$ z; p5 l. C5 c
- #include "stm32f4xx.h"; v' _2 h v8 }6 }
3 Z- C! J, y' w" e3 \/ \- /* Macro definitions ---------------------------------------------------------*/
+ w( ?; z5 V" v: N0 I5 b2 u - #define PVD_IRQ_PreemptionPriority (0)
! h) k2 s& I9 g' u6 H1 Q - #define PVD_IRQ_SubPriority (0)- b4 F+ u" C R6 Q T* V7 \4 x+ x
. C" Q S5 E {# }* l& L3 l- /* Type definitions ----------------------------------------------------------*/
( n7 T, h5 |' @6 u$ J) Q) H - typedef enum& b0 i, Q! W! F7 o: k
- {
8 m, T* ]. }0 P |* |/ ~. a - PVD_Level_2V0 = PWR_PVDLevel_0,
3 d9 a; s! X8 X ^, l - PVD_Level_2V1 = PWR_PVDLevel_1," X% t% @, @1 `6 ~
- PVD_Level_2V3 = PWR_PVDLevel_2,! ~+ e: q1 |0 n$ S
- PVD_Level_2V5 = PWR_PVDLevel_3,4 S" K, H% F% F6 B) y6 q
- PVD_Level_2V6 = PWR_PVDLevel_4,
/ j0 {, |' }- k y3 X( v& g2 o - PVD_Level_2V7 = PWR_PVDLevel_5,6 m* {: {* f) a- L
- PVD_Level_2V8 = PWR_PVDLevel_6,& d9 S/ I$ ~8 P3 @, J! X6 _8 ~7 `
- PVD_Level_2V9 = PWR_PVDLevel_7
- z. w2 h! d9 M- p! U - }PVD_Level;! O a1 p' W" e7 ]; ?4 a( b
- # g! Y H8 I6 C- S
- typedef enum/ r( _, m# E# k
- {+ t/ V7 i7 O4 S, ?0 a
- PVD_Output_High = 0,
/ G8 f1 h: m ]0 B: M - PVD_Output_Low = 1& W8 K( x% R/ W8 Y5 [ q+ l
- }PVD_Output;
; z1 p7 | _4 J9 R - |" T3 v1 w% z$ \) C5 Z, Z
- typedef void (*PVD_Callback)(PVD_Output output); ^. h8 [; c* O# @; H
- % p3 G0 W- W. h. p1 o; Y+ A
- /* Variable declarations -----------------------------------------------------*/
" p5 |, t1 b2 q0 I" [6 B - /* Variable definitions ------------------------------------------------------*/
! ~* n( E! A* z* i4 {& V - /* Function declarations -----------------------------------------------------*/
2 e* A+ [) N4 z* U1 o - void PVD_Init(PVD_Level level, PVD_Callback function);
; B6 c. |* [) S: s- }+ a - void PVD_DeInit(void); }! s+ s4 j- s7 X V2 `
- . g5 H% T6 @ G4 | V1 J7 g
- void PVD_SetLevel(PVD_Level level);
( v! h( e6 \1 @' t- ?. Y: [ - PVD_Level PVD_GetLevel(void);
# V$ m( U& D" ~$ D- F. r. k# U! E
0 |# W# H. r+ i+ i D+ o2 B- void PVD_SetCallback(PVD_Callback function);
$ u4 h4 {1 D7 {# _# ` - PVD_Callback PVD_GetCallback(void);
; ]$ V+ b. Z- a4 \
r h% Q. t- h. k( L$ S3 q- p# R- PVD_Output PVD_GetOutput(void);
3 ~$ G# R9 s2 k- n
: ], H% Q6 n, B7 C6 V5 a- /* Function definitions ------------------------------------------------------*/
2 r, `1 I2 {0 O( u! T
. S* B6 }% [ p, P, F( m! y- #ifdef __cplusplus
* P1 @1 \' M) t2 ?. a* a& e5 p - }3 J: S* C7 t: H/ ~0 x% g# k2 L
- #endif
( r% O1 R6 c3 t" G/ v4 @; y9 |
- x7 N/ P9 X/ H- #endif /* __PVD_H */
, R/ O1 [+ G& e* V+ T3 ]
复制代码
. }5 c0 j+ }1 ?/ Z7 }0 e8 H PVD.c 文件" u7 s5 v6 k% u3 w
- /**
( O% i* s% `9 a7 V - ******************************************************************************& B- O: i! g3 d
- * @file PVD.c6 @2 [* T, w! q9 s" ?2 \
- * @author XinLi% T5 L- n, n- |/ K# U) x
- * @version v1.0
! n+ I: @0 F5 @' f) y0 `4 [5 ? - * @date 24-October-2017
' r# _: l4 B7 M+ t - * @brief Power voltage detector driver.; |6 O0 O7 c* M8 i' a$ o! V) v
- ******************************************************************************
" @: a& q: l) m% G! W! D/ _ u1 O - * @attention* x- d. T) w5 ^3 L; f1 i
- *
$ V- ^% A' | ^$ d( O# @8 ? - * <h2><center>Copyright © 2017 XinLi</center></h2>$ M8 K0 j/ o; w8 l
- *6 S. j s4 E4 H4 G
- * This program is free software: you can redistribute it and/or modify! s1 I* p% V d: |
- * it under the terms of the GNU General Public License as published by
2 l0 n6 {1 x! A& M - * the Free Software Foundation, either version 3 of the License, or
2 W# w/ H8 t* Q: y9 n- o% n9 z" K - * (at your option) any later version.0 v+ T9 i( M6 }+ e/ }) r7 u; U$ W
- *7 U! ?, H! x4 F7 f
- * This program is distributed in the hope that it will be useful,
9 h; l( P0 Z u9 O6 s- c - * but WITHOUT ANY WARRANTY; without even the implied warranty of. i, K' v# d4 j8 B' ^" F2 Z1 K* O: W
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7 y+ C8 y4 K1 j5 s2 ~. V+ @7 \) ` - * GNU General Public License for more details.
0 n1 X: `7 i2 w# r+ u" H2 | - *
: [( m" }7 g0 O- ^, v - * You should have received a copy of the GNU General Public License
1 J" ^% x# u, r# i, ` - * along with this program. If not, see <http://www.gnu.org/licenses/>.$ _) X$ e0 Y8 m# x) [2 H& n
- *1 o4 N0 t. \& F: \
- ******************************************************************************
6 s, E8 h# J2 {3 R+ |# ]) @8 e - */
/ |! O, Z6 ^1 B( f
0 }7 y. @6 ?' l- /* Header includes -----------------------------------------------------------*/$ ` P( [1 I; T/ C
- #include "PVD.h"# v3 ^* s3 F7 L3 w d7 Y
- #include <string.h>6 ^/ t: [4 k6 U2 e& A/ S* K; c
0 E4 z7 J" n' m9 B( l- /* Macro definitions ---------------------------------------------------------*/
& b( l4 s1 ?: }6 ^: J" Y8 k8 J - /* Type definitions ----------------------------------------------------------*/
7 }3 v4 |* }6 |8 Q - /* Variable declarations -----------------------------------------------------*/& u+ T' x& l& P' v
- /* Variable definitions ------------------------------------------------------*/
/ _5 u3 g" F& F - static __IO PVD_Callback callback = NULL;( o- P4 f5 P. \' N( J1 [
- a2 z8 g, `, K @( T- /* Function declarations -----------------------------------------------------*/
" V. `, I. K: {, _$ { G% {% i - /* Function definitions ------------------------------------------------------*/
0 i* E# h( C) V( U$ h' b, |9 K
$ O f- N @1 i. v- /**7 G# u1 O) s5 G" F. Q
- * @brief Power voltage detector initialize.& Y7 s( }6 `! X. |0 D# e
- * @param [in] level: Power voltage detector level.9 I) Q+ Q: m4 V
- * @param [in] function: Power voltage detector callback.
; _" m8 Z* v# n4 P" j7 ~! T - * @return None./ d7 a3 ~+ i5 X; ~7 I; J
- */
2 t: |1 Q- x& d6 R" u$ C2 u - void PVD_Init(PVD_Level level, PVD_Callback function) @1 Y: @9 O# U* C4 n! i4 e
- {! `8 W' D4 C. |( S1 u
- EXTI_InitTypeDef EXTI_InitStructure = {0};
+ \5 M. u! C3 h c3 S" K! y, D - NVIC_InitTypeDef NVIC_InitStructure = {0};
0 @$ H7 _1 \3 a" F -
, ~9 ~+ L9 \1 G: V - RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
) N& I+ h, m- ]' j( [6 [" \ -
( y- K' u a& d ?& X/ @9 l: I* | - EXTI_InitStructure.EXTI_Line = EXTI_Line16;
! \; @* p/ A: C5 \% F3 G) ] - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;2 L* b4 W# t) D% X0 t
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;2 ^1 j5 e# @6 [# I% K' {
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
9 i4 [3 {7 H, Q9 Q U- T* Z - EXTI_Init(&EXTI_InitStructure);
# U, Q& g7 i8 B( b - 8 ?- l8 M& m+ T
- NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;% ?7 ~6 ? G3 t' P
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;
. N* |/ B+ ~4 ^: u+ X/ k" a& t2 m - NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;
( Z6 l# \% a5 u" D! F- u3 b, U% m - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;( x) g2 }5 P2 g6 c: u; n
- NVIC_Init(&NVIC_InitStructure);6 S4 c8 Q5 t b* p" R+ T: u
- ! Y% w8 r6 c9 f7 u4 N+ W0 I
- PWR_PVDLevelConfig(level);
2 Q* ?; u4 S1 X: Y4 @4 } - PWR_PVDCmd(ENABLE);) N2 G# s/ `/ y( W7 A4 _3 C
-
( ]+ _0 b7 S) i( R9 P - callback = function;
( T5 w0 d* T# q) Z - }
1 s, ^: a' d" j0 D5 Q# \& F' t
! p7 p r8 |1 J" ~( G* b" `" A- /**9 ?9 F1 X& f$ {% j$ H
- * @brief Power voltage detector deinitializes.
9 ~! [; _' ~; {- | - * @param None.
, }" L9 E0 E; D5 J3 N - * @return None.
S: ?/ Z5 f# W1 a" B - */
$ S. ?) X; U. Q& ` - void PVD_DeInit(void); W0 H& e/ W! ~4 o8 L6 _& w5 W: l6 M1 \
- {
* E$ Q+ c$ D" [ c! F - EXTI_InitTypeDef EXTI_InitStructure = {0};
; k) ^# s4 t4 r3 S8 k8 ` - NVIC_InitTypeDef NVIC_InitStructure = {0};* W3 e! o6 O" j% l
-
/ K% F2 |7 n7 n# o" p1 J - EXTI_InitStructure.EXTI_Line = EXTI_Line16;1 v# K, o( N0 L% S
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
z! g( P" }% M; Z' } S0 l. \ - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
$ Z; {+ H+ t& u - EXTI_InitStructure.EXTI_LineCmd = DISABLE;7 c! G/ R, O5 }; \2 t& z, ^
- EXTI_Init(&EXTI_InitStructure);- E! v0 A9 n" Y
-
8 E4 Y; N/ e: H+ ? - NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;+ k9 x; ? a0 R5 e" @( C( L
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;% w6 g* x% B) k
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;
7 X& z0 |+ N2 w. w; K( g2 y - NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
; `$ N3 k2 t2 U* L4 M3 K. Y - NVIC_Init(&NVIC_InitStructure); y! o( _6 ]8 j% [
-
4 W! t: S% P0 c2 _ - PWR_PVDCmd(DISABLE);
8 T! F) e! S7 `* k+ [4 C6 t8 t -
$ X2 _. J" X, x8 E7 H" X0 d& X - callback = NULL;
$ u# g1 _0 W% L8 P - }$ @& y+ `: s S1 m* C. o
7 F. V$ {7 ?2 Y8 ?+ T- /**, Z" N. @! o: H1 K
- * @brief Set power voltage detector level.
% n% w3 [2 u0 u1 \# ? - * @param [in] level: Power voltage detector level.4 a' `$ N! _0 b* E- X- X: ^
- * @return None.0 q- _! ~6 j; K% P6 [+ x
- */
) c* h# \# Q& I- n1 k8 t. ?3 X; b* j - void PVD_SetLevel(PVD_Level level)
6 O) d7 U! x3 W- T, ]# ^7 c+ d - {
2 z4 p+ y# ? b( b4 [ - PWR_PVDLevelConfig(level);. ^1 B, d6 @; f t- A3 f
- }
4 i5 r* s M/ y( m$ P - 8 W% `9 ]4 g$ n0 x8 V% w) o
- /**) ~2 E) {7 ?# [9 k; e$ f. H! F& y( U
- * @brief Get power voltage detector level.5 t. Q0 s) {# m1 _; W
- * @param None.' g+ V; L9 \# q0 c! } g' m' ?
- * @return Power voltage detector level.- Y/ D9 t# v# q# K. _$ {7 \
- */! i' r1 H# J" K9 {! x
- PVD_Level PVD_GetLevel(void)
: {) s3 z/ ^& o! V3 Z2 m - {
$ w/ Y) Q8 h! q8 t# m/ t - uint32_t tmpreg = PWR->CR;
2 C4 \5 O/ t/ C) f+ Q4 ?! o9 E - " R8 k& _0 _, |0 G( M: B
- return (PVD_Level)(tmpreg & 0xE0);/ C& U- w" n8 ?+ i# e
- }% [, h: e" Y. h
9 @$ U. }! x3 Z# u7 B* c4 R- /**9 _0 e) ~/ g9 \* o
- * @brief Set power voltage detector callback.7 v% x/ h; O2 y
- * @param [in] function: Power voltage detector callback.8 D6 N" c# ]( I
- * @return None.6 i) m: f% Z$ M! o8 S/ r
- */
0 O- j c2 q# `- H; M }8 O" z9 r4 w - void PVD_SetCallback(PVD_Callback function)
t7 B( G* X& r: ^5 [1 s - {7 R- \7 G9 f8 d* g2 G m1 n
- callback = function;
" i7 @$ w1 J& N( f, a - }5 I7 p- u4 D3 D5 C3 {7 b% }- a
0 c3 G/ ^$ Q4 N* c8 X l ^* `- /**
' K l" `* Z8 S- R- Y& i - * @brief Get power voltage detector callback.
! m8 B7 f' y, Z6 R& F - * @param None.8 r+ t5 g$ e4 n. T* y4 e
- * @return Power voltage detector callback., W/ J" z# P v- y! C# I5 t; i
- */
. K; ^ [* g: r) Q% Q( p - PVD_Callback PVD_GetCallback(void)8 l0 {1 @/ E4 N/ V' @
- {
% v Z. G6 R5 z: \: X1 p - return callback;1 _# p+ s# C$ C/ W+ i
- }6 B0 C" M% b0 [# `; C$ `1 R$ x% q
+ R! s8 b' G% _- C+ D- /**
0 H3 g5 j) I; b4 z" H5 j; ~% e - * @brief Get power voltage detector output.5 Q: ^! B6 ?( F/ ?, W* F
- * @param None.
8 [9 c# w3 A5 x8 m- p# j - * @return Power voltage detector output.
: T! q! W$ Y# p9 @4 x - */
) ?" ^5 a! H: d& m+ H, s1 u3 O' J% t - PVD_Output PVD_GetOutput(void)
: t& n& E" N% P8 m# Q8 k$ @) a - {6 H) g" D9 Q/ o
- uint32_t tmpreg = PWR->CSR;
/ c/ A, b" N5 H; u3 F5 {( N - 6 ~ {" |! w. L6 g, y3 x
- return (PVD_Output)((tmpreg >> 2) & 0x01);
9 R, Y! P7 e' M - }, f5 w+ m: ~ u( r3 N B
- . \4 k) X% d; K1 j
- /**
: b! h! F. n+ \& N& t f - * @brief This function handles the PVD Output interrupt request.
" e) v0 j- m1 z' Y* n - * @param None.
" K7 E: ^) k, [ l - * @return None.3 E0 d* B1 R1 r
- */
- l( \6 [8 }: w) K7 W- ? - void PVD_IRQHandler(void)
4 n0 W: {* M, n! J1 ~ - {
( a3 a/ o, F" f- _! ?+ x% H - if(EXTI_GetITStatus(EXTI_Line16) != RESET)) T1 a% h. e$ K% k
- {1 ?9 ~9 H* d6 g' }' J& E) E+ M
- EXTI_ClearITPendingBit(EXTI_Line16);
- k6 \) @+ d4 O* N5 \( A - ! n/ L$ L5 l0 K* x4 X
- if(callback != NULL)( ]% R1 t0 R! e. h
- {7 F4 P4 h3 b% x2 f! E
- callback(PVD_GetOutput());
" Y1 h2 g4 ?+ `: Q - } s/ ]7 H' q7 Q0 q
- }& s4 j C) t- @" Q
- }( s' G; J4 Q, W" T
复制代码
1 \# D* Y- p/ N# h$ v( q# n( f# H5 s( V, z" } x7 s
main.c 文件3 Z/ o: z8 d. E8 C6 `
- /**
) |# a7 Z4 B% i0 a) i3 {; ]. E - ******************************************************************************* Y* v- a6 s6 U0 X
- * @file main.c2 ]" X2 s9 h2 S2 l7 k/ m9 A6 w
- * @author XinLi1 z; G( r, i. w! u
- * @version v1.0
2 Y9 e) f4 X* U T. {$ @ - * @date 24-October-20170 Z3 l3 p( S' B; ^
- * @brief Main program body.
1 M( k9 m% l/ M6 e6 G! N - ******************************************************************************( A5 F& P' x, H. A* c
- * @attention7 @8 x8 E1 c0 \2 k
- *
% s ]! `- @7 r3 R6 z - * <h2><center>Copyright © 2017 XinLi</center></h2>
$ {- W; t+ ^$ s9 g' l - *
4 Y2 h c& M \) H/ U2 l - * This program is free software: you can redistribute it and/or modify
" U( l4 r5 p0 m2 z" g; z - * it under the terms of the GNU General Public License as published by' k& }, e! `' W; a0 `% Z o" F) }
- * the Free Software Foundation, either version 3 of the License, or* B1 o% U- L- s2 _
- * (at your option) any later version.7 `- z# ` P4 Y/ U- P) {
- *
) {+ \& M, V! ~5 \, O" T; v. K - * This program is distributed in the hope that it will be useful,
$ Z- y( y1 ~/ o - * but WITHOUT ANY WARRANTY; without even the implied warranty of9 R; N; w. b$ k6 |9 f$ }# X8 [
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the7 s6 C3 h4 R6 U3 c5 s9 N
- * GNU General Public License for more details.8 _# z7 i, {( B# Q }% h. [* l; K$ I
- *
! i- g3 V# n) R/ ^5 \$ ^+ L - * You should have received a copy of the GNU General Public License" T5 Y6 r4 A2 V/ G0 o ]( a% x6 |. o, b
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
; B: h" `6 g& u q' Y! g: _ - *+ m7 ~6 d' u. x' v& o6 Z3 v
- ******************************************************************************* I! @, E# B& r+ ~/ J4 X
- */: v" _+ x) ^. w4 d$ l B7 [
- 0 T2 G3 M$ j7 r- \& t# r
- /* Header includes -----------------------------------------------------------*/2 m- U6 L; T; T' j" d$ o( @9 O
- #include "main.h"
5 q0 }4 ^; p' ?/ b: P3 a* L1 x - #include "PVD.h"
0 h! k: P4 j; E2 Z+ E. O - #include "LED.h"& c+ @! e# L2 i5 G5 {& l, w
% j5 [% }* }$ Q/ V. e- /* Macro definitions ---------------------------------------------------------*/
% Y2 e# V" \- S3 V: {! h - /* Type definitions ----------------------------------------------------------*/
+ K4 P& V+ u: c' C/ j4 T - /* Variable declarations -----------------------------------------------------*/
, ~- N$ Y0 Q9 c% R0 p7 f% w - /* Variable definitions ------------------------------------------------------*/
6 Z' i5 q4 u! L, t - /* Function declarations -----------------------------------------------------*/1 {- v- `2 d- T" D
- static void PowerDownProtect(PVD_Output output);
7 a. C( }5 F" \: i2 G! e - # R' a, _! e d+ E7 a% F/ s
- /* Function definitions ------------------------------------------------------*/
1 |6 v0 N2 C X; {. T9 Q6 ^% ^7 u8 K - 9 [; U9 H+ E2 e ?/ n
- /**
" |3 L1 r" t* _8 ] - * @brief Main program.8 Q- e4 S! Z9 _' c
- * @param None.
5 K7 z" K0 z' t- _. t+ t - * @return None.6 |& I# ^& r$ N. V7 b
- */3 m* k* w+ e6 `* z$ I/ I
- int main(void)
9 ^( z1 o, A5 E - {
& i( o" B/ k6 A6 B0 z4 ?* F - LED_SetStatus(LED_Pin1, LED_On);
! k9 ~" P1 c9 m$ B( \5 V - LED_SetStatus(LED_Pin2, LED_Off);
3 t! q% P7 F- m1 h* ~ - + U3 F' F7 [# ~& m
- PVD_Init(PVD_Level_2V5, PowerDownProtect);! a" v* V8 E, B+ U# p
- ; C$ I+ c& {6 }3 N1 `
- for(;;)
, j3 o1 x3 E7 j, d# S% \ - {
0 E& Z" j5 _) C: j/ O -
) w v. ^: m+ Q. _& ` - }
" s# T4 b3 E7 @! R0 j - }) d# Q- D) R; [+ N1 h* K2 I- s
- 4 F; q( i+ E) ^+ O. Z4 F
- /**
& p9 p" \+ O# |6 `1 ]% c8 N' L; \ - * @brief Power voltage detector callback.
w4 @0 O1 ^5 i' U - * @param [in] output: Power voltage detector output.
& \& f+ ]; g1 {/ |* ~8 Q% v9 U - * @return None.
3 {' A# s# D( X7 c+ V4 Q - */
+ P4 O \. V2 c0 v2 z% h" ?6 S4 _ - static void PowerDownProtect(PVD_Output output)
% g- m! Z N2 [+ t1 L/ s7 W - {
& N9 Z; M- {% P1 u% ~$ P3 l$ b - if(output == PVD_Output_High)2 A5 O* E3 S$ s3 `
- {6 v6 |$ i0 Q5 q0 x a
- LED_SetStatus(LED_Pin1, LED_On);
$ k8 _ Z f5 E - LED_SetStatus(LED_Pin2, LED_Off);6 t; R' E) y p6 f
- }2 |* g+ f/ f2 Z1 e3 r7 k' h
- else% w# a, t3 ]3 @- b+ k) E* O/ K' D
- {
; G& h H+ m3 \! k0 }3 [ - LED_SetStatus(LED_Pin1, LED_Off);
7 q L" V$ j/ E7 p% G& V - LED_SetStatus(LED_Pin2, LED_On);
) V& `) P0 Y1 {6 N* r3 s6 q! o8 D - }9 \7 c- e. f# x. u
- }
- S ?' Z& R6 G0 e9 t4 W5 H
复制代码
* q% \! e+ n* z0 L6 A- F% Y9 Z) [; d# m
[( J7 @# _5 n( q7 K1 P: d+ ^9 O
M& T# v; P8 q# W2 `
|