#include "reg51.h"
char code keycode[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
char code segcode[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
delay(int x)
{ int i,j;
for(i=1;i<x;i++)
for(j=1;j<140;j++);
}
main()
{unsigned char temp,t;
int n;
P1=0X00;
P3=0xfe;
while(1)
{ t=P3;
temp=P3&0xf0;
if(temp!=0xf0)
{ delay(5);
temp=P3&0xf0;
if (temp!=0xf0)
{ do
{ if (P3==keycode[n])
break;
n++;
}while(n<16);
delay(5);
P1=segcode[n];
} }
n++;
P3=t;
P3=P3<<1|0X01;
if(P3==0xef)
{P3=0XFE; n=0;}
} }
下面程序为非循环的
#include "reg51.h"
char code keycode[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};
char code segcode[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
delay(int x)
{ int i,j;
for(i=1;i<x;i++)
for(j=1;j<140;j++);
}
main()
{unsigned char temp,t;
int num;
while(1)
{P3=0Xfe;
temp=P3&0xf0;
if(temp!=0xf0)
{
delay(5);
if(temp!=0xf0)
{temp=P3;
switch(temp)
{ case 0xee:num=0;
break;
case 0xde:num=1;
break;
case 0xbe:num=2;
break;
case 0x7e:num=3;
break;
}
P1=segcode[num];
}
}
P3=0Xfd;
temp=P3&0xf0;
if(temp!=0xf0)
{delay(5);
if(temp!=0xf0)
{temp=P3;
switch(temp)
{ case 0xed:num=4;
break;
case 0xdd:num=5;
break;
case 0xbd:num=6;
break;
case 0x7d:num=7;
break;
}
P1=segcode[num];
}
}
P3=0Xfb;
temp=P3&0xf0;
if(temp!=0xf0)
{delay(5);
if(temp!=0xf0)
{temp=P3;
switch(temp)
{ case 0xeb:num=8;
break;
case 0xdb:num=9;
break;
case 0xbb:num=10;
break;
case 0x7b:num=11;
break;
}
P1=segcode[num];
}
}
P3=0Xf7;
temp=P3&0xf0;
if(temp!=0xf0)
{delay(5);
if(temp!=0xf0)
{temp=P3;
switch(temp)
{ case 0xe7:num=12;
break;
case 0xd7:num=13;
break;
case 0xb7:num=14;
break;
case 0x77:num=15;
break;
}
P1=segcode[num];
}
}
} }
|