27,516
社区成员
发帖
与我相关
我的任务
分享#include <reg51.h>
typedef unsigned char uchar;
typedef unsigned int uint;
code char tab[16] =
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x80
};
void delay(void)
{
uchar i;
for (i = 254; i > 0; i--);
}
uchar keyscan(void)
{
uchar sccode;
uchar recode;
P3 = 0xf0;
if ((P3 & 0xf0)!= 0xf0)
{
delay();
if ((P3 & 0xf0) != 0xf0)
{
sccode = 0xfe; //键盘逐行扫描
while ((sccode & 0x10) != 0)
{
P3 = sccode;
if ((P3 & 0xf0)!= 0xf0)
{
recode=(P3 & 0xf0); //被按键的行玛
sccode = sccode & 0x0f;
return ((sccode)+(recode));
}
else
{
sccode = (sccode << 1) | 0x01; //修改扫描玛
}
}
}
}
}
void display(uchar keydisp)
{
uchar keytab[18] =
{
0x7e,0x7d,0x7b,0x77,0xbd,0xbb,0xb7,0xdb,0xd7,0xbe,0xde,0xee,0xed,0xeb,0xe7
};
uchar m;
for (m = 0; m < 16; m++)
{
if (keytab[m] == keydisp) //查键码表,m为键值
{
P2 = 0xfe;
P0 = tab[m]; //键值显示
}
}
}
void main()
{
uchar key;
P2 = 0xfe;
P0 = 0x40;
P3 = 0xff;
if (P3 == 0xff)
{
while(1)
{
key = keyscan();
display(key);
}
}
}