当前位置:网站首页>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 .
边栏推荐
- What is solid angle
- Pytoch freeze pre training weights (feature extraction and BN layer)
- CXGRID keeps the original display position after refreshing the data
- csdn涨薪技术之Jmeter接口测试数据库断言的实现与设计
- Matplotlib drawing Chinese garbled code
- TensorFlow 2. X multi graphics card distributed training
- Using atexit to realize automatic destruct of singleton mode
- How to solve practical problems through audience positioning?
- #pragma comment(lib,“urlmon.lib“)
- Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
猜你喜欢

leetcode743. Network latency (medium, Dijkstra)

水管工遊戲

Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)

三、上传织物图片至SQL Server并提供name进行展示织物照片

leetcode743. 网络延迟时间(中等, dijkstra)

Introduction to ROS runtime

如何通过受众群体定位解决实际问题?

Jeux de plombiers

pringboot之restfull接口规范注解(二)

Implementation of pointer linked list
随机推荐
Establishment of microservice development environment
Use mediapipe+opencv to make a simple virtual keyboard
4、 Improvement of warehousing management function
PyFlink实现自定义SourceFunction
五、库存查询功能的完善
About inquirerjs
How do you use your own data to achieve your marketing goals?
什么是立体角
Pyflink implements custom sourcefunction
详细受众特征详细解释
Devaxpress Chinese description --tcximagelist (enhanced image list control)
Devaxpress Chinese description --tcxpropertiesstore (property store recovery control)
Differences among bio, NiO and AIO
Logging system in chromium
Plumber game
Machine learning basic SVM (support vector machine)
5、 Improvement of inventory query function
Phaser3 load
Project training (XVII) -- personal work summary
Delphi implements adding a column of serial number to the CXGRID list