Lookup table matrix keyboard

Publisher:平安宁静Latest update time:2015-07-22 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//P1 port is connected to an external 3*4 matrix keyboard, where p1^0 does not need to be set to 1. Common methods for reading key values ​​are:
//"Table Lookup Method" and "Reverse Method". The table lookup method is the shortest program, but it takes up a little more time.
// A little storage space; the inversion method has the fastest execution speed, but requires the interface to be
//Bidirectional.
#include
#include 
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
~0x3F,~0x06,~0x5B,~0x4F,~0x66,
~0x6D,~0x7D,~0x07,~0x7F,~0x6F,
~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71}; //The common anode LED code is exactly opposite to the common cathode, so the cathode code is inverted
uchar num;
uchar keyscan();
/*void delay(uint z)
{
 uint x,y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}*/
void main()
{
 P0=table[0];
 while(1)
 {
    num=keyscan();
    if(num!=16)
    P0=table[num];
  
 
 }
}
uchar keyscan()
{
 uchar code table1[3][4]=
  {//0xee,0xde,0xbe,0x7e,
   0xed,0xdd,0xbd,0x7d,
   0xeb,0xdb,0xbb,0x7b,
   0xe7,0xd7,0xb7,0x77};
  uchar temp1=0xfd,temp2,i,j; //If it is 3*4, just change the initial value of temp1 to p1^0 without setting 1 and changing the table value
  for(i=0;i<4;i++)//Scan the lower four bits//If 3*4 keyboard i<3 is also possible
  { P1=temp1; //Output a line of 0
   temp2=P1; //Read the status of P1 port immediately
   if((temp2&0xf0)!=0xf0)//If a key is pressed
   {
    for(j=0;j<4;j++)//Scan the upper four bits j=0 j=1 j=2 j=3  
   {
    if(temp2==table1[i][j])//look up table i=0 0 1 2 3
     return i*4+j; //If found, return the key value i=1 4 5 6 7
   } // i=2 8 9 10 11
   } // i=3 12 13 14 15
  else temp1=_crol_(temp1,1); //
          // i*4+j
  }
  return 16; //No code found, the return key is released
}   
Reference address:Lookup table matrix keyboard

Previous article:Play with LED dot matrix - scrolling display
Next article:STC MCU power-off free download

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号