Summary on the performance improvement of single chip key

  • 2020-06-12 10:09:24
  • OfStack

Recently read a book "8051 single-chip microcomputer innovation course", this book is better than the university textbook, in the university textbook, our common key scanning program is as follows:


unsinged char KeyScan(void)
{
 unsigned char KeyValue=0;
 if(KEY_IO != 0xFF) // A key press detected 
 {
 DelayNms(20); // Time delay  20  Millisecond (seriously affects the operation efficiency of MCU) 
 if(KEY_IO != 0xFF)// Confirm that the button is pressed 
 {
  switch(KEY_IO)
  {
  case 0xFE: KeyValue=1;break;
  case 0xFD: KeyValue=2;break;
  default : KeyValue=0;break;
  }
 }
 }
 return KeyValue;
}

In reality, the forum to see the following code, we might as well so, no delay, run efficiency greatly improved! The real - time performance of single - chip microcomputer has been significantly improved!


// Read the keys 
uchar read_key()
{
  static int Key_on_off = 0 ;// Key self - lock variable 
  uchar num , temp ;
  num = P2 ; 
  num &= 0xf0 ; // Will be low 4 A clear 0
  if(num != 0xf0)
  {
   if(Key_on_off == 0)
  {
   Key_on_off = 1 ;
  switch(num)
  {
  // Returns the code of the key 
  case 0xe0 : temp = 1 ; break ; 
  case 0xd0 : temp = 2 ; break ; 
  case 0xb0 : temp = 3 ; break ; 
  case 0x70 : temp = 4 ; break ; 
  }
  }  
  }
  else
   Key_on_off = 0 ;
 return temp ;
}

conclusion


Related articles: