知识介绍工程文件放置 . I8 Z' D! g8 q% I" q1 _; l5 B
只有在添加进这一串之后,才能使用头文件集合 stm32f10x_conf.h ! y7 k1 X# `% L
$ G9 g6 ~4 ]3 n$ Q
选择类型 初始化时钟从哪找在stm32f10x_rcc.c中。
2 F6 S: u6 J, u! r; D
6 X/ V2 s8 |5 r" y4 z9 a1 S
断言函数
" _/ |$ e/ \+ m8 L$ l
此工程需要编写以下五个文档
?6 s- o& r8 E5 B y; l
LED.h - #ifndef __LED_H
, V+ D( r1 V* k; D) z2 V- B4 e, K `/ c - #define __LED_H
7 H$ X% X0 h) M/ D - #include "stm32f10x.h"- [6 U6 k7 ^/ O- A# U
- #define LED_B_GPIO_PIN GPIO_Pin_1
: `! K/ @' A6 y. ^ - #define LED_R_GPIO_PIN GPIO_Pin_5# j* @' k+ Y" f# q/ D5 H% \
- #define LED_GPIO_PORT GPIOB% [# `5 S/ t7 d
- #define LED_GPIO_CLK RCC_APB2Periph_GPIOB
! m: S: w! K, @" x - //反转& t e! E+ n5 x, ^- ` ?& i( k
- #define LED_R_TOGGLE {LED_GPIO_PORT->ODR ^= LED_R_GPIO_PIN;}//异或可以改变原来的状态* c! f. h. m- ]
- #define LED_B_TOGGLE {LED_GPIO_PORT->ODR ^= LED_B_GPIO_PIN;}//异或可以改变原来的状态, I* N6 J+ v8 w& S; B% g: g ^
- void LED_GPIO_Config(void);5 J# E s. i4 P& X8 i7 l; c j
- #endif /* __LED_H */
复制代码 ( [$ o6 K) j( X; M. |
LED.c - #include "YANG_LED.h" o9 J% F+ a* b* U/ b8 M6 }% ~
- void LED_GPIO_Config(void)
% m# @8 [# [2 ^+ S; ^2 A - {) M+ m% H+ y z- `* @
- /*定义3个GPIO_InitTypeDef 类型的结构体*/8 m: R& r: b) D# g
- GPIO_InitTypeDef GPIO_InitStruct1;& }. A7 A4 x0 l# }
- GPIO_InitTypeDef GPIO_InitStruct2;
. E2 Y* V. A& M0 q$ f" C$ W' } - /*开启 LED 相关的 GPIO 外设时钟*/
8 \% c4 _$ L U0 o - RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE);' B! k- X' n. g9 |
- GPIO_InitStruct1.GPIO_Pin = LED_B_GPIO_PIN;
0 _( {( K! u( ~ - GPIO_InitStruct2.GPIO_Pin = LED_R_GPIO_PIN;" _6 y& A/ S8 s" L9 c
- GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_Out_PP;
# m9 P* B+ H) \! y( j- W9 X0 [( R - GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz;
D! S& k2 y' e* B! G0 e" I3 ` - GPIO_InitStruct2.GPIO_Mode = GPIO_Mode_Out_PP;( D- E2 ^, G$ l1 u& l5 Y5 R; L
- GPIO_InitStruct2.GPIO_Speed = GPIO_Speed_50MHz;
" \. d; E6 @6 J1 o$ G* D O2 P5 F5 Y - GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct1); - c0 g7 c* N) s- U
- GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct2);/ d) o3 H: Z: ^- |* C8 \9 @( Q
- }
复制代码
/ K3 Z, T' H* O- s# q! W) Q4 M
7 N# q+ G/ k6 s% z! q }3 v% S. v2 I8 X R3 x8 ^! b
KEY.h% b5 Q, ?/ A! d% j; a" e$ K& }4 c
- #ifndef __KEY_H
. R1 t1 t* ^# T5 _$ O2 n: t8 u - #d#include "stm32f10x.h"
8 N+ ]8 t$ A" W7 W' e$ d - #define KEY_ON 1
0 e1 k* T; l* W1 P* A - #define KEY_OFF 03 G* ~# G! e; d# u
- #define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA! y0 N: ^8 {$ ^7 l$ L
- #define KEY1_GPIO_PORT GPIOA' D X5 H& [4 R
- #define KEY1_GPIO_PIN GPIO_Pin_0
* h+ w2 X3 q+ I7 [& m! R! L8 G - #define KEY2_GPIO_CLK RCC_APB2Periph_GPIOC
( b2 R6 b( L. i - #define KEY2_GPIO_PORT GPIOC5 a7 _1 o$ S- B7 {1 l: A, I( n/ g
- #define KEY2_GPIO_PIN GPIO_Pin_131 ~: a& J! a" b5 P) S" A% s
- void Key_GPIO_Config(void);
4 n% U& X6 M4 Q* G/ c, M - uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);) b! i8 U7 y, r1 [
- #endif /* __KEY_H */
复制代码
- N) O$ G9 y/ Z1 A H2 c: PKEY.c - #include "KEY.h"2 \) a: Y% U# ?% o* E7 w" V+ _% W
- void Key_GPIO_Config(void); |5 s+ r% B" ]* S+ K$ ~( c5 x# A
- {4 I4 S; d: h1 \; z8 R. H
- GPIO_InitTypeDef GPIO_InitStructure;
. D0 N: h' ^3 o4 t( ^+ T' J5 p - RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);
# e3 ^4 |# ?/ F - //定义第一个按键
3 U/ b" Q+ Y- E4 Y9 z - GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
7 j7 P1 y$ _2 b# p$ t - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
6 N& d& c D0 M - GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);5 H3 f* F2 J8 s; G x! M8 E/ r/ Q
- //定义第二个按键3 k: x- N! h: M J# a# t
- GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN; ; B% E# l, l$ V
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;: a5 l# z1 x. o' t9 V' ~2 i
- GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);0 o5 Y7 E: S- U$ |
- $ i5 n3 Z- H* L2 X* N* T7 |
- }6 I( ?7 o5 C8 G( N! Q! T2 `
- uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)
i6 F2 U# L7 V% f" x - {9 {% R; X- o4 z& j7 L/ X
- if( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON )
) Y1 U& {& ^+ a5 U - {
8 H. T% r& e7 f3 O$ B2 G - while( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON );
3 m* T/ O* p$ H5 k: M( X - return KEY_ON;/ S6 n% Z- @3 h( D* O8 e
- }
- k! N; o; \, n. \ - else return KEY_OFF; y2 I6 L; {. x% a
- }
复制代码 \2 K% B7 R0 k9 Y
main.c - #include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h># i! J1 X: S; Q9 Y+ q; ?
- #include "LED.h"
* O: V2 h" i- c - #include "KEY.h"
; W' i1 Z' b+ `& r4 R6 u - int main(void)
2 w6 }6 O2 \. {+ ^+ B7 V- E - {: @7 @% m) p$ n4 F" u, o/ n
- LED_GPIO_Config();# q* ?, J0 _* b1 \
- Key_GPIO_Config();+ k6 D1 f" A3 r5 v. }" Q% s
- while (1) ) h6 s" ]. M- h" f" i
- {
( H- J8 O, C. N - if ( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON ) 8 g0 z* t! O2 d- r
- LED_B_TOGGLE;, E% R$ p3 U/ ^* ?. R& p* H
- if ( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON )
) }) a) a& S- W# F m - LED_R_TOGGLE; : t' Q! a+ Q5 O" B/ T/ x
- }
4 A- u/ C3 t5 |, B9 A& x
复制代码 8 u$ x$ o4 p: L1 v9 d" |
仿真图
# M8 J+ ?8 B' {# H7 X" R
- e l( h0 x* u5 l# T) G6 Q9 A! n! W) @
|