c语言键盘扫描程序

c语言键盘扫描程序,第1张

c语言键盘扫描程序

4*4键盘扫描C语言程序

 /*  4x4矩阵键盘的扫描程序

                  测试通过*/
                  #include
      sbit P1_0=P1^0;
      sbit P1_1=P1^1;
      sbit P1_2=P1^2;
      sbit P1_3=P1^3;
      sbit P2_7=P2^7;
      sbit P3_7=P3^7;

                  unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99, //0~4
                         0x92,0x82,0xF8,0x80,0x90, //5~9
                         0x88,0x83,0xC6,0xA1,0x86,0x8E}; //A~F
                  void KeyScan();
                  void delay10ms(unsigned char TIme);
                  void Dispaly(unsigned char k);
      void buzzer();
                  unsigned char key,temp;
                  void main() //主程序
                  {
                   while(1)
                   {
                    KeyScan();
                   }
                  }
                  void KeyScan() //按键扫描子程序
                  {
                   P1=0xFF;
                   P1_0=0;
                   temp=P1;
                   temp&=0xF0;
                   if(temp !=0xF0)
                   {buzzer();
                    delay10ms(10);
                    temp=P1;
                    temp&=0xF0;
                    if(temp !=0xF0)
                    {
                     temp=P1;
                     temp&=0xF0;
                     switch(temp)
                     {
                     case 0xE0:
                      key=0;break;
                     case 0xD0:
                      key=4;break;
                     case 0xB0:
                      key=8;break;
                     case 0x70:
                      key=12;break;
                     }
                     Dispaly(key);
                    }
                   }
                   P1=0xFF;
                   P1_1=0;
                   temp=P1;
                   temp&=0xF0;
                   if(temp !=0xF0)
                   {buzzer();
                    delay10ms(10);
                    temp=P1;
                    temp&=0xF0;
                    if(temp !=0xF0)
                    {
                     temp=P1;
                     temp&=0xF0;
                     switch(temp)
                     {
                     case 0xE0:
                      key=1;break;
                     case 0xD0:
                      key=5;break;
                     case 0xB0:
                      key=9;break;
                     case 0x70:
                      key=13;break;
                     }
                     Dispaly(key);
                    }
                   }
                   P1=0xFF;
                   P1_2=0;
                   temp=P1;
                   temp&=0xF0;
                   if(temp !=0xF0)
                   {buzzer();
                    delay10ms(10);
                    temp=P1;
                    temp&=0xF0;
                    if(temp !=0xF0)
                    {
                     temp=P1;
                     temp&=0xF0;
                     switch(temp)
                     {
                     case 0xE0:
                      key=2;break;
                     case 0xD0:
                      key=6;break;
                     case 0xB0:
                      key=10;break;
                     case 0x70:
                      key=14;break;
                     }
                     Dispaly(key);
                    }
                   }
                   P1=0xFF;
                   P1_3=0;
                   temp=P1;
                   temp&=0xF0;
                   if(temp !=0xF0)
                   {buzzer();
                    delay10ms(10);
                    temp=P1;
                    temp&=0xF0;
                    if(temp !=0xF0)
                    {
                     temp=P1;
                     temp&=0xF0;
                     switch(temp)
                     {
                     case 0xE0:
                      key=3;break;
                     case 0xD0:
                      key=7;break;
                     case 0xB0:
                      key=11;break;
                     case 0x70:
                      key=15;break;
                     }
                     Dispaly(key);
                    }
                   }
                  }
                  //延时程序
                  void delay10ms(unsigned char TIme)
                  {
                   unsigned char i;
       while(TIme--)
                  {
                     for(i=0;i<120;i++) ;
                  }
      }
                  void Dispaly(unsigned char k) //显示程序
                  {
                   P0=table[k];
                   P2_7=0;
                  }
      void buzzer()
      {
       P3_7=0;
       delay10ms(20);
       P3_7=1;
       }

6x3扫描键盘驱动程序(C语言编写)

〖说明〗18个按键扫描驱动程序,横向扫描第一行:p0.6  第二行:p0.7  第三行:p2.6
        纵向扫描:第一行:p0.0  第二行:p0.1  第三行:p0.2  第四行:p0.3  第五行:
        p0.4  第六行:p0.5
〖文件〗key.c  2002/12/10
作者〗鞠春阳
〖信箱〗jcygood@163.net   jcygood@sina.com
〖版本〗v1.00a build
--------------------------------------------------------------------------------*/


//#i nclude "reg51.h"
//#i nclude "lcd12864.h"

#define   port    p0  //定义p0口
sbit      p2_6=p2^6;  //定义p2.6口
unsigned int zdTIme=0;
unsigned int pingbaotime=0;
/*定义18个按键的返回值*/

#define  runkey                 1
#define  stopkey                2
#define  upkey                  3
#define  downkey                4
#define  a_c                    5
#define  d_f                    6
#define  g_i                    7
#define  j_l                    8
#define  m_o                    9
#define  p_s                    10
#define  t_v                    11
#define  w_z                    12
#define  pageupkey              13
#define  pagedownkey            14
#define  enterkey               15
#define  backkey                16
#define  chineseorenglish       17
#define  cancelkey              18

