GPIO实验
一 灯光闪烁实验
小灯正极插在电路 负极接引脚PA0处(处于低电平是小灯点亮 处于高电平时小灯熄灭)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include "stm32f10x.h" #include "Delay.h"
int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_0);
while (1) { GPIO_ResetBits(GPIOA, GPIO_Pin_0); Delay_Ms(500); GPIO_SetBits(GPIOA, GPIO_Pin_0); Delay_Ms(500); } }
|
步骤:
1.首先需要开启时钟
1
| RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
2.初始化GPIO引脚
1 2 3 4 5
| GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
|
3.设置引脚的高低电平,一共有三个函数 :
GPIO_SetBits(引脚分组,引脚)为高电平
GPIO_ ResetBits(引脚分组,引脚)为低电平
1 2 3 4 5
| GPIO_SetBits(GPIOA, GPIO_Pin_0);
|
GPIO_WriteBit(引脚分组,引脚,高低电平)其中Bit_RESET为低电平,Bit_SET为高电平
4.要想让小灯一直工作需要放在死循环里 while (1)中
1 2 3 4 5 6
| while (1) { GPIO_ResetBits(GPIOA, GPIO_Pin_0); Delay_Ms(500); GPIO_SetBits(GPIOA, GPIO_Pin_0); Delay_Ms(500); }
|
二 流水灯实验
流水灯GPIOA(0~7引脚) 负极插在引脚

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include "stm32f10x.h" #include "Delay.h"
int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
while (1) {
for (uint16_t i = 0; i < 8; i++) { GPIO_Write(GPIOA, ~(0x0001 << i)); Delay_Ms(500); }
uint16_t pins[] = {0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080 };
for (int i = 0; i < 8; i++) { GPIO_Write(GPIOA, ~pins[i]); Delay_Ms(500); } } }
|
1.首先需要开启时钟
1
| RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
2.初始化GPIO引脚
1 2 3 4 5
| GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
|
3.采用三种方法遍历0~7引脚
第一种常规方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
|
GPIO_Write(引脚,16进制数)函数 ,前面代表写哪个引脚后面表示指定了要写入端口输出数据寄存器的值。
第二种采用for循环方法
1 2 3 4
| for (uint16_t i = 0; i < 8; i++) { GPIO_Write(GPIOA, (0x0001 << i)); Delay_Ms(500); }
|
第三种采用数组for循环方法
1 2 3 4 5 6 7 8 9
| uint16_t pins[] = {0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080 };
for (int i = 0; i < 8; i++) { GPIO_Write(GPIOA, pins[i]); Delay_Ms(500); }
|
三 蜂鸣器鸣响实验
实现蜂鸣器急促滴滴响,使用GPIOB 12的引脚

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| #include "stm32f10x.h" #include "Delay.h"
int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
uint16_t DelayTime = 500; uint16_t Step = 10; uint16_t MinDelay = 10;
while (1) {
if (DelayTime == MinDelay) { GPIO_ResetBits(GPIOB, GPIO_Pin_12); continue; }
GPIO_ResetBits(GPIOB, GPIO_Pin_12); Delay_Ms(DelayTime); GPIO_SetBits(GPIOB, GPIO_Pin_12); Delay_Ms(DelayTime);
DelayTime -= Step; } }
|
1.首先需要开启时钟
1
| RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
2.初始化GPIO引脚
1 2 3 4 5
| GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
|
3.变量定义部分
1 2 3
| uint16_t DelayTime = 500; uint16_t Step = 10; uint16_t MinDelay = 10;
|
4.判断是否达到最小时延
1 2 3 4
| if (DelayTime == MinDelay) { GPIO_ResetBits(GPIOB, GPIO_Pin_12); continue; }
|
当延时时间减少到10ms时,蜂鸣器持续发声(长鸣)
GPIO_ResetBits是让蜂鸣器开始工作
5.产生滴滴声音
1 2 3 4
| GPIO_ResetBits(GPIOB, GPIO_Pin_12); Delay_Ms(DelayTime); GPIO_SetBits(GPIOB, GPIO_Pin_12); Delay_Ms(DelayTime);
|
先响一段时间,再停一段时间,形成"滴"的一声
刚开始时 DelayTime=500,所以是响0.5秒,停0.5秒
6.加快节奏
每循环一次,延时减少10ms
声音会越来越快:500ms → 490ms → 480ms ...
直到达到10ms的最小值
四 按键控制小灯实验
本实验采用模块化编程,可清晰知道每个模块是什么功能
实现按钮与小灯的交互的小实验,材料为两个小灯、两个按钮 。
简述
使用两个小灯,分别使用GPIOA的PA1与PA2的两个引脚
使用两个按键,分别使用GPIOB的PB1与PB11的两个按钮
按键PB1控制小灯PA1,按键PB11控制小灯PA2

