Key scanning and matrix key of 51 single chip microcomputer

catalogue

preface

1, Key and matrix keyboard

2, Introduction to key module

1. Independent key

2. Matrix key

3, Program implementation

1. Independent key scanning control LED

2. Matrix keyboard controls nixie tube output 0-F

summary

preface

The key input of 51 single chip microcomputer is not only a switch, but also an input peripheral. When the key is used on the power line, it is called a switch. For example, the power switch of our development board is also a key. When the key is connected to the io port, it is an input peripheral, such as the keyboard. This paper is based on the 51 single chip microcomputer development board to operate the LED / nixie tube through the key

1, Key and matrix keyboard

2, Introduction to key module

1. Independent key

About the use of independent keys, we should first understand the IO port of 51 single chip microcomputer. The four IO ports on the right of 51 single chip microcomputer are P0, P1, P2 and P3 respectively. The interior of the four IO ports is not exactly the same. According to the contents given in the 51 manual, the IO port of 51 single chip microcomputer has three working types, which are: quasi two-way port / weak pull-up, only for input (high resistance) or open drain output. Port P0 is open drain output after power on, and port P1/P2/P3 is weak pull-up mode after power on. The P3IO port we show is high level when the key is not pressed, and the IO port is grounded to low level when the key is pressed. You can learn about open drain and weak pull-up.

2. Matrix key

Matrix keys use fewer io ports to control most keys, so as to achieve the effect of saving io ports. When there are more keys, the effect is about obvious. Generally, the matrix with keys arranged as 4x4 or 8x8 is scanned row by row and column by column to judge whether the keys are pressed.

    

3, Program implementation

1. Independent key scanning control LED

The code is as follows (example): press the LED on, release the led off key, scan the key value returned by the packaging function, and further programming can be carried out.

In the process of code implementation, I have encountered many difficulties. I always think it is very difficult. I have also seen other people's code implementation. After all, 1000 people have 1000 different codes. They come to the same goal by different ways. When they see other people's codes, they always feel very complex.

#include <REG51. h> / / 51 exclusive header file
#include <public. h> 	// Delay function header file
#define LED P2 		// Define LED
sbit  key1   =P3^1;
sbit  key2   =P3^0;
sbit  key3   =P3^2;
sbit  key4   =P3^3;

int key_function()
{
    delay_ms(2);						 //Debounce
    if(key1==0||key2==0||key3==0||key4==0)	 //Judge whether it is pressed
	  {   if(key1==0)
	         return 1;    //Return key value 1
	   else if(key2==0)
	         return 2;	  //Return key value 2
	   else if(key3==0)
	         return 3;	  //Return key value 3
	   else if(key4==0)
	         return 4;	  //Return key value 4
	  }
	  return 0;				   				 //No press to return to 0
}
int main()
{  
  
  while(1)
    {
   if(key_function()==2)					 //Judgment return value
     LED = 0X00; 							 //Light up
	else 
	LED=0xFF;								//Turn off the lights
    }

return 0;
}

2. Matrix keyboard controls nixie tube output 0-F

The code is as follows (example):

Row column scanning

#include <REG51. h> / / 51 exclusive header file
#include <public. h> 	// Delay function header file
#define led P0 		// Nixie tube io
#define key P1 		// Define matrix keyboard register



int led_display[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
                             0x6f,0x77,0xff,0xb9,0xbf,0xf9,0xf1};	//Define 0-f

int Matrix_keyboard()             //Custom matrix keyboard functions
{
	int key_value=0;              //key_value is used to return a value
	key=0xf7;                     //Make a column of the matrix 0
	if(key!=0xf7)                 //If key= 0xf7 a key is pressed
	 {	
	  delay_ms(1);                //Debounce
	  switch(key)                 //Line scan and output the corresponding key value
	     {
		  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!=0xf7);           //A key is pressed, waiting to let go

	  	key=0xfb;                 //The second column is low level
	if(key!=0xfb)
	 {	
	  delay_ms(1);
	  switch(key)
	     {
		  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!=0xfb);

	   	  	key=0xfd;              //The third column is low level
	if(key!=0xfd)
	 {	
	  delay_ms(1);
	  switch(key)
	     {
		  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!=0xfd);

	   	   	  	key=0xfe;          //The fourth column is low level
	if(key!=0xfe)
	 {	
	  delay_ms(1);
	  switch(key)
	     {
		  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!=0xfe);

	  return key_value;
}
int main()                        //Main function
{  int num=0;
   while(1)
   {
   int num=0;                      //Used to receive key values
   num=Matrix_keyboard();
   if(num!=0)
      {
	  led=led_display[num-1];      //The subscript of the first bit of the array is 0, and the return value is 1
	  }
   }
   return 0;
}

summary


The above is today's content. It mainly operates independent keys and matrix keys. It is also constantly debugged in the process of operation. Especially when the matrix keyboard is not so clear, it is easy to make mistakes, and it is complex in my heart. I keep understanding it. Finally, my kung fu pays off. I hope you can also form your own ideas and style.

I am a Xiaobai. I hope you can give more guidance, pay more attention, paste water together and make progress together.

Added by mattsutton on Sun, 09 Jan 2022 13:56:57 +0200