51 MCU Learning Record (5) Matrix Buttons

1. The control principle of matrix buttons

When the independent buttons are connected to the microcontroller, each button needs an I/O port of the microcontroller. If a single-chip system needs more buttons, using independent buttons will occupy too many I/O port resources. The I/O port resources in the single-chip microcomputer system are often more precious. When multiple keys are used, in order to reduce the I/O port pins, a matrix key is introduced.

The schematic diagram of the matrix button on this development board is as follows:

insert image description here
It can be seen that the 16 buttons are arranged in 4 rows and 4 columns. The first four rows are respectively connected to each row of the io port, and the last four rows are respectively connected to each column of the io port. In this way, each io port is connected to four A button, also can realize the layout such as 3X3, 5X5 etc. through this way.

Then how to realize the detection, the detection of this kind of key is generally realized by scanning,First enable a certain column to be low level, and the other columns are all high level, and then immediately check whether each row has low level in turn. If not, it means that no button is pressed in this row. If there is Low level, it can indicate that there is a pressed button in this row, so the position of the button is locked through the row and column, and the low level enable of this column is also carried out in turnIn this way, the detection of the key can be realized.

2. Realization of matrix keys

1. Realize matrix buttons by determinant method

Let’s first look at the definition of the matrix keys, as shown below:
insert image description here
Let’s take the first step as an example, first assign a value to our corresponding io port, here the value is 0XF7, which is 1111 0111, and the first column is all assigned a value of 0, The rest are all 1:
insert image description here
After that, check whether any button is pressed in the first row. If there is a button pressed, it should be the following situations. Enumerate these situations one by one, and the results are as follows
insert image description here
: The second line
insert image description here
below the third line
insert image description here
below the fourth line
insert image description here
is reflected in the program as the following code part: the
insert image description here
complete code is as follows: ( Note: The code here comes from Puzhong Technology 51 MCU routine )

u8 key_matrix_ranks_scan(void) //行列式扫描实现矩阵按键检测
{
    
    
	u8 key_value=0;

	KEY_MATRIX_PORT=0xf7;//给第一列赋值0,其余全为1
	if(KEY_MATRIX_PORT!=0xf7)//判断第一列按键是否按下
	{
    
    
		delay_100ms();//消抖
		switch(KEY_MATRIX_PORT)//保存第一列按键按下后的键值	
		{
    
    
			case 0x77: key_value=1;break;
			case 0xb7: key_value=5;break;
			case 0xd7: key_value=9;break;
			case 0xe7: key_value=13;break;
		}
	}
	while(KEY_MATRIX_PORT!=0xf7);//等待按键松开	
	
	KEY_MATRIX_PORT=0xfb;//给第二列赋值0,其余全为1
	if(KEY_MATRIX_PORT!=0xfb)//判断第二列按键是否按下
	{
    
    
		delay_100ms();//消抖
		switch(KEY_MATRIX_PORT)//保存第二列按键按下后的键值	
		{
    
    
			case 0x7b: key_value=2;break;
			case 0xbb: key_value=6;break;
			case 0xdb: key_value=10;break;
			case 0xeb: key_value=14;break;
		}
	}
	while(KEY_MATRIX_PORT!=0xfb);//等待按键松开	
	
	KEY_MATRIX_PORT=0xfd;//给第三列赋值0,其余全为1
	if(KEY_MATRIX_PORT!=0xfd)//判断第三列按键是否按下
	{
    
    
		delay_100ms();//消抖
		switch(KEY_MATRIX_PORT)//保存第三列按键按下后的键值	
		{
    
    
			case 0x7d: key_value=3;break;
			case 0xbd: key_value=7;break;
			case 0xdd: key_value=11;break;
			case 0xed: key_value=15;break;
		}
	}
	while(KEY_MATRIX_PORT!=0xfd);//等待按键松开	
	
	KEY_MATRIX_PORT=0xfe;//给第四列赋值0,其余全为1
	if(KEY_MATRIX_PORT!=0xfe)//判断第四列按键是否按下
	{
    
    
		delay_100ms();//消抖
		switch(KEY_MATRIX_PORT)//保存第四列按键按下后的键值	
		{
    
    
			case 0x7e: key_value=4;break;
			case 0xbe: key_value=8;break;
			case 0xde: key_value=12;break;
			case 0xee: key_value=16;break;
		}
	}
	while(KEY_MATRIX_PORT!=0xfe);//等待按键松开
	
	return key_value;		
}

2. Line flip method to realize matrix buttons

The process implemented by the line inversion method is as follows :First, when all the row lines are at low level, then detect whether all the column lines are at low level, and if so, record the value of the column line; then turn over, make all the column lines at low level, and detect the value of all row lines , because a button is pressed, the value of the row line will also change, record the value of the row line. Thus, all keys can be detected.

The specific implementation process of the line flip method is
insert image description here
as follows: The complete code is as follows: ( Note: The code here comes from the 51 MCU routine of Puzhong Technology )

u8 key_matrix_flip_scan(void) //线翻转法实现矩阵按键
{
    
    
	static u8 key_value=0;

	KEY_MATRIX_PORT=0x0f;//给所有行赋值0,列全为1
	if(KEY_MATRIX_PORT!=0x0f)//判断按键是否按下
	{
    
    
		delay_100ms();//消抖
		if(KEY_MATRIX_PORT!=0x0f)
		{
    
    
			//测试列
			KEY_MATRIX_PORT=0x0f;
			switch(KEY_MATRIX_PORT)//保存行为0,按键按下后的列值	
			{
    
    
				case 0x07: key_value=1;break;
				case 0x0b: key_value=2;break;
				case 0x0d: key_value=3;break;
				case 0x0e: key_value=4;break;
			}
			//测试行
			KEY_MATRIX_PORT=0xf0;
			switch(KEY_MATRIX_PORT)//保存列为0,按键按下后的键值	
			{
    
    
				case 0x70: key_value=key_value;break;
				case 0xb0: key_value=key_value+4;break;
				case 0xd0: key_value=key_value+8;break;
				case 0xe0: key_value=key_value+12;break;
			}
			while(KEY_MATRIX_PORT!=0xf0);//等待按键松开	
		}
	}
	else
		key_value=0;		
	
	return key_value;		
}

3. Button actual combat

Use the previous serial port function to realize button printing, the code is as follows:
insert image description here
Download the program to the development board, you can see the following phenomenon
insert image description here

Guess you like

Origin blog.csdn.net/m0_51220742/article/details/125031295