|||
今儿整理了一个他人的C语言4*4键盘程序,用着还不错.
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define duank P1 //键盘到单片机的端口
//DB 50H,1CH,39H,16H,38H,78H,70H,0FEH,0EFH,0FFH;A B, C, D, E, ,F ,P, -, ,. ,COLSE
//DB 01H,0C7H,22H,82H,0C4H,88H,08H,0C3H,00H,80H;带小数点的字形码0.,1.,-----9.
uchar code table[]={ //键盘编码"1~F~0"
0xD7,0x32,0x92,0xD4,
0x98,0x18,0xD1,0x10,
0x90,0x50,0x1C,0x39,
0x16,0x38,0x78,0x11};
uchar num,temp,num1;
void delay(uint z) //延时子程序 Zms
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
uchar keyscan(); //键盘函数声明
void display(uchar aa);//显示函数声明
void main() //主函数
{
while(1)
{
display(keyscan());
}
}
void display(uchar aa) //显示子程序
{
//dula=1;
//P0=table[aa-1];
//dula=0;
SBUF=table[aa-1];
}
uchar keyscan() //键盘扫描程序,取回一个键盘号
{
duank=0xfe;
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=duank;
switch(temp)
{
case 0xee:num=1;
break;
case 0xde:num=2;
break;
case 0xbe:num=3;
break;
case 0x7e:num=4;
break;
}
while(temp!=0xf0)
{
temp=duank;
temp=temp&0xf0;
}
}
}
duank=0xfd;
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=duank;
switch(temp)
{
case 0xed:num=5;
break;
case 0xdd:num=6;
break;
case 0xbd:num=7;
break;
case 0x7d:num=8;
break;
}
while(temp!=0xf0)
{
temp=duank;
temp=temp&0xf0;
}
}
}
duank=0xfb;
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=duank;
switch(temp)
{
case 0xeb:num=9;
break;
case 0xdb:num=10;
break;
case 0xbb:num=11;
break;
case 0x7b:num=12;
break;
}
while(temp!=0xf0)
{
temp=duank;
temp=temp&0xf0;
}
}
}
duank=0xf7;
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=duank;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp=duank;
switch(temp)
{
case 0xe7:num=13;
break;
case 0xd7:num=14;
break;
case 0xb7:num=15;
break;
case 0x77:num=16;
break;
}
while(temp!=0xf0)
{
temp=duank;
temp=temp&0xf0;
}
}
}
return num;
}