|
STM32F4(PVD) GitHub 仓库:http://github.com/XinLiGH/STM32F4xx_PVD_Example
) {" q4 Y! |7 CPS:博文不再更新,后续更新会在 GitHub 仓库进行。
0 R0 y& ~& L, V* A0 H n% j
* I. t& \3 J! W# X0 r 在实际的产品需求中,常常需要产品在断电时保存一些参数或做一些断电保护,这就需要 MCU 能够检测到断电的过程,STM32 片上有电源电压检测器(PVD)给工程师的开发提供了方便,可以比较简单的检测到产品断电的过程。
7 N1 G. P; T& T3 @& S% `
% o* v* I5 Y' o7 x8 O
8 m" J0 ?8 t* m' X1,开发环境
9 k8 X$ |$ H" I9 e8 }* \/ L/ F) h* F" f6 t, C4 }, v) w
1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0" ^* q& B) E& L3 A! o
2,编译器:ARMCC V5.06& o5 B5 `. c- e" j' R
3,IDE:Keil uVision5
6 q* E) X# w4 i 4,操作系统:Windows 10 专业版$ l3 |: B! F& J2 w7 A1 H% @
! s4 }' k$ G% d: d- p! b3 U
2,程序源码9 x/ M2 s; B; y
PVD.h 文件
% z0 [4 x6 A0 H& J& U- /**
" t0 Q7 I' }' h$ K - ******************************************************************************/ L$ Z& b6 d6 K. J9 L" Z; C1 H
- * @file PVD.h" n" F3 U, }- G! S
- * @author XinLi! F! O. [& Y% V" B
- * @version v1.04 \; \) Y+ u9 q" q! V8 P3 @7 R5 i
- * @date 24-October-20171 p! p4 p D g Q
- * @brief Header file for PVD.c module.
. {2 m8 s$ p. O! S1 p) L( v - ******************************************************************************6 r! p4 ^9 t+ w( W7 V
- * @attention/ }, u2 b! w% M2 R3 `
- *0 o* A; C+ F% B* o: x
- * <h2><center>Copyright © 2017 XinLi</center></h2>! t! ]0 f3 d* N+ G
- *
& L3 Y! k2 H, i7 b0 g, r! U - * This program is free software: you can redistribute it and/or modify
0 o/ r2 V1 L- K- U. m7 V$ i1 h1 W& d& g - * it under the terms of the GNU General Public License as published by
) b& z/ d. @' c0 d - * the Free Software Foundation, either version 3 of the License, or
+ y5 |7 N3 d) G9 P - * (at your option) any later version.
# b- z8 U* O' w0 J; I4 ~) r% l - *
0 w# Y% g, \9 F& ]( S3 g, ` - * This program is distributed in the hope that it will be useful,
U1 E' F4 i* }% q0 M - * but WITHOUT ANY WARRANTY; without even the implied warranty of
0 S4 b( ~! @8 G/ O% \8 f - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* T4 x) V& R$ J5 Y/ M
- * GNU General Public License for more details.
' Q4 g ? Y, D: ^* ? - *) x. b6 r( e$ q% Q2 U- z) x
- * You should have received a copy of the GNU General Public License1 I5 a: @+ Q7 Y; K$ P
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ ^) A' t2 H" ?1 e! k5 y: [ - *
" [, i7 {2 V. X ]4 I! s/ ^; {" }' X* X - ******************************************************************************. q+ z; B0 r# a! h1 @2 o& M/ H
- */
; s" I: p5 I5 @7 i% g0 X
F- @+ d; L! F- #ifndef __PVD_H& q+ R) ^2 Q* E) k
- #define __PVD_H
1 ], h" X' ?6 D) S5 g& e/ L7 c7 @5 O - - |/ G7 q$ t u
- #ifdef __cplusplus
2 [; g8 x9 B' L' r4 R- B9 o - extern "C" {, G( T9 p0 z3 x% @6 S$ }
- #endif
# F% Y8 W6 x$ Y! R% z - # e9 h3 J6 c8 `$ B" o; o. U
- /* Header includes -----------------------------------------------------------*/$ Q& B( E' j# k5 D/ u+ Q
- #include "stm32f4xx.h"' n$ Q# m3 B5 @0 }5 p: A
- 0 w( f. ], B9 I
- /* Macro definitions ---------------------------------------------------------*/# B# J% k2 i+ |/ H1 m0 K
- #define PVD_IRQ_PreemptionPriority (0)
( K2 H! B, {9 j6 o2 ? - #define PVD_IRQ_SubPriority (0)
. ]3 p5 q- |6 r8 R7 _+ y0 J0 Q' I
. l$ N6 e J8 M) o. S- /* Type definitions ----------------------------------------------------------*/& Q6 V8 U4 w8 F' [
- typedef enum" K' Y8 s3 i- O5 ?
- {
5 H3 E/ {: Y+ z6 ~( y- P - PVD_Level_2V0 = PWR_PVDLevel_0," @6 p! L+ g) p9 A1 @8 ^
- PVD_Level_2V1 = PWR_PVDLevel_1,
* n9 I7 s5 a8 U, X - PVD_Level_2V3 = PWR_PVDLevel_2,) X) W; t8 J/ z; \, {
- PVD_Level_2V5 = PWR_PVDLevel_3,. P7 ]0 U: G! x% j% ]- b3 t
- PVD_Level_2V6 = PWR_PVDLevel_4,8 S1 S& i5 v" t6 N& b
- PVD_Level_2V7 = PWR_PVDLevel_5,: D; @* l/ x9 t8 D
- PVD_Level_2V8 = PWR_PVDLevel_6,
: O8 L: C5 Z. l' h$ p3 X - PVD_Level_2V9 = PWR_PVDLevel_7! ^+ c! L5 r( G, Y2 `' u6 u5 b! G
- }PVD_Level;: _* U- p" Y" Z- r4 h
- ) G, ? C2 k$ y6 _# R) {2 J6 m
- typedef enum6 ` E: F' N S' k( F7 z
- {
9 l# ^6 Z2 L6 \9 R0 } - PVD_Output_High = 0,
( ^5 l6 ~ R* {6 |2 E - PVD_Output_Low = 1
, U! |; i7 \: [8 i - }PVD_Output;4 z* o. `# A& t7 I0 T
7 K. Q' [2 W. @- typedef void (*PVD_Callback)(PVD_Output output);
8 Q6 d+ b/ A1 `+ `
, @, h" |& B7 E# y2 ?& E% j- /* Variable declarations -----------------------------------------------------*/; s; \, K5 _5 N) c
- /* Variable definitions ------------------------------------------------------*// u6 r* @/ W/ Q5 V# |
- /* Function declarations -----------------------------------------------------*/
0 p8 W1 M/ ?# I4 f- Z. c - void PVD_Init(PVD_Level level, PVD_Callback function);
, O( M4 W2 W8 `2 I( S9 t0 P: u - void PVD_DeInit(void);" a3 t0 G$ S+ W' n; y( d
. @( S# y( x$ N- void PVD_SetLevel(PVD_Level level);: E7 p/ J0 z6 O: ?
- PVD_Level PVD_GetLevel(void);5 a- ?$ ]" X w+ K) }
- 6 r0 x0 w, h( n, i- W( y( f
- void PVD_SetCallback(PVD_Callback function);
2 K4 O9 }* X1 O, b; A# X; `, ] - PVD_Callback PVD_GetCallback(void);
* a3 I" ?, e5 z - ( v. P- T9 a4 ?: `' S4 b" S) Z
- PVD_Output PVD_GetOutput(void);
o7 w2 D& n: s. B9 t. x( v$ U
! X4 L6 ^( L5 S0 b5 ?$ X- /* Function definitions ------------------------------------------------------*/+ j4 E# ]5 w' a. z
3 Q* h, x( m( f- #ifdef __cplusplus
* t7 K* U; j! d+ }% T) P6 r! c - }
# e ^: S+ V4 { ]$ C - #endif, ~; @) j" M: @
- 0 J" |7 z6 L* o' Q7 Y6 U
- #endif /* __PVD_H */
. V# ~' L+ L/ E/ K m0 A X
复制代码 0 t1 Z( K) n! T8 E
PVD.c 文件; r4 d1 ~3 o' ^8 H7 h$ c- F; K3 @
- /**( s" K$ a5 L: ]2 v+ C: N' P( u8 n4 x
- ******************************************************************************3 Z1 I' m. ^0 s, a
- * @file PVD.c5 g# ?# u1 R- d7 v' [/ c* }
- * @author XinLi
T" V8 J- ]5 } g/ P5 X% l; E - * @version v1.0
) p9 R( n0 h7 L+ p: i9 h7 F - * @date 24-October-20176 s- j) f5 l0 z( m4 ]# T( O5 N* |; s
- * @brief Power voltage detector driver.0 m# s6 [* R4 n3 W; [) Y+ n
- ******************************************************************************' g* Q2 d3 a1 G3 L
- * @attention' v" a) C/ y3 n6 k& R
- *+ z1 U I- t: z% ^
- * <h2><center>Copyright © 2017 XinLi</center></h2>
2 o6 P; P7 f G1 Z& t8 m - *
3 C% Q1 \! e3 o. {7 \0 g - * This program is free software: you can redistribute it and/or modify
& R3 q; T$ \2 b - * it under the terms of the GNU General Public License as published by
) t/ L, N+ I( `! F) E0 N - * the Free Software Foundation, either version 3 of the License, or
% [6 d! l+ r; c7 e, g7 b& r6 g# J - * (at your option) any later version.* c Y4 R% X# `; u, @
- *: B. D9 v' Z- q' K$ @
- * This program is distributed in the hope that it will be useful,, X" F4 v5 J" B |
- * but WITHOUT ANY WARRANTY; without even the implied warranty of! o( z z0 Y8 M) U h$ A
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the' Z; T' _8 `: N3 u" |7 \9 Q/ \
- * GNU General Public License for more details.
# F5 N/ y! k3 f8 U - *
% y; I1 b6 D9 G5 U* S) ] - * You should have received a copy of the GNU General Public License' G; K; o ]9 L2 L& r
- * along with this program. If not, see <http://www.gnu.org/licenses/>.1 q, f. ^* P9 T2 U, i: j
- *% v# ^9 C' I& _& b0 A
- ******************************************************************************4 H* S) Z# r! D. U. k
- */
3 k/ r7 t# U. x4 ]6 _* L6 E9 Y
- d, L& n- Z" A2 F3 _9 F8 d- /* Header includes -----------------------------------------------------------*/
3 q4 {) t. t- @! C7 o5 P; G - #include "PVD.h"
# ]( I3 M5 i" [8 } - #include <string.h>
& F% a& ^3 T% i/ C
0 W$ @& R( N: I2 e* K: O- /* Macro definitions ---------------------------------------------------------*/, Q# T) c, B1 l w' w3 |2 f5 B
- /* Type definitions ----------------------------------------------------------*/
0 f7 `, E* n/ ?4 ]0 @& J4 x8 d - /* Variable declarations -----------------------------------------------------*/# ?9 h: i1 e, F4 D% ?" H
- /* Variable definitions ------------------------------------------------------*/; P" A" h. ?" n5 W
- static __IO PVD_Callback callback = NULL;$ y' E! ]* C7 B, Q
6 m% A4 Q& T D. Y+ j8 p/ e- /* Function declarations -----------------------------------------------------*/0 l6 M$ w% o" R H2 [) ~0 r
- /* Function definitions ------------------------------------------------------*/, o4 X# u! H5 T- W, F3 H; i$ C
- ( W7 t9 \+ W. x" K Z, [
- /**
- E% {, }8 w2 {, _% \) Z/ Z+ Z5 f - * @brief Power voltage detector initialize.
7 X3 d$ Y% j* q - * @param [in] level: Power voltage detector level.
4 Y9 W/ [. H7 D$ k' Y+ J - * @param [in] function: Power voltage detector callback.
; b, F8 z2 K9 m8 ]$ u - * @return None.8 T) F5 y0 U0 k; m, l
- */
; D! y$ k( W7 `& r$ j# w/ F - void PVD_Init(PVD_Level level, PVD_Callback function)
+ H" `3 T+ H/ S) v6 \0 P" K" o - {
. i! z( P2 ?& G" ?( @ Z) Y+ ` - EXTI_InitTypeDef EXTI_InitStructure = {0};
7 E6 A0 e. F- H; k - NVIC_InitTypeDef NVIC_InitStructure = {0};: r" c2 T5 X6 w( }
-
& r: z$ t* a9 Q3 z3 j - RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);, ^- Y9 e7 f; J6 r
-
" ^2 ]9 [7 ~# N$ X* `" ~" Q - EXTI_InitStructure.EXTI_Line = EXTI_Line16;) n0 s; r2 z- J; r4 I* w/ Q! x1 i
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;! t. G6 a* ]- e% R6 f+ X G
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
0 ^8 `! ?' ~$ L0 @ - EXTI_InitStructure.EXTI_LineCmd = ENABLE;
8 M: W- `/ I3 ]8 @& \* t; _9 {6 T - EXTI_Init(&EXTI_InitStructure);
; k H) Z l- J9 m+ r) T4 T2 V - 3 p" C) g6 m) r2 \; H: C
- NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;, z f8 w( s: D) Z0 R% C
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;2 W+ Y2 I# A" f: _) m% w! A
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;$ u6 ~9 P1 W# d* y
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;7 H7 t7 _* y$ ^# R0 Y! u! Y
- NVIC_Init(&NVIC_InitStructure);5 N1 t" y+ C) `0 Z S
- % a2 K0 V2 a* l: j2 d
- PWR_PVDLevelConfig(level);2 i5 O+ Q- o U5 f" `
- PWR_PVDCmd(ENABLE);
6 E% V/ o. a4 U2 R+ k: d1 s+ b - - W! K1 m: v4 T, [0 ]8 `0 s ?2 m
- callback = function; O. H7 P; a6 ]2 t
- }
' T j! b3 a4 R! k5 j( D' p - V$ t4 Q( {( \* N- u/ |
- /** R( Y: b4 ?5 @- H% Z* j
- * @brief Power voltage detector deinitializes.0 t, T0 n; n% L& u ?% m
- * @param None.; I: j) R2 {) n( a4 q( Z5 }& p* q& \
- * @return None.3 _9 ]6 G6 Z- w6 E
- */0 Q2 x6 `- }' e% j2 m+ O: z+ m5 w6 L
- void PVD_DeInit(void): S! ]7 G$ |9 v6 Y) m) h- n' }
- {
7 j4 E4 w+ S' v- D S; {5 e, e' ]+ o0 Q - EXTI_InitTypeDef EXTI_InitStructure = {0};. C) k( d& e3 G1 l9 i$ q+ I
- NVIC_InitTypeDef NVIC_InitStructure = {0};
" I: X5 P" ^, I' X( Y* E - ' Y2 ]9 C" G2 Z' j7 ?) v- V
- EXTI_InitStructure.EXTI_Line = EXTI_Line16;5 p. [" p! p4 c! o4 B) N! R( o
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
t2 V1 M$ w4 ^& x4 R, N; T! U' f - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
7 K- n/ e7 w5 Q: @) `- z2 ^ - EXTI_InitStructure.EXTI_LineCmd = DISABLE;
6 x* Q8 [0 O; r- ]7 g$ S! e, V' P2 @ - EXTI_Init(&EXTI_InitStructure); p q( z9 {: V2 Q
-
: {2 S8 i9 r0 P9 d$ ^ - NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;' t' N: @. }- G, T. Y: C
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;: B2 b' @- M$ y# m5 R
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;+ k$ m; P0 U( \; H( ^% N! }
- NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
1 `0 @) c q n9 B. _ - NVIC_Init(&NVIC_InitStructure);+ g; ~+ h. o7 F n# u! F2 g
-
6 t: E- g7 u( G - PWR_PVDCmd(DISABLE);
9 Y5 M# ` Y1 Q& Y0 p( H -
: F4 J& S. Q* E5 b C" D' b - callback = NULL;5 x; R# Q: d# V. l: l" v: G
- }% p& f; |& L9 O$ ? Q
- , O' v& H, b) w% s$ t3 M8 t: ]
- /**
2 M8 v$ k) X! X' q - * @brief Set power voltage detector level.
1 `6 C9 v3 g* o1 X, D1 J2 o - * @param [in] level: Power voltage detector level.
. }/ a! e2 _& K - * @return None.0 @, g8 g- k6 @ u/ ?
- */
$ ]! Q$ @6 i( H - void PVD_SetLevel(PVD_Level level)* L4 f. d* b# O1 t
- {
3 n- p+ i2 s! v( n H" m - PWR_PVDLevelConfig(level);
5 A1 [3 }6 m! k' X; m - }4 k8 i) r8 {* F
, `* z, P, O% u9 c( g0 O- /**
, M, s/ D" d/ h - * @brief Get power voltage detector level.
' y4 @) ~4 _& L/ M7 ]3 ] - * @param None.; t3 a5 y' \& x1 ]. {+ I* r
- * @return Power voltage detector level.; H; n( M( O" Q# k
- */
' T0 n1 Q5 q- R t6 @/ s# l/ m: G - PVD_Level PVD_GetLevel(void). u" F; |1 I4 U' ^. l
- {
5 Q* Q: O( Q. e# N6 I3 K8 R: I - uint32_t tmpreg = PWR->CR;
' L+ u! O$ R" R4 W- ] -
+ k7 ~9 p4 b( P& ~ - return (PVD_Level)(tmpreg & 0xE0);% v9 S9 t/ I I& [* [
- }
4 U f8 M" o/ `5 N5 g. }
7 C3 r5 h; M$ n' G9 t4 g% d' e- /**6 S" O* o" m4 H+ t
- * @brief Set power voltage detector callback.5 T1 d& Y3 z8 E1 Z& j, b
- * @param [in] function: Power voltage detector callback./ }, s D/ K5 u* O9 H
- * @return None.
( _+ _. _, a6 X2 D6 }7 ^5 X - */: u2 T+ ]% T* W) O/ d- `7 W
- void PVD_SetCallback(PVD_Callback function)
, g) t. x" \+ } @& n - {
) @' N! W$ Z' X: e) H* k - callback = function;9 h' m' a/ [6 G
- }: {5 J; s- x4 p" m5 v n
- , j4 r V8 [8 e* j, X0 b. Z
- /**7 D+ {$ b5 S8 d2 a; y- F2 {
- * @brief Get power voltage detector callback.( H t# l H; e! Q [
- * @param None.
3 G' d Q' {! h0 I$ j - * @return Power voltage detector callback.
8 }6 `- T! a5 V( }* s" D) O - */6 v! L. M4 b' [) ?
- PVD_Callback PVD_GetCallback(void)5 j, x# ^, T3 S. z: o u3 ]- _
- {
' h3 c% R5 N" G+ ?. e, ? - return callback;2 {/ V: i1 B- v0 ?- P
- }" w# T6 B; k( U' c# Y* D
- . k' F4 \0 |* c- y, B# z( I
- /**
4 ^1 d, I) b! @ - * @brief Get power voltage detector output.& J6 ]; g! a( e
- * @param None.7 d0 r2 e' t" y
- * @return Power voltage detector output.
" G4 S' E" [9 s3 ] - */) N0 a2 ^, c9 |$ z9 g. o! f
- PVD_Output PVD_GetOutput(void), y0 V4 z- z( m8 ?# [4 D
- {
3 ^1 w9 _8 N5 b* ?( M - uint32_t tmpreg = PWR->CSR;3 r# S" e9 D5 U6 E- a% y8 K
-
. w: q2 ~( l/ b7 n" @ - return (PVD_Output)((tmpreg >> 2) & 0x01);* O' o9 v8 j3 H2 x! B# |- w
- }
0 _: [ z! U; e& N, N6 R0 O4 H( m
- m7 d4 c2 H$ Q& l- v- /**
1 ~* y, n' J" ^* `& I% s - * @brief This function handles the PVD Output interrupt request.; f. }; K% z. W1 g! X; |/ @7 k
- * @param None.2 w$ ]/ d$ t7 B: z
- * @return None.! y1 T F; X! P ]3 g0 t, O
- */2 Q" a4 t2 r' l8 R" }. o
- void PVD_IRQHandler(void)
5 v1 K& e" G$ @. B9 a - {, ]6 N$ p7 D( j1 v8 x
- if(EXTI_GetITStatus(EXTI_Line16) != RESET)6 S) }$ y3 j+ x6 `
- {
% I' a* |6 U3 V/ `) C+ | - EXTI_ClearITPendingBit(EXTI_Line16);
' m" \- ?5 Q! e5 D -
# o5 e& b5 ]" U# F2 O - if(callback != NULL)
) h2 O: N' [3 B6 h8 r6 _ - {
5 n& e4 i @6 q1 o% d+ J! k' a* x - callback(PVD_GetOutput());
5 p/ j/ t5 ^; |3 d - }
# t8 ]4 O7 y8 S4 M. U - }) e. s4 L# C0 r: v: j9 ?
- }
! N0 t6 B/ Z7 o- j2 t; @6 V
复制代码
/ _( A* r! ?& e% m
# Q8 @! N2 s. ] main.c 文件, u, B* g- @2 v' { |
- /**. g' K/ c( z5 t( Y; U
- ******************************************************************************7 b: f( S, N8 }
- * @file main.c
, ?, h$ K7 t0 R - * @author XinLi' y I; z2 G& Y& g; y1 a
- * @version v1.0
) }! a8 _4 K3 z8 Y3 C$ i+ E+ H1 { - * @date 24-October-2017
; D7 ~5 B+ f. R1 S1 e - * @brief Main program body.4 J) L; V, W( C* F
- ******************************************************************************
$ u1 i$ M% w& O# ?8 I - * @attention! o# j3 B5 T" M+ d$ n; W
- *
2 T5 o' l5 b4 R+ @$ K - * <h2><center>Copyright © 2017 XinLi</center></h2>
/ g. _5 _ s, G, V - *
; [: N& w) J/ p- a! ] - * This program is free software: you can redistribute it and/or modify7 r- X5 r+ k% S$ X
- * it under the terms of the GNU General Public License as published by
- M# X$ u2 R9 p+ F, I# ]7 x - * the Free Software Foundation, either version 3 of the License, or( t% y8 v$ D1 ?2 n* C1 [- P
- * (at your option) any later version.# X% G' Q% x! x; R$ J3 l
- *% C% a. E: i0 {, Q# a) H$ H
- * This program is distributed in the hope that it will be useful,8 w# b* @2 E+ F% W p" |
- * but WITHOUT ANY WARRANTY; without even the implied warranty of$ I& J/ U3 ?2 j/ c, d' e
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! ^" d5 x: u; x9 s7 Z/ |# R8 u - * GNU General Public License for more details./ I3 d% y9 O' g; T
- *
: r8 R, a6 N& y% a1 ^! k - * You should have received a copy of the GNU General Public License5 g( F, }, C: l }: j
- * along with this program. If not, see <http://www.gnu.org/licenses/>.) i. `3 A* c, V+ d2 q0 ]
- *% }8 y0 a: A4 ?; p
- ******************************************************************************1 X# r5 Z# o/ F9 I- P
- */- h9 E# @) D7 [' j& e/ U2 G
0 i* t/ r+ L$ `0 c! D; w& u8 E1 l. E. E- /* Header includes -----------------------------------------------------------*/9 r, X( T* R! N" u) A6 X9 K9 o
- #include "main.h"! e9 p0 F. }* ?! M3 E. {- {
- #include "PVD.h"- d0 B3 w; h2 k4 ~/ G6 g3 P
- #include "LED.h"
6 E& b4 L0 N2 E - : ?- `+ D, X. ~2 I0 b
- /* Macro definitions ---------------------------------------------------------*/" _* C" l* q# y) M# D) g" s9 g
- /* Type definitions ----------------------------------------------------------*/ G5 s& k h3 `) D9 l6 v. |
- /* Variable declarations -----------------------------------------------------*/5 b4 y" U7 I/ `+ }
- /* Variable definitions ------------------------------------------------------*/
2 Q7 ?0 A9 a. F$ o# o; g$ v - /* Function declarations -----------------------------------------------------*/, x4 Q3 ^, N, [6 C2 |+ `$ Q" Y' x) c
- static void PowerDownProtect(PVD_Output output);' X8 G3 M2 D B
5 }# I- Y; k. A7 f- /* Function definitions ------------------------------------------------------*/- ?1 E5 g7 c# L, b# C6 F
- 4 l9 t- {0 @- G4 k" ~
- /**$ d. F; H6 |. U! ~0 t
- * @brief Main program.3 D( u: E( t* M$ y
- * @param None.) }0 ^$ n- V$ ^0 n$ b# O! s
- * @return None.1 v R) c _. A, S
- */
9 {7 I% G' }' ~* k - int main(void)
: o% T' P* T) d0 m8 p* n o2 _* a4 h - {# H9 x* P, s3 K( `) l* h& D( I
- LED_SetStatus(LED_Pin1, LED_On);! U+ A6 A8 n) Z2 ^) T( m- C8 p. K
- LED_SetStatus(LED_Pin2, LED_Off);
* s4 V, n' h7 v3 j: t& \* z! \4 W - 6 \5 i9 B e4 O0 V5 ~, [$ K
- PVD_Init(PVD_Level_2V5, PowerDownProtect);
5 O8 |1 [0 D3 `: { -
: X; p! q J$ `: u - for(;;)! M) K) z; w2 x( K$ I) u- c
- {7 i% [! E4 c7 K; ^" \
- ; K- r( r4 H2 L" }7 C* k
- }, h0 a$ x. W! ~5 W4 }
- }' J! B- E, Z" \ Z
- # d. u9 W' `: v
- /**
! Y' E7 H# t5 t/ s0 [# x' l - * @brief Power voltage detector callback.
; z; a) G9 s0 \+ x: I* p% M' L - * @param [in] output: Power voltage detector output.( I# n0 V$ ^: _& |( F
- * @return None.2 s1 t1 _7 x7 V1 r* o
- */6 q+ [# P( K# r8 A
- static void PowerDownProtect(PVD_Output output)
' @1 V$ H7 S" `: u; L - {. \3 v7 v7 ] ]2 F7 w7 L
- if(output == PVD_Output_High). {; v3 h: F1 ], Y
- {
+ {$ p1 l) Z4 E2 a, S - LED_SetStatus(LED_Pin1, LED_On);6 r" F; w. W5 h7 f1 N# y- [
- LED_SetStatus(LED_Pin2, LED_Off);
2 M: b0 z. ~! A# t1 ?" k - }
) [$ r1 g9 ^- L2 D - else8 T) o5 }: I; E+ F8 P1 j9 u
- {3 M3 f" Z+ r0 H* l4 o
- LED_SetStatus(LED_Pin1, LED_Off);* U; C; e) q" @
- LED_SetStatus(LED_Pin2, LED_On);) @% |* c; s; \
- }
4 X9 j* ^/ \- }* e+ `0 Y, f9 F - }; f7 G" f8 A5 p% ]8 d( L
复制代码
, _) P0 S7 J ^" @; }; d* O+ a& [" t5 c% N) H) Y' [
$ s L: b2 X, R. B$ @' V. r
, Z0 V) t' D: y/ f$ C
) ]1 M, m1 b2 s$ E7 i1 O# T1 z |