Easily handle independent keyboard control and 4*4 matrix keyboard scanning

Publisher:koimqerolulkLatest update time:2015-07-21 Source: eechina Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The circuit diagram is very simple so I won't draw it. The circuit connection is that port p1 is connected to the matrix keyboard, port p3 is connected to the independent keyboard, and 8 LED lights are connected to port p0.
 #include
 #define uchar unsigned char
 #define uint unsigned int
 #define on 0
 #define off 1
 sbit led1=P0^0;
 sbit led2=P0^1;
 sbit led3=P0^2;
 sbit led4=P0^3;
 sbit led5=P0^4;
 sbit led6=P0^5;
 sbit led7=P0^6;
 sbit led8=P0^7;

 sbit key1=P3^0;
 sbit key2=P3^1;
 sbit key3=P3^2;
 sbit key4=P3^3;
 sbit key5=P3^4;
 sbit key6=P3^5;
 sbit key7=P3^6;
 sbit key8=P3^7;

 void delay_ms(uchar time)
 {
  flying i,j;
  for(i=time;i>0;i--)
  {
   for(j=85;j>0;j++)
   {;}
  }
 }
 uchar scan_key()
 {
  //fly i,j;
  //P3=0xff;
  if((P3&0xff)!=0xff)
  {
   delay_ms(30);
   if((P3&0xff)!=0xff)
   {
   
    if(key1==0)
    return(1);
    if(key2==0)
    return(2);
    if(key3==0)
    return(3);
    if(key4==0)
    return(4);
    if(key5==0)
    return(5);
    if(key6==0)
    return(6);
    if(key7==0)
    return(7);
    if(key8==0)
    return(8);
   }
  }
  return 0;
 }

 void main(void)
 {
  uchar key_value;
  key_value=scan_key();
  switch(key_value)
  {
   case 1:led1=on;break;
  case 2:led2=on;break;
  case 3:led3=on;break;
  case 4:led4=on;break;
  case 5:led5=on;break;
  case 6:led6=on;break;
  case 7:led7=on;break;
  case 8:led8=on;break;
  default:P0=0xff;
   }
 }

4*4 matrix keyboard

 #include
 sbit led1 =P0^0 ;
 sbit led2 = P0^1 ;
 sbit led3 = P0^2 ;
 sbit led4 =P0^3 ;
 sbit led5 =P0^4 ;
 sbit led6 =P0^5 ;
 sbit led7 = P0^6 ;
 sbit led8 = P0^7 ;
 #define on 0
 #define off 1
 #define uchar unsigned char
 #define uint unsigned int

 void delay_ms(uint time)
 {
  uint i,j;
  for(i=time;i>0;i--)
  for(j=112;j>0;j--)
  {}
 }
 //d/*
 uchar s_scan(void)
 {
  uchar scan_code,temp;
  P1=0xf0;
  if((P1&0xf0)!=0x0f)
  {
   delay_ms(30);
   if((P1&0xf0)!=0xf0)
   {
    scan_code=0xfe;
    while((scan_code&0x10)!=0)
    {
     P1=scan_code;
     if((P1&0xf0)!=0xf0)
     {
      temp=(P1&0xf0)|0x0f;
      while((P1&0xf0)!=0xf0)
      return ((~scan_code)+(~temp));
     }
     else
      scan_code=(scan_code<<1)|0x01;
    }
   }
  }
  return 0;
 }
//*/
/*
 uchar s_scan(void)
 {
  flying scanh,scanl;
  P1=0xf0;
  if((P1&0xf0)!=0xf0)
  {
   P1=0xf0;
   delay_ms(30);
   if((P1&0xf0)!=0xf0)
   { 
     P1=0xfe ;
     //P1=scan;
    if((P1&0xf0)!=0xf0)
    {
     P1&=0xf0;
     if(P1==0xe0)
     return(1);
     if(P1==0xd0)
     return(2);
     if(P1==0xb0)
     return(3);
     if(P1==0x70)
     return(4);
    }
      P1=0xfd;
    if((P1&0xf0)!=0xf0)
    {
     P1&=0xf0;
     if(P1==0xe0)
     return(5);
     if(P1==0xd0)
     return(6);
     if(P1==0xb0)
     return(7);
     if(P1==0x70)
     return(8);
    }
     P1=0xfb;
    if((P1&0xf0)!=0xf0)
    {
     P1&=0xf0;
     if(P1==0xe0)
     return(9);
     if(P1==0xd0)
     return(10);
     if(P1==0xb0)
     return(11);
     if(P1==0x70)
     return(12);
    }  
      P1=0xf7;
    if((P1&0xf0)!=0xf0)
    {
     P1&=0xf0;
     if(P1==0xe0)
     return(13);
     if(P1==0xd0)
     return(14);
     if(P1==0xb0)
     return(15);
     if(P1==0x70)
     return(16);
    }
   }
  }
  return 0;
 }
 */
 void main(void)
 { 
 
  uchar key_code;
  while(1)
  {
   key_code=s_scan();
   if(key_code)
   P0=key_code;
 /*    
   delay_ms(300);
   led1=~led1;
   delay_ms(300);
  
   switch(key_code)
   {
    case 1:led1=on;break;
    case 2:led2=on;break;
    case 3:led3=on;break;
    case 4:led4=on;break;
    case 5:led5=on;break;
    case 6:led6=on;break;
    case 7:led7=on;break;
    case 8:led8=on;break;
    default:P0=0xff;
   }
  */
  }
 }
Reference address:Easily handle independent keyboard control and 4*4 matrix keyboard scanning

Previous article:LCD12232 Serial Display C51 Program
Next article:C0851F410 and 12864 LCD display

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号