注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
鸿泽的个人空间 https://home.eeworld.com.cn/space-uid-546247.html [收藏] [复制] [分享] [RSS]
日志

STC51单片机关于键盘的程序,不知道哪里错了,求解答

已有 608 次阅读2014-5-4 22:42 |个人分类:单片机问题| 单片机, 程序, 键盘

#include<reg52.h>
#define uc unsigned char
#define ui unsigned int 
sbit wela=P2^7;
sbit dula=P2^6;
uc temp,key,a[100],i=0;
uc code table[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uc code table2[]={
0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uc xms)
{
ui ii,j;
for(ii=xms;xms>0;xms--)
for(j=110;j>0;j--);

}
void display(ui we,uc num)
{
dula=1;
P0=table[num];
dula=0;
P0=table2[we];
wela=1;
wela=0;


}

void keyscan()
{ ui j;

P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=1;break;
case 0xde:key=2;break;
case 0xbe:key=3;break;
case 0x7e:key=4;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}

P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=5;break;
case 0xde:key=6;break;
case 0xbe:key=7;break;
case 0x7e:key=8;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}

P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=9;break;
case 0xde:key=10;break;
case 0xbe:key=11;break;
case 0x7e:key=12;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}

}

P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{case 0xee:key=13;break;
case 0xde:key=14;break;
case 0xbe:key=15;break;
case 0x7e:key=16;break;}
a[i]=key;i++;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
for(j=i;j>0;j--)
display(j,a[j]);


}

void main()
{
while(1)
{
keyscan();
}
}
评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章