RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启APB2时钟
GPIO_InitTypeDef GPIO_InitStructure;//定义结构体,并进行参数设置。GPIO_InitStructure为参数名称
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//对输入输出模式进行配置,GPIO_Mode_Out_PP为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//选择引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置输出速度
GPIO_Init(GPIOA,&GPIO_InitStructure);
1.先对APB2的时钟使能。![](https://i-blog.csdnimg.cn/blog_migrate/a134422a6577cc65023448552b5cf4f1.png)
使用RCC_APB2PeriphClockCmd函数进行配置,第一个参数为选择连接在APB2总线上的外设。
APB(Advanced Peripheral Bus)高级外设总线。
第二个参数为ENABLE or DISABLE。
2.定义结构体,并对结构体参数进行设置。
定义结构体使用GPIO_InitTypeDef
typede的使用
1.GPIO_Mode(设置输入输出模式)
(1) GPIO_Mode_AIN 模拟输入
(2) GPIO_Mode_IN_FLOATING 浮空输入
(3) GPIO_Mode_IPD 下拉输入
(4) GPIO_Mode_IPU 上拉输入
(5) GPIO_Mode_Out_OD 开漏输出
(6) GPIO_Mode_Out_PP 推挽输出
(7) GPIO_Mode_AF_OD 复用开漏输出
(8) GPIO_Mode_AF_PP 复用推挽输出
2.GPIO_Pin(选择所用的引脚)
引脚可以为0---15,或者为ALL。
3.GPIO_Speed(对输出速度配置)![](https://i-blog.csdnimg.cn/blog_migrate/5e7af9e18c987327a298069e3478aa94.png)
typedef在这里定义一个结构体enum,在程序中相当于宏变量。
3.GPIO_Init
对结构体配置,第一个参数选择GPIO,第二个参数为结构体地址。
![](https://i-blog.csdnimg.cn/blog_migrate/af6c2b0f60b3672fd16125a5d8521cf9.png)
二、LED点灯
在进行初始化之后,对引脚电平高低进行配置,即可控制LED的亮灭。
// GPIO_ResetBits(GPIOA,GPIO_Pin_0);//将端口输出为低电平
// GPIO_SetBits(GPIOA,GPIO_Pin_0);//端口输出为高电平
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);//设置端口的高低电平
详情参考库函数手册。
LED闪烁
while(1)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);//设置端口的高低电平
Delay_ms(500); //延时500ms
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);//设置端口的高低电平
Delay_ms(500); //延时500ms
}
使用Delay函数进行延时,在循环中实现。
三、LED流水灯
while (1)
{
GPIO_Write(GPIOA, ~0x0001); //0000 0000 0000 0001
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0002); //0000 0000 0000 0010
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0004); //0000 0000 0000 0100
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0008); //0000 0000 0000 1000
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0010); //0000 0000 0001 0000
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0020); //0000 0000 0010 0000
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0040); //0000 0000 0100 0000
Delay_ms(100);
GPIO_Write(GPIOA, ~0x0080); //0000 0000 1000 0000
Delay_ms(100);
}
GPIO_Write向指定 GPIO 数据端口写入数据。
直接写入数据即可0为低电平,1为高电平。
四、蜂鸣器
while (1)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_ms(700);
}
}
实质还是对端口的电平进行配置
五、按键控制LED亮灭
1.初始化,使能时钟、参数设置(代码省略)
2.采用模块化编程,在工程文件中新建Hardware文件夹
新建LED.c 与LED.h文件
初始化省略
void LED1_Turn(void)//改变灯的亮灭
{
if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
}
编写改变LED灯控制亮灭的函数,以便在main中调用直接改变亮灭。
使用GPIO_ReadOutputDataBit函数读取对应端口的值。
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED1_Turn(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED2_Turn(void);
#endif
在LED.h文件中声明所可以被调用的函数名。
固定格式
2.编写完控制LED灯的代码后我们还需要编写一个函数用来控制何时改变LED的亮灭。
新建Key.c 和Key.h文件用以控制。
uint8_t Key_GetNum(void)//获取按下哪个开关
{
uint8_t KeyNum = 0;
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
Delay_ms(20);
KeyNum = 1;
}
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
{
Delay_ms(20);
while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
Delay_ms(20);
KeyNum = 2;
}
return KeyNum;
}
GPIO_ReadInputDataBit 读取对应端口的输入值
Delay函数delay20ms是为了消除按键在被按下时产生的振荡的影响
当 按下时不返回keynum的值,在while中循环,只有当抬起后,GPIO_ReadInputDataBit的值不为0后,才会返回keynum的值。
#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
在key.h中声明可以被调用的函数名。
3.main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
uint8_t KeyNum;
int main(void)
{
LED_Init();
Key_Init();
while (1)
{
KeyNum = Key_GetNum();
if (KeyNum == 1)
{
LED1_Turn();
}
if (KeyNum == 2)
{
LED2_Turn();
}
}
}
即可实现按键控制亮灭