Privacy statement: Your privacy is very important to Us. Our company promises not to disclose your personal information to any external company with out your explicit permission.
Matrix keyboard is a keyboard group similar to the matrix used in the external device of the single-chip microcomputer. Because more external input is required during circuit design, a single key control wastes a lot of IO resources, so there is a matrix keyboard, commonly used The matrix keyboard has 4 * 4 and 8 * 8, among which the most used is 4 * 4.
I searched two physical pictures on the Internet, you can take a look
As you can see from the picture above, the 4 * 4 keyboard is still used in many places ^ _ ^
Second, the principle of the matrix keyboardThe matrix keyboard is also called a row-column keyboard. It is a keyboard composed of 4 I / O lines as row lines and 4 I / O lines as column lines. At each intersection of the row and column lines, set a button. In this way, the number of keys in the keyboard is 4 & TImes; 4. This kind of determinant keyboard structure can effectively improve the utilization rate of I / O ports in the single-chip system. Because the IO port of the single-chip microcomputer has the function of wired AND, when any button is pressed, a line is wired in the row and column, and the coordinates of the button can be obtained through operation to judge the value of the button key.
The principle is like the determinant of the matrix sequence
The key matrix built with a 4 * 4 matrix is relatively simple. Some people add diode protection and pull-up resistors to ensure the stability of the circuit, but generally do not need to add circuits that are not too high, set them below Circuit like that
The software design procedure according to the principle of 4 * 4 keyboard is as follows:
// **************************************************** ***
// Function function:
// Single chip drive 4 * 4 keyboard, read in keyboard value and use digital tube static
// Display the key value, if there is no digital tube, it can also display LED light with 8421 code
// Principle of reading: first make the output of MCU port 11110000 read once, then
// Let the microcontroller output 00001111 to read once and add the two values together, and finally
// Get a value with 0 in the high and low bits to determine the key value,
// like 11101110
// **************************************************** ***
#include
#define uchar unsigned char
#define uint unsigned int
unsigned char code smg_d [] = {0x3f, 0x06,0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07,0x7f, 0x6f, 0x77,0x7c, 0x39,0x5e, 0x79,0x71};
// ****************************************************
// Delay function at a crystal frequency of 12MHz
// A delay of about 50us
// ****************************************************
void delay_50us (uint t)
{
uchar j;
for (; t> 0; t--)
for (j = 19; j> 0; j--);
}
void main ()
{
uchar key_l, key_h;
uchar key;
while (1)
{
P1 = 0xf0;
key_l = P1; // Read the value of P1 port
key_l = key_l & 0xf0; // Let the lower 4 bits be 0
if (key_l! = 0xf0) // judge whether there is a key press
{
delay_50us (100);
if (key_l! = 0xf0)
{// If a key is pressed, switch the high and low 4 digits to judge the key value
key_l = P1 & 0xf0; // 11100000 get the first key value
key_l = key_l "0x0f; // 11101111
P1 = key_l; // Reverse the reading again, because the time for pressing the key manually is short
key_h = P1; // For MCU, it is long enough, so you can read the value twice
key_h = key_h & 0x0f; // 00001110
key_l = key_l & 0xf0; // 11100000
key = key_h + key_l; // 11101110
}
}
switch (key)
{
case 0xee: P2 = smg_d [0]; break;
case 0xde: P2 = smg_d [1]; break;
case 0xbe: P2 = smg_d [2]; break;
case 0x7e: P2 = smg_d [3]; break;
case 0xed: P2 = smg_d [4]; break;
case 0xdd: P2 = smg_d [5]; break;
case 0xbd: P2 = smg_d [6]; break;
case 0x7d: P2 = smg_d [7]; break;
case 0xeb: P2 = smg_d [8]; break;
case 0xdb: P2 = smg_d [9]; break;
case 0xbb: P2 = smg_d [10]; break;
case 0x7b: P2 = smg_d [11]; break;
case 0xe7: P2 = smg_d [12]; break;
case 0xd7: P2 = smg_d [13]; break;
case 0xb7: P2 = smg_d [14]; break;
case 0x77: P2 = smg_d [15]; break;
January 14, 2024
November 23, 2023
อีเมล์ให้ผู้ขายนี้
January 14, 2024
November 23, 2023
September 25, 2024
September 24, 2024