当前位置: 首页 > news >正文

STM32-第二节-GPIO输入(按键,传感器)

 一、按键与传感器:

1.介绍:

按键:按下连通,松手断开。

注意:按下按键时会有5-10ms的晃动,延时一下再判断就好,故下面代码delay_ms(20)。

传感器:

2.使用原理:

将引脚配置为上拉输入(默认为1)模式,当按键按下,输入为0,按键抬起,输入为1。

3.接线图:

4.GPIO读取函数:

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

5.组合代码:

注:先看下面代码封装,再回来看这里。

#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();}}
}

二、封装常用函数:

使用哪个引脚,就要修改下面代码中的GPIOx与GPIO_Pin_x。

1.LED.c

(1)LED初始化函数:
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);
}
(2)LED开关函数:
void LED1_ON(void)
{GPIO_ResetBits(GPIOA, GPIO_Pin_1);
}void LED1_OFF(void)
{GPIO_SetBits(GPIOA, GPIO_Pin_1);
}
(3)LED翻转函数:
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.Buzzer.c

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);}
}

3.Key.c

(1)按键初始化函数:
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);
}
(2)获取按键信息函数:

调用此函数时,返回按下的按键编号,未抬起时在此函数中循环,抬起返回。

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;
}

4.LightSensor.c

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);
}

http://www.xdnf.cn/news/1079407.html

相关文章:

  • C盘爆满元凶!WinSxS组件解密
  • JsonCpp的核心类及核心函数使用汇总
  • Web 服务器架构选择深度解析
  • Linux常见指令以及权限理解
  • Flowable12基础流程实战资金申请------------持续更新中
  • 埃及黑白沙漠:2亿年风蚀岩的“外星登陆现场“
  • 未来之窗冥界调试工具—东方仙盟
  • LTspice仿真10——电容
  • A模块 系统与网络安全 第四门课 弹性交换网络-1
  • 在小程序中实现实时聊天:WebSocket最佳实践
  • Verilog 语法介绍 1-1结构
  • Spring Boot + 本地部署大模型实现:基于 Ollama 的集成实践
  • KMP(Kotlin Multiplatform)改造(Android/iOS)老项目
  • PHP语法基础篇(八):超全局变量
  • 转录组分析流程(零):流程介绍
  • 【二分】-----【Music Notes S】
  • 【Git】同时在本地使用多个github账号进行github仓库管理
  • 通过Curtain 解决方案保障BIM模型安全共享—建筑业的防泄密实战
  • react-打包和本地预览 ——打包优化
  • 【数据结构】C++的unordered_map/set模拟实现(开散列(哈希桶)作底层)
  • npm 命令入门指南(前端小白版)
  • contenteditable网页富文本编辑无法选中图片
  • 从0到1实战!用Docker部署Qwerty Learner输入法的完整实践过程
  • curl for android
  • Linux多线程(十三)之【POSIX信号量基于环形队列的生产消费模型】
  • OpenCV CUDA模块设备层-----在 GPU上高效地执行两个uint类型值的最小值比较函数vmin2()
  • LeetCode 317 最短距离选址问题详解(Swift 实现 + BFS 多源遍历)
  • 高边驱动 低边驱动
  • 多模态AI Agent技术栈解析:视觉-语言-决策融合的算法原理与实践
  • Kafka生态整合深度解析:构建现代化数据架构的核心枢纽