当前位置: 首页 > 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.lryc.cn/news/579627.html

相关文章:

  • Linux基本指令(下)
  • 建设工程停工损失从哪些方面取证,如何取证?
  • 经典灰狼算法+编码器+双向长短期记忆神经网络,GWO-Transformer-BiLSTM多变量回归预测,作者:机器学习之心!
  • 在鸿蒙(HarmonyOS)中安装 .app 格式的应用包(即 HAP 或 APP 文件),可以通过以下方法实现
  • 服务器如何配置SSH密钥登录提高安全性?
  • 基于Anything LLM的本地知识库系统远程访问实现路径
  • vue2+elementui使用compressorjs压缩上传的图片
  • 机器人“触摸”水果成熟度突破:SwishFormer模型与DIGIT视触觉传感器在HelloRobot上的水果检测应用
  • 从0到1解锁Element-Plus组件二次封装El-Dialog动态调用
  • Unity-Shader-几何着色器
  • 学习设计模式《十六》——策略模式
  • Linux 73 LAMP4
  • 离线迁移 Conda 环境到 Windows 服务器:用 conda-pack 摆脱硬路径限制
  • 从0开始学习R语言--Day37--CMH检验
  • VR 果蔬运输开启农业物流新变革
  • AI无标记动捕如何结合VR大空间技术打造沉浸式游戏体验
  • 从0到1实战!用Docker部署Qwerty Learner输入法的完整实践过程
  • https如何利用工具ssl证书;使用自己生成的证书
  • 创建 TransactionStatus
  • rabbitmq 与 Erlang 的版本对照表 win10 安装方法
  • Debian-10-standard用`networking`服务的`/etc/network/interfaces`配置文件设置多网卡多IPv6
  • 贝叶斯深度学习:赋予AI不确定性感知的认知革命
  • 日本IT|日本做后端开发需要具备什么技能开发经验?
  • 深入理解CSS中的BFC 与IFC , 布局的两大基础概念
  • Day50 预训练模型+CBAM模块
  • 【Python】图像识别的常用功能函数
  • golang json omitempty 标签研究
  • 服务器如何配置防火墙规则开放/关闭端口?
  • 数据库运维指导书
  • 74. 搜索二维矩阵