STM32F4(LED) GitHub仓库:http://github.com/XinLiGitHub/STM32F4xx_LED_Example( t# ^9 m/ x% D& ?
PS:博文不再更新,后续更新会在GitHub仓库进行。
' y" u @1 ?0 r
# N/ O% F$ c( G1 k+ Y7 t" N
; O7 ~% ~9 J7 k4 c/ H: H* | 在实际的项目开发过程中,常常会遇到硬件电路的修改,然后修改的部分就需要修改驱动程序。想这样需求该来该去是程序员们最烦闷的事情(重复劳动痛不欲生啊~)。为了避免或减少重复劳动,就需要在程序的架构上下功夫。接下来以最常见的LED驱动程序为例,进行程序结构设计。; s6 f6 c2 w* F, d3 ~, ~
6 E# n& Y% ` n8 u8 K5 `9 W I1,开发环境
, k2 r7 C- V0 U( s3 J# h0 U8 j1 P0 V2 S& s& b4 R
1,固件库:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0
3 y! ^: M3 U: p1 R! D& f5 ?* ~ 2,编译器:ARMCC V5.06
& G/ p5 t1 B1 K! m: \ 3,IDE:Keil uVision5
" x% S" C I' T+ B 4,操作系统:Windows 10 专业版
* w+ M* G' ^& m1 V$ ]
' b% K4 e4 [( P. U" |( O. }3 K
9 I! n4 [' f) m2 Y1 f4 N2,程序源码7 E2 v* a# h6 K" j; \6 ?
LED.h文件
- @3 A# z& `5 o$ @$ s3 ?- /**# d0 g* n& K" d# B
- ******************************************************************************. x2 ]8 H3 \& H- I6 q
- * @file LED.h2 y/ U8 }8 \& m3 _- w( Z
- * @author XinLi- J$ }7 u+ z N( l3 A0 w/ y t! N# }
- * @version v1.0
o; h( g4 R' D- }$ Y - * @date 24-October-2017: z, y# e1 c( {2 f! r p
- * @brief Header file for LED.c module.$ ~# l% T0 d, `$ e2 d
- ******************************************************************************+ x" }/ ~! `8 K1 Q+ ^5 {
- * @attention
9 t7 x1 r5 E5 }2 C/ O - *
! t; ?1 \9 i q& p9 t - * <h2><center>Copyright © 2017 XinLi</center></h2>+ ^: H: w$ k/ L, }* R
- *
) b3 E. U* x% A4 ^: B% ~ - * This program is free software: you can redistribute it and/or modify3 W: f( p h7 c9 |
- * it under the terms of the GNU General Public License as published by, _" y+ M1 D8 ^: p& m9 X/ _* B) t) a
- * the Free Software Foundation, either version 3 of the License, or
! D* u1 B/ e, V% C E - * (at your option) any later version., A- u, {$ q( u8 {/ U7 ?
- *
4 f- i( w3 {& F: z# i: X$ g; y - * This program is distributed in the hope that it will be useful,3 H. _% c1 g7 \
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
) D/ U, }, J6 q O0 M; W% @ - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the: H8 r: g$ E/ L7 n
- * GNU General Public License for more details.3 L, P# u& X6 A$ r P1 v- M
- * m1 e) A1 q: C1 n" G1 C& ^6 D2 ?
- * You should have received a copy of the GNU General Public License% [) @: S' ?+ L2 y2 F+ \2 N
- * along with this program. If not, see <http://www.gnu.org/licenses/>.5 _/ [' ]8 @& h2 I; O% V
- *
- p1 p# U* x, B3 Y - ******************************************************************************% N0 K$ r f& l* R! W% @% p
- */
9 J0 Z/ ?- t% y - ; {0 i' V- p/ q, B8 P4 _: H7 o2 U7 r- K
- #ifndef __LED_H
' ^( M3 W+ [6 j8 _ - #define __LED_H/ Q$ y! ^3 o; ]2 n* P( b5 C
- % ^8 V, p) t6 t& n
- #ifdef __cplusplus
8 d/ r+ J9 R2 |+ \ - extern "C" {5 q: r4 E$ T- V5 l7 U( R
- #endif" y( F+ I7 p& R- c' F
- ?$ i+ Q# o+ i8 ]
- /* Header includes -----------------------------------------------------------*/; R; Y6 j( @$ |/ y# _, B
- #include "stm32f4xx.h"& N$ I# O* L& Y# k
9 O D3 y7 ?4 s; Q) Z# X* {- /* Macro definitions ---------------------------------------------------------*/9 o) o" H: d7 Q, {" V5 ^
- #define LEDn (4)
0 g; r3 m4 C* J% x8 j% J/ U$ U
( s& w. U8 |5 q7 B: L* V- #define LED1_RCC_AHB1Periph_GPIO RCC_AHB1Periph_GPIOC
n9 W' K& q+ T0 ` - #define LED1_GPIO GPIOC8 J3 L# f: H2 M& k# g$ X) R* q+ z
- #define LED1_GPIO_Pin GPIO_Pin_05 x8 [# Y2 ?5 F, i3 p
+ B4 K% u$ G3 m, z' [" _- #define LED2_RCC_AHB1Periph_GPIO RCC_AHB1Periph_GPIOC
2 A7 z: {/ k' e0 p. Z$ D% H - #define LED2_GPIO GPIOC+ P( d: U2 `* m2 T9 f* N# d
- #define LED2_GPIO_Pin GPIO_Pin_18 d8 a7 @% k F4 C. e
5 d2 \( |% C" ?% r! {0 n' K. I0 \- #define LED3_RCC_AHB1Periph_GPIO RCC_AHB1Periph_GPIOB9 m. p; x$ E4 m2 {
- #define LED3_GPIO GPIOB$ L$ p; R0 J/ q1 ]: J% @& \
- #define LED3_GPIO_Pin GPIO_Pin_3
7 h& y( n3 E. I4 V
7 U( c$ t4 }3 M; r+ r L8 \- #define LED4_RCC_AHB1Periph_GPIO RCC_AHB1Periph_GPIOB7 [# Q$ [* J* m7 P
- #define LED4_GPIO GPIOB
8 e8 u7 F& e i: b5 L - #define LED4_GPIO_Pin GPIO_Pin_49 r( K9 Z6 S) O
- ; P" P. Z. y! s# T9 M
- /* Type definitions ----------------------------------------------------------*/
' j% @. h3 I6 b - typedef enum. @) `, a* h3 e9 q& @# R
- {: {) H( u# Q2 _+ V/ m2 `
- LED_Pin1 = 0,
1 _/ T0 U$ s% S( f2 Y2 j. Y1 N' {' m - LED_Pin2 = 1,; [$ e' c8 ~; A; m% r" ?& W) }+ V: [
- LED_Pin3 = 2,
) G7 z# X% c8 E4 k3 v - LED_Pin4 = 3
7 @, i: r; K, }4 A( j) n% Q - }LED_Pin;1 \0 }- c: Y3 Q: A' I1 A1 \
- 0 A2 {& j) t" H* g
- typedef enum
6 u$ Y8 [. C- l K - {: ]$ W, a9 E3 O. P- V
- LED_Off = 0,+ ]5 W8 R% h5 x/ o( T, ~
- LED_On = 1% e3 X( h3 O7 H
- }LED_Status;0 h' t5 C$ ^0 d9 P( R/ _5 V: ~9 |
% z* s( f( ?5 V/ z$ @; X- /* Variable declarations -----------------------------------------------------*/8 P" x/ w, ^+ x) Q3 F z4 u
- /* Variable definitions ------------------------------------------------------*/* e2 |5 e* |6 K* N; l( _
- /* Function declarations -----------------------------------------------------*/
. ^; ?3 D& Q) `9 w - void LED_SetStatus(LED_Pin pin, LED_Status status);
0 I3 E0 r/ Q! `" a4 i* O H - LED_Status LED_GetStatus(LED_Pin pin);
; {% K* R! m8 i+ F$ Q3 } - 0 `8 X5 u: D6 ]2 T3 d: I. c7 j
- /* Function definitions ------------------------------------------------------*/9 N f4 Z1 q1 u- \
- + U% ^( T! r F
- #ifdef __cplusplus
) A- R$ F7 ?! A" ]1 k - }0 t# l6 A. E! k, I4 t5 Q& M
- #endif) \$ ~' k3 Y2 }3 X' Y
- " q a9 Y I G; y H
- #endif /* __LED_H */+ W) L M: U( g, {* |
复制代码
% f( E8 U1 l, p; N {2 t" Q LED.c文件1 C! Y6 t- |! k j- V9 ]
- /**
( y4 O5 w; L, E! m - ******************************************************************************5 y& `) `0 ?0 B
- * @file LED.c
2 X1 y4 U- b, c( I: B/ n! f - * @author XinLi: y+ z+ G" J4 f3 m
- * @version v1.05 l) |. ^8 _4 V) G9 j' T
- * @date 24-October-2017# `6 t2 u! A* ^! B4 [# N" C
- * @brief LED module driver.
: r a$ \" q/ z5 c - ******************************************************************************. C' p5 P' W$ p$ u* @- [: e
- * @attention8 A/ a6 q0 S/ H1 |; a
- *
$ m7 M# w1 u: f! [/ [: I# q: b - * <h2><center>Copyright © 2017 XinLi</center></h2>% f' C# N3 [9 g* x6 W
- *1 u: |$ }* m% K
- * This program is free software: you can redistribute it and/or modify. a$ ~9 F! x8 d1 K9 F# h- \0 D" [
- * it under the terms of the GNU General Public License as published by
) ?! q1 h1 s3 Q U8 V; b) _ - * the Free Software Foundation, either version 3 of the License, or
. P2 L* C: c8 s6 ]( y) d7 ^$ d - * (at your option) any later version./ ~4 F: Z3 u0 q4 X, c: V
- *
: `; {& `7 s- z# i - * This program is distributed in the hope that it will be useful,
- ]3 o" V' g2 R' i% X( w* C9 X2 L - * but WITHOUT ANY WARRANTY; without even the implied warranty of
- n K, q9 C3 ]& M6 D - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5 n2 B1 W: P3 H1 ^4 Z' p) ` - * GNU General Public License for more details.: S+ y" l0 @6 Q w: G, W5 Y
- *
! h- p1 ?0 ^; z- T - * You should have received a copy of the GNU General Public License3 E5 \+ ~& z! w$ H A
- * along with this program. If not, see <http://www.gnu.org/licenses/>.2 y# c& i9 c' u( J3 P' D! g
- *
" P {4 N' ~/ L Q - ******************************************************************************
p8 t( ~0 C+ x - */( Z; E! S8 r0 y- a, S
5 k6 n: _- U" a. ~9 c$ y& n- /* Header includes -----------------------------------------------------------*/+ O. h% O% u+ @
- #include "LED.h") W' j8 z0 y7 E v
- #include <stdbool.h>
( y/ N- [3 \8 ~; B7 s - 9 \* K' f( n/ V5 `' e6 P( P
- /* Macro definitions ---------------------------------------------------------*/
; Z+ n7 V, `7 v3 Q, l7 u - /* Type definitions ----------------------------------------------------------*/
( T6 n2 R- I# J/ V+ a - /* Variable declarations -----------------------------------------------------*/
2 h' h6 p2 w) c' n - /* Variable definitions ------------------------------------------------------*/4 X$ p, c0 B5 z/ K: s0 }
- static GPIO_TypeDef *LED_GPIO[LEDn] = {LED1_GPIO, LED2_GPIO, LED3_GPIO, LED4_GPIO};
: B$ X0 G% D/ \* {4 { - static uint16_t LED_GPIO_Pin[LEDn] = {LED1_GPIO_Pin, LED2_GPIO_Pin, LED3_GPIO_Pin, LED4_GPIO_Pin};
; J+ n4 x1 ]9 ` - static uint32_t LED_RCC_AHB1Periph_GPIO[LEDn] = {LED1_RCC_AHB1Periph_GPIO, LED2_RCC_AHB1Periph_GPIO, LED3_RCC_AHB1Periph_GPIO, LED4_RCC_AHB1Periph_GPIO};
% k) [- a0 `1 K1 n: X7 v - static __IO LED_Status ledStatus[LEDn] = {LED_Off, LED_Off, LED_Off, LED_Off};- x* V) R* R2 O6 \) }( \* g7 n
- * N2 E+ T5 i* C
- /* Function declarations -----------------------------------------------------*/
% q I/ m4 G2 a% U% G/ x - static void LED_Init(void);# U1 X# Z n3 r& m7 |$ Z* E, D
- 6 P8 D; M9 M% I" D( H& t
- /* Function definitions ------------------------------------------------------*/
Q* I4 [, t* T, u& A' { - + D6 E. p- N, R, Y' T. [0 Z3 h
- /**
3 h1 V9 Q# w. a2 Q- s" N5 l8 w - * @brief Led initializes.6 w9 N8 v% L- V* O9 B/ }
- * @param None.9 Z2 M$ T7 S E) T# b U) L4 r
- * @return None.
7 L, E/ w' W/ D0 W6 c( a - */$ O* E8 Y) O; F, ]& R( Z2 }
- static void LED_Init(void)
. E: W* N9 T8 }& X. s2 q W - {
! G! [- U: h8 [ - static bool init_flag = false;$ s3 X: y, O4 M# M& D- B
- 4 B5 O/ y# ]8 p+ y; ^! v: R
- if(init_flag == false)
5 v6 U5 k' P3 }. D - {
0 n7 o, S" |( C8 ]0 @3 v - init_flag = true;
8 ?+ x' S+ u4 K% T7 Q -
( j: a; p6 _5 n - for(int i = 0; i < LEDn; i++)( K6 J/ O0 X8 S6 [ t, i
- {' ?! X% L. A% g# \3 ^! w6 R
- GPIO_InitTypeDef GPIO_InitStructure = {0};
1 H% M- ~1 Q. k( ?3 T - - ^9 Z; K# x W: o! x# ^. v: b& C
- RCC_AHB1PeriphClockCmd(LED_RCC_AHB1Periph_GPIO[i], ENABLE);
" ?, ~( Y" I: w. E/ W -
' k1 l/ _. T- [3 n8 ~" l2 }2 [ - GPIO_InitStructure.GPIO_Pin = LED_GPIO_Pin[i];
4 D' O, f$ M# Y - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
% z6 L7 u' `. V7 l* g& G! J - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;4 D1 G: h( r! i# W/ R3 a3 l& E
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;5 t: ]/ C5 N, V# e; K
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
/ t6 J2 s3 ]# z. T6 c" Z, [ - GPIO_Init(LED_GPIO[i], &GPIO_InitStructure);, s3 X5 k B: \: f5 f, q
-
" n' a; ~, |3 D# _- K8 l5 } - GPIO_WriteBit(LED_GPIO[i], LED_GPIO_Pin[i], (BitAction)LED_Off);
( u' \5 J! L; X6 I: C - }
: B& w& Q1 T8 r7 d0 x) ]" f; {# s - }+ v7 ]2 O5 a9 ~# D
- }/ H X2 h" j6 i8 @3 [$ Z8 u
- 2 L' ]0 S3 u' r; n3 i; j a' o
- /**: d; |/ B1 C' m5 X7 e2 `4 o
- * @brief Set led status.
9 }+ w V- }/ w; q b+ l - * @param [in] pin: That led.
7 i$ p* }6 p9 D/ X- h% K1 } - * @param [in] status: Led status.; h6 s8 ^! o6 X8 C" ~
- * @return None.2 |8 u7 Y% V* L. |& v
- */
: c& R g, L1 P9 x - void LED_SetStatus(LED_Pin pin, LED_Status status)5 Q" O: |# t0 ?, B0 v% _
- {
4 Q$ X+ k7 }, Y* E7 |) v - if(status != ledStatus[pin])
9 [1 f6 i5 [' q! D E - {1 b4 F; r) l' F# q& S# V
- ledStatus[pin] = status;4 m+ \7 @3 h' ^8 _; D
- 5 v. T+ X" D7 g" n _; G9 [
- LED_Init();( t$ a0 U5 Z- E; w# X
- # z% I) N) h% f- H$ u
- GPIO_WriteBit(LED_GPIO[pin], LED_GPIO_Pin[pin], (BitAction)status);. v, K* }, `5 M1 k5 u
- }
; ]$ n; x1 }% P' B( F - }1 g- O/ [$ W2 d- k
6 U& z- x9 F+ p/ e( ~. ?- w" H, h+ F- /**
% x. j6 d2 n1 G" x - * @brief Get led status.5 I" U A5 C& A- L* I4 M
- * @param [in] pin: That led.1 K" L$ e, e( X0 o6 L
- * @return Led status.
! M4 }$ |3 c% ^; h0 w& k - */
4 \. [+ n( _" Q5 j - LED_Status LED_GetStatus(LED_Pin pin)( ]9 l) t# }+ K2 o7 N- J
- {, T3 M* S% P3 @' y
- return ledStatus[pin];2 a7 D" p2 P% Q
- }2 n8 [# O, X* Q- i5 Q( T& d
复制代码 0 ^5 h$ Y8 I! D0 J0 H
main.c文件+ V$ n' D0 g2 c# l* K* L$ _9 i
- /**- J$ ?4 k: o/ S' {! v& F) H
- ******************************************************************************) e8 @( |8 r, A2 C/ q; _9 N I$ N
- * @file main.c
0 P$ C" f& {- O1 }, g A1 c - * @author XinLi
! m2 r9 }# O0 K# T8 l4 F( m - * @version v1.0
9 |5 O# R( g! g6 O - * @date 24-October-2017$ O8 v) C% N) X W
- * @brief Main program body.5 u. Z1 B) Y* I1 Y5 @
- ******************************************************************************9 B- ^3 [- m6 X7 u Z
- * @attention
1 z8 r) }% z/ @# {: R - *. p0 p: k$ m: K2 t. w
- * <h2><center>Copyright © 2017 XinLi</center></h2>+ d9 z2 {) j' s
- *$ s; C* w4 x* {
- * This program is free software: you can redistribute it and/or modify6 L2 o, `0 g! X/ A. m s. e
- * it under the terms of the GNU General Public License as published by
- {& G: r R+ [ - * the Free Software Foundation, either version 3 of the License, or
( ]7 Z8 ~' z! H0 M+ y - * (at your option) any later version., k* |( C# ]8 n; z; _
- *( m' Y- y3 D# V: I/ W" S+ w
- * This program is distributed in the hope that it will be useful,/ A% N- v* w; {( W/ w
- * but WITHOUT ANY WARRANTY; without even the implied warranty of _4 e* {% {, ?
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the& w6 b# n. y5 j1 `/ K8 n* u) g
- * GNU General Public License for more details.( P. c& t1 w e
- *! x* u8 ]9 X( r3 o
- * You should have received a copy of the GNU General Public License
" } @) C& E4 z - * along with this program. If not, see <http://www.gnu.org/licenses/>.
7 |9 Z( r2 w ]0 O: \; m1 h% w - *
0 Q$ L) L0 M( g( C% @: f - ******************************************************************************
" Q6 E; u& ^6 n& S2 {- F - */5 q% L) c, t S4 u
- 8 J. j( y5 v4 p, K' }
- /* Header includes -----------------------------------------------------------*/! k# W6 q2 o. F: Q! M9 I6 K
- #include "main.h"+ J0 H( U( u5 U6 H0 Z
- #include "Delay.h"
' j8 u9 C; _! v( C3 E: g: t - #include "LED.h"/ {2 @4 G0 w: N+ C ~ D: Q( P0 a
" p$ X/ U( x! g( N- /* Macro definitions ---------------------------------------------------------*/
) ^7 o( |: T* O2 e% V - /* Type definitions ----------------------------------------------------------*/
8 d7 J& E+ t7 T+ a3 o) L - /* Variable declarations -----------------------------------------------------*/2 {* U( Z2 X% _6 p
- /* Variable definitions ------------------------------------------------------*/4 M5 i7 h/ ^) v
- /* Function declarations -----------------------------------------------------*/* N3 @. f" Y2 v9 {, b3 e8 b2 m
- /* Function definitions ------------------------------------------------------*/
8 R! D4 F, g7 Y; O# o
6 d6 x* I1 O: p- /**% d1 T0 a7 b! z" y R. I
- * @brief Main program.: z) K, }4 ]( d! E0 i
- * @param None.) C0 D& b5 D3 d/ u) e. ?9 C
- * @return None.
! ~# T8 L$ R1 f - */
, @6 m# q+ b7 G* D/ ?4 ?# F* b - int main(void)! O% |* n5 m: F J% ^
- {
# g4 j# O9 O4 a5 g - for(;;)2 n' }& J* w8 V, w4 N
- {. b% I3 Y8 b* l: J. T; Q
- Delay_ms(500);/ N9 ~+ Y5 g1 F7 `
- LED_SetStatus(LED_Pin1, LED_On);/ W _; l0 \1 ]5 B5 Z( W. L
- Delay_ms(500);! y7 _5 c$ j3 ?% E) l
- LED_SetStatus(LED_Pin2, LED_On);
! a! r% F6 g) U4 V4 V - Delay_ms(500);
; B% Q& W" t5 u, ?' J% Y1 ^ - LED_SetStatus(LED_Pin1, LED_Off); k( e! `8 ]7 D
- Delay_ms(500);
% i' e/ {: \% Q) T* w - LED_SetStatus(LED_Pin2, LED_Off);) Y8 O3 Y# [" o |% N
- }
0 s2 k' @5 H+ E - }& U- S( [% O: ?7 p! a& t. K& B
复制代码 4 `6 c& z i( P3 X& U# g. u/ |: s
3,注意
. p2 e9 A E' s 修改接口需要注意,LED的驱动方式和增减LED的个数。对应修改LED_Status枚举和源文件的变量定义。
- R+ D! Q+ W- v9 R3 E9 f5 Z3 U' g' n* ?7 f' H
|