当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

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

解题元宇宙,网络游戏中的多元通信方案

Summer rainbow comes for dinner

Optional类的高级使用

啃下大骨头——排序(一)

Three development trends of enterprise application viewed from the third technological revolution

math_基本初等函数图型(幂函数/指数/对数/三角/反三角)

华为7年经验的软件测试总监,给所有想转行学软件测试的同学的几个建议

Daily mathematics serial 54: February 23

掌握这28张图,面试再也不怕被问TCP知识了
随机推荐
《天天数学》连载54:二月二十三日
服务器快速搭建AList集成网盘网站【宝塔面板一键部署AList】
Underlying principles of file operations (file descriptors and buffers)
Efficient implementation of dynamiccast with template function and specialization function
MooseFS基本概念总结
MySQL lock common knowledge points & summary of interview questions
static关键字续、继承、重写、多态
Is it safe to open a stock account? Shanghai stock account opening.
阶段性总结与思考
5-2Web应用程序漏洞扫描
深入解析kubernetes controller-runtime
Online text digit recognition list summation tool
grpc的开发详解
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
[php8+oracle11g+windows environment without tools] Intranet / no network /win10/php connecting to Oracle database instance
Number theory - division and blocking
解题元宇宙,网络游戏中的多元通信方案
If I am in Zhuhai, where can I open an account? Is it safe to open an account online?
科大讯飞 AI 学习机暑期新品发布会 AI + 教育深度结合再创产品新高度
Is it safe to open an account on the flush? Where to apply for opening an account