A Practical Design of Single Chip Microcomputer Matrix Keyboard

Publisher:cheng1984Latest update time:2017-09-02 Source: elecfansKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The matrix keyboard is a keyboard group with a matrix-like layout used in the external device of the single-chip microcomputer. The keyboard with a matrix structure is obviously more complicated than the direct method, and the recognition is also more complicated. The column line is connected to the positive power supply through a resistor, and the I/O port of the single-chip microcomputer connected to the row line is used as the output end, and the I/O port connected to the column line is used as the input. The advantage of the matrix keyboard is that it saves the IO port of the single-chip microcomputer. For example, the 8 IO ports of an ordinary keyboard can only be used as 8 keys, while the matrix keyboard can be used as 16 keys.

This article uses 51 single-chip microcomputer as the carrier to introduce the principle, circuit and software design points of a 4*4 matrix keyboard to realize 16 key operations.

1. General circuit of matrix keyboard

 A Practical Design of Single Chip Microcomputer Matrix Keyboard

Figure 1 Matrix keyboard circuit

As shown in the matrix keyboard circuit of Figure 1

, the 4*4 matrix keyboard has 4 rows and 4 columns of keys. The 4 I/O ports of the microcontroller are connected to the row lines of the matrix keyboard, and the other 4 I/O ports are connected to the column lines of the matrix keyboard. The key recognition and operation are completed by operating the row and column lines.

2. The principle of matrix keyboard

The process of matrix keyboard key recognition is generally as follows:

(1) Set the first row to low level (0) and the other rows to high level. Read the column line data. A low level on the column line indicates that a key is pressed in this row.

(2) Set the second row to a low level (0) and the remaining rows to a high level. Read the column line data. A low level on the column line indicates that a key is pressed in this row.

......

(N-1) According to the different levels of the row and column lines, it is possible to identify whether a key is pressed, which key is pressed, and obtain the key number.

(N) Jump to the corresponding key processing procedure according to the key number.

3. An example of a matrix keyboard program

 A Practical Design of Single Chip Microcomputer Matrix Keyboard

Figure

2 shows the main flow of key processing. The idea is to pull down each row of keys in turn, and then read the data of the column line. If the column line has a low level, it is considered that a key is pressed in this row, and the key is marked as pressed in this row and the row value is stored.

The idea of ​​reading column line data is to read columns 1-4 in sequence. If the column is low, mark and store the column value.

Here is some code:

//(1) Button recognition program

void key() //Key scan

{

unsigned char key_value_temp; //temporary key value, default 1111 1111 (binary)

    key_value=0xff;

 //key value

key_value_temp=0xff; //Key value temporary variable

//Matrix keyboard program flow

// Pull down the 1234th row in turn and read the column line data

Pin_r_1=0; Pin_r_2=1;Pin_r_3=1;Pin_r_4=1; //Pull row 1 low.

//Pin_r_1 is the first row of wire, Pin_r_2 is the second row of wire, and the rest are similar

key_value_temp=read_column(); //Read column data

if (key_value_temp != 0xff)

 // indicates that a key is pressed in the first row

{key_value=key_value_temp & 0x1f;
//Get the key number, for example 0001

1101 means the key at row 1, column 2 is pressed

}

Pin_r_1=1; Pin_r_2=0;Pin_r_3=1;Pin_r_4=1; //Pull down the 2nd line

key_value_temp=read_column(); //Read column data

if (key_value_temp != 0xff)

 // indicates that a key is pressed in row 2

{key_value=key_value_temp &
0x2f; 

}

Pin_r_1=1; Pin_r_2=1;Pin_r_3=0;Pin_r_4=1; //Pull down line 3

key_value_temp=read_column(); //Read column data

if (key_value_temp != 0xff)

 // indicates that a key is pressed in row 3

{key_value=key_value_temp &
0x3f; 

}

Pin_r_1=1; Pin_r_2=1;Pin_r_3=1;Pin_r_4=0; //Pull down the 4th line

key_value_temp=read_column(); //Read column data

if (key_value_temp != 0xff)

 // indicates that a key is pressed on line 4

{key_value=key_value_temp &
0x4f; 

}

}

//(2) Read column line data

unsigned char read_column() //Read the column of the matrix keyboard

{unsigned char key_column;

key_column=0xff;

if(Pin_c_1==0 ) key_column=key_column & 0xfe;
//1110

means the key in column 1 is pressed. The lower 4 bits of key_column represent the key number, and the upper 4 bits are usually 1111

if(Pin_c_2==0 ) key_column=key_column & 0xfd;
//1101

means the key in the second column is pressed.

if(Pin_c_3==0 ) key_column=key_column & 0xfb;
//1011

means the key in the third column is pressed.

if(Pin_c_4==0 ) key_column=key_column & 0xf7;
//0111

means the key in the 4th column is pressed.

return key_column;

}

The key value table corresponding to the above program is as follows:

A Practical Design of Single Chip Microcomputer Matrix Keyboard

Finally, process the key value in the program and jump to the corresponding operation.

There is a Proteus simulation example of a 4*4 matrix keyboard that implements the following functions:

1. Use the row scanning method to read the key values ​​of the 4x4 matrix keyboard;

2. The value of the pressed key is displayed on LCD1602.


Keywords:MCU Reference address:A Practical Design of Single Chip Microcomputer Matrix Keyboard

Previous article:Ultrasonic distance measurement C language program
Next article:Design of fire alarm based on DS18B20 temperature sensor and MQ2 smoke sensor

Recommended ReadingLatest update time:2024-11-17 07:24

MCU Programming Technology Learning Guide
The climax of learning and applying single-chip microcomputers is rising on a large scale in factories, schools, enterprises and institutions. Engineers and technicians who used to be accustomed to the traditional electronic field are facing new challenges. If they cannot learn single-chip microcomputers in a short pe
[Microcontroller]
About PIC microcontroller PAGE and BANK
Let's analyze why PIC has BANK and PAGE settings from the instruction structure of PIC microcontroller. First, let's see why PIC divides the RAM area into multiple BANKs. Carefully observe the format of PIC microcontroller assembly language instructions. A complete assembly language instruction statement is usually
[Microcontroller]
Solution to the error of sprintf converting characters in 51 MCU
sprintf is a function in the C language library that converts data types. There is a situation where In 51 MCU, define unsigned char temp; Then I want to use the serial port to print out temp in decimal form, so I convert it like this: sprintf((char*)tem, "%d", temp); that is, first convert temp into the decimal chara
[Microcontroller]
51 MCU custom function to realize printf() of any serial port
In the process of embedded development, multiple serial ports are often required for communication. Using the printf function is a good choice. It is convenient and practical after formatting the output. Regardless of the type of microcontroller, as long as it supports the standard C compiler, this function can be i
[Microcontroller]
51 MCU custom function to realize printf() of any serial port
Complete machine design of digital voltmeter based on microcontroller and AD678 chip
Digital voltmeters have been designed and developed in many types and styles. Traditional digital voltmeters have their own characteristics. They are suitable for manual measurement on site. To complete remote measurements and further analyze and process the measurement data, traditional digital voltmeters cannot comp
[Microcontroller]
Complete machine design of digital voltmeter based on microcontroller and AD678 chip
PIC microcontroller read and write 24LC02 routine
I2C bus features      The main advantages of the I2C bus are its simplicity and effectiveness. Since the interface is directly on the components, the I2C bus takes up very little space, reducing the space on the circuit board and the number of chip pins, reducing the cost of interconnection. The bus can be up to 25 fe
[Microcontroller]
MSP430 series microcontroller issues
How to solder the MSP430 series microcontroller? First, apply tin paste (a solder material used on chip mounters , priced at 150 yuan/0.25KG) on the circuit board, then solder with a constant temperature soldering iron, or use a hot air soldering machine (about 500 yuan) to solder. How to ad
[Microcontroller]
Understanding the classification and performance requirements of automotive control chips (MCU) in one article
01 Introduction to control chips Control chips mainly refer to MCU (Microcontroller Unit), which is a microcontroller, also known as a single-chip microcomputer. It appropriately reduces the CPU's main frequency and specifications, and integrates multiple functional modules and interfaces such as memory, t
[Embedded]
Understanding the classification and performance requirements of automotive control chips (MCU) in one article
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号