The concept and principle of single chip independent key and matrix keyboard

Publisher:TechVoyagerLatest update time:2018-11-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 1. Basic knowledge


1.Key classification and input principle


According to the structural principle, the keys are divided into two categories: one is the contact switch key, such as mechanical switch, conductive rubber switch light; the other is the non-contact switch key, such as electrical key, magnetic induction key, etc. The former is low in cost, and the latter has a long life. At present, the most common type of switch key in the microcomputer system is the contact switch key.


In the single-chip microcomputer application system, except for the reset button which has a dedicated reset circuit and a dedicated reset function, other buttons are used to set control functions or input data in a switch state. When the set function key or number key is pressed, the computer application system should complete the function set by the button. Key information input is a process closely related to the software structure.


For a group of keys or a keyboard, there is always an interface circuit connected to the CPU. The CPU can use query or interrupt mode to know whether a key is input, check which key is pressed, send the key number to the accumulator, and then jump to the function program that executes the key through a jump instruction, and return to the main program after execution.


2.Key structure and features


Microcomputer keyboards usually use mechanical contact key switches, whose main function is to convert mechanical on and off into electrical logical relationships. In other words, it can provide standard TTL logic levels to facilitate the compatibility of logic levels of general digital systems. When a mechanical key is pressed or released again, due to the influence of mechanical elasticity, it is usually accompanied by a certain period of mechanical contact jitter before its contact stabilizes. The jitter process is shown in Figure 1 below. The length of the jitter time is related to the mechanical characteristics of the switch, generally 5-10ms. Detecting the on and off of the key during the contact jitter may lead to misjudgment, that is, a single press or release of the key is mistakenly considered as multiple operations, which is not allowed. In order to overcome the misjudgment caused by the mechanical jitter of the key contact, de-jittering measures must be taken. When there are fewer keys, hardware de-jittering can be used; when there are more keys, software de-jittering can be used.


1.png 

Figure 1 Mechanical jitter of key contacts


(1)Key coding


A group of keys or keyboards must query the switch status of the keys through the I/O port line. Different encodings are used according to the keyboard structure. Regardless of whether there is encoding or not, and what encoding is used, it must be converted into a key value corresponding to the value in the accumulator in the end to realize the jump of the key function program.


(2) Keyboard Program


A complete keyboard control program should have the following functions:


a. Detect whether any key is pressed and take hardware or software measures to eliminate jitter.


b. Have a reliable logic processing method. Only one key is processed at a time, and any key operation during the process will not affect the system. No matter how long a key is pressed, the system will only execute the key function program once.


c. Accurately output the key value (or key number) to meet the jump instruction requirements.


3. Independent buttons and matrix keyboard


(1) Independent buttons


In a single-chip microcomputer control system, if only a few function keys are required, an independent key structure can be used.


Independent key type is a single key circuit directly constructed by I/O port line. Its characteristics are that each key occupies one I/O port line separately, and the operation of each key will not affect the status of other I/O port lines. The typical application of independent keys is shown in the figure. The independent key circuit has flexible configuration and simple software structure, but each key must occupy one I/O port line. Therefore, when there are many keys, the I/O port line is wasted a lot and it is not suitable to use it. Independent keys are shown in Figure 2.


2.png 

Figure 2 Independent keyboard


The software for independent buttons often adopts a query structure. First, the input status of the I/O port lines is queried bit by bit. If the input of an I/O port line is low, it can be confirmed that the button corresponding to the I/O port line has been pressed, and then the function processing program of the key is turned to.


(2) Matrix keyboard


In a single-chip microcomputer system, if a large number of keys are used, such as an electronic password lock, a telephone keypad, etc., there are generally at least 12 to 16 keys, and a matrix keyboard is usually used.


Matrix keyboard is also called row-column keyboard. It is a keyboard composed of four I/O lines as row lines and four I/O lines as column lines. A key is set at each intersection of row lines and column lines. In this way, the number of keys on the keyboard is 4*4. This row-column keyboard structure can effectively improve the utilization rate of I/O ports in the single-chip microcomputer system.


How Matrix Keyboard Works


The most common keyboard layout is shown in Figure 3. It is generally composed of 16 keys. In the single-chip microcomputer, one P port can be used to realize the functions of 16 keys. This is also the most commonly used form in the single-chip microcomputer system. The internal circuit of the 4*4 matrix keyboard is shown in Figure 4.


3.png 

Figure 3 Matrix keyboard layout

4.png 

Figure 4 Internal circuit diagram of matrix keyboard


When no key is closed, there is an open circuit between P3.0~P3.3 and P3.4~P3.7. When a key is closed, the two I/O port lines connected to the closed key are short-circuited. The method to determine whether a key is pressed is: the first step is to set the column lines P3.4~P3.7 to the input state, output low level from the row lines P3.0~P3.3, read the column line data, if a column line is low level, then a key is closed on the column line. The second step is that the row lines output low level in turn, read data from the column lines P3.4~P3.7, if a column is low level, then a key is pressed on the corresponding row line. Combining the results of the first and second steps, the key number can be determined. However, the key function operation can only be performed once when the key is closed, so it is necessary to wait until the key is released before performing the key function operation, otherwise pressing the key once may cause the same key operation to be performed multiple times in a row.


There are many ways to identify keys, among which the most common method is scanning.


When a key is pressed, the row line and column line connected to this key are connected, and the row line is at a high level when no key is pressed. If all column lines are at a high level, whether a key is pressed or not will not cause the row line level to change, so all column lines must be at a level. In this way, when a key is pressed, the row level where the key is located will change from high to low. Only then can it be determined that a key is pressed in the corresponding row.


The number of independent keys is small, and they can be flexibly encoded according to actual needs. In a matrix keyboard, the position of a key is uniquely determined by the row number and column number, so the row number and column number can be binary encoded separately, and then the two values ​​are combined into a byte, with the upper 4 bits being the row number and the lower 4 bits being the column number.


4. How the keyboard works


The response to the keyboard depends on the working mode of the keyboard. The working mode of the keyboard should be determined according to the working condition of the CPU in the actual application system. The principle of selection is to ensure that the CPU can respond to key operations in time without taking up too much CPU working time. Usually there are three working modes of the keyboard: programmed scanning, timed scanning and interrupt scanning.


(1) Programming scanning method


The program scanning method uses the idle time of the CPU to complete other tasks and calls the keyboard scanning subroutine to respond to keyboard input requirements. When executing the key function program, the CPU will no longer respond to key input requirements until the CPU rescans the keyboard.


(2) Timing scanning mode


The timing scanning mode is to scan the keyboard once every certain period of time. It uses the internal timer of the microcontroller to generate a certain time (for example, 10ms) timing. When the timing is reached, a timer overflow interrupt is generated. After the CPU responds to the interrupt, it scans the keyboard, identifies the key when it is pressed, and then executes the function program of the key.


(3) Interrupt scanning mode


In the above two keyboard scanning modes, the CPU will scan the keyboard regularly regardless of whether a key is pressed or not. However, when the microcontroller application system is working, keyboard input is not often required. Therefore, the CPU is often in an empty scanning state.


In order to improve the CPU working efficiency, the interrupt scanning working mode can be adopted. Its working process is as follows: when no key is pressed, the CPU handles its own work, and when a key is pressed, an interrupt request is generated, and the CPU switches to execute the keyboard scanning subroutine and identify the key number.


Reference address:The concept and principle of single chip independent key and matrix keyboard

Previous article:Really useful! Getting started from 51 to stm32 development
Next article:51 MCU program download, ISP and serial port basics

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号