#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)
{
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;
index++;
index = index & 0x03;
keyout = P1 & 0x0F;
keycode = KEYIN + keyout;
// }
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;
}
|