1.首先我们来编写小灯的程序分别创建两个文件 ‘’ LED.h’‘ 与 ‘’LED.c ‘’文件
LED.h文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #ifndef __LED_H #define __LED_H
void LED_Init(void);
void LED1_ON(void);
void LED1_OFF(void);
void LED2_ON(void);
void LED2_OFF(void);
void LED1_Turn(void); void LED2_Turn(void);
#endif
|
LED.c 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include "stm32f10x.h"
void LED_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2);
}
void LED1_ON(void) {
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
void LED1_OFF(void) {
GPIO_SetBits(GPIOA, GPIO_Pin_1);
}
void LED2_ON(void) {
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
void LED2_OFF(void) {
GPIO_SetBits(GPIOA, GPIO_Pin_2);
}
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);
}
}
void LED2_Turn(void) { if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_2) == 0) {
GPIO_SetBits(GPIOA, GPIO_Pin_2); } else {
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
}
|
2.再我们来编写按钮的程序分别创建两个文件 ‘’ Key.h’‘ 与 ‘’Key.c ‘’文件
Key.h 文件
1 2 3 4 5 6 7 8 9
| #ifndef __KEY_H #define __KEY_H #include <stdint.h>
void Key_Init(void); uint8_t Key_GetNum(void);
#endif
|
Key.c 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include "stm32f10x.h" #include "Delay.h" void Key_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
}
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;
}
return KeyNum;
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;
}
|
3.最后我们编写主程序main函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| #include "stm32f10x.h" #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();
}
} }
|
五 光敏电阻传感器控制蜂鸣器实验
本实验采用模块化编程,可清晰知道每个模块是什么功能
实现光敏电阻传感器与蜂鸣器的交互的小实验,材料为光敏电阻传感器、蜂鸣器
简述
使用光敏电阻传感器接在GPIOB上的PB13引脚
使用蜂鸣器接在GPIOB上的PB12引脚
光线暗的时候蜂鸣器响 光线强的时候蜂鸣器不响

1.首先我们来编写光敏电阻传感器的程序分别创建两个文件 ‘’ LightSensor.h’‘ 与 ‘’ LightSensor.c ‘’文件
LightSensor.h 文件
1 2 3 4 5 6 7 8 9 10 11
| #ifndef __LIGHTSENSOR_H #define __LIGHTSENSOR_H #include <stdint.h>
void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
#endif
|
LightSensor.c 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include "stm32f10x.h"
void LightSensor_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); }
uint8_t LightSensor_Get(void) { return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13); }
|
2.接下来我们来编写蜂鸣器的程序分别创建两个文件 ‘’ Buzzer.h’‘ 与 ‘’Buzzer.c ‘’文件
Buzzer.h 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #ifndef __BUZZER_H #define __BUZZER_H
void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_Turn(void);
#endif
|
Buzzer.c 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include "stm32f10x.h"
void Buzzer_Init(void){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_12); }
void Buzzer_ON(void) {
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
void Buzzer_OFF(void) {
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void Buzzer_Turn(void) { if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0) {
GPIO_SetBits(GPIOB, GPIO_Pin_12); } else {
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
}
|
3.最后我们编写主程序main函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include "stm32f10x.h" #include "Delay.h" #include "Buzzer.h" #include "LightSensor.h"
int main(void) { Buzzer_Init(); LightSensor_Init();
while (1) { if(LightSensor_Get() == 1){ Buzzer_ON(); }else{ Buzzer_OFF();
} } }
|