当前位置:网站首页>Touch key and key control corresponding LED status reversal
Touch key and key control corresponding LED status reversal
2022-06-29 22:42:00 【Let everything burn】
List of articles
Mission requirements :
As shown in the figure :
Task a :
Touch the key to control the corresponding LED State flip
Thought analysis :
Touch key implementation LED The principle of state reversal is the same as that of mechanical key , And there is no need to use delay jitter
.
Completion code
int main (void){
// The main program
RCC_Configuration(); // System clock initialization
LED_Init();//LED initialization
TOUCH_KEY_Init();// Key initialization
while(1){
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)){
// Read the level of the key interface
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)){
// Read the level of the key interface
GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //LED Take the opposite
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_C)); // Wait for the key to release Prevent the program from running repeatedly without releasing the key
}
}
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)){
// Read the level of the key interface
if(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)){
// Read the level of the key interface
GPIO_WriteBit(LEDPORT,LED2,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED2))); //LED Take the opposite
while(!GPIO_ReadInputDataBit(TOUCH_KEYPORT,TOUCH_KEY_D)); // Wait for the key to release Prevent the program from running repeatedly without releasing the key
}
}
}
}
Task 2 :
Key control corresponds to LED State flip
Code :
//main.c
int main (void){
// The main program
// Initializer
RCC_Configuration(); // The clock is set
LED_Init();//LED initialization
KEY_Init();// Key initialization
// Main circulation
// Initialization start LED Is off
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1));
GPIO_WriteBit(LEDPORT,LED4,(BitAction)(1));
while(1){
// There is a latch
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
// Read the level of the key interface
delay_ms(20); // Delay de jitter
if(!GPIO_ReadInputDataBit(KEYPORT,KEY1)){
// Read the level of the key interface
GPIO_WriteBit(LEDPORT,LED3,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED3))); //LED Take the opposite
while(!GPIO_ReadInputDataBit(KEYPORT,KEY1)); // Wait for the key to release
}
}
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2)){
// Read the level of the key interface
delay_ms(20); // Delay de jitter
if(!GPIO_ReadInputDataBit(KEYPORT,KEY2)){
// Read the level of the key interface
GPIO_WriteBit(LEDPORT,LED4,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED4))); //LED Take the opposite
while(!GPIO_ReadInputDataBit(KEYPORT,KEY2)); // Wait for the key to release
}
}
}
}
Be careful :
The corresponding should also be modified , Because the two one. LED The corresponding interface is inconsistent with that of the two small lights on the core board
led.h
#define LEDPORT GPIOA // Definition IO Interface
#define LED3 GPIO_Pin_2 // Definition IO Interface
#define LED4 GPIO_Pin_3 // Definition IO Interface
led.c
void LED_Init(void){
//LED Lamp interface initialization
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = LED3; // Select the port number (0~15 or all)
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // choice IO How the interface works
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Set up IO Interface speed (2/10/50MHz)
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
边栏推荐
- 联通入库|需要各地联通公司销售其产品的都需要先入总库
- 5-1 system vulnerability scanning
- Phpspreadsheet reading and writing Excel files
- Is it safe to open an account on the flush? Where to apply for opening an account
- Does Australia require that PVC plastic sheets comply with as/nzs 1530.3 with a flame spread index of 0?
- 5分钟快速上手 pytest 测试框架
- 【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例
- MySQL lock common knowledge points & summary of interview questions
- MooseFS基本概念总结
- Is it safe to open a stock account? Shanghai stock account opening.
猜你喜欢
![[multithreading] how to implement timer by yourself](/img/a9/dd9489c7a0028dd9d3a6dae9a71deb.png)
[multithreading] how to implement timer by yourself

AI scene Storage Optimization: yunzhisheng supercomputing platform storage practice based on juicefs
Why does copying files on a shared folder on a local area network (ERP server) result in the loss of the local Internet

免费将pdf转换成word的软件分享,这几个软件一定要知道!
详细聊聊MySQL中auto_increment有什么作用

在线文本数字识别列表求和工具

What if MySQL fails to store emoticons

2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合

新手必须知道的 Kubernetes 架构

还天天熬夜加班做报表?其实你根本不懂如何高效做报表
随机推荐
math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
Huawei's software testing director with 7 years' experience, several suggestions for all students who want to switch to software testing
0. grpc环境搭建
laravel 关联模型 多态关系
每日刷题记录 (八)
解题元宇宙,网络游戏中的多元通信方案
Summer rainbow comes for dinner
Ce CDC Flink peut - il être utilisé pour la synchronisation incrémentale d'Oracle à MySQL?
正如以往我们对于互联网的看法一样,我们对于互联网的认识开始变得深度
Analyze apache SH script
LeetCode85+105+114+124
Use the leader election mechanism in kubernetes to complete your own ha application
触摸按键与按键控制对应的LED状态翻转
leetcode:91. 解码方法【dfs + 记忆化】
从检查点恢复后读取不到mysql的数据有那位兄台知道原因吗
Numpy array creation
Phpspreadsheet reading and writing Excel files
深入解析kubernetes中的选举机制
华为7年经验的软件测试总监,给所有想转行学软件测试的同学的几个建议
Digital tracking analysis of insurance services in the first quarter of 2022