keypad.c

来自「数字钟」· C语言 代码 · 共 53 行

C
53
字号
#include<reg51.h>
#define uchar unsigned char
uchar  key_code[]={0x7e,0x7d,0x7b,0x77,0xbe,0xbd,0xbb,0xb7,0xde,0xdd,0xdb,0xd7,0xee,0xed,0xeb,0xe7};
uchar duanma[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x73,0x3e,0x6e,0x1c,0x23,0x40,0x03};
       
uchar code ksp[4]={0x7f,0xbf,0xdf,0xef};
void delay(void)
{
  uchar i,j;
   for(i=0;i<5;i++)
    for(j=0;j<255;j++);
      
}
uchar keypad_scan()
{
  uchar key,i;
   P2=0xf0;
    while(P2!=0xf0);
   do{
      for(i=0;i<4;i++)
       {
        P2=ksp[i];
        if(P2!=ksp[i])
           {
             delay();
             key=P2;
             if(key!=ksp[i])
               {return(key);}
           }
   
      }
	 }while(1);
}

char gotkey()
{
  uchar i;
  char temp;
  temp=keypad_scan();
   for(i=0;i<16;i++)
       {
         if(temp==key_code[i]) return(i);
         }
}
void main()
{  char ch;
   do{
      ch=gotkey();
      P1=duanma[ch]; 
     }while(1);
      
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?