////////////////////////////////////////////////////
#include<reg51.H>
unsigned char k;
char code tab[4][4]={{0x3f,0x66,0x7f,0x39},
{0x06,0x6d,0x6f,0x5e},
{0x5b,0x7d,0x77,0x79},
{0x4f,0x07,0x7c,0x71}};
void delay()
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
}
char kbscan()
{
unsigned char hang,lie;
if(P1!=0x0f)
{
delay();
if(P1!=0x0f)
{
switch(P1&0x0f)
{
case 0x0e:hang=0;break;
case 0x0d:hang=1;break;
case 0x0b:hang=2;break;
case 0x07:hang=3;break;
}
P1=0xf0;
switch(P1&0xf0)
{
case 0xe0:lie=0;break;
case 0xd0:lie=1;break;
case 0xb0:lie=2;break;
case 0x70:lie=3;break;
}
P1=0x0f;
while(P1!=0x0f);
k=tab[hang][lie];
}
}
else
k=0;
return (k);
}
void main()
{
P2=0xfe;
while(1)
{
kbscan() ;
if(k!=0)
P0=k;
}
}
////////////////////////////////////////////////////////
#ifndef __4_4keyborad_H__
#define __4_4keyborad_H__
#define keyport P1
uchar key;
#define enter 20
#define set 21
#define right 22
#define left 23
#define up 24
#define down 25
#define null 26
#define Null 27
void keyscan()
{
unsigned char temp,hang,lie; //hang 为keyport的高4位,列为keyport的低4 位
unsigned char jianz;
bit scanover=0;
if((keyport&0x0f)!=0x0f)
{
delayms(10);
if((keyport&0x0f)!=0x0f)
{
while(scanover==0)
{
temp=keyport&0x0f;//屏蔽列
switch(temp)
{
case 0x0e: hang=0;break;
case 0x0d: hang=1;break;
case 0x0b: hang=2;break;
case 0x07: hang=3;break;
default: break;
}
keyport=0xf0;
temp=keyport&0xf0;//屏蔽行
switch(temp)
{
case 0xe0: lie=0;break;
case 0xd0: lie=1;break;
case 0xb0: lie=2;break;
case 0x70: lie=3;break;
default: break;
}
scanover=1;
}
jianz=hang*4+lie;
switch(jianz)
{
case 0: key=1; break;
case 1: key=2; break;
case 2: key=3; break;
case 3: key=enter;break;
case 4: key=4; break;
case 5: key=5; break;
case 6: key=6; break;
case 7: key=set; break;
case 8: key=7; break;
case 9: key=8; break;
case 10: key=9; break;
case 11: key=up; break;
case 12: key=left;break;
case 13: key=0; break;
case 14: key=right; break;
case 15: key=down; break;
}
}
}
while(keyport!=0x0f)
{
keyport=0x0f;
}
delayms(10);
keyport=0x0f;
}
#endif
|