STM32 GPIO输入
本文是STM32学习笔记,方便后续复习,学习视频是b站江科大STM32学习教程 江科大GPIO输入
C语言基础知识
宏定义
//关键字:#define
//用途:用一个字符串代替一个数字,便于理解,防止出错;
// 提取程序中经常出现的参数,便于快速修改
#define ABC 12345 //定义宏定义
int a = ABC; //引用宏定义,等效于a = 12345;
typedef
只可以对变量类型改名
//关键字:typedef
//用途:将一个比较长的变量类型名换个名字,便于使用
typedef unsigned char unit8_t; //定义typedef
uint8_t a; //引用typedef,等效于unsigned char a;
结构体
理解为一种数据类型
//关键字:struct
//用途:数据打包,不同类型变量的集合
//******************一般形式******************
struct {char x; int y; float z} StructName;
StructName.x = 'A';
StructName.y = 66;
StructName.z = 1.23;
//或者以下形式,pStructName为结构体的地址
//pStructName -> x = 'A';
//pStructName -> y = 66;
//pStructName -> z = 1.23;
//*******************************************
//****************使用typedef****************
//结构体变量类型较长,通常使用typedef改变变量类型名
typedef struct {
char x;
int y;
float z
} StructName_t;
StructName_t a;
StructName_t b;
//*******************************************
枚举
//关键字:enum
//用途:定义一个取值受限制的整形变量,用于限制变量取值范围;可当作宏定义的集合
//******************一般形式******************
enum {false = 0, true = 1} EnumName;
EnumName = false;
EnumName = true;
//*******************************************
//****************使用typedef****************
//枚举变量类型较长,通常使用typedef改变变量类型名
typedef enum {
false = 0,
true = 1
} EnumName_t;
//*******************************************
GPIO读取函数
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); //读取输入寄存器某一位
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx); //读取整个GPIO输入寄存器
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); //读取输出寄存器某一位
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); //读取整个GPIO输出寄存器
按键控制LED
硬件接线图
LED1 -> PA1 ; LED2 -> PA2; 低电平点亮
KEY1 -> PB1 ; KEY2 ->PB11; 按下为低电平
LED驱动代码
LED.h
#ifndef __LED_H
#define __LED_H
//LED1 -> PA1 低电平点亮
//LED2 -> PA2 低电平点亮
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.c
#include "stm32f10x.h" // Device header
//配置GPIO
//LED1 -> PA1 低电平点亮
//LED2 -> PA2 低电平点亮
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); //默认关闭
}
//点亮LED1
void LED1_ON(void)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}
//熄灭LED1
void LED1_OFF(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_1);
}
//LED1翻转
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);
}
}
//点亮LED2
void LED2_ON(void)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
}
//熄灭LED2
void LED2_OFF(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_2);
}
//LED2翻转
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);
}
}
按键驱动代码
Key.h
#ifndef __KEY_H
#define __KEY_H
//KEY1 -> PB1 低电平按下
//KEY2 -> PB11 低电平按下
void Key_Init(void);
uint8_t Key_GetNum(void);
#endif
Key.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
//配置GPIO
//KEY1 -> PB1 低电平按下
//KEY2 -> PB11 低电平按下
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);
}
//读取按键
//KEY1按下返回1;KEY2按下返回2;否则返回0
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;
}
主函数
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) //KEY1被按下
{
LED1_Turn(); //LED1取反
}
if (KeyNum == 2) //KEY2被按下
{
LED2_Turn(); //KEY2取反
}
}
}
光敏传感器控制蜂鸣器
光敏传感器控制蜂鸣器接线图
蜂鸣器驱动代码
Buzzer.h
#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
#include "stm32f10x.h" // Device header
//配置GPIO
//信号 -> PB12 低电平有效
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(GPIOB, &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);
}
}
光敏传感器驱动代码
LightSensor.h
#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
#endif
LightSensor.c
#include "stm32f10x.h" // Device header
//配置GPIO
//DO(Digit Out) -> PB13
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);
}
//读取光敏传感器DO(Digit Out)
uint8_t LightSensor_Get(void)
{
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}
主函数
main.c
#include "stm32f10x.h" // Device header
#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();
}
}
}