51 MCU Tutorial from Scratch - 27 Matrix Keyboard Interface Technology and Program Design

Publisher:CW13236066525Latest update time:2012-02-16 Keywords:keyboard Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Matrix keyboard interface technology and program design

When there are many buttons in the keyboard of the single-chip computer system, in order to reduce the occupation of the I/O port, the buttons are often arranged in a matrix form, as shown in Figure 1. In a matrix keyboard, each horizontal line and vertical line are not directly connected at the intersection, but are connected through a button. In this way, one port (such as P1 port) can form 4*4=16 buttons, which is twice as many as directly using the port line for the keyboard, and the more lines there are, the more obvious the difference is. For example, adding one more line can form a 20-key keyboard, while directly using the port line can only add one more key (9 keys). It can be seen that when the number of keys required is relatively large, it is reasonable to use the matrix method to make the keyboard.

Click to browse the next page

The matrix keyboard is obviously more complicated than the direct method, and the recognition is also more complicated. In the figure above, the column line is connected to the positive power supply through a resistor, and the I/O port of the microcontroller connected to the row line is used as the output end, while the I/O port connected to the column line is used as the input. In this way, when the button is not pressed, all output ends are high level, indicating that no key is pressed. The row line output is low level. Once a key is pressed, the input line will be pulled low. In this way, by reading the state of the input line, it can be known whether a key is pressed. The specific recognition and programming methods are described as follows.

Button identification method of matrix keyboard

A "row scanning method" is introduced to determine which key is pressed on a matrix keyboard.

Row scanning method Row scanning method is also called row (or column) scanning query method. It is one of the most commonly used button recognition methods. As shown in the keyboard above, the process is introduced as follows.

To determine whether a key is pressed on the keyboard, set all row lines Y0-Y3 to low level, and then detect the status of the column lines. As long as the level of one column is low, it means that a key is pressed on the keyboard, and the closed key is located in the 4 buttons where the low-level line intersects with the 4 row lines. If all column lines are high, no key is pressed on the keyboard.

After confirming that a key is pressed, the process of determining the specific closed key can be started. The method is: set the row lines to low level in sequence, that is, when a row line is set to low level, the other lines are high level. After determining that a row line is at a low level, the level state of each column line is detected row by row. If a column is low, the button at the intersection of the column line and the row line set to low level is the closed button.

Here is a specific example:

The diagram is still as shown above. The P1 port of the 8031 ​​microcontroller is used as the keyboard I/O port. The column line of the keyboard is connected to the lower 4 bits of the P1 port, and the row line of the keyboard is connected to the upper 4 bits of the P1 port. The column lines P1.0-P1.3 are respectively connected to the positive power supply +5V with 4 pull-up resistors, and the column lines P1.0-P1.3 are set as input lines, and the row lines P1.4-P.17 are set as output lines. The 4 row lines and the 4 column lines form 16 intersections.

Detect whether any key is currently pressed. The detection method is to output all "0" from P1.4-P1.7, read the status of P1.0-P1.3, if P1.0-P1.3 are all "1", no key is closed, otherwise a key is closed.

Remove key jitter. When a key is detected to be pressed, a delay is made before the next step of detection and judgment.

If a key is pressed, it should be identified which key is closed. The method is to scan the rows of the keyboard. P1.4-P1.7 outputs in the following 4 combinations:

P1.7 1 1 1 0

P1.6 1 1 0 1

P1.5 1 0 1 1

P1.4 0 1 1 1

Read P1.0-P1.3 when each group of rows is output. If all are "1", it means that the row "0" has no key closure, otherwise the key is closed. The row value and column value of the closed key are obtained, and then the row value and column value of the closed key can be converted into the defined key value by calculation or table lookup.

In order to ensure that the CPU only processes once each time the key is closed, the jitter when the key is released must be eliminated.

Click to browse the next page

Click to browse the next page


"MCU Matrix Keyboard Interface Technology and Programming"

Keyboard Scanner:

The flowchart of the MCU keyboard scanning program obtained from the above analysis is shown in Figure 2. The program is as follows

SCAN: MOV P1,#0FH

MOV A,P1

ANL A,#0FH

CJNE A,#0FH,NEXT1

SJMP NEXT3

NEXT1: ACALL D20MS

MOV A,#0EFH

NEXT2: MOV R1,A

MOV P1,A

MOV A,P1

ANL A,#0FH

CJNE A,#0FH,KCODE;

MOV A,R1

SETB C

RLC A

JC NEXT2

NEXT3: MOV R0,#00H

RIGHT

KCODE: MOV B,#0FBH

NEXT4: RRC A

INC B

JC NEXT4

MOV A,R1

SWAP A

NEXT5: RRC A

INC B

INC B

INC B

INC B

JC NEXT5

NEXT6: MOV A,P1

ANL A,#0FH

CJNE A,#0FH,NEXT6

MOV R0,#0FFH

RIGHT

This is just a brief introduction to the keyboard handler. In fact, keyboard and display processing are very complex. They generally account for most of the code of an application, which shows its importance. However, this complexity does not come from the microcontroller itself, but from the operator's habits and other issues. Therefore, before writing the keyboard handler, it is best to sort it out logically first, then express it with an appropriate algorithm, and finally write the code. In this way, the code can be written quickly and effectively.

Keywords:keyboard Reference address:51 MCU Tutorial from Scratch - 27 Matrix Keyboard Interface Technology and Program Design

Previous article:51 MCU Tutorial from Scratch—— 26 MCU Keyboard Interface Programming
Next article:51 MCU tutorial from scratch - some basic concepts about MCU

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号