当前位置:网站首页>Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
Learning notes 51 single chip microcomputer keyboard (non coding keyboard and coding keyboard, scanning mode of non coding keyboard, independent keyboard, matrix keyboard)
2022-06-13 01:47:00 【It's Beichen bupiacra】
This blog post mainly records 51 Single chip computer keyboard ( Non coding keyboard and coding keyboard 、 Scanning mode of non coding keyboard 、 Independent keyboard 、 Matrix keyboard 、 Keyboard shake elimination, etc ) Including schematic diagram 、 Code etc.
( One ) Basic supplement
1. Keyboard tasks
(1) Determine whether a key is pressed ? If you have any , Go to the next step .
(2) Identify which key is pressed , And find the corresponding key value .
(3) According to the key value , Find the handler entry for the corresponding key .
2. Keyboard recognition

Whether the key is closed or not , Reflected in the output voltage of the line, it shows a high level or a low level , The single chip microcomputer detects the high and low states of the line level , You can confirm whether the key is pressed or released . In order to ensure that the single-chip computer only confirms that the key is valid once for a key action ( The so-called keys are effective , After pressing the key , Be sure to loosen it again ), The jitter period must be eliminated t1 and t3 Influence .
3. How to eliminate key jitter
(1) Use software delay to eliminate key jitter , The basic idea is : When a key press is detected , The line corresponding to this key is low level , Execute a delay 10ms After subroutine of , Confirm whether the line level is still low , If it is still low , Then confirm that there are keys pressed in this line . When the key is released , The low level of the line changes to the high level , Execute a delay 10ms After subroutine of , Detect that the line is at high level , It indicates that the key has indeed been released .
(2) Use a dedicated keyboard / Display interface chip , This kind of chip has a hardware circuit to automatically remove jitter .
4. Non coding keyboard and coding keyboard
(1) A non coded keyboard is a key press , Key number information cannot be obtained directly , To obtain through software . Non coding keyboard has two common structures: independent keyboard and matrix keyboard .
(2) The coding keyboard means that when the key is pressed , You can get the key number of the key directly , For example, use a dedicated keyboard interface chip .
5. Scanning mode of non coding keyboard
(1) Query scan :
When the SCM is idle , Call the keyboard scanning subroutine , Scan the keyboard repeatedly , To respond to keyboard input requests , If the query frequency of the single chip microcomputer is too high , Although it can respond to keyboard input in time , But it will also affect the progress of other tasks . If the frequency of queries is too low , There may be missing judgment of keyboard input . So according to the busy degree of the single-chip microcomputer system and the operating frequency of the keyboard , To adjust the frequency of keyboard scanning .
(2) Time scan :
SCM can scan the keyboard every certain time , I.e. regular scanning . This method usually uses the timer in the MCU to generate the timing interrupt , Scan the keyboard after entering the interrupt subroutine , Recognize the pressed key when a key is pressed , And execute the corresponding key handler . As the time of each key press is generally not less than 100ms, So in order not to miss the valid keys , The period of timing interrupt is generally less than 100ms.
(3) Interrupt scan :
In order to further improve the working efficiency of the single chip computer scanning keyboard , Interrupt scanning mode can be adopted , That is, the keyboard only works when a key is pressed , Will send an interrupt request signal to the MCU . MCU response interrupt , Execute the keyboard scan interrupt service subroutine , Identify the pressed key , And jump to the handler of the key . If no key is pressed , SCM will ignore the keyboard . The advantage of this method is that it can only be processed when a key is pressed , So it has strong real-time , Work efficiently .
( Two ) Independent keyboard
1. Schematic diagram of independent keyboard

The characteristic of a freestanding keyboard is that the keys are independent of each other , Each button is connected to one I/O Mouth line , By testing I/O The level state of the input line of the port , It's easy to tell which button is pressed .
2. Independent keyboard K1 control LED1 Code implementation
#include<reg52.h>
sbit led1=P2^0;// because led1 from p2^0 Mouth control
sbit k1=P3^1;//P31 The output level of the port is determined by the key k1 control
void delay(int i)
{
while(i--);
}
void keyproc()
{
if(k1==0)
{
delay(1000);// Delay chattering
if(k1==0)
{
led1=~led1;
}
while(!k1) ;
}
}
void main()
{
while(1)
{
keyproc();
}
}
( 3、 ... and ) Matrix keyboard
1. Schematic diagram of matrix keyboard

