Principle and example of line inversion method identification of MCU peripheral matrix keyboard

Principle and example of line inversion method identification of MCU peripheral matrix keyboard

1 Overview

This article mainly introduces how the microcontroller receives instructions from the 4X4 matrix keyboard and makes corresponding feedback. It mainly introduces the identification principle and practical operation of the matrix keyboard line reversal method.

2. Principle of matrix keyboard line reversal recognition

2.1. Matrix keyboard hardware wiring principle

There are many hardware wiring methods for matrix keyboards. The following is the commonly used matrix wiring.
The 4X4 matrix keyboard consists of 4 rows and 4 columns. The rows and columns are made of two layers of metal wire films, with an insulating layer in the middle. There is an opening at the position of each key. When a key at a certain position is pressed, At this time, the row film sheet and the column film sheet will fit together, and current will be conducted at this time. It is determined that the button is pressed by detecting the row and column positions of the conductive current.
Insert image description here

2.2. Principle of line reversal recognition

1. Introduction to the principle of line reversal recognition

The design of line reversal recognition is very clever, simplifying the complexity and redundancy of the code. First identify the column number, then identify the row number, and determine the key position after two identifications, so it is called line reversal identification.

The steps for identifying line reversals are as follows:

  1. Set column recognition mode
    • The lower 4 bits of the row and the higher 4 bits of the column are set to 0 in the rows and 1 in the columns. The hexadecimal value is0xf0
  2. Identify column number
    • When the button is pressed, the value of a certain column will change to 0, so the corresponding column number can be obtained, for example, press the first column 1110
  3. Invert set line recognition mode
    • The lower 4 bits of the row and the higher 4 bits of the column are set to 1 in the rows and 0 in the columns. The hexadecimal value is0x0f
  4. Identify line number
    • When the button is pressed, the value of a certain row will become 0, so the corresponding row number can be obtained, for example, pressing the first row 1110
  5. Row number + column number = key number
    • OR the row number and column number to get an 8-digit binary value. Convert it to hexadecimal and get the key code corresponding to the key.
2. Examples of line reversal recognition

After introducing the principle of line reversal recognition above, let's learn how to convert the principle into actual results through an example. In the example, button No. 1 is used as an example. The calculation method for other button code values ​​is the same.

Example of calculating the key code of button No. 1.
Calculate the key code of button No. 1 based on the 4X4 wiring diagram in the above figure. In the figure, KEYIN1~KEYIN4are rows and KEYOUT1~KEYOUT4columns.

  1. Set column recognition mode

    • Set KEYIN1~KEYIN4all to low level mode, binary means 0000, KEYOUT1~KEYOUT4set all to high level mode, binary means 1111, the column is the high 4 bits, and the behavior is the low 4 bits, so the combined 8-bit binary is:1111 0000
  2. Identify column number

    • Press button No. 1. The wiring of button No. 1 is the first column. The binary number of the column is the 1110binary number of the row 0000. The combined 8-bit binary number is:1110 0000
  3. Invert set line recognition mode

    • Set KEYIN1~KEYIN4all to high level mode, binary means 1111, KEYOUT1~KEYOUT4set all to low level mode, binary means 0000, the column is the high 4 bits, and the behavior is the low 4 bits, so the combined 8-bit binary is:0000 1111
  4. Identify line number

    • Press button No. 1. The wiring of button No. 1 is the first row. The binary number of the row is the 1110binary number of the column 0000. The combined 8-bit binary number is:0000 1110
  5. Row number + column number = key number

    • Compute the row number and column number (1110 0000 | 0000 1110) to obtain an 8-digit binary value 1110 1110, which is converted to hexadecimal0xEE

3. Experiment on matrix keyboard lighting up LED lights

Experimental goals

In the program, the code value of the keyboard key is received, the code value is judged, and the LED light is controlled to turn on and off.

Hardware circuit principle
  • The positive pole of the LED light is connected to VCC pin No. 20, and the negative pole is connected to P3.7 pin No. 11
  • There are numbers on the keyboard cable. Number 1 corresponds to P1.0 pin No. 12 in ascending order, and No. 8 corresponds to P1.7 pin No. 19.
    Insert image description here
Core code introduction

KEY()The function of the function is to identify the key value of the key through the line inversion method, and then make the corresponding action through the program. This function implements the above line flipping principle.

