键盘显示
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
#define LEDLen 6
#define OUTBIT XBYTE[0x8002]
#define OUTBEG XBYTE[0x8004]
#define IN XBYTE[0x8001]
#define uchar unsigned char
#define uint unsigned int
unsigned char LEDBuf[LEDLen];
unsigned char const LEDMAP[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
unsigned char dispbuff[6];
void delay_1ms(void)
{
unsigned char temp=249;
while(--temp);
temp=249;
while(--temp);
}
void delay(uint z) //延时1ms
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void disp()
{
unsigned char i;
unsigned char dssel=0xfe;
dssel=0xfe;
for(i=0;i<6;i++)
{
OUTBIT =~dssel;
OUTBEG=LEDMAP[dispbuff[i]];
delay_1ms();
dssel>>=1;
dssel|=0x20;
}
}
unsigned char const KeyTable[] = {
0x16, 0x15, 0x14, 0xff,
0x13, 0x12, 0x11, 0x10,
0x0d, 0x0c, 0x0b, 0x0a,
0x0e, 0x03, 0x06, 0x09,
0x0f, 0x02, 0x05, 0x08,
0x00, 0x01, 0x04, 0x07
};
unsigned char TestKey()
{
OUTBIT = 0; // 输出线置为0 return (~IN & 0x0f); // 读入键状态) }
unsigned char GetKey()
{
unsigned char Pos;
unsigned char i;
unsigned char j;
unsigned char k;
i = 6;
Pos = 0x20; // 找出键所在列
do {
OUTBIT = ~ Pos;
Pos >>= 1;
k = ~IN & 0x0f;
} while ((--i != 0) && (k == 0));
// 键值= 列X 4 + 行
if (k != 0)
{
i *= 4;
if (k & 2)
i += 1;
else if (k & 4)
i += 2;
else if (k & 8)
i += 3;
OUTBIT = 0;
do
{
delay(10);
}
while (TestKey()); // 等键释放
return(KeyTable[i]); // 取出键码
}
else return(0xff);
}
void main()
{
unsigned char m,n; dispbuff[0]=0;
while(1)
{
disp();
n= TestKey();
if (n !=0)
{
m=GetKey();
dispbuff[0]=m&0x0f;
}
}
}