Matrix form ( Also called determinant ) The keyboard is usually used for occasions with a large number of keys , It consists of row lines and column lines , The key is on the line 、 At column intersections , The interface circuit is shown in the figure above .
2. Matrix keyboard corresponds to nixie tube output 0 To code implementation
#include "reg52.h"
typedef unsigned char uchar;
typedef unsigned int uint;
#define GPIO_DIG P0
#define GPIO_KEY P1
uchar code smgduan[16]= {
0x3f, 0x06, 0x5b, 0x4f,
0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c,
0x39, 0x5e, 0x79, 0x71};// Static nixie tube code value
uint KeyColValue;
uint KeyLineValue;
void delay(uint i) // The time delay function
{
while(i --);
}
void KeyDown() // Keyboard key scanning function
{
char a;
GPIO_KEY = 0x0f;
if(GPIO_KEY != 0x0f)// testing 4 Whether or not the key in which row of the row is pressed
{
delay(1000); // Delay chattering
if(GPIO_KEY != 0x0f) // Test again 4 Whether or not the key in which row of the row is pressed
{
switch(GPIO_KEY) // according to IO To determine which line of keys to press
{
case(0x07): KeyColValue = 0; break;
case(0x0b): KeyColValue = 1; break;
case(0x0d): KeyColValue = 2; break;
case(0x0e): KeyColValue = 3; break;
}
}
}
GPIO_KEY = 0xf0;
if(GPIO_KEY != 0xf0) // testing 4 Whether the key in which column of the row is pressed
{
delay(1000); // Delay chattering
if(GPIO_KEY != 0xf0) // Test again 4 Whether the key in which column of the row is pressed
{
switch(GPIO_KEY) // according to IO To determine which column of keys to press
{
case(0x70): KeyLineValue = 0; break;
case(0xb0): KeyLineValue = 1; break;
case(0xd0): KeyLineValue = 2; break;
case(0xe0): KeyLineValue = 3; break;
}
}
while((a < 50) && (GPIO_KEY != 0xf0)) // Time delay , Make sure no key is pressed again
{
delay(1000);
a ++;
}
}
}
void main()
{
while(1)
{
KeyDown();// Check whether the key is pressed
GPIO_DIG = smgduan[KeyLineValue*4 + KeyColValue];// According to the row and column values of the keys , The static digital tube displays the corresponding value
}
}
The last thing I want to say is , See the last friends , If it works for you , Like collection !
It's not easy to create , We still need your support , Continue to update later 51 MCU series .
边栏推荐
- PyFlink实现自定义SourceFunction
- Matplotlib drawing Chinese garbled code
- [official document summary] writing standards for academic dissertations of National University of science and technology
- 指针链表的实现
- Wsl2 + vcxsrv + opengl3.3 configuration
- [Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析
- 开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧
- Pytoch freeze pre training weights (feature extraction and BN layer)
- MySQL ---- where后使用字段别名
- Copy (copy) constructors and assignment overloaded operators=
猜你喜欢

How to solve the problems when using TV focusable to package APK in uni app

Detailed understanding of white noise

开发者来稿|AMD赛灵思中文论坛分享 - 提问的智慧

Magics 23.0 how to activate and use the slice preview function of the view tool page

白噪声的详细理解

Jeux de plombiers

Use koa to mock data and set cross domain issues

Alertwindowmanager pop up prompt window help (Part 1)

Introduction to ROS runtime

Establishment of microservice development environment
随机推荐
Plumber game
一种不带CPU的DPU架构:Hyperion
Pyflink implements custom sourcefunction
【官方文件汇总】国科大学位论文撰写规范
The first cell of devaxpress CXGRID after inserting a row is in focus editing status
[learn FPGA programming from scratch -21]: Advanced - Architecture - VerilogHDL coding specification
Numpy multidimensional array transpose transpose
This of phaser3 add. add. image
Delphi7 compressed pictures (BMP, JPG, PNG)
指针链表的实现
V-inline-date, similar to Ctrip, flying pig, time selection with price
Topic creation and running example of ROS
How to solve practical problems through audience positioning?
Stm32 3*3 matrix key (register version)
About tkinter Canvas does not display pictures
Shell command notes
【软考】软件设计师知识点整理(待更新)
6、 Implementation of warehouse out management function
深度学习调参技巧详解
About inquirerjs