|
STM32F4(PVD) GitHub 仓库:http://github.com/XinLiGH/STM32F4xx_PVD_Example4 [" h6 T2 d' _7 o* c# R4 o
PS:博文不再更新,后续更新会在 GitHub 仓库进行。
7 z& U/ u0 r1 u# z1 p, p! u- {* w; Y8 V! h% _+ u J
在实际的产品需求中,常常需要产品在断电时保存一些参数或做一些断电保护,这就需要 MCU 能够检测到断电的过程,STM32 片上有电源电压检测器(PVD)给工程师的开发提供了方便,可以比较简单的检测到产品断电的过程。' N$ w9 o. r. d- s$ D6 c7 L/ q
+ m% Z* _' A$ j8 V/ E: f U
1 D) G7 @ f, E" \
1,开发环境
7 _3 L* W" ^& G# p, ]) W# ^ F' j+ i
. Z7 N/ L4 h% R4 M& r* C; J 1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
2 R. z) u, ?2 A( h" M 2,编译器:ARMCC V5.06
* G& N" v( s: b7 f! c. Y- A 3,IDE:Keil uVision5
0 l2 N2 i# g# y( u0 ?0 m# E! { 4,操作系统:Windows 10 专业版) Q" m' F8 b9 U0 r( y) X4 U0 P5 Y
5 b2 M' ~+ e3 s4 C4 Q2,程序源码% ~6 { Z' W8 F) ~: e
PVD.h 文件4 I9 H) O2 {0 k
- /**
8 D8 G k. A, ^: J3 O) t - ******************************************************************************- P. u- Q$ U. i( J% ~/ G' n
- * @file PVD.h
\( v( u F r' @ - * @author XinLi
# \! }5 H( Y' ]$ ]9 h k - * @version v1.06 T: T1 g- \6 P6 W2 D4 P
- * @date 24-October-2017% B. U; Q; N/ k
- * @brief Header file for PVD.c module.
2 n4 d! s" O2 ]! K9 r - ******************************************************************************( r4 b5 R! s# N$ {
- * @attention' L0 a2 }& L# F- A( S
- *
& I- J3 b' H& t2 p, P0 }* S - * <h2><center>Copyright © 2017 XinLi</center></h2>
* x- Z' S6 E B& D. _5 L9 u - *
8 f" L5 b8 J2 b8 Q9 I - * This program is free software: you can redistribute it and/or modify% o4 t3 S6 u: l8 ^; T8 a- K
- * it under the terms of the GNU General Public License as published by- |% z. H, D% @0 O
- * the Free Software Foundation, either version 3 of the License, or3 j- q% `' W) M* c% z0 B% w
- * (at your option) any later version.
. i% T; v, t+ c I/ y - *8 D9 h# u9 a7 J7 L0 Z
- * This program is distributed in the hope that it will be useful,
6 j' }4 D6 K7 j) r2 Q* H - * but WITHOUT ANY WARRANTY; without even the implied warranty of. `, c3 m0 ]5 S- U- X7 L6 j5 ~
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the2 @: d- f8 R$ }+ W
- * GNU General Public License for more details.% i' f; i$ b1 ?" }: Z9 V/ I
- *
2 D. `1 T* `4 X. X( |( x5 F6 p+ _( R5 R7 f - * You should have received a copy of the GNU General Public License2 n; T" M; k( ` [# Y& q
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
" u. \' @) N Y( p$ G1 Y - *
: C* `2 @+ t) Y8 y6 m - ******************************************************************************
' ^! _# N! u8 d" @$ P/ E) C - */. Y W: h$ I f" l5 u: x
8 d, j; R: |' p& |" ^- #ifndef __PVD_H, D! N$ f; x6 F* G* p n
- #define __PVD_H
' _" ]) E( E. i& x4 W
/ ~1 L7 H, Q0 @) r1 }) ^& [- #ifdef __cplusplus
9 I4 d" X7 p8 f. R0 U - extern "C" {
) R1 u7 c. S, B: M - #endif
/ Q7 a' E ~- Y* E1 e( R) H3 T j& a - 8 Z) d* \. D. |+ N/ p* B* L" N. ~0 \
- /* Header includes -----------------------------------------------------------*/
6 e) p5 ~0 `6 T - #include "stm32f4xx.h"3 O# [3 a" q5 N+ T! t
; U6 V8 k5 E: M+ }& l3 @4 ]" m0 L. x- /* Macro definitions ---------------------------------------------------------*/) ~& B$ R5 P7 ?) n
- #define PVD_IRQ_PreemptionPriority (0)
4 ^8 ?8 f& E3 O1 h - #define PVD_IRQ_SubPriority (0)" U8 Y$ ?4 F1 {* m3 H ~
- t" e3 k: h" \8 @) y# o2 X- /* Type definitions ----------------------------------------------------------*/, F8 O% P8 Z/ d) {: c' ~+ X
- typedef enum t6 m" n% r f3 U. k
- {" Y9 W* j3 [3 P
- PVD_Level_2V0 = PWR_PVDLevel_0,$ ?. z0 F! V! P# M7 f2 H
- PVD_Level_2V1 = PWR_PVDLevel_1,
/ u7 [. D: p/ E - PVD_Level_2V3 = PWR_PVDLevel_2,2 K1 m4 V! k _ d
- PVD_Level_2V5 = PWR_PVDLevel_3,
+ q. G8 }6 c7 X: A# [1 W' J4 c5 e - PVD_Level_2V6 = PWR_PVDLevel_4,
3 C- @' \- N2 z& R& \4 g5 y - PVD_Level_2V7 = PWR_PVDLevel_5,
1 c, t' @" a8 {- N4 j' d - PVD_Level_2V8 = PWR_PVDLevel_6,3 N3 C7 M0 G( u5 T! g0 Y/ w& x
- PVD_Level_2V9 = PWR_PVDLevel_7( C6 \) ?7 s5 q9 W9 I' b% T
- }PVD_Level;0 H* j. s' a3 M6 m( Q
- ) m: ?; W- b U
- typedef enum
8 [0 S' a- h! l4 R8 \ - {: m8 e3 `! E$ \* d
- PVD_Output_High = 0,
4 R( }: }5 V [" D% R - PVD_Output_Low = 1- q1 D- O4 \$ s7 P4 H' o3 e r0 Q+ s. b
- }PVD_Output;9 F; o0 a+ |3 O9 g
- 1 O) U$ x) R, ~/ \7 |9 H( v$ C
- typedef void (*PVD_Callback)(PVD_Output output);
" J; }5 f) w l4 _- I - / U: F6 p R. R$ K
- /* Variable declarations -----------------------------------------------------*/( K( i3 @0 `4 f' c% b3 N
- /* Variable definitions ------------------------------------------------------*/' y8 a7 V, N9 c- c0 @
- /* Function declarations -----------------------------------------------------*/
" i0 M) ^/ U& d6 c8 w8 D. w; Q - void PVD_Init(PVD_Level level, PVD_Callback function);
; ?# j0 A0 Y6 {1 ]- b- s - void PVD_DeInit(void);
D3 r0 H2 ^# o' `. C
$ }9 j% U. b- b3 V k% }2 I" V) D! h- void PVD_SetLevel(PVD_Level level);
% E1 |6 N! _$ f - PVD_Level PVD_GetLevel(void);
$ g4 r2 S8 J2 U! _# H1 l0 _ - 7 ~ ?' u8 S# O9 @! P/ }
- void PVD_SetCallback(PVD_Callback function);3 U2 d i5 p3 o. y% l- N
- PVD_Callback PVD_GetCallback(void);9 k$ s$ N2 A6 _9 `, A' _
' l0 B& v5 F( q0 d# z- PVD_Output PVD_GetOutput(void);* w/ b# C) I7 y+ n$ }
- 2 A, [; x+ Z8 c8 v1 e
- /* Function definitions ------------------------------------------------------*/8 _: M' b% {7 q! X& d$ V2 u
- " e, u" m" T2 }( Y7 H, k* N
- #ifdef __cplusplus
2 p: S" M) o1 \! b5 J* c4 Y - }! |6 V2 b D, T" x j# x P# Z
- #endif
9 K& g( r0 K8 k+ f" s" ^
) n% `! h# t, q4 W6 Q- #endif /* __PVD_H */
/ K, P* W( {4 K! L: c/ M+ F3 }
复制代码
D, W3 u% g0 y& h8 X9 l PVD.c 文件 V9 [2 k% u7 e) }# |
- /**
( M- D3 L1 q, L, e - ******************************************************************************, a! r! p" j6 v- a
- * @file PVD.c
( i: h7 O5 U: m; r5 E @ - * @author XinLi" p0 G; O5 ^6 I* R- A
- * @version v1.02 t3 V0 v8 Y( y$ x' e3 ^: Q
- * @date 24-October-2017
$ {! d( U( k7 V* ~' U - * @brief Power voltage detector driver.
& l+ H" L5 a5 Y - ******************************************************************************5 r2 Z6 M7 N8 j8 I. {# {& F
- * @attention
4 g2 W5 T! N5 e - *
$ |9 |( q% e1 u - * <h2><center>Copyright © 2017 XinLi</center></h2>
6 \" j' O, Z3 X+ [7 ?* u - *9 H2 J2 U5 s% v5 a% U D; o
- * This program is free software: you can redistribute it and/or modify! a0 W" M! _4 P) ]
- * it under the terms of the GNU General Public License as published by1 G4 v0 A( d5 L
- * the Free Software Foundation, either version 3 of the License, or" D5 B7 v1 w9 }. ]' O$ ~, `; S; c
- * (at your option) any later version.9 N: B; O; B3 J) S% d# e5 K
- *. _/ d1 ]* n- y6 c7 {4 u2 w
- * This program is distributed in the hope that it will be useful,
# R0 V4 o* m1 |$ I) w9 [& [7 D2 T - * but WITHOUT ANY WARRANTY; without even the implied warranty of& T0 A# N+ n, k9 ]
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
z7 D5 M; t: h% ^2 g - * GNU General Public License for more details.
Q8 M- [% ?. H# p - *
; u% l) G# Y( t: W, K9 V - * You should have received a copy of the GNU General Public License
# U% _# k) g4 Z9 K+ S - * along with this program. If not, see <http://www.gnu.org/licenses/>.
% A- ^7 Q, V8 s9 Y- T6 x - *
* F, D1 [; O8 X - ******************************************************************************2 H$ H5 D3 O2 m7 @+ n* z/ U
- */
6 N. f* W: X) E+ T* o: J6 l; u W
1 B6 _& i6 j _, R$ g D& Q- /* Header includes -----------------------------------------------------------*/
- z- K- d5 N+ M' U4 N$ [( N - #include "PVD.h"4 Z# d, R. x( J% p8 O' d& W
- #include <string.h># E5 U) J2 C9 ~- v7 a3 `
% ?; s7 F0 U) _ L- /* Macro definitions ---------------------------------------------------------*/; J* D1 Z2 K0 h% D
- /* Type definitions ----------------------------------------------------------*/4 P/ q5 n5 f4 u0 r
- /* Variable declarations -----------------------------------------------------*/
5 p6 D" J7 \0 M6 D4 X6 Y - /* Variable definitions ------------------------------------------------------*/
. g: r9 i F* O- M' ^ - static __IO PVD_Callback callback = NULL;) b# n" T1 u9 i& g- M( K0 T
- - ?" H& L: }& q+ U7 l0 I
- /* Function declarations -----------------------------------------------------*/
! h3 ]/ j, B/ A - /* Function definitions ------------------------------------------------------*/7 [: |$ s: |, K# |% `
3 X" ~. @& K; d3 N1 [- /**
5 ]$ u: m4 N2 d* l! Y1 z - * @brief Power voltage detector initialize.
2 f( L) X- G% Z1 t - * @param [in] level: Power voltage detector level.* e& E( I! F. ?0 R% [/ m
- * @param [in] function: Power voltage detector callback.
) Q3 q- {- G3 V4 B- V7 b/ f1 m7 { - * @return None.- y5 k: ~! m) C- h8 z. M- ]$ |
- */: F9 N( s( a2 y* r0 c% ^# l
- void PVD_Init(PVD_Level level, PVD_Callback function)
# M) K( N0 }6 z5 c# ?. O - {% N$ W. ^0 a9 S6 V W8 ]
- EXTI_InitTypeDef EXTI_InitStructure = {0};3 u6 g2 A5 y6 |+ a: C$ w7 t' Z
- NVIC_InitTypeDef NVIC_InitStructure = {0};' ]4 F. c# U2 q
-
; H) {9 Z+ Y+ S, @( b3 M. l/ ]: y - RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
4 G8 t; T1 v% V1 P+ Y -
; s9 d& }. W9 Y' E T - EXTI_InitStructure.EXTI_Line = EXTI_Line16;9 e/ b) F! v/ J& q) L- P
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
+ N7 A# h- {, R, M( j - EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;/ J) o6 e: k4 |, e0 `1 o2 i
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;! j* v; c2 @- t6 ]: j0 B
- EXTI_Init(&EXTI_InitStructure);0 E, w4 q$ K; J
- ( ~8 W/ e0 y0 w( S2 B" r' @
- NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
) s7 x! [/ n# K - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;
2 S8 H6 {" l4 z( \1 n. V' _ - NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;/ ^& v, \2 }0 o" }
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;7 S2 P8 C: w3 T' f! P! @2 A
- NVIC_Init(&NVIC_InitStructure);9 C1 @- b& q ]. p
-
$ T$ K! t( M, g f - PWR_PVDLevelConfig(level);( e: ]9 Z7 h3 b
- PWR_PVDCmd(ENABLE);/ I2 a" a4 r& k
- 9 j# V' l; W# b- Q4 G
- callback = function;5 h- F) t) G" j
- }
* m* L7 ?( A' t - 5 G h: _1 ?4 J( ?, V$ P
- /**2 v0 [' T! ?$ f5 k" n1 Y: E
- * @brief Power voltage detector deinitializes.
6 h$ F8 G2 h4 @, y/ f3 b1 s - * @param None.% t1 B# Q3 V" _- P( P5 M0 T7 ^) t
- * @return None.* |" `" H& a( v3 `
- */
: Q. X( [& E* v0 Z0 [9 W; L1 d ? - void PVD_DeInit(void)
3 L* o# @! Q8 H. G8 R3 i: O3 M - {
* E7 H; {& P4 [& m2 i1 b$ ? - EXTI_InitTypeDef EXTI_InitStructure = {0};
1 D8 q5 i% V) @/ D - NVIC_InitTypeDef NVIC_InitStructure = {0};
" l R4 V& K+ G: s -
) U! H7 {, [% u2 `1 o$ b - EXTI_InitStructure.EXTI_Line = EXTI_Line16;
2 H- p8 ^6 l& x& y0 C" @ - EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;" X2 [, J' t5 o( k3 c2 B+ X' Q9 S
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
" G7 D6 S! j6 p3 c& M8 V/ l4 }6 r - EXTI_InitStructure.EXTI_LineCmd = DISABLE;
) J0 i( r3 p5 O& d - EXTI_Init(&EXTI_InitStructure);
9 \4 B% O9 J e3 k! F: o - ) `; e7 M2 v! h0 F- T
- NVIC_InitStructure.NVIC_IRQChannel = PVD_IRQn;
5 j( |4 Y& d1 Z1 e - NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PVD_IRQ_PreemptionPriority;' h0 b4 C6 Q( \! ]9 f2 K
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = PVD_IRQ_SubPriority;! z; l3 l+ c' T! g6 W; y
- NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
0 X9 O# E0 P3 ?1 S/ ` - NVIC_Init(&NVIC_InitStructure);1 U) D- j4 [. I: n( \! [
- ' u8 T- q8 F# z, ?5 r* f# r
- PWR_PVDCmd(DISABLE);
9 J) s) A5 j8 E9 ?; J* u -
- v" u- l7 U' F$ G - callback = NULL;0 G( |) w7 Q- I& J& Q& I$ w
- }
! q$ y4 O% a5 v
* x7 Q* Z* y; V7 v+ W0 ^& G- /**
/ k9 h [, D! A$ O2 ?) W3 Z - * @brief Set power voltage detector level.
4 S5 E, c( b' Z - * @param [in] level: Power voltage detector level.
& `* \ [" P. x0 j( p - * @return None.
2 e, S0 M$ Z# I7 m. P* F' T/ W - */+ R4 H* K1 o# a7 G5 \
- void PVD_SetLevel(PVD_Level level)4 x9 M% C/ ^, M L2 S+ u% r+ ~
- {
4 i6 e. I e8 U( X1 E% @ - PWR_PVDLevelConfig(level);, G B& ?/ a9 F! }6 x
- }; ^% K# H4 Z: P
$ D% ?; f2 L: I! i% r2 }- /**
8 x# m' W3 _8 H - * @brief Get power voltage detector level.
5 C* Q3 B' X* F$ t - * @param None.
: y1 W3 Y3 L' |* B& k/ J$ h - * @return Power voltage detector level.3 b$ i: F9 ]0 c% V9 s
- */
( @5 `5 f O! t$ o" _' S: X - PVD_Level PVD_GetLevel(void)
+ W, [4 }/ B, Y; N0 a: @* `- u6 C - {$ ?! {/ N! y% W$ n( } x
- uint32_t tmpreg = PWR->CR;
8 v- f! n1 Z6 l# S2 v -
' [, B4 G0 Z7 ^) `9 M - return (PVD_Level)(tmpreg & 0xE0);
- ]4 L% e; u, Z; X - }/ a- F3 a# d; {$ T' K$ e
/ E$ i% P7 [+ W7 x, X$ ?0 ]- /**) u9 p( a1 m L% ~/ G; [* x
- * @brief Set power voltage detector callback.
- |- X' k$ W+ m& Y; \# Y - * @param [in] function: Power voltage detector callback.) s2 d; h* C& P) ^# A+ J) z& @( m
- * @return None.
g% a. O/ q$ U. O { - */0 k; {) W# I/ m+ f% d
- void PVD_SetCallback(PVD_Callback function)1 W1 j" b" m, c/ N1 Q" A( n
- {
8 o* ]% _( ]+ u; K! `0 _ [ - callback = function;+ q3 w) @$ {0 g7 W i
- }% ~* Q, i- _1 @- v! S( I
- . v6 t# h0 |% ~
- /**/ T" T$ |; o6 L9 v" B$ o# h! c
- * @brief Get power voltage detector callback.
2 t! n3 w( `4 F - * @param None.
" ?8 W% h7 n B - * @return Power voltage detector callback.- x2 [0 t8 W K% x/ S: Y8 ]/ U
- */
: j' L$ U/ [+ g7 q - PVD_Callback PVD_GetCallback(void)
. s/ o+ y: z9 ?3 l. o6 E - {4 y6 Q1 u4 J3 w% b( E" D" }2 i+ P
- return callback;: E; v }' Z4 c
- }
$ D& M' f0 y7 p7 k1 N
0 K& M, M; V. B" K: z( T- g- /**
" q$ L: b9 M. ]' [2 E/ ~, O - * @brief Get power voltage detector output.
2 E; l1 j: u8 L+ c7 U8 M - * @param None.8 s* }/ c! T" j- ]
- * @return Power voltage detector output.5 s. M* \4 |" E! h l$ i1 J
- */
+ t" c" w0 X/ w) q - PVD_Output PVD_GetOutput(void)
1 K L! Q! u) X2 Y) y! c - {! e) {. O3 a; c' D# M
- uint32_t tmpreg = PWR->CSR;% } [9 u, D8 `) N
- # B, G+ k/ ]$ g! E# M8 U. q) k5 Y
- return (PVD_Output)((tmpreg >> 2) & 0x01);! ]( Q8 k. \1 h; F/ w
- }/ I7 u, E3 E, y% z& N7 d) S
- . A7 T# H) @: I2 @8 h3 z! K7 d
- /**; p& c" N# B3 e2 Z# S
- * @brief This function handles the PVD Output interrupt request.
% j R' ]7 L& u% S8 U% ~! p, \ - * @param None.! T7 ]' v1 D3 z& }9 k7 {
- * @return None.
% Q$ u0 X" p0 V - */
' H- D. p) u2 Y - void PVD_IRQHandler(void)
$ _& h% y1 w. Z- J2 D3 I7 g: N, I - {8 s- U8 |# |5 I7 V" e( Y
- if(EXTI_GetITStatus(EXTI_Line16) != RESET)
3 r. g! V- Q; n% I* @) v - {
6 F6 _/ p7 Y' S. Y& M3 N - EXTI_ClearITPendingBit(EXTI_Line16);. s- y2 a; b/ T3 x0 B$ A
-
( ~# b z# L" s, M - if(callback != NULL)+ P, Y) [: ~# J4 W8 A8 k( N
- {% u6 L% n' t- y+ j
- callback(PVD_GetOutput());2 H& j8 q: b& B$ @" U, ?
- } Q- H% K7 y& |1 R c" N& g% n
- }
9 j* o: B! q, u7 O - }
% K! E" S+ M' {
复制代码 9 s! r2 y! Q5 }: \
9 \/ x8 K ^$ G" u main.c 文件
' L' `. ?& R+ `0 w1 e- /**
! s' D6 w1 W+ r - ******************************************************************************( L1 ]; R" Z5 W& M0 p t
- * @file main.c( L) r: f( V+ o3 i W6 p
- * @author XinLi
& m% Z* R: E" p U% C. _5 N5 |4 Y9 b - * @version v1.0
: @' t2 B" F ]1 a! F- h - * @date 24-October-20170 ^" j6 p& z. l% w8 A
- * @brief Main program body.( ]3 }1 n0 X# l& [' H; e1 c& @& `
- ******************************************************************************' a1 Z" F6 |, A* T
- * @attention3 z/ U T: p2 s# v7 F9 [! t
- *
' A$ ]& j2 N2 d* z6 ] - * <h2><center>Copyright © 2017 XinLi</center></h2>
* Z: m! @2 L$ r6 l - *& \! c8 g" I, c! w6 n0 t9 r6 t4 b
- * This program is free software: you can redistribute it and/or modify
8 e4 v6 n; B+ \; H1 _! G+ ~ - * it under the terms of the GNU General Public License as published by8 p+ y: z5 S8 g" m
- * the Free Software Foundation, either version 3 of the License, or
+ \( T6 \' D; B+ {1 c$ u8 k - * (at your option) any later version.
5 @0 z- a( Z% \/ C7 u# h - *# N% c* g) V4 \$ s3 c5 b( b
- * This program is distributed in the hope that it will be useful,; v3 c7 D+ l' K
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
& t, o3 v( ~. L. f9 v. Z# o; N" _, e - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
" N: {! v9 f0 V5 V" S - * GNU General Public License for more details. L/ B& B2 l A' o. G0 S
- *7 L4 t3 m' D+ T; e' _! q' P
- * You should have received a copy of the GNU General Public License v. J3 r$ G9 D
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
( M- v$ e6 B# R% { - *
: _/ B+ A" G- E+ J( b - ******************************************************************************' D, ^* b3 o8 e3 L8 ?! u' m
- */- Y- o2 d7 E2 c+ Z
- * O/ }" ~1 _; c' D2 u
- /* Header includes -----------------------------------------------------------*/8 Z* ?, k* p" r) N& K) p5 P
- #include "main.h"1 p0 `6 z" ^3 J Y
- #include "PVD.h"
$ y& y/ c. s; C0 p5 {7 I3 S% p - #include "LED.h"9 y+ A/ l/ X* ]
3 \% U" A+ A+ `* x; }" m9 W0 N- /* Macro definitions ---------------------------------------------------------*/
6 W. f: [1 x2 d9 u7 o4 D- u7 E - /* Type definitions ----------------------------------------------------------*/1 f3 w7 v' g5 R# h: g
- /* Variable declarations -----------------------------------------------------*/
9 g3 w( f/ j9 u - /* Variable definitions ------------------------------------------------------*/
9 r4 w* F6 Z. J" M" Q! V& T - /* Function declarations -----------------------------------------------------*/$ W. }5 \' f! M4 z2 v
- static void PowerDownProtect(PVD_Output output);
, A9 y( `& T& N* ^2 G2 `
6 p9 d& ^9 e+ Q4 L! W, O; f+ ?- /* Function definitions ------------------------------------------------------*/
, V! R7 a4 v' R* f6 j4 Q5 _
/ L* \/ O& T( H8 R4 m- /**" q8 C: y" s3 }% H4 Y
- * @brief Main program.
; h3 m& m* |7 R% X6 R8 y" Q/ H - * @param None.3 m6 E5 B& ~) U! l! h& U
- * @return None.
8 D/ A- P. }# H& f: A - */) z& z" ^7 w7 ~9 b" M
- int main(void)" a/ E Z, y6 o" c5 e8 U4 L
- {) J1 S9 U' d8 c6 V
- LED_SetStatus(LED_Pin1, LED_On);4 H, r" v T9 w+ H, f& J# S
- LED_SetStatus(LED_Pin2, LED_Off);
( Q2 |( ^/ n# z$ \% g8 K, C# k/ R4 z - 6 D5 X' ?& T2 @* t6 P5 l
- PVD_Init(PVD_Level_2V5, PowerDownProtect);
! R4 ~! m) _. H3 Z! f - ! D$ [/ i3 v8 Z1 V; A
- for(;;)
7 N/ N7 S7 m+ U/ K8 s! J' n8 A - {
$ d' q% Y4 i. q& h0 T3 ?, G" ^+ { -
' \3 ?3 M- l$ |: m l* Y - }9 U0 ]$ N e k+ L, \5 l& N \
- }
. i3 N4 n/ V; C+ D+ X& b - 5 u3 h6 n) U; J; ^/ S4 ^& M
- /**
2 A/ c8 A. W3 j y - * @brief Power voltage detector callback.
, j2 G$ t) {" _$ Y8 [ ?. ~( P3 C - * @param [in] output: Power voltage detector output.
1 n3 t9 U# f \ - * @return None.
5 V8 z+ t [6 q* x6 s - */# @' m- m; |& ^" f+ u1 O3 @+ i- ^
- static void PowerDownProtect(PVD_Output output)- o$ U% l$ v9 s) @# R$ \/ B6 ?6 L
- {
) a, B1 o& c$ N% H; ^6 a" H5 J0 p - if(output == PVD_Output_High), v) B- G4 O z0 D
- {
" l! Y: E# P7 a- ~) O; p/ e - LED_SetStatus(LED_Pin1, LED_On);7 P) z" z/ \" p( D
- LED_SetStatus(LED_Pin2, LED_Off);
# @: |- T6 Q& H5 n8 h - }
* O3 ~2 N* [ v4 ?) g& F - else
7 z7 b; D7 B: e: m/ E$ Y7 ~ V - {
3 q$ K! w0 }0 @( e0 S8 } - LED_SetStatus(LED_Pin1, LED_Off);
9 q, ~$ r& C( [' z/ W! } - LED_SetStatus(LED_Pin2, LED_On);
( Y! f! E7 _0 s - }
5 |* N4 ^) `' f. Y2 N: R0 F1 M8 r - }
& N3 k: F0 e! l* {$ l
复制代码 9 e: E# L, X. X
: Q+ h% b* y& w. {+ l9 q
: q/ g, s" r0 v9 X
) s }% C, A4 Z' z$ M+ J* Y \; g& Y, k# m
|