当前位置:网站首页>触摸按键与按键控制对应的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);
}
边栏推荐
- LeetCode85+105+114+124
- Live broadcast platform development, enter the visual area to execute animation, dynamic effects and add style class names
- Introduction, deployment and application of moosefs
- Dynamic planning learning (continuous update)
- IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products
- Digital tracking analysis of insurance services in the first quarter of 2022
- Cloud native enthusiast weekly: cool collection of grafana monitoring panels
- The correct method for Navicat to connect to mysql8.0 (valid for personal testing)
- 阶段性总结与思考
- Summary of basic concepts of moosefs
猜你喜欢

Guangzhou launched a campaign to promote the safety of bottled gas and popularized the knowledge of gas safety

Steady! The best posture for thousands of microservices to access Zadig (helm chart)
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

What if MySQL fails to store emoticons

论文浅尝 | KR-GCN: 知识感知推理的可解释推荐系统

26岁,0基础转行软件测试,从月薪3k到16k,我整理的超全学习指南

Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning

软件测试方法和技术知识点有哪些?
![leetcode:91. Decoding method [DFS + memorization]](/img/8d/9f61961fa9cfc6809a7800913e8761.png)
leetcode:91. Decoding method [DFS + memorization]

Arrange the array into the smallest number_ Reverse pairs in an array (merge Statistics)_ Number of occurrences of a number in an ascending array_ Ugly number (Sword finger offer)
随机推荐
Can cdc2.2.1 listen to multiple PgSQL libraries at the same time?
Static keyword continuation, inheritance, rewrite, polymorphism
math_ Basic elementary function graph (power function / exponent / logarithm / trigonometry / inverse trigonometry)
Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning
Qt5.14.2 error connecting to the MySQL database of Ubuntu 20.04
把数组排成最小的数_数组中的逆序对(归并统计法)_数字在升序数组中出现的次数_丑数(剑指offer)
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
【多线程】 如何自己实现定时器
What if MySQL fails to store emoticons
为什么在局域网(ERP服务器)共享文件夹上拷贝文件时导致全局域英特网断网
直播平台开发,进入可视区域执行动画、动效、添加样式类名
Does rapid software delivery really need to be at the cost of security?
股票开户安全吗?上海股票开户。
How to use filters in jfinal to monitor Druid for SQL execution?
5-2Web应用程序漏洞扫描
Basic use of Nacos configuration center
2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合
Phpspreadsheet reading and writing Excel files
#第三天
Realizing deep learning framework from zero -- LSTM from theory to practice [theory]