前言, I+ @! f% Y' I( `8 b$ j# G
配置GPIO引脚前先查看板子原理图,LED配置在哪个IO口,本篇以正点原子、STM32F103RCT6为例。' {* a, N4 l" ]3 W' ?4 k" {
/ S$ ^, L- Y Y7 a! xLED灯我们主要用到的固件库文件是:# A. s1 U/ {$ y9 W- y
stm32f10x_gpio.c /stm32f10x_gpio.h
6 r7 r0 {$ Q J' g" ]stm32f10x_rcc.c/stm32f10x_rcc.h6 {3 V# I/ ]* K# v, r) Q0 g
misc.c/ misc.h; I- f/ R9 ]' W9 U1 g4 h
stm32f10x_usart /stm32f10x_usart.h
4 u4 D6 g8 i6 H; Z' A7 E: h+ b也可把外设固件库全部加入到工程,方便延伸。 d3 m2 k# a, |
) D) `6 {6 B/ t
一、创建
B3 T* c& L6 k6 n" o& @
" Q/ J$ n5 b/ m" _$ [7 x5 t' j% @ V
8 x6 U* s+ u/ G$ L9 P' L7 P4 P
& [+ M; |( p6 F8 E! Z# P# Q
0 ]1 ^ h$ H2 F0 y二、代码部分% P _2 C+ w; b9 d7 k' D/ @( Z( {
1.main.c" l7 R4 I9 j4 s3 @. M9 x! s9 F
- int main(void)8 y3 B3 B3 o% E- U
- {
1 C$ r5 o/ N! P* J# ] - delay_init(); //延时函数初始化
, e. z; E/ u$ ?5 o+ G' ]* Q - LED_Init(); //初始化与LED连接的硬件接口% }8 v; E! F" ?( s& q6 Q) u
- while(1)/ G9 r2 p5 @2 Z4 W" V+ w
- {
3 F# d, [5 [$ |: l- E p; V ] - GPIO_ResetBits(GPIOA,GPIO_Pin_8); //LED0输出低
t" t1 C3 F+ V5 u3 V - GPIO_SetBits(GPIOD,GPIO_Pin_2);//LED1输出高% c3 i4 B# a( {# q. U' o& u" x
- delay_ms(1000); //延时1000ms闪一次* x$ ]; D6 E0 @' R. n
- GPIO_SetBits(GPIOA,GPIO_Pin_8);//LED0输出高
$ E2 X$ h* u) s4 ~+ l- Y$ d - GPIO_ResetBits(GPIOD,GPIO_Pin_2);//LED1输出低
" l, V% ^$ F/ w2 V - delay_ms(1000);
0 }: N* |5 n( `" W# R - }
% O1 H" D7 U6 ?) h x) V, b - }
复制代码
0 X& \* N! B0 k6 A; y! i4 V6 r2.led.c# X3 P j9 W0 `; d5 _ q+ g
- void LED_Init(void)
6 `' c/ t; _$ ]8 a$ }3 f - {+ ~ C& R3 _; e8 r) E% @& A0 y5 P w
- % V8 N$ @6 ?& c) H( F6 B2 K
- GPIO_InitTypeDef GPIO_InitStructure;0 ?& |$ V7 n; m6 W `& |- H
- 8 o/ p# E( I6 G7 l7 \
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE); //使能PB,PE端口时钟* }7 |8 T8 c; k6 ]0 `
- $ U' u' D) `' H; d l" h
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PB.5 端口配置" u# r$ P$ t a: c
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出6 N2 L4 ]' U4 `; B1 d0 w
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz; V+ o8 ~+ E3 w& U2 \" g+ i' F) Q
- GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.56 ~- F4 ?+ j1 K9 ?3 m2 r
- GPIO_SetBits(GPIOA,GPIO_Pin_8); //PB.5 输出高
) v7 t, {1 U Q - ; q+ ^7 J+ G3 b1 j4 w4 Q
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1-->PE.5 端口配置, 推挽输出
% @5 d8 t# b, X - GPIO_Init(GPIOD, &GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
+ Z o, P/ [8 p+ {- B( X$ G3 G: @ - GPIO_SetBits(GPIOD,GPIO_Pin_2); //PE.5 输出高 $ Y# B& ? G/ q
- }
复制代码
$ g3 N7 T0 P$ _4 W$ a" J3.led.h" t: ?. U, C* K
- #ifndef __LED_H q- p% W& T3 M+ ?
- #define __LED_H : w& ~9 H: V7 S% ]2 e
- #include "gpio.h"4 t% p8 L$ } {3 N" b5 w
: E5 e& [- i8 j
7 _ z2 a# e, p& x: I: S$ P% l- #define LED0 PAout(8) // PA8
- d4 K- I/ n# x8 p, H* [4 D - #define LED1 PDout(2) // PD2 & d' D4 \( Z8 c6 s6 m6 b
: }7 ?6 U( [% N( z- void LED_Init(void);//初始化
& x6 P- V2 n2 ^7 @
7 y" h% H7 V& Z# S8 w% @# H-
9 H7 O, y; z& g3 O) L - #endif
复制代码
+ `. `. S8 f3 C
2 V% S( R% [% q! H& l1 `6 M( l. t* x; ]# { ]- ~
2 W2 q# S# r4 ^2 d4 X
|