51 MCU Tutorial (4): Independent-Matrix Keyboard Application and Design

The previous lecture introduced and applied the serial port communication of the single-chip microcomputer, and gave an example. From this lecture, the peripheral circuits of the microcontroller will be introduced. This lecture introduces the circuit of the external keyboard of the single-chip microcomputer. Through this lecture, readers can master the working principle of the external keyboard of the single-chip microcomputer to realize the circuit and program design.

Introduction: The above lecture introduces and applies the serial communication of the single-chip microcomputer, and gives an example. From this lecture, the peripheral circuits of the microcontroller will be introduced. This lecture introduces the circuit of the external keyboard of the single-chip microcomputer. Through this lecture, readers can master the working principle of the external keyboard of the single-chip microcomputer to realize the circuit and program design.

1. Introduction to the principle

The keyboard interface circuit is a very important part of the single-chip system design, as the most commonly used input device in the human-computer interaction interface. We can realize simple man-machine communication by entering data or commands through the keyboard. Before designing the keyboard circuit and program, we need to understand some knowledge of the keyboard and the keys that make up the keyboard.

1. Classification of buttons

Generally speaking, buttons can be divided into two categories according to the structural principle, one is contact switch buttons, such as mechanical switches, conductive rubber switches, etc.; the other is non-contact switch buttons, such as electrical buttons. Magnetic induction keys, etc. The former is low in cost and the latter has a long lifespan. At present, the most common type of microcomputer system is the contact switch button (such as the button used on this learning board).

According to the interface principle, the keys can be divided into two types: coded keyboards and non-coded keyboards. The main difference between the two types of keyboards is the method of identifying key symbols and giving corresponding key codes. The coded keyboard mainly uses hardware to realize the key recognition, and the non-coded keyboard mainly uses the software to realize the keyboard recognition.

The full-coded keyboard is realized by a special chip to recognize keys and output corresponding codes. Generally, it also has protection circuits for de-jittering, multi-key and key shifting. This kind of keyboard is convenient to use, has a large hardware overhead, and generally has fewer small embedded application systems. use. Non-coding keyboards can be divided into independent type and matrix type according to the connection mode, and other tasks are mainly completed by software. Because of its economy and practicality, it is mostly used in single-chip microcomputer systems (this learning board also uses non-coding keyboard).

2. Key input principle

In single-chip application systems, mechanical contact type key switches are usually used, whose main function is to convert mechanical on-offs into electrical logical relationships. In other words, it can provide standard TTL logic levels to be compatible with the logic levels of general digital systems. In addition, except for the reset button which has a dedicated reset circuit and a dedicated reset function, the other buttons are used to set the control function or input data in the switch state. When the set function key or number key is pressed, the computer application system should complete the function set by the key. Therefore, key information input is a process closely related to the software structure. For a group of keys or a keyboard, it is connected to the single-chip microcomputer through an interface circuit. The single-chip microcomputer can use query or interruption to find out whether there is a key input and check which key is pressed. If a key is pressed, it will jump to the corresponding keyboard processing program to execute it. If no key is pressed, it will continue to execute other programs.

3. Key features and debounce

When a mechanical button is pressed or released again, due to the effect of mechanical elasticity, it is usually accompanied by a certain period of mechanical vibration of the contact, and then the contact is stabilized. The jitter process is shown in Figure 1(a). The length of the jitter time is related to the mechanical characteristics of the switch, which is generally 5 to 10 ms. It can be seen from the figure that detecting the on and off state of the button during the period of contact shaking may lead to errors in judgment. That is, a key press or release is mistakenly considered as multiple operations, this situation is not allowed. In order to overcome the detection misjudgment caused by the mechanical jitter of the button contacts, debounce measures must be taken, which can be considered from both the hardware and software aspects. Generally speaking, when the number of keys is small, hardware debouncing can be used, and when the number of keys is large, software debouncing can be used. (This learning board uses software debounce method). The flowchart of software debounce is shown in Figure 1(b).

51 MCU Tutorial (4): Independent-Matrix Keyboard Application and Design

From the key debounce flow chart, we can know that when it detects that a key is pressed, it should wait for a period of time (you can call a 5ms~10ms delay subroutine), and then judge whether the key has been pressed again. If it is judged that the button is still pressed, the button is considered valid. If it is judged that the button is not pressed at this time, it means that the button is jittering or interference, and you should return to judge again. The corresponding processing procedure can only be carried out when the keyboard is actually pressed. At this time, even if the key input is basically realized, it can be further judged whether the key is released.

Second, the circuit is explained in detail

The circuit diagram is shown in Figure 2.

51 MCU Tutorial (4): Independent-Matrix Keyboard Application and Design

It can be seen from Figure 2 that the independent buttons adopt a separate I/O line structure for each button. When the key is pressed and released, the level input to the I/O port of the microcontroller is different. Therefore, it can be judged whether there is a key pressed and which key is pressed according to the change of the level of different ports. It can be seen from Figure 2(a) that the button is connected to the MCU pin and a pull-up resistor is added, so that when no button is pressed, the I/O input level is high. When a button is pressed When, the I/O input level is low.

Although the independent button circuit configuration is flexible and the software structure is simple, each button must occupy an I/O port line. Therefore, when there are more buttons, the I/O port line is wasted. For more complex systems or occasions with more keys, a matrix keyboard can be used. The 4×4 matrix keyboard shown in Figure 2(b) is similar to other matrix keyboards.

