#include<reg51.h>
#include<stdio.h>
#include<stdlib.h>
#include<intrins.h>
#include<absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define nop _nop_()
sbit pc=P2^0;
sbit pd=P2^1;
code uchar table[16]={0xfc,0x60,0xda,0xF2,0x66,
0xB6,0xBE,0xE0,0xFE,0xf6,
0x10,0x11,0x12,0x13,0x14,0x15};
uchar keycode;
//----------------------------------------------
void sendData(char c)
{
char i;
bit temp;
c=~c;
for(i=0;i<8;i++)
{
if (c&0xfe==0xfe) temp=0;
else temp=1;
pc=0;
pd=temp;
pc=1;
c>>=1;
}
}
//---------------------------------
void delay2ms()
{
uchar t=255;
while(t--);
t=52;
while(t--);
}
//---------------------------------
void delay10ms()
{
uchar i=5;
while(i--) delay2ms();
}
//---------------------------------
void detectkey(void)
{
uchar key,line,col;
P1=0xf0;
key=P1&0xf0;
if(key!=0xf0)
{
delay10ms();
P1=0xf0;
key=P1&0xf0;
if(key!=0xf0) //confirm
{
if(key==0xe0) line=0;
if(key==0xd0) line=1;
if(key==0xb0) line=2;
if(key==0x70) line=3;
P1=0xfe;
key=P1&0xf0;
if(key!=0xf0) col=0;
P1=0xfd;
key=P1&0xf0;
if(key!=0xf0) col=1;
P1=0xfb;
key=P1&0xf0;
if(key!=0xf0) col=2;
P1=0xf7;
key=P1&0xf0;
if(key!=0xf0) col=3;
keycode=(line*4+col);
sendData(table[keycode]);
P1=0xf0;
key=P1&0xf0;
while(key!=0xf0)
{
P1=0xf0;
key=P1&0xf0;
};//wait until release
}
}
}
//==================================
void main(void)
{
while(1) detectkey();
}