STM32F4(PVD) GitHub 仓库:http://github.com/XinLiGH/STM32F4xx_PVD_Example
( F- x( \' G6 d3 U# `. `( u$ f9 K' nPS:博文不再更新,后续更新会在 GitHub 仓库进行。
1 U; M/ E/ H* ~7 c, c: s4 R n. }( z: o
在实际的产品需求中,常常需要产品在断电时保存一些参数或做一些断电保护,这就需要 MCU 能够检测到断电的过程,STM32 片上有电源电压检测器(PVD)给工程师的开发提供了方便,可以比较简单的检测到产品断电的过程。
) X" w( I! N/ x0 X
/ E/ k6 _: ~8 o) W" y3 e2 ^* s$ G/ V( Z' Z0 K& c! x
1,开发环境
( x' R0 M3 r& U! X; x5 U& v7 H; t* H
1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
& [" @, D( a/ j- r) q) a 2,编译器:ARMCC V5.06. j8 [4 G( @, H
3,IDE:Keil uVision5$ Y" g4 o" K, Z6 q7 w% r$ v
4,操作系统:Windows 10 专业版
# e% ^$ a/ c c- N% G
0 l/ |, I- _4 ~6 ^6 M9 [2,程序源码
" N! H' ~( P) e& c. N" Z/ _ PVD.h 文件5 v( q+ r7 g1 A
- /**- c- c' H$ t% ^7 N+ c0 N4 l/ f6 @
- ******************************************************************************- \& g8 b# m! C+ c% Y
- * @file PVD.h
, K" i! l& Y0 B - * @author XinLi
3 @; j. p1 y1 ~; q: V1 V - * @version v1.0 I; N' w- I9 n
- * @date 24-October-2017% ` U, q! Q, g# r6 e
- * @brief Header file for PVD.c module.
9 @) z0 Q2 r- F - ******************************************************************************& _3 o% E. g6 M5 z" v# n
- * @attention- L) S, }6 q* h q! p7 h
- *: z7 U# Q& F4 G6 G* C0 ~1 A7 S: y. p5 }9 w
- * <h2><center>Copyright © 2017 XinLi</center></h2>4 `* ~3 Z+ Q6 X8 \+ C* R' G# ]3 i
- *
- C) `2 D! Z1 M- _5 k( v - * This program is free software: you can redistribute it and/or modify) B9 z I: T& M7 _3 W; f4 d9 s- u
- * it under the terms of the GNU General Public License as published by- n, B1 l$ c' L. A" f) E+ |3 L
- * the Free Software Foundation, either version 3 of the License, or4 J+ E3 d q9 c: ~4 Z
- * (at your option) any later version.
9 X* _: N% e' G: U: }4 X& } - *
/ j& U" i x+ {8 f6 {" v) y - * This program is distributed in the hope that it will be useful,
* p$ j2 l9 ]" _" l6 c( O# r - * but WITHOUT ANY WARRANTY; without even the implied warranty of
" c" W! ~, N% C" [, w - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the, \9 V' K% d( r5 k/ j& s( |
- * GNU General Public License for more details.! g u( a8 I% L; D" @
- *. j8 O& f' F- m$ L% J; t
- * You should have received a copy of the GNU General Public License
4 }. u3 I; x9 p% u- v. P - * along with this program. If not, see <http://www.gnu.org/licenses/>.' O6 ~6 f0 v) e+ Z
- *
* F9 {# \4 C' f3 r1 N# K- S - ******************************************************************************" [5 Q' b/ W5 M' a$ g+ V
- */
) }; E9 H, t* v/ } - 6 h- }1 a% R: z. C# H' g, K. M- V" |
- #ifndef __PVD_H
) {; V# c. ]7 P5 n/ c+ a - #define __PVD_H* D' O# l& X/ H7 }" Q4 g/ x2 V
1 b8 V+ k- w# G/ Y' Z0 F" }- #ifdef __cplusplus% U3 Y5 H9 |. d: S
- extern "C" {, S* L5 A; | w7 B; k d3 ~
- #endif
3 S& S2 Q7 {( b, f+ g1 D! R - " L* x# o& q; |5 F
- /* Header includes -----------------------------------------------------------*/
: U. `+ ]" N0 a: C% g - #include "stm32f4xx.h"
/ k- U1 o0 y! x( m D( q
0 K- }, b G' H' w$ c2 p' k- /* Macro definitions ---------------------------------------------------------*/
, \1 w- T6 k2 l+ c7 \ - #define PVD_IRQ_PreemptionPriority (0)
+ F. K+ t* U5 y2 P! a - #define PVD_IRQ_SubPriority (0)+ ~& J7 _) e5 w* O+ x, A& i v. o
! K- R( b5 e- G1 Z3 k; M! a# i- /* Type definitions ----------------------------------------------------------*/
9 Z9 ~9 ~- B: y( G( T4 P - typedef enum( M0 r' N- X7 x& ^/ S3 D' c
- {+ o) a( ], s3 K+ X5 o6 o
- PVD_Level_2V0 = PWR_PVDLevel_0,1 G, }4 ]; x% s- A; J
- PVD_Level_2V1 = PWR_PVDLevel_1," b7 L# ?1 o* T$ Y7 u
- PVD_Level_2V3 = PWR_PVDLevel_2,; r$ O9 m) K( Y A
- PVD_Level_2V5 = PWR_PVDLevel_3,
O) F$ n+ _$ \0 x0 O - PVD_Level_2V6 = PWR_PVDLevel_4,8 w$ I9 L. H1 U" u* R0 D$ R
- PVD_Level_2V7 = PWR_PVDLevel_5,! ^$ v1 J$ z. }6 r
- PVD_Level_2V8 = PWR_PVDLevel_6,
. @6 J# V3 [# h5 _2 M$ W* _ - PVD_Level_2V9 = PWR_PVDLevel_7
$ O' q. ], ~! C) ^; j/ K+ f - }PVD_Level;
( ^' |4 I6 ^" j7 r' r1 h+ s - 0 o( @6 X a' W* s. v; R
- typedef enum6 H6 ~- O. i0 Y2 g& E
- {
( S" }# b. ^8 N- p& h4 B0 X- N# r - PVD_Output_High = 0,/ ^/ e3 L5 A' Z$ C
- PVD_Output_Low = 1* O0 w) i% Q( f$ W/ Y
- }PVD_Output;
1 r7 C2 [& O; Q7 }5 n$ Q
' Z8 a1 R# Q( l ~) P- typedef void (*PVD_Callback)(PVD_Output output);* H! ]( x1 Z" B* ~9 u
, r% ^2 S" I1 ?8 H/ u0 V# L: o" c2 i- /* Variable declarations -----------------------------------------------------*/
" Y, \5 u+ Z( T7 w+ E0 _ - /* Variable definitions ------------------------------------------------------*/- F& ~4 G) y% U6 D P3 d7 \3 a
- /* Function declarations -----------------------------------------------------*/
, q4 O: a9 f( @7 i% u - void PVD_Init(PVD_Level level, PVD_Callback function);/ ^) A6 @1 ?" B% i0 Q; v
- void PVD_DeInit(void);, g1 f+ C4 d! y. I$ d: O3 l- ^. V! k
- # d4 A3 y1 O7 z. W
- void PVD_SetLevel(PVD_Level level);
6 h! k" r- x+ J1 | - PVD_Level PVD_GetLevel(void);' o1 U' _. e& v% m6 i
# s8 S# A% _: f F0 D) r- void PVD_SetCallback(PVD_Callback function);0 H, r" B. [* o8 m' T; }
- PVD_Callback PVD_GetCallback(void);
. o- ~8 W$ m0 i. I( U
+ T' @2 x- q( }8 c. x- PVD_Output PVD_GetOutput(void);
2 i1 E4 i7 u) d% [
8 @( V' e; {7 w" c: `, U- /* Function definitions ------------------------------------------------------*/. U7 q2 ?. v* q. ] y$ R4 ]
1 r5 \! p [" ]& H U4 P/ G- #ifdef __cplusplus
) ?5 Z2 d+ T8 A6 U9 y! [ - }: G; l/ W/ A+ r' v ?
- #endif
7 A4 q ~' F: n$ d& O - " C2 Z- d( h0 ?" @: `: j `
- #endif /* __PVD_H */
& P# m; A: f6 o! t2 |
复制代码
' E7 m }% ~/ c) j' u) k PVD.c 文件 k! e6 c5 x9 H5 x f
- /**
( Q, P( Z% k2 l6 l \ - ******************************************************************************
! W& G4 ]# s3 S$ |7 d8 a- ~ - * @file PVD.c8 ~4 g: J: V; g! d' v$ | {* t
- * @author XinLi8 ~' \9 C5 h7 |; l' @9 J
- * @version v1.0) }, T4 F k3 [2 Y$ f, m
- * @date 24-October-2017
( |$ t& ^0 l$ C _7 x' j% s; r - * @brief Power voltage detector driver.6 ~# w/ T/ {$ B+ K
- ******************************************************************************6 \ _/ d' @1 m
- * @attention
9 U& x$ g- @" ~9 l m: y4 ~! O0 e - *
/ g4 H1 J$ C0 X+ p2 ] - * <h2><center>Copyright © 2017 XinLi</center></h2>
+ ^& l7 l' `) F - *8 G7 v( n1 h m, R+ {4 ]
- * This program is free software: you can redistribute it and/or modify
: L1 J0 u2 [) G4 l8 u - * it under the terms of the GNU General Public License as published by1 d' N$ c2 J! p2 b4 ~
- * the Free Software Foundation, either version 3 of the License, or
6 Z- W& a1 W& F! R) @) N @0 f - * (at your option) any later version.
6 M; e4 U. L% c - *
' F5 _& x% ]! N# o4 S - * This program is distributed in the hope that it will be useful,
5 N+ F! d! Y) l3 H1 u5 c! ^8 Z - * but WITHOUT ANY WARRANTY; without even the implied warranty of
" L- h1 c! }# {3 L2 l1 K, s - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9 c* C G" h; {1 {1 a) Z
- * GNU General Public License for more details.. e4 u0 w2 G5 _2 v
- *5 W' e* k4 s" ]+ k/ Q) D4 H
- * You should have received a copy of the GNU General Public License$ J" H, b# E5 I2 E( }
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
% ~& ~4 M0 {5 k7 g ^ - *
7 K" ~8 s7 T% S' J( H - ******************************************************************************
, Q6 l c* K4 F( j' U& I - */
/ w- J$ O Z5 Y5 P7 V - . J( G9 |5 b& [7 V; n& {' L4 _- i
- /* Header includes -----------------------------------------------------------*/8 _5 ?8 b9 \, u8 |
- #include "PVD.h"
: L7 j# V. W; x: m4 D. [ - #include <string.h>$ U9 [- U0 _2 R' o9 ? p" E' ?3 R6 Y
. m1 E( q& E9 n+ B- /* Macro definitions ---------------------------------------------------------*/
1 q4 M: M0 f, t8 B$ {, t& Q - /* Type definitions ----------------------------------------------------------*/, [2 k- {& y5 x0 U
- /* Variable declarations -----------------------------------------------------*/
, \9 F- V8 S+ ~9 |0 w% C4 o - /* Variable definitions ------------------------------------------------------*/
% N3 a' g, g2 \: }' C. x3 j - static __IO PVD_Callback callback = NULL;0 U$ r7 g; J6 D* A
- " z% b* u/ G' D# a
- /* Function declarations -----------------------------------------------------*/0 p4 O' u. P3 r5 C% X' s8 P" u
- /* Function definitions ------------------------------------------------------*/
! T) k0 Y2 Z. C" c. c! l/ u% h - 1 ]* l G$ a: H4 W/ m
- /**- Z; \+ F0 D- N2 D/ t
- * @brief Power voltage detector initialize.
7 ~6 v2 ?' T! B% k' v - * @param [in] level: Power voltage detector level.
w; m+ \! h; s2 A1 a7 h( T - * @param [in] function: Power voltage detector callback.
1 `0 v- Z7 u/ l" `9 l - * @return None., O/ h4 h/ f3 ]: N1 M
- */5 s6 F; _2 X* ?. Q
- void PVD_Init(PVD_Level level, PVD_Callback function)! d2 v) @6 s6 G0 N. a
- {% ~# k8 J: `! R" s8 F. m s
- EXTI_InitTypeDef EXTI_InitStructure = {0};
7 l; x* R7 L- o; p - NVIC_InitTypeDef NVIC_InitStructure = {0};
+ [- l9 F; N1 K - $ T0 Z* R+ D3 @! i' f
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
; U( K" [" }) w0 A3 r5 M1 I - Q) M9 G3 L0 P& u4 I. {/ V7 M
- EXTI_InitStructure.EXTI_Line = EXTI_Line16;" Q2 d( C2 ^( O/ {8 r) U
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
' F6 V+ H! u: O5 |0 q2 M - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;0 _9 \$ v, O- i. p4 i+ g: J. |
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
. S+ V5 L# H* }+ _ - EXTI_Init(&EXTI_InitStructure);
- Q1 w5 G: C% k$ E7 o( z# x( p. q -
; q( J: U/ e5 C3 U! e - NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
1 j5 v8 ?7 J. ^ - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;0 B' s* _% N7 {! V$ E
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;
6 t: ], R" E' w - NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;9 V9 _& v7 o2 a9 d& E3 x0 |
- NVIC_Init(&NVIC_InitStructure);
* n x# M* \1 m& |; F2 K9 o - 4 a& f9 Q0 a4 b2 w* a
- PWR_PVDLevelConfig(level);8 u4 F8 s" o6 x8 `! T! _
- PWR_PVDCmd(ENABLE);
+ q/ M, M" ^+ f/ b8 | - 6 @' z5 z4 L! W6 P7 n- J: Z
- callback = function;+ u4 U& [" c1 z2 \
- }; o" @/ @. R% y8 h; J
4 o1 k c3 I1 z0 ^6 ^- /**
& ]% z ?, d! y6 b( R ^1 t - * @brief Power voltage detector deinitializes.+ `' i* D' ]1 F6 k% P7 Q) O
- * @param None.
) @- ^" H3 Q6 e$ M - * @return None.) c" A, j8 {' A* S8 a
- */3 I. x3 H* a+ b4 l
- void PVD_DeInit(void)
' L0 U1 D, A( R. t# I - {
: v2 N5 Y. E* ^& ]2 z. } - EXTI_InitTypeDef EXTI_InitStructure = {0};
9 s: y. R" t8 `: Y% e9 T3 k2 ^7 G# P3 p! l - NVIC_InitTypeDef NVIC_InitStructure = {0};; M3 y. k, D$ s- b) E' m
- # g, S5 s* l: n. O: \) q" }
- EXTI_InitStructure.EXTI_Line = EXTI_Line16;% {5 h# F0 B% M( n/ @' ?
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
$ v6 n: m w: y8 | - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;. d% \. ~- a9 c0 O
- EXTI_InitStructure.EXTI_LineCmd = DISABLE;
& `4 |4 B0 F# l0 I1 ~% ? - EXTI_Init(&EXTI_InitStructure);
+ I7 U" X( d& R0 j, ]+ M! o -
- a3 P- a8 E; X7 A- [$ t" C# ? - NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;9 ~, N( C% v+ |% j6 K+ O
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;
" E4 {! O2 Y; B& p' v - NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;
; a1 R5 V/ z7 x% r/ g - NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
" C9 T! z' x2 g: T5 D0 m - NVIC_Init(&NVIC_InitStructure);
5 H. R- ~0 N* X( l -
* v. v5 N1 U9 M; D' _ ? - PWR_PVDCmd(DISABLE);
" Q4 Y% X ]: y5 U -
2 T A' r/ \5 h) D* t% } - callback = NULL;( B; p: Y0 a7 p* C3 `5 M
- }
" ?% }; H9 O; a/ r1 S3 b - 2 _. w, r) ~( l9 f; m
- /**
; U) P* k/ K+ y6 I2 `* T8 S5 P - * @brief Set power voltage detector level. V3 a; L$ T& c& j) c# a0 y5 {
- * @param [in] level: Power voltage detector level.
( I3 f: A' U) |+ x. N5 M - * @return None.; u5 s+ i! q2 ~* O0 n8 F5 A
- */. s W# I& _: a" m
- void PVD_SetLevel(PVD_Level level)
+ U$ h9 D p q% S/ @2 D3 I - {5 \) O* q) B/ b2 y2 z& t
- PWR_PVDLevelConfig(level); Q5 o" \, f$ i/ d3 ?/ K
- }9 Q" O2 a+ L7 B& `, d, |# r; B
2 p. `4 z/ U, J8 s- /**
) J% ?: Z& k$ r' h8 X8 L5 x - * @brief Get power voltage detector level.
) k9 C8 k! r+ J1 N6 S4 P - * @param None.9 N4 e, N( z& p3 O# u3 v+ F
- * @return Power voltage detector level.
- w9 a. R9 w `) G$ V# u: z - */& o7 y5 {' n! P3 o. c6 ?
- PVD_Level PVD_GetLevel(void)4 M# s) A+ K3 m. Y- D
- {
, ^ u, @; r: r% W0 e% d; R) o - uint32_t tmpreg = PWR->CR;
; l: B: D+ D7 i - / d6 S6 n' ? \$ a
- return (PVD_Level)(tmpreg & 0xE0);4 ~. ]2 Z2 C; D! `
- }
0 O! Q% I5 W: r, ~ B" Q. i - 8 R; u2 t' ?# v( T) o @6 N
- /**! A- M7 p; Z* v. n
- * @brief Set power voltage detector callback.. A9 _# b4 k. m/ S, T5 q
- * @param [in] function: Power voltage detector callback.
( {! Y' w% Y) ~0 {( U - * @return None.5 h0 k8 m/ [; O" T' N
- */
# v. S% D1 U- H9 b1 h - void PVD_SetCallback(PVD_Callback function); w9 h* Y$ ?% b2 W- A& L
- {
; N% `' @; \' |0 r- p - callback = function;/ p( ~" `0 f# W/ m+ w
- }
; H0 s. b' y$ f# \5 u, D
/ F; S- H$ [2 t# H0 G- /**9 q7 i. c' f# R6 }9 k6 }1 s: ^
- * @brief Get power voltage detector callback.7 I( g2 F5 g5 ~; ~8 w
- * @param None.) s2 L7 E- K0 f5 V2 u; z+ [; |$ q
- * @return Power voltage detector callback.4 e! C! ^. t* k2 N ^. y
- */& G8 \. Y3 }5 i- Y
- PVD_Callback PVD_GetCallback(void)4 W5 e% L7 k' H' W, y1 X/ T. b1 f
- {
/ K0 F; K# D D: w: [8 @ - return callback;6 K, U, a$ R' C
- }/ Q7 _. ^9 V, S/ N6 B
) O Y; t7 z0 e. s v- /**2 f* J: W7 I; ^
- * @brief Get power voltage detector output.4 [5 s) r% G8 z2 G G
- * @param None.: q0 G3 \. P, P; v1 W
- * @return Power voltage detector output.
! L! L; w; V& O* q* a/ v( \ z - */7 }1 K. K$ u4 u+ `# d- j. }
- PVD_Output PVD_GetOutput(void)
; l6 A: d# t; Y1 x9 }8 \" n: X- D - {
j4 N2 G9 w9 ]% M; e! _# | - uint32_t tmpreg = PWR->CSR;
6 n, Z: A$ a9 @$ J F7 P - 8 B) Y! P I! J$ o! [
- return (PVD_Output)((tmpreg >> 2) & 0x01);. B& u' x0 K6 U2 n8 g* G; U
- }6 Q5 Q. ^4 U7 R2 ~5 s! M
- , v O: E$ j" |
- /**
( J$ b; B8 x! u" { - * @brief This function handles the PVD Output interrupt request.
+ j. t2 G2 x9 J - * @param None.' ]: `# t# H# w% ?% Y) A1 v4 W
- * @return None.
0 X2 j; S" I7 u - */$ J# E% L- e/ W' j
- void PVD_IRQHandler(void)
5 j4 E5 }, n* y( ^ - {
) m- j4 \ y: Z - if(EXTI_GetITStatus(EXTI_Line16) != RESET)
E; ^9 Q# S2 @' @+ t! v6 P - {
1 |+ Z% s& ^$ x" i% O" y( N - EXTI_ClearITPendingBit(EXTI_Line16);
; ^7 U9 d. Q$ y2 ?, }( b3 l -
5 u9 Z/ k; o$ z/ Y8 M) \* C - if(callback != NULL)
1 H) b6 p/ W# S, c - {* s, F; y5 z: V8 [: e6 C" ~6 y1 D
- callback(PVD_GetOutput());6 r! d8 j0 n8 w* e- S
- }6 c G8 P$ O& v& h- K* {3 ? v
- }
+ J' L8 J* H8 D2 w% J - }
9 O( k, J# f- L7 S5 z# a% p
复制代码
" p0 m# Q7 A/ Y& K2 Y( R
, Y0 e) h' ^" E3 C% o- z2 G main.c 文件" T: d1 d. G) c# Q8 M9 Q( Y8 ^
- /**, e9 c: B4 j9 z* ?
- ******************************************************************************
4 ?- A' L# R9 p. X' o5 o: S: ?/ e8 @' n% ` - * @file main.c
^" ~5 L6 X. V: v2 H' }( O - * @author XinLi
3 Z8 j) i" g: a! s2 _/ k - * @version v1.0+ x ~# ]! Z: ~7 U
- * @date 24-October-20170 x @" G, B+ i9 \" X6 f |2 z& O, g
- * @brief Main program body.0 y7 p) l# x/ J( I6 t3 ]
- ******************************************************************************
0 N5 R( f7 ?- P7 {3 w2 t0 w% O$ m1 j, ]! e - * @attention6 a: `( J7 F7 @1 n
- *, j- y0 H* B9 h& R! T
- * <h2><center>Copyright © 2017 XinLi</center></h2>
7 u& |( @* b- E! k q" J. J - *0 H5 E, w6 u" D8 c; u
- * This program is free software: you can redistribute it and/or modify
$ q. D; g/ w- B( b: j+ Z! s& U - * it under the terms of the GNU General Public License as published by
5 z! D8 x- Q! H( | - * the Free Software Foundation, either version 3 of the License, or
4 c1 ]+ j( @* |0 q. S. h8 [& d- I5 W - * (at your option) any later version.
, Q8 ?# C4 q/ s: s - *& ?) Q! W3 e1 e+ Z0 T- L/ X
- * This program is distributed in the hope that it will be useful,
9 v+ E: R7 l9 V - * but WITHOUT ANY WARRANTY; without even the implied warranty of
! [- A! W4 Z' x - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the( d- a/ n( L; @3 L
- * GNU General Public License for more details.: }$ |( B6 x! m+ Y+ D; S
- *
) n" K3 p) w' w8 X$ ^ - * You should have received a copy of the GNU General Public License/ C8 U% k' u6 _2 U0 L9 E) S% Q' ^
- * along with this program. If not, see <http://www.gnu.org/licenses/>.6 h9 e/ k; u6 |# X
- *! d/ G& ~. V: T
- ******************************************************************************
8 c( E! L" j' h+ d0 Y4 {" f - */
8 n% n6 I/ H8 X$ N
! M, b3 T$ g) j; d: ~2 Z1 s: b- /* Header includes -----------------------------------------------------------*/, J5 M! {/ y- b0 W& y/ L, {
- #include "main.h"
" L7 q7 S) ]& n/ {5 G' ^& J - #include "PVD.h"
7 l: Q, i) x! M& @* Y& C - #include "LED.h"
% B: {- E& s) y& }. k - 3 v' p& ~- @; v. y( g: Y
- /* Macro definitions ---------------------------------------------------------*/1 y$ ^2 Z1 v' [# Q) J. s' H8 p
- /* Type definitions ----------------------------------------------------------*/
- ^% m/ ?. I1 F5 U2 o! M* X4 Y - /* Variable declarations -----------------------------------------------------*/; R f% U1 q* G9 V8 t: j
- /* Variable definitions ------------------------------------------------------*/) r9 A+ b8 o( X2 Y
- /* Function declarations -----------------------------------------------------*/( o) ~* D' I1 P
- static void PowerDownProtect(PVD_Output output);5 z6 r; E1 n: H9 u
- 0 C- h% X3 Y3 f" k9 u7 D3 H+ E5 l
- /* Function definitions ------------------------------------------------------*/! F4 w, J1 y; y; C* V
) e o3 r+ c# h6 H, z; o- /**7 h. _+ R+ N0 M8 U9 I3 H. I7 i$ {
- * @brief Main program.
2 J [/ W! e/ \; w( W - * @param None./ | P( p! E5 U' d
- * @return None." W* \' [1 f. L) J. l1 R$ [
- */
. ?, E. i" |; _ - int main(void)
* F- w+ a* N& ?& p2 Z }0 b% { - {6 w! F6 A; I8 h; P# Y5 Q' ^2 u; g
- LED_SetStatus(LED_Pin1, LED_On);
& w2 @$ d9 }& u! G/ i5 O' R# B7 V - LED_SetStatus(LED_Pin2, LED_Off);# l Y) t- o! P
- 7 f% w! w) [: \# G
- PVD_Init(PVD_Level_2V5, PowerDownProtect);4 ]3 R, j; n: S1 J! d
-
* O3 S6 o w2 e - for(;;). n. Z/ K) l7 J6 `+ r) [$ {
- {
2 o/ c; L1 O! S9 V1 n! @ -
/ D0 Q: {- v- M7 k3 N - }
3 _* x( ]$ Q2 w" N* w - }
3 n7 j+ V/ o0 {( H. l: D
& f, V( w" b7 U4 j! I- /**& v5 r+ N2 a8 W. |+ h: i; y$ v
- * @brief Power voltage detector callback.
/ z- _9 x. B6 [# { - * @param [in] output: Power voltage detector output.
$ x& F! Y. m7 A k' v' Z7 g a' m - * @return None.
. G; m3 A; r6 ]3 {% d! W1 | - */! N* u/ i; ^, h0 h
- static void PowerDownProtect(PVD_Output output)
3 Y9 W+ S( P$ P3 d0 s2 W9 H8 Y* c - {2 u0 j5 x9 m- W4 h/ d' K q3 F
- if(output == PVD_Output_High)4 e2 V: B9 M! W
- {
, I5 W4 Z- W! w$ |4 n - LED_SetStatus(LED_Pin1, LED_On);
6 |* I7 s: K/ `% f - LED_SetStatus(LED_Pin2, LED_Off);- p1 h2 J3 N) C/ b
- }
D: n* W# }8 o) o: X3 C) D - else, h' b. _% i( @& z
- {( t$ f) t2 q5 c+ K: s) K5 G1 @) E9 Y
- LED_SetStatus(LED_Pin1, LED_Off);
, `9 e: _/ p& a' T9 t# Z: w - LED_SetStatus(LED_Pin2, LED_On);
$ @8 T- O) P: U - }) A" {0 U& |- \3 @
- }! j* m5 v, i0 E
复制代码 9 w5 K7 `5 y9 h* t
3 a3 {3 z, k1 d K& ~; r) a* d4 _1 ~) v1 M
; V r8 L" r; { b7 [
2 _4 g: ~6 }0 g! j% p |