/*
函数名:线翻转法读取键盘按键值
调  用:? =  Key (void);
参  数:无
返回值:0~0xff
结  果:连接Px接口的键盘读出返回值
备  注:
*/
unsigned char Key (void){
    
    //键盘处理函数
	unsigned char a,b,c;//定义3个变量
	//设置列识别模式:行为低4位,列为高4位,将行全部设置为0,列全部设置为1,二进制为:11110000,十六进制值为 0xf0
	KEY = 0xf0;
	if(KEY != 0xf0){
    
     //查寻键盘口的值是否变化
		DELAY_MS (20);//延时20毫秒,去除按键抖动
   		if(KEY != 0xf0){
    
    //有键按下处理
	  		a = KEY;//键值放入寄存器a
		}
		//翻转设置行识别模式:行为低4位,列为高4位,将行全部设置为1,列全部设置为0,二进制为:00001111,十六进制值为 0x0f
		KEY = 0x0f;
		c = KEY;//将第二次取得值放入寄存器c
		a = a|c;//将两个数据相或
		switch(a){
    
    //对比数据值
			case 0xee: b = 1; break;//对比得 到的键值给b一个应用数据
			case 0xed: b = 2; break;
			case 0xeb: b = 3; break;
	   		case 0xe7: b = 4; break;
	   		case 0xde: b = 5; break;
	   		case 0xdd: b = 6; break;
	   		case 0xdb: b = 7; break;
	   		case 0xd7: b = 8; break;
	   		case 0xbe: b = 9; break;
	   		case 0xbd: b = 10; break;
	   		case 0xbb: b = 11; break;
	   		case 0xb7: b = 12; break;
	   		case 0x7e: b = 13; break;
	   		case 0x7d: b = 14; break;
	   		case 0x7b: b = 15; break;
	   		case 0x77: b = 16; break;
			default: b = 0; break;//键值错误处理
	   }
	}
	return (b);//将b作为返回值
}
Complete code
/*
程序名:线翻转法操作矩阵键盘   
编写人:bruce     
编写时间:2023年12月
硬件支持:STC12C2052AD系列  
接口说明:    
修改日志:  
  NO.1-								
*/

#include <STC12C2052AD.H> //STC12Cx052或STC12Cx052AD系列单片机头文件


sbit LED = P3 ^ 7;

#define KEY P1 

/*
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
*/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( a-- != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}


/*
函数名:线翻转法读取键盘按键值
调  用:? =  Key (void);
参  数:无
返回值:0~0xff
结  果:连接Px接口的键盘读出返回值
备  注:
*/
unsigned char Key (void){
    
    //键盘处理函数
	unsigned char a,b,c;//定义3个变量
	//设置列识别模式:行为低4位,列为高4位,将行全部设置为0,列全部设置为1,二进制为:11110000,十六进制值为 0xf0
	KEY = 0xf0;
	if(KEY != 0xf0){
    
     //查寻键盘口的值是否变化
		DELAY_MS (20);//延时20毫秒,去除按键抖动
   		if(KEY != 0xf0){
    
    //有键按下处理
	  		a = KEY;//键值放入寄存器a
		}
		//翻转设置行识别模式:行为低4位,列为高4位,将行全部设置为1,列全部设置为0,二进制为:00001111,十六进制值为 0x0f
		KEY = 0x0f;
		c = KEY;//将第二次取得值放入寄存器c
		a = a|c;//将两个数据相或
		switch(a){
    
    //对比数据值
			case 0xee: b = 1; break;//对比得 到的键值给b一个应用数据
			case 0xed: b = 2; break;
			case 0xeb: b = 3; break;
	   		case 0xe7: b = 4; break;
	   		case 0xde: b = 5; break;
	   		case 0xdd: b = 6; break;
	   		case 0xdb: b = 7; break;
	   		case 0xd7: b = 8; break;
	   		case 0xbe: b = 9; break;
	   		case 0xbd: b = 10; break;
	   		case 0xbb: b = 11; break;
	   		case 0xb7: b = 12; break;
	   		case 0x7e: b = 13; break;
	   		case 0x7d: b = 14; break;
	   		case 0x7b: b = 15; break;
	   		case 0x77: b = 16; break;
			default: b = 0; break;//键值错误处理
	   }
	}
	return (b);//将b作为返回值
}

void main (void){
    
     //主程序 
	while(1){
    
    
		if(Key() == 1){
    
    
			LED = 0;		//取LED相反状态
		}
		if(Key() == 2){
    
    
			LED = 1;		//取LED相反状态
		}
	}
}

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/135225329