//按键扫描函数
unsigned char getkey()
{
  unsigned char a,b;
  unsigned int delay=0; //延时变量

  port=0xff;          //p0口复位
  p2_6=1;             //p2.6复位

  //扫描第三行
  p2_6=0;
   while(!(port&0x01))
     {
      if(++delay>0x1ff0) {  return(pagedownkey);}
     }
   while(!(port&0x02))
     {
      if(++delay>0x1ff0) {  return(chineseorenglish);}
     }
   while(!(port&0x04))
     {
      if(++delay>0x1ff0) {  return(backkey);}
     }
   while(!(port&0x08))
     {
      if(++delay>0x1ff0) {  return(pageupkey);}
     }
   while(!(port&0x10))
     {
      if(++delay>0x1ff0) {  return(cancelkey);}
     }
   while(!(port&0x20))
     {
      if(++delay>0x1ff0) {  return(enterkey);}
     }


   port=0xff;          //p0口复位
   p2_6=1;             //p2.6复位

   //扫描第一行
   port=0x7f;
   delay=0;
  while(!(port&0x01))
     {
      if(++delay>0x1ff0) {  return(downkey);}
     }
  if(delay>0xff0)  { return(downkey); }

  while(!(port&0x02))
     {
      if(++delay>0x1ff0) {  return(stopkey);}
     }
  if(delay>0xff0)  { return(stopkey); }

   while(!(port&0x04))
     {
      if(++delay>0x5fd0) {  return(w_z);}
     }
   if(delay>0xff0)  { return(w_z); }


   while(!(port&0x08))
     {
      if(++delay>0x5fd0) {  return(t_v);}
     }
   if(delay>0xff0)  { return(t_v); }


   while(!(port&0x10))
     {
      if(++delay>0x5fd0) {  return(p_s);}
     }
   if(delay>0xff0)  { return(p_s); }

   while(!(port&0x20))
     {
      if(++delay>0x5fd0) {  return(m_o);}
     }
  if(delay>0xff0)  { return(m_o); }


  port=0xff;          //p0口复位
  p2_6=1;             //p2.6复位

  //扫描第二行
   port=0xbf;
   delay=0;
   while(!(port&0x01))
     {
      if(++delay>0x1ff0) {  return(upkey);}
     }
   if(delay>0xff0)  { return(upkey); }

   while(!(port&0x02))
     {
      if(++delay>0x1ff0) {  return(runkey);}
     }
   if(delay>0xff0)  { return(runkey); }

   while(!(port&0x04))
     {
      if(++delay>0x5fd0) {  return(j_l);}
     }
   if(delay>0xff0)  { return(j_l); }

   while(!(port&0x08))
     {
      if(++delay>0x5fd0) {  return(g_i);}
     }
   if(delay>0xff0)  { return(g_i); }

   while(!(port&0x10))
     {
      if(++delay>0x5fd0) {  return(d_f);}
     }
   if(delay>0xff0)  { return(d_f); }

   while(!(port&0x20))
     {
      if(++delay>0x5fd0) {  return(a_c);}
     }
   if(delay>0xff0)  { return(a_c); }

   return(0);
}

3x4矩阵键盘的扫描程序(C语言)

/*  3x4矩阵键盘的扫描程序
按相应的按键,数码管显示相应的数字,星号键和井号键分别显示为E和F
*********************************
  Create by:左秀龙 
  support:
  Tel:0755-82879296
  email: cutedesign@tom.com
  Date: 2005/06/23
*********************************
测试通过*/

#include


unsigned char code table[]={0xC0,0xF9,0xA4,0xB0,0x99, //0~4
       0x92,0x82,0xF8,0x80,0x90, //5~9
       0x88,0x83,0xA7,0xA1,0x86,0x8E}; //A~F

void KeyScan();
void delay10ms(unsigned char time);
void Dispaly(unsigned char k);

unsigned char key,temp;

void main() //主程序
{
 while(1)
 {
  KeyScan();
 }
}

void KeyScan() //按键扫描子程序
{
 P1=0xFF;
 P1_3=0;
 temp=P1;
 temp&=0xF0;
 if(temp !=0xF0)
 {
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
   temp=P1;
   temp&=0xF0;

   switch(temp)
   {
   case 0x70:
    key=1;break;
   case 0xB0:
    key=2;break;
   case 0xD0:
    key=3;break;
   }
   Dispaly(key);
  }
 }

 P1=0xFF;
 P1_2=0;
 temp=P1;
 temp&=0xF0;
 if(temp !=0xF0)
 {
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
   temp=P1;
   temp&=0xF0;

   switch(temp)
   {
   case 0x70:
    key=4;break;
   case 0xB0:
    key=5;break;
   case 0xD0:
    key=6;break;
   }
   Dispaly(key);
  }
 }

 P1=0xFF;
 P1_1=0;
 temp=P1;
 temp&=0xF0;
 if(temp !=0xF0)
 {
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
   temp=P1;
   temp&=0xF0;

   switch(temp)
   {
   case 0x70:
    key=7;break;
   case 0xB0:
    key=8;break;
   case 0xD0:
    key=9;break;
   }
   Dispaly(key);
  }
 }

 P1=0xFF;
 P1_0=0;
 temp=P1;
 temp&=0xF0;
 if(temp !=0xF0)
 {
  delay10ms(1);
  temp=P1;
  temp&=0xF0;
  if(temp !=0xF0)
  {
   temp=P1;
   temp&=0xF0;

   switch(temp)
   {
   case 0x70:
    key=14;break;
   case 0xB0:
    key=0;break;
   case 0xD0:
    key=15;break;
   }
   Dispaly(key);
  }
 }
}

//延时程序
void delay10ms(unsigned char time)
{
 unsigned char a,b,c;
 for(a=0;a  for(b=0;b<10;b++)
   for(c=0;c<120;c++)
   ;
}

void Dispaly(unsigned char k) //显示程序
{
 P0=table[k];
 P2_1=0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/dianzi/2447909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-03
下一篇 2022-08-03

发表评论

登录后才能评论

评论列表(0条)