The 4×4 matrix keyboard is composed of 4 row lines and 4 column lines intersecting, and the keys are located at the intersections of the rows and columns, thus forming 16 keys. The row and column lines at the intersection are not connected. When the button is pressed, the row and column lines at the intersection are turned on. Figure 2(b) The row line is connected to VCC through a pull-up resistor. When no key is pressed, the row line is at a high level; when a key is pressed, the row and column lines are turned on at the intersection. At this time, the level of the row line will be determined by the level of the column line connected to the row line . This is the key to identifying whether the button is pressed. However, each row line in the matrix keyboard intersects with 4 column lines. Whether the key at the intersection point is pressed or not affects the level of the row line and column line where the key is located. The keys will affect each other. The key analysis must be Only when the row line and column line signals are combined for proper processing, the position of the closed key can be determined.

It is worth noting that the matrix keyboard introduced in this article adds a four-input AND gate chip 74HC21 to the output of the traditional matrix keyboard. When one of the four inputs is low, the output is low. Connect the output terminal of 74HC21 to the external interrupt 0 (P32 pin) of the microcontroller, so that in the case of high real-time requirements, set P00~P03 to be all low and wait for the button to be triggered. When any button is pressed , The system will enter the interrupt service program, which improves the keyboard response time, which is very practical in the case of high real-time requirements of the system.

Three, program design

The key procedures of this design example are as follows.

Independent keystroke program

#define keyio P0 (1)

#define key1 P0_3 (2)

keyio|=0X0F; (3)

if (key1==0) (4)

{

delay_nms(20); (5)

if (key1==0) (6)

{

while (key1==0); (7)

return 1; (8)

}

}

Program description:

(1) Define the button pins.

(2) Define button connection pins.

(3) Connect the button to the pin to output a high level to receive input.

(4) If the button connected to the pin is pressed at this time.

(5) Delay for a period of time to debounce.

(6) If the key is still pressed at this time, the key is valid at this time.

(7) Waiting for the button to be lifted, endless loop, if the button is kept pressed, it will wait forever.

(8) Return the key value.Matrix keyboard program

#define KEYIO P0 (1)

code ksp[4]={0x7F, 0xBF, 0xDF, 0xEF}; (2)

unsigned char keypad_scan() (3)

{

char key, i; (4)

KEYIO=0xF0; (5)

if (KEYIO!=0xF0) (6)

{

for(i=0;i《=3;i++) (7

{

delaykey(10); (8)

KEYIO=ksp[i]; (9)

delaykey(10); (10)

if (KEYIO!=ksp[i]) (11)

{

delaykey(10); (12)

key=KEYIO; (13)

while (KEYIO==key); (14)

return (key); (15)

}

}

}

}

Program description:

(1) Define the matrix keyboard pins.

(2) Define the four output level states of the pins used during scanning into an array.

(3) Key scan program.

(4) Define two temporary variables key, i.

(5) Let the high four digits of the keyboard pins output high level, and the fourth digit is low level, ready for scanning keys.

(6) If the pin status level changes at this time.

(7) Assign the values ​​in the previously defined array to the pins and start scanning one by one.

(8) Delay for a period of time to debounce.

(9) Output the level of the scan button.

(10) Delay for a while.

(11) If the key pin level is still not the default output level at this time, it means that a key is pressed.

(12) Delay for a period of time to stabilize the level.

(13) Read the current key pin level, that is, the key value.

(14) Waiting for the button to be lifted, an endless loop, if the button is kept pressed, it will wait forever.

(15) Return the key value.

Four, debugging points and experimental phenomena

Connect the hardware and generate the program by cold start. After the hex file is downloaded to the microcontroller and running, open the serial debugging assistant software, set the baud rate to 9600, reset the microcontroller, and then press any of the 4×4 buttons on the board, and pay attention to the Display on the serial debugging assistant. (See Figure 3), you can observe the data Display with buttons in the receiving window.

51 MCU Tutorial (4): Independent-Matrix Keyboard Application and Design

In addition, in the experimental program attached to this article, the sending character function and the sending character string function are called in the communication with the serial port. When there is no emulator and some prompt information needs to be displayed, the serial port printing method can be used, which is not only intuitive and convenient but also does not increase other costs.

Five, summary

This article introduces the working principle of the external keyboard of the single-chip microcomputer and gives an example. Through this article, we can know that a perfect keyboard control program should have the following functions:

(1) Detect whether a button is pressed, and take hardware or software measures to eliminate the influence of the mechanical contact jitter of the keyboard buttons.

(2) There is a reliable logical processing method. Only one button is processed at a time, during which any button operation will not affect the system, and no matter how long one button time is, the system only executes the button function program once.

(3) Accurately output the key value (or key number) to meet the key function requirements. For the matrix keyboard, the row line and column line signals must be combined for proper processing to determine the position of the closed key.

In addition, there are many ways to scan buttons. The one described in this article is the program scan method. In addition, there are also regular scan methods and interrupt scan methods. These methods can be implemented on this learning board. You can write and debug the program by yourself. The next lecture will describe the principle and examples of the dynamic drive of the single-chip microcomputer, so stay tuned.

The Links:   LM190E08-TLG4 ADV7612BSWZ

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *