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
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
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:
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.
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
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- FPGA Implementation of ECT Image Reconstruction Algorithm
- Why does the AD21 shortcut key not work after modification?
- Design of high-power laser power supply based on MAX1647
- [ESP32-Audio-Kit Audio Development Board Review] Basic Use of the Development Board
- Can the SSTX and SSRX of USB3.0 be identified and adjusted? Is it necessary to cross it when making a schematic diagram?
- The battery output is DC3.0V after LDO. Why does it slowly rise from 0V to 3V when tested with an oscilloscope?
- Four advantages and applicable fields of LoRaWAN
- [New Year's Flavor Competition] + New Year's Eve dinner, visit lanterns, live a wonderful life!
- The multimeter burned up, the cow dung chip exploded, I took it apart and replaced it with a hard seal, and I actually fixed it!
- Review the basics of static electricity control