当前位置:网站首页>(4) Independent key
(4) Independent key
2022-06-26 08:19:00 【I love notes】
Previously, I recalled the relevant contents of key output , Now you can continue to work on the contents of the key input , In this section, we will find out the relevant contents of an independent key to recall the relevant contents of key input , Make an... By the judgment of an independent key led The light is on and off .
1. About keys
The keyboard is divided into coding keyboard and non coding keyboard , The recognition of the coding keys on the keyboard is realized by a special hardware encoder , And generate code number or key value is called coding keyboard , Such as computer keyboard . The keyboard recognized by software programming is called non coding keyboard . Non coding keyboard is divided into independent keyboard and determinant keyboard . The keyboard on the single-chip microcomputer is generally a non coding keyboard , In this section, I mainly talk about the independent keyboard .
2. Hardware
In fact, the detection of cases is relatively simple , The detection principle of our button is very simple , We first pass gpio The port is connected with the key and the ground , As shown in the figure below :

our gpio As input, it defaults to high level , At this time, we check the status of the four buttons at the same time , If gpio Low level detected , Then this button is pressed . But in practice, when we press , He didn't immediately switch from high to low , It's a period of jitter , Is a period of high and low levels , We can complete the work by hardware and software , But the hardware circuit is usually used to eliminate the jitter , This is more complicated and costly , So we basically choose the way of software anti shake , If a key has been pressed , We delayed about 10ms about , Check again , If it is still low level , It proves that the key was pressed .
3. Software
After pressing the key, the corresponding led The main procedures for light on are as follows :
#include "key.h"
void delay(int xms){
int i,j;
for(i = 0;i < xms;i++){
for(j = 0;j < 100;j++);
}
}
int key_scan(){
if(k0==0){
delay(10);
if(k0==0){
led0 = !led0;
}
while(!k0);
}
else if(k1==0){
delay(10);
if(k1==0){
led1 = !led1;
}
while(!k1);
}
else if(k2==0){
delay(10);
if(k2==0){
led2 = !led2;
}
while(!k2);
}
if(k3==0){
delay(10);
if(k3==0){
led3 = ~led3;
}
while(!k3);
}
else {
}
return 0;
}
The principle has been made clear , By the way, put the calling function and the header file together , The header file code is as follows :
#ifndef _KEY_H_
#define _KEY_H_
#include "reg52.h"
sbit k1 = P3^1;
sbit k0 = P3^0;
sbit k2 = P3^2;
sbit k3 = P3^3;
sbit led0 = P2^0;
sbit led1 = P2^1;
sbit led2 = P2^2;
sbit led3 = P2^3;
int key_scan();
void delay();
#endifThe main function calling functions are as follows :
#include "reg52.h"
#include "key.h"
void main(){
while(1){
key_scan();
}
}Here we are , Our individual buttons are complete
边栏推荐
- JS Date object
- Method of measuring ripple of switching power supply
- Software engineering - high cohesion and low coupling
- Jz-063- median in data stream
- 批量修改文件名
- 教你几招:30句哄女孩的“霸道”温馨话,不看后悔!
- CodeBlocks integrated Objective-C development
- Relevant knowledge of DRF
- Crawler case 1: JS reversely obtains HD Wallpapers of minimalist Wallpapers
- MySQL query time period
猜你喜欢

Win11 open folder Caton solution summary

Go语言浅拷贝与深拷贝

Chapter VII (structure)

Color code

Real machine debugging of uniapp custom base

Ora-12514: tns: the listener currently does not recognize the service requested in the connection descriptor

Read excel table and render with FileReader object
GHUnit: Unit Testing Objective-C for the iPhone

Oracle database self study notes

WiFi-802.11 2.4G频段 5G频段 信道频率分配表
随机推荐
RF filter
drf的相关知识
Go language shallow copy and deep copy
Idea automatically sets author information and date
Monitor iPad Keyboard Display and hide events
Real machine debugging of uniapp custom base
Common uniapp configurations
golang json unsupported value: NaN 处理
Uniapp uses uviewui
"System error 5 occurred when win10 started mysql. Access denied"
What is Qi certification Qi certification process
Test method - decision table learning
Mapping '/var/mobile/Library/Caches/com. apple. keyboards/images/tmp. gcyBAl37' failed: 'Invalid argume
ASP. Net and Net framework and C #
[postgraduate entrance examination] group planning exercises: memory
Undefined symbols for architecture i386 is related to third-party compiled static libraries
Learn signal integrity from zero (SIPI) - (1)
1002: easy to remember phone number
JWT in go
Rewrite string() method in go language