MCU reads 4*4 matrix keyboard

Publisher:快乐的天使Latest update time:2019-08-16 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. What is a matrix keyboard?

A matrix keyboard is a keyboard group with a matrix-like layout used in external devices of a single-chip microcomputer. Since more external inputs are required during circuit design, controlling a single button would waste a lot of IO resources, so a matrix keyboard was created. Commonly used matrix keyboards are 4*4 and 8*8, of which 4*4 is the most commonly used.

I searched for two pictures of the real thing on the Internet, you can take a look


As you can see from the picture above, 4*4 keyboards are still used in many places^_^


2. The principle of matrix keyboard

A matrix keyboard is also called a column-type keyboard. It is a keyboard that uses 4 I/O lines as row lines and 4 I/O lines as column lines.

A key is set at each intersection of a row line and a column line, so the number of keys in the keyboard is 4×4.

This matrix keyboard structure can effectively improve the utilization rate of I/O ports in the single-chip microcomputer system.

There is a wired AND function, so when any key is pressed, a line in the row and column is wired ANDed, and the operation can be done

Get the coordinates of the key to determine the key value.

The principle is the same as the determinant of a matrix series.


3. Driving circuit diagram


The key matrix built with a 4*4 matrix is ​​relatively simple. Some people also add diode protection and pull-up resistors to ensure the stability of the circuit, but generally, circuits with low requirements do not need to add them. The circuit set up below can be used.


4. Software Programming


According to the principle of 4*4 keyboard, the software design procedure is as follows:


//***************************************************

//Function function:

//MCU drives 4*4 keyboard, reads keyboard value and uses digital tube static

//Display the key value. If there is no digital tube, you can also use 8421 code to display the LED light.

// Reading principle: First, let the microcontroller port output 11110000 to read once, then

//Let the microcontroller output 00001111 and read it once, add the two values ​​together, and finally

//Get a value with 0 in high and low bits to determine the key value.

//such as 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 12MHz crystal frequency

//About 50us delay

//************************************************

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; //Set the lower 4 bits to 0

 if(key_l!=0xf0) // Check if a key is pressed

 {

  delay_50us(100);

  if(key_l!=0xf0)

  { //If a key is pressed, switch the high and low 4 bits to determine the key value

   key_l=P1&0xf0; //11100000 Get the first key value

   key_l=key_l|0x0f;//11101111   

   P1=key_l; //Read it again in reverse, because the time when the key is pressed by hand is short,

   key_h=P1; //For the microcontroller, it is long enough, so the value can be read 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;

 }

 }

}


Keywords:MCU Reference address:MCU reads 4*4 matrix keyboard

Previous article:4*4 matrix keyboard scanning using interrupts
Next article:Detailed explanation of matrix keyboard scanning principle - single chip microcomputer

Latest Microcontroller Articles
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号