4X4 matrix keyboard (lookup table method) C language programming

Publisher:CW13236066525Latest update time:2016-10-27 Source: eefocusKeywords:4X4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*The row lines are connected to P1.0-P1.3, and the column lines are connected to P1.4-P1.7.


According to the key value code returned by scanning the keyboard, the key value is looked up in the key value code table to obtain the key value and send it to the digital tube for display.

When the power is turned on, the digital tube displays “-”.

When a key is pressed, the digital tube displays the key value of the pressed key and the buzzer sounds once. */

#include
#include

#define uchar unsigned char
#define uint  unsigned int

sbit BEEP = P3^6; //Buzzer drive line

uchar key;

unsigned char code disp_code[]={
              0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
              0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xbf};

unsigned char code key_code[]={
              0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d, 
              0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77 };

/**********************************************************

  Delay subfunction

**********************************************************/
void delayms(uint ms) 
{
   uchar t;
   while(ms--)
   { 
     for(t = 0; t < 120; t++);
   }
}

/**********************************************************

  x*0.14MS delay sub-function

**********************************************************/
void delay0(uchar x)    
{
   uchar i;
   while(x--)
   {
     for (i = 0; i<13; i++) {;}
   }
}

/**********************************************************

  Buzzer driver subfunction

**********************************************************/
void beep()
{
  uchar i;
  for (i=0;i<180;i++)
  {
    delay0(5);
    BEEP=!BEEP; //BEEP inversion
  } 
   BEEP=1; //turn off the buzzer
   delayms(250); //delay     
}

/**********************************************************

Keyboard scanning subroutine

****************************************************** ********/
fly keyscan()
{
   fly scan1,scan2,keycode,j;

   P1=0xf0;
   scan1=P1;
   if((scan1&0xf0)!=0xf0) //Judge whether the key is pressed
   {
     delayms(30); //Delay 30ms
     scan1=P1;
     if((scan1&0xf0)!=0xf0) //Secondary judgment whether the key is pressed
     {
        P1=0x0f;
        scan2=P1;
        keycode=scan1|scan2; //Combined into key code

        for(j=0;j<=15;j++)
        {
           if(keycode== key_code[j]) //Look up the table to get the key value
           {
              key=j;
              return(key);
           }
        } 
     }
   }
   else P1=0xff;

   return (16);
}

/**********************************************************

  Determine whether the key is pressed sub function

**********************************************************/
void  keydown()
{  
   P1=0xf0;
   if((P1&0xf0)!=0xf0)
   {
     keyscan();
     P0=disp_code[key];
     beep();
   }
}

/**********************************************************

Main function

**************************************************************/
main()
{
   P0 = 0xbf;
   P2 = 0x7f; //The digital tube displays "-" 
    P1 = 0xff;

   while(1)
   {
     keydown();     
   }
}

Keywords:4X4 Reference address:4X4 matrix keyboard (lookup table method) C language programming

Previous article:Matrix keyboard key value display program C language programming
Next article:Digital tube display number 5 C language programming

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号