Identification of 89C52 Matrix Keyboard

Publisher:dfdiqcLatest update time:2015-12-21 Source: eefocusKeywords:89C52 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The matrix keyboards I have seen generally have two identification methods: scanning method and line inversion method. Taking a 4X4 matrix keyboard as an example, the scanning method is to set each row to 0 in turn, and then read the status of the column, so that the key status of the entire matrix keyboard can be identified through four scans. The amount of code required for this method is relatively long; while the line inversion method is relatively concise. It sets all 4 rows to 0 (0xf0) first, then reads the status of the column, then sets all the columns to 0 (0x0f), reads the status of the row, and determines the status of the entire row and column through (row|column). The scanning method takes up more program storage space (Program Space) than the line inversion method, but takes up less data storage space (Data Space).

Identification of 89C52 Matrix Keyboard
The procedure is as follows

 

Scanning method:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num,c;
uchar code SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
 uint x;
 uchar y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

void keyscan()
{
 uchar temp;
 P2=0xfe;
 temp=P2&0xf0;
 if(temp!=0xf0)
 {
  delay1ms(10);
  temp=P2&0xf0;
  if(temp!=0xf0)
  {
   temp=P2;
   switch(temp)
   {
    case 0xee:num=0;break;
    case 0xde:num=1;break;
    case 0xbe:num=2;break;
    case 0x7e:num=3;break;
   }
   while(temp!=0xf0)//松手检测
   {
    temp=P2;
    temp=temp&0xf0;
   }
  
 }
 P2=0xfd;
 temp=P2&0xf0;
 if(temp!=0xf0)
 {
  delay1ms(10);
  temp=P2&0xf0;
  if(temp!=0xf0)
  {
   temp=P2;
   switch(temp)
   {
    case 0xed:num=4;break;
    case 0xdd:num=5;break;
    case 0xbd:num=6;break;
    case 0x7d:num=7;break;
   }
   while(temp!=0xf0)
   {
    temp=P2;
    temp=temp&0xf0;
   }
  
 }
 P2=0xfb;
 temp=P2&0xf0;
 if(temp!=0xf0)
 {
  delay1ms(10);
  temp=P2&0xf0;
  if(temp!=0xf0)
  {
   temp=P2;
   switch(temp)
   {
    case 0xeb:num=8;break;
    case 0xdb:num=9;break;
    case 0xbb:num=10;break;
    case 0x7b:num=11;break;
   }
   while(temp!=0xf0)
   {
    temp=P2;
    temp=temp&0xf0;
   }
  
 }
 P2=0xf7;
 temp=P2&0xf0;
 if(temp!=0xf0)
 {
  delay1ms(10);
  temp=P2&0xf0;
  if(temp!=0xf0)
  {
   temp=P2;
   switch(temp)
   {
    case 0xe7:num=12;break;
    case 0xd7:num=13;break;
    case 0xb7:num=14;break;
    case 0x77:num=15;break;
   }
   while(temp!=0xf0)
   {
    temp=P2;
    temp=temp&0xf0;
   }
  
 }
}

void main()
{
 num=0xff;
 while(1)
 {
  keyscan();
  P1=~SSEG[num];
 }

}

 

Line reversal method:

#include
#define uchar unsigned char
#define uint unsigned int
uchar num;
uchar code SSEG[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void delay1ms(uint z)
{
 uint x;
 uchar y;
 for(x=z;x>0;x--)
  for(y=110;y>0;y--);
}

void keyscan()
{
 uchar temp,z;
 P2=0xf0;
 if(P2!=0xf0)
 {
  delay1ms(10);
  if(P2!=0xf0)
  {
   temp=P2;
   P2=0x0f;
   z=temp|P2;
   temp=P2&0x0f;
   while(temp!=0x0f)//松手检测
   {
    temp=P2&0x0f; 
   }
   switch(z)
   {
    case 0xee: num =0; break;
          case 0xde: num =1; break;
          case 0xbe: num =2; break;
          case 0x7e: num =3; break;
          case 0xed: num =4; break;
          case 0xdd: num =5; break;
          case 0xbd: num =6; break;
          case 0x7d: num =7; break;
          case 0xeb: num =8; break;
          case 0xdb: num =9; break;    
          case 0xbb: num =10;break;
          case 0x7b: num =11;break;
          case 0xe7: num =12;break;
          case 0xd7: num =13;break;
          case 0xb7: num =14;break;
          case 0x77: num =15;break;
   }
  }
 }
}

void main()
{
 num=0xff;
 P2=0xff;
 while(1)
 {
  keyscan();
  P1=~SSEG[num];
 }
}

Keywords:89C52 Reference address:Identification of 89C52 Matrix Keyboard

Previous article:89C52 and L298 control DC motor
Next article:A perpetual calendar optimization algorithm source program for 51 series single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 13:55

89C52 single chip microcomputer controls ISD4004 voice chip, multi-segment recording and playback source code
89C52 single chip microcomputer controls ISD4004 voice chip, multi-segment recording and playback The microcontroller source program is as follows: //AT89c52, ISD4004 //To record, press the K1 key twice, LED1 will light up, then you can play K2. //After recording and playing, the address changes to the next one, and t
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号