#include<reg51.h>
sbit ADDR0 = P3^0;
sbit ADDR1 = P3^1;
sbit ADDR2 = P3^2;
sbit KEY_IN_0 = P2^7;
sbit KEY_IN_1 = P2^6;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^4;
sbit KEY_OUT_0 = P2^3;
sbit KEY_OUT_1 = P2^2;
sbit KEY_OUT_2 = P2^1;
sbit KEY_OUT_3 = P2^0;
unsigned char code ledchar[] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
unsigned char keysta[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
void main()
{
unsigned char i,j;
unsigned char backup[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
ADDR0 = 1;
ADDR1 = 1;
ADDR2 = 1;
TMOD = 0X01;
TH0 = 0xfc;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
P2 = 0xff;
P0 = ledchar[0];
while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(backup[i][j] != keysta[i][j])
{
if(backup[i][j] == 0)
{
P0 = ledchar[i*4+j];
}
backup[i][j] = keysta[i][j];
}
}
}
}
}
void interruptTimor0() interrupt 1
{
static unsigned char keyout=0;
unsigned int i=0;
static unsigned char keybuf[4][4] ={
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff}
};
TH0 = 0xfc;
TL0 = 0x18;
keybuf[keyout][0] = ((keysta[keyout][0]<<1) | KEY_IN_0);
keybuf[keyout][1] = ((keysta[keyout][1]<<1) | KEY_IN_1);
keybuf[keyout][2] = ((keysta[keyout][2]<<1) | KEY_IN_2);
keybuf[keyout][3] = ((keysta[keyout][3]<<1) | KEY_IN_3);
for(i=0;i<4;i++)
{
if(keybuf[keyout][i] & 0x0f == 0x00 )
{
keysta[keyout][i] = 0;
}
else if(keybuf[keyout][i] & 0x0f == 0x0f )
{
keysta[keyout][i] = 1;
}
}
keyout++;
keyout = keyout & 0x03;
switch(keyout)
{
case 0: KEY_OUT_0 = 0; KEY_OUT_1 = 1; KEY_OUT_2 = 1; KEY_OUT_3 = 1;break;
case 1: KEY_OUT_0 = 1; KEY_OUT_1 = 0; KEY_OUT_2 = 1; KEY_OUT_3 = 1;break;
case 2: KEY_OUT_0 = 1; KEY_OUT_1 = 1; KEY_OUT_2 = 0; KEY_OUT_3 = 1;break;
case 3: KEY_OUT_0 = 1; KEY_OUT_1 = 1; KEY_OUT_2 = 1; KEY_OUT_3 = 0;break;
default: break;
}
}
sbit ADDR0 = P3^0;
sbit ADDR1 = P3^1;
sbit ADDR2 = P3^2;
sbit KEY_IN_0 = P2^7;
sbit KEY_IN_1 = P2^6;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^4;
sbit KEY_OUT_0 = P2^3;
sbit KEY_OUT_1 = P2^2;
sbit KEY_OUT_2 = P2^1;
sbit KEY_OUT_3 = P2^0;
unsigned char code ledchar[] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71
};
unsigned char keysta[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
void main()
{
unsigned char i,j;
unsigned char backup[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
ADDR0 = 1;
ADDR1 = 1;
ADDR2 = 1;
TMOD = 0X01;
TH0 = 0xfc;
TL0 = 0x18;
EA = 1;
ET0 = 1;
TR0 = 1;
P2 = 0xff;
P0 = ledchar[0];
while(1)
{
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(backup[i][j] != keysta[i][j])
{
if(backup[i][j] == 0)
{
P0 = ledchar[i*4+j];
}
backup[i][j] = keysta[i][j];
}
}
}
}
}
void interruptTimor0() interrupt 1
{
static unsigned char keyout=0;
unsigned int i=0;
static unsigned char keybuf[4][4] ={
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff},{0xff,0xff,0xff,0xff}
};
TH0 = 0xfc;
TL0 = 0x18;
keybuf[keyout][0] = ((keysta[keyout][0]<<1) | KEY_IN_0);
keybuf[keyout][1] = ((keysta[keyout][1]<<1) | KEY_IN_1);
keybuf[keyout][2] = ((keysta[keyout][2]<<1) | KEY_IN_2);
keybuf[keyout][3] = ((keysta[keyout][3]<<1) | KEY_IN_3);
for(i=0;i<4;i++)
{
if(keybuf[keyout][i] & 0x0f == 0x00 )
{
keysta[keyout][i] = 0;
}
else if(keybuf[keyout][i] & 0x0f == 0x0f )
{
keysta[keyout][i] = 1;
}
}
keyout++;
keyout = keyout & 0x03;
switch(keyout)
{
case 0: KEY_OUT_0 = 0; KEY_OUT_1 = 1; KEY_OUT_2 = 1; KEY_OUT_3 = 1;break;
case 1: KEY_OUT_0 = 1; KEY_OUT_1 = 0; KEY_OUT_2 = 1; KEY_OUT_3 = 1;break;
case 2: KEY_OUT_0 = 1; KEY_OUT_1 = 1; KEY_OUT_2 = 0; KEY_OUT_3 = 1;break;
case 3: KEY_OUT_0 = 1; KEY_OUT_1 = 1; KEY_OUT_2 = 1; KEY_OUT_3 = 0;break;
default: break;
}
}