Jan 6, 2017

How to interface 4x4 matrix keyboard with 8051 microcontroller

We are using 4x4 matrix keyboard for giving inputs through key presses. 4x4 matrix keyboard is used because it provides us 16 keys with 8 input lines. Number of input lines can be calculated as 2*sqrt(n) for n keys square matrix keyboard. 4x4 matrix keyboard has 4 rows and 4 columns.
Here we are connecting rows to port 2.0-2.3 and columns to 2.4-2.7
Method used in this article to find which key is pressed:
Make all columns high, all rows high but row 1 low. If any key is pressed in row 1 then corresponding column gets connected to row 1 that is logic 0. For example is key number 3 is pressed then column three will get connected to row 1 and it will have logic 0. by checking all 4 columns, if any key in row 1 pressed then it can be detected by reading 4 columns(by finding if any column is at logic level 0).
This process is repeated but for next time row 2 at logic 0 and after that do same for row 3 and row 4. If no key is pressed then give a predefined value to key (here 255 is used). This checking is done in checkkey() function.
In main function, we first check if any key is pressed then after giving some delay (near to 50ms or 100ms) we again check for key press. If both key pressed gives same value other than 255 then we will execute corresponding task. We are checking key pressed 2 times with some time interval because this will ensure that obtained key pressed is due to real key press and not by any noise. Follow code given below and if you have any suggestions then comment below.

 //header file  
 #include<reg51.h>  
   
 //function definitions  
 unsigned char checkkey();  
 void delay(unsigned char);  
   
 int main(void)  
 {  
      unsigned char key1, key2;  
      while(1)  
      {  
           //check if any key is pressed  
           key1=checkkey();  
           delay(1);  
             
           //check again if any key is pressed after some delay  
           key2=checkkey();  
             
           //if both the values are same or no key is pressed  
           if((key1==key2)&&(key1!=255))  
           {  
                //task if key is pressed  
                P1=key1;  
           }  
           else  
           {  
                P1=0x00;  
           }  
      }  
 }  
   
 unsigned char checkkey()  
 {  
      //all column high, all row high except row 1  
      switch(P2)  
      {  
           //if column 1 is zero  
           case 0xee:  
                return 1;  
                break;  
             
           //if column 2 is zero  
           case 0xde:  
                return 2;  
                break;  
             
           //if column 3 is zero  
           case 0xbe:  
                return 3;  
                break;  
             
           //if column 4 is zero  
           case 0x7e:  
                return 4;  
                break;  
      }  
        
      //all column high, all row high except row 2  
      P2=0xfe;  
      switch(P2)  
      {  
           //if column 1 is zero  
           case 0xee:  
                return 1;  
                break;  
                  
           //if column 2 is zero  
           case 0xde:  
                return 2;  
                break;  
             
           //if column 3 is zero  
           case 0xbe:  
                return 3;  
                break;  
             
           //if column 4 is zero  
           case 0x7e:  
                return 4;  
                break;  
      }  
        
      //all column high, all row high except row 3  
      P2=0xfd;  
      switch(P2)  
      {  
           //if column 1 is zero  
           case 0xed:  
                return 5;  
                break;  
                  
           //if column 2 is zero  
           case 0xdd:  
                return 6;  
                break;  
             
           //if column 3 is zero  
           case 0xbd:  
                return 7;  
                break;  
             
           //if column 4 is zero  
           case 0x7d:  
                return 8;  
                break;  
      }  
        
      //all column high, all row high except row 4  
      P2=0xfb;  
      switch(P2)  
      {  
           //if column 1 is zero  
           case 0xeb:  
                return 9;  
                break;  
                  
           //if column 2 is zero  
           case 0xdb:  
                return 10;  
                break;  
             
           //if column 3 is zero  
           case 0xbb:  
                return 11;  
                break;  
             
           //if column 4 is zero  
           case 0x7b:  
                return 12;  
                break;  
      }  
      P2=0xf7;  
      switch(P2)  
      {  
           //if column 1 is zero  
           case 0xe7:  
                return 13;  
                break;  
                  
           //if column 2 is zero  
           case 0xd7:  
                return 14;  
                break;  
             
           //if column 3 is zero  
           case 0xb7:  
                return 15;  
                break;  
             
           //if column 4 is zero  
           case 0x77:  
                return 16;  
                break;  
      }  
      return 255;  
 }  
   
 //delay function  
 void delay(unsigned char n)  
 {  
      unsigned char i,j;  
   
      for(i=0;i<n;i++)  
           for(j=0;j<255;j++);  
 }  
   

4x4 matrix keyboard is connected to port 2 of 8051 microcontroller
Keyboard with 8051

No comments:

Post a Comment