当前位置:网站首页>触摸按键与按键控制对应的LED状态翻转
触摸按键与按键控制对应的LED状态翻转
2022-06-29 22:21:00 【让一切都燃烧】
任务要求:
如图所示:
任务一:
触摸按键控制对应的LED状态翻转
思路分析:
触摸按键实现LED状态翻转与机械按键原理是一样的,而且不需要用延时抖动
。
完成代码
int main (void){
//主程序
RCC_Configuration(); //系统时钟初始化
LED_Init();//LED初始化
TOUCH_KEY_Init();//按键初始化
while(1){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)){
//读按键接口的电平
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)){
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //LED取反
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)); //等待按键松开 防止程序在按键没有松开的情况下反复运行
}
}
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)){
//读按键接口的电平
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)){
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED2,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED2))); //LED取反
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)); //等待按键松开 防止程序在按键没有松开的情况下反复运行
}
}
}
}
任务二:
按键控制对应LED状态翻转
代码:
//main.c
int main (void){
//主程序
//初始化程序
RCC_Configuration(); //时钟设置
LED_Init();//LED初始化
KEY_Init();//按键初始化
//主循环
//初始化起始LED为熄灭状态
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));
GPIO_WriteBit(LEDPORT,LED4,(BitAction)(1));
while(1){
//有锁存
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
//读按键接口的电平
delay_ms(20); //延时去抖动
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED3))); //LED取反
while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); //等待按键松开
}
}
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2)){
//读按键接口的电平
delay_ms(20); //延时去抖动
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2)){
//读按键接口的电平
GPIO_WriteBit(LEDPORT,LED4,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED4))); //LED取反
while(!GPIO_ReadInputDataBit(KEYPORT,KEY2)); //等待按键松开
}
}
}
}
注意:
对应的也得修改,因为两个LED对应的接口与核心板上两个小灯对应的接口不一致
led.h
#define LEDPORT GPIOA //定义IO接口
#define LED3 GPIO_Pin_2 //定义IO接口
#define LED4 GPIO_Pin_3 //定义IO接口
led.c
void LED_Init(void){
//LED灯的接口初始化
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = LED3; //选择端口号(0~15或all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //选择IO接口工作方式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置IO接口速度(2/10/50MHz)
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
边栏推荐
- Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning
- Code sharing for making and developing small programs on the dating platform
- 从第三次技术革命看企业应用三大开发趋势
- Realizing deep learning framework from zero -- LSTM from theory to practice [theory]
- 直播平台开发,进入可视区域执行动画、动效、添加样式类名
- leetcode:91. Decoding method [DFS + memorization]
- 如果我在珠海,到哪里开户比较好?究竟网上开户是否安全么?
- laravel 关联模型 多态关系
- Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
- Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
猜你喜欢

Online text digit recognition list summation tool

Problem solving metauniverse, multi communication scheme in online games
![Realizing deep learning framework from zero -- RNN from theory to practice [practice]](/img/a0/d64b69dec4a8f3a3dbc2eb47df9372.png)
Realizing deep learning framework from zero -- RNN from theory to practice [practice]

AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs

leetcode:91. 解码方法【dfs + 记忆化】

Steady! The best posture for thousands of microservices to access Zadig (helm chart)

便携式4K音视频会议终端一体机带8倍数字变焦
详细聊聊MySQL中auto_increment有什么作用

MySQL backup and restore

Mysql入库不了表情符号怎么办
随机推荐
深入解析kubernetes中的选举机制
Conceptual understanding of deep learning (notes)
正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
MooseFS基本概念总结
软件快速交付真的需要以安全为代价吗?
If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview
Summary of basic concepts of moosefs
MySQL backup database Linux
PhpSpreadsheet读写Excel文件
[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance
Realizing deep learning framework from zero -- RNN from theory to practice [practice]
Does rapid software delivery really need to be at the cost of security?
Advanced use of the optional class
LeetCode85+105+114+124
Does rapid software delivery really need to be at the cost of security?
从检查点恢复后读取不到mysql的数据有那位兄台知道原因吗
#第三天
Daily mathematics serial 54: February 23
Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures