当前位置: 首页 > 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.dtcms.com/a/265866.html

相关文章:

  • [科普]UART、RS232、RS422、RS485、TTL:深入解析串行通信家族
  • uniapp 使用ffmpeg播放rtsp
  • 网络基础(1)
  • 铁血联盟3 中文 免安 离线运行版
  • 基于路径质量的AI负载均衡异常路径检测与恢复策略
  • HAL库(Hardware Abstraction Layer,硬件抽象层)核心理解
  • 遇到该问题:kex_exchange_identification: read: Connection reset`的解决办法
  • VBA初学3----实战(VBA实现Excel转csv)
  • 《2025年攻防演练必修漏洞清单》
  • C++11 shared_ptr 原理与详细教程
  • uniapp打包微信小程序主包过大问题_uniapp 微信小程序时主包太大和vendor.js过大
  • C++ 实现简单二叉树操作:插入节点与数据打印
  • 【playwright篇】教程(十七)[html元素知识]
  • 【NLP入门系列四】评论文本分类入门案例
  • 设计模式-观察者模式、命令模式
  • Java连接阿里云MaxCompute例
  • Qt宝藏库:20+实用开源项目合集
  • NV133NV137美光固态闪存NV147NV148
  • Git协作开发:feature分支、拉取最新并合并
  • 这才叫窗口查询!TDEngine官方文档没讲透的实战玩法
  • ModbusRTU转Profinet网关在工业自动化中的应用与价值
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | DragNDrop(拖拽占用组件)
  • 力扣 hot100 Day33
  • 快速搭建大模型web对话环境指南(open-webUI)
  • 双向链表的实现
  • [创业之路-468]:企业经营层 - 使用“市场-需求-竞争”三维模型筛选细分市场(市场维度、客户需求维度、竞争维度)
  • JavaEE-Linux环境部署
  • Java 核心技术与框架实战十八问
  • 专题:2025即时零售与各类人群消费行为洞察报告|附400+份报告PDF、原数据表汇总下载
  • 模拟IC设计提高系列6-Library导入与新建Library