当前位置:网站首页>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 .
边栏推荐
- Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
- 兴趣相似的受众群体
- Note: common gadgets in project architecture
- Stack stack LIFO
- leetcode743. 网络延迟时间(中等, dijkstra)
- Padavan mounts SMB sharing and compiles ffmpeg
- Should the audience choose observation mode or positioning mode?
- 路径字段是什么? ——竞价广告
- Copy (copy) constructors and assignment overloaded operators=
- 什么是立体角
猜你喜欢

6、 Implementation of warehouse out management function
![[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析](/img/b5/7ea603775dc0448368d209de037a43.png)
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析

关于tkinter.Canvas 不显示图片的问题

Implementation of pointer linked list

Project training (XVII) -- personal work summary

Run Presto under docker to access redis and Bi presentation

Startup, connection and stop of MySQL service

指针链表的实现

4、 Improvement of warehousing management function

谷歌加大型文字广告是什么?怎么用?
随机推荐
Compiling minicom-2.7.1 under msys2
Design of distributed game server
MySQL download and installation
Matplotlib drawing Chinese garbled code
Temporary objects and compilation optimization
[WSL2]限制WSL2可访问的硬件资源(CPU/内存)
VI keyboard diagram
[WSL2]WSL2迁移虚拟磁盘文件ext4.vhdx
Traversal of binary tree - first order traversal, middle order traversal, and second order traversal
A DPU architecture without CPU: Hyperion
Delphi Google API text to speech MP3 file
Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
About retrieving ignored files in cornerstone
Record the VMware installation process of VMware Tools and some problems encountered
6、 Implementation of warehouse out management function
dfs与bfs解决宝岛探险
如何利用您的自有数据来实现营销目标?
How do you use your own data to achieve your marketing goals?
TensorFlow 2.x 多显卡分布式训练
[Andoid][踩坑]CTS 11_r3开始出现的testBootClassPathAndSystemServerClasspath_nonDuplicateClasses FAIL问题分析