中级会员
- 积分
- 217
- 金钱
- 217
- 注册时间
- 2016-8-2
- 在线时间
- 27 小时
|
1金钱
本帖最后由 小分子 于 2016-8-9 06:32 编辑
[mw_shl_code=c,true]#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar KEYIN;
void DelayMs(uint ms);
uchar KeyPress(void);
uchar KeyScan(void);
void main(void)
{
while (1)
{
if (KeyPress())
{
P0 = KeyScan();
while (KeyPress());
}
}
}
void DelayMs(uint ms) //11.0592MHZ 约为1.007MS;
{
uint x,y;
for(x=ms;x>0;x--)
{
for(y=113;y>0;y--);
}
}
uchar KeyPress(void)
{
uchar keypress;
P1 = 0xFF;
keypress = P1 & 0xF0;
if (keypress != 0xF0)
{
KEYIN = keypress;
return 1;
}
else
{
return 0;
}
}
uchar KeyScan(void)
{
uchar keyout,index,keycode,keyvalue;
DelayMs(20);
P1 = 0xF7;
index = 0;
if (KeyPress())
{
while (1)
{
P1 = (0xF7 >> index) | 0xF0; //I/O端口不能做为中间变量;
index++;
index = index & 0x03;
keyout = P1 & 0x0F;
if ((P1 & 0xF0) != 0xF0)
{
keycode = KEYIN + keyout;
break;
}
}
switch (keycode)
{
case 0x77 : keyvalue = 0xC0; break;
case 0x7B : keyvalue = 0xF9; break;
case 0x7D : keyvalue = 0xA4; break;
case 0x7E : keyvalue = 0xB0; break;
case 0xD7 : keyvalue = 0x99; break;
case 0xDB : keyvalue = 0x92; break;
case 0xDD : keyvalue = 0x82; break;
case 0xDE : keyvalue = 0xF8; break;
case 0xB7 : keyvalue = 0x80; break;
case 0xBB : keyvalue = 0x90; break;
case 0xBD : keyvalue = 0x88; break;
case 0xBE : keyvalue = 0x83; break;
case 0xE7 : keyvalue = 0xC6; break;
case 0xEB : keyvalue = 0xA1; break;
case 0xED : keyvalue = 0x86; break;
case 0xEE : keyvalue = 0x8E; break;
default : keyvalue = 0xFF;
}
}
else
{
keyvalue = 0xFF;
}
return keyvalue;
}[/mw_shl_code]
|
|