|
知识介绍工程文件放置 # y( W8 P5 R3 K3 S
只有在添加进这一串之后,才能使用头文件集合 stm32f10x_conf.h $ W9 c3 r) n8 n' L& `
# X8 j% i3 u3 C& i0 L
选择类型 初始化时钟从哪找在stm32f10x_rcc.c中。
/ n" a+ x; x4 K9 F# c
) C$ T0 a& b/ V* n* P9 Z
断言函数
5 K; x5 \0 e3 I
此工程需要编写以下五个文档
) C2 o: t7 s$ c
LED.h - #ifndef __LED_H
2 U0 |/ ~& f! E1 K" L% b/ p" q - #define __LED_H9 a! \ N4 W5 q7 |
- #include "stm32f10x.h"/ ^ t' [( X) F
- #define LED_B_GPIO_PIN GPIO_Pin_1
) {& d6 |' |. L* c8 { A& N1 f% ] - #define LED_R_GPIO_PIN GPIO_Pin_5
5 e: t6 r9 I% S" A6 @- u8 S - #define LED_GPIO_PORT GPIOB/ Q2 P. ?8 o* u6 T
- #define LED_GPIO_CLK RCC_APB2Periph_GPIOB1 e6 v/ a* E/ U* d8 l/ B% _
- //反转) [: [& X& r9 U2 ^) J1 }6 L
- #define LED_R_TOGGLE {LED_GPIO_PORT->ODR ^= LED_R_GPIO_PIN;}//异或可以改变原来的状态; {" t; ?( E9 C4 Y7 T( z
- #define LED_B_TOGGLE {LED_GPIO_PORT->ODR ^= LED_B_GPIO_PIN;}//异或可以改变原来的状态
+ f; o- G$ `4 A$ W - void LED_GPIO_Config(void);
9 x, C4 _2 ?* {/ g8 L B3 _ - #endif /* __LED_H */
复制代码
( G. E: S. A* E0 R3 w! y. c LED.c - #include "YANG_LED.h"1 W; h' F4 E5 Y9 V( s6 ^
- void LED_GPIO_Config(void)
( n4 L, X+ U. B+ T s - {
" @, [. Y4 ]: K0 [2 J" q! V - /*定义3个GPIO_InitTypeDef 类型的结构体*/$ A; a. h9 B. u! Y7 N. c
- GPIO_InitTypeDef GPIO_InitStruct1;# p' S/ \5 h2 _2 P
- GPIO_InitTypeDef GPIO_InitStruct2;
r* `0 b8 Y5 y0 j - /*开启 LED 相关的 GPIO 外设时钟*/# H) O' `- v; {2 X/ {# P {
- RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE); G( b1 l( F. B V6 I
- GPIO_InitStruct1.GPIO_Pin = LED_B_GPIO_PIN;! f- ?. F( n$ Y8 c2 n, X+ j9 ~
- GPIO_InitStruct2.GPIO_Pin = LED_R_GPIO_PIN;
) m: J3 M, x$ t& \7 g" Y% V! ` - GPIO_InitStruct1.GPIO_Mode = GPIO_Mode_Out_PP;0 i( b9 [+ T- O9 t9 z
- GPIO_InitStruct1.GPIO_Speed = GPIO_Speed_50MHz;2 p" P9 F- `9 S/ s1 r% G# W$ Y$ W
- GPIO_InitStruct2.GPIO_Mode = GPIO_Mode_Out_PP;
* p, j4 h5 }7 w# ^9 ~# S - GPIO_InitStruct2.GPIO_Speed = GPIO_Speed_50MHz;& e/ `8 z5 d7 V$ T
- GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct1); , _6 L- M* V; y7 h' `, o* |- h
- GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct2);
' r# ?, w/ p% K: D$ C; t4 w - }
复制代码
5 V6 h* M% Y/ t& K- L( j1 i1 {5 E! l6 {8 C5 ?
" I7 |/ F+ l9 x$ S: eKEY.h D+ ~4 b1 k# g0 Q( g2 R) G
- #ifndef __KEY_H
2 P& @: Q* g' U0 Y$ r' |9 d& G& q - #d#include "stm32f10x.h"$ R9 r3 t# U$ b$ y4 `
- #define KEY_ON 1
* o5 J* T- @& @3 w6 D* c8 } - #define KEY_OFF 09 F; V, k' F) ^
- #define KEY1_GPIO_CLK RCC_APB2Periph_GPIOA- e! q) @# Q/ o" j$ L5 {* |. a
- #define KEY1_GPIO_PORT GPIOA r# Z1 } g& n% L
- #define KEY1_GPIO_PIN GPIO_Pin_0
) X2 E7 z6 t2 ~+ Q5 C1 v2 i - #define KEY2_GPIO_CLK RCC_APB2Periph_GPIOC5 Z$ v% i, Y1 }. ?$ `& `: N) k5 }
- #define KEY2_GPIO_PORT GPIOC5 T2 y* \; ~; S
- #define KEY2_GPIO_PIN GPIO_Pin_133 c1 t/ c% U6 q
- void Key_GPIO_Config(void);
8 b, L, Z& ?. T* ?- Y - uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin);5 y7 e$ a3 l7 I8 h
- #endif /* __KEY_H */
复制代码
x/ ?" m( F( MKEY.c - #include "KEY.h" m, \. n9 l* p/ J1 T
- void Key_GPIO_Config(void): H4 C3 F) a! f( { ]* y# d
- { j- B( Z2 ?; A6 O% g2 H3 a6 r& @3 x
- GPIO_InitTypeDef GPIO_InitStructure;9 r9 m# B2 M6 F
- RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK|KEY2_GPIO_CLK,ENABLE);2 b+ }- {3 U0 {. z2 E! v
- //定义第一个按键7 X5 l& e7 E: z/ A) ?' ]8 N
- GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
, V% h, l$ p0 P" G i4 u - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;1 }" R; f. D' f$ f' I& B4 k+ V, K( e
- GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
4 V( Z2 ^) A5 T' c& ^ - //定义第二个按键- M' z7 N% l. V( _# }. k
- GPIO_InitStructure.GPIO_Pin = KEY2_GPIO_PIN; $ x/ j7 \) }* `
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;" |* @7 V+ J. C* ?+ ]
- GPIO_Init(KEY2_GPIO_PORT, &GPIO_InitStructure);) {$ C W! R) |2 m+ M& l) y
- . X' Y+ {% Z( ]; _8 @3 C
- }
( `) U l( H8 G( l - uint8_t Key_Scan(GPIO_TypeDef *GPIOx,uint16_t GPIO_Pin)* m5 P* u2 _: @
- {
% U/ O2 K& [( u+ Y1 H! c; z - if( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON )5 h% P- s9 r ]% }
- {
$ i( H, _! r8 U. b - while( GPIO_ReadInputDataBit(GPIOx, GPIO_Pin) == KEY_ON );2 j) |3 M$ y O9 X7 y( l
- return KEY_ON;: X( S: _) U2 g0 S" q$ M
- }0 W5 a& D0 H. d+ g
- else return KEY_OFF;
" t8 Y- n/ I$ ]7 O- @. l) N" I - }
复制代码 0 i' H' d# V) T% l ?, o
main.c - #include "stm32f10x.h" // 相当于51单片机中的 #include <reg51.h>
- L. k1 X; o" A& J% E - #include "LED.h"- X( ]( ?( G: t+ S7 T: O8 t$ x
- #include "KEY.h"
0 }: t$ I; R' D/ K# D - int main(void)$ n. l0 H! u5 s. R' }7 w' m! L
- {0 y% X" f7 c( ]4 W4 L9 q
- LED_GPIO_Config();
6 R: r# H3 c8 x4 |5 n/ h - Key_GPIO_Config();6 H/ f4 {5 J. N# A$ v
- while (1)
& q. _4 `/ ^5 j6 v1 Q - {
+ l+ _- ]8 E/ |4 i% p4 a) A9 ^ - if ( Key_Scan(KEY1_GPIO_PORT,KEY1_GPIO_PIN) == KEY_ON ) $ Y. d3 w' h8 y! Z( ~
- LED_B_TOGGLE;
" o4 i3 a( d7 |' A! O0 Z3 o - if ( Key_Scan(KEY2_GPIO_PORT,KEY2_GPIO_PIN) == KEY_ON ) 0 |3 Q% L7 K5 ]4 t
- LED_R_TOGGLE; 6 C. b) r6 v3 ^* ~4 w' ?% y
- }6 \# G# O3 }$ G6 W, k( y
复制代码
9 W; o* B% E* x8 _/ Y# x仿真图
, U o5 v4 p% G: \& C4 Y' i7 D
5 G. G& V: F, S0 D# X
" R$ C- ^8 Z% V! _7 N2 s% u |