用矩阵键盘做一个计算器

  • 格式:txt
  • 大小:4.20 KB
  • 文档页数:2

#include //52系列单片机头文件
#define uchar unsigned char
#define uint unsigned int
sbit dula=P2^6; //申明U1锁存器的锁存端
sbit wela=P2^7; //申明U2锁存器的锁存端
unsigned char a1,a2,num;
unsigned char code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delayms(uint xms)
{
unsigned int i,j;
for(i=xms;i>0;i--) //i=xms即延时约xms毫秒
for(j=110;j>0;j--);
}
void display(uchar tum,uchar dat)
{
uchar i;
dula=0;
P0=table[dat]; //显示函数只送段选数据
dula=1;
dula=0;
wela=0;
i=0xff;
i=i&(~((0x01)<<(tum)));
P0=i;
wela=1;
wela=0;
}
void matrixkeyscan()
{
uchar temp,a3;
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xee:
a3=1;

break;
case 0xde:
a3=2;

break;
case 0xbe:
a3=3;

break;
case 0x7e:
a3=4;

break;
}
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}



while(temp!=0xf0)
{
temp=P3;
temp=a3&0xf0;
}

}
}


P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xed:
a3=5;

break;
case 0xdd:
a3=6;

break;
case 0xbd:
a3=7;

break;
case 0x7d:
a3=8;

break;
}
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}


}

while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}


}




P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xeb:
a3=9;
if(num>0)
{
a2=a3;
display(1,a2);

}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}




break;
case 0xdb:
a3=0;
if(num>0)
{
a2=a3;
display(1,a2);
}
if(num==0)
{
a1=a3;
display(0,a1);
num++;
}



break;
case 0xbb:
a3=11; display(2,a1+a2);
break;
case 0x7b:
a3=11;display(2,a1-a2);
break;
}

while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}

}
}


P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:
a3=11;display(2,a1*a2);
break;
case 0xd7:
a3=11;display(2,a1/a2);
break;
case 0xb7:
a3=11;
break;
case 0x77:
a3=11;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void main()
{

while(1)
{
matrixkeyscan();//不停调用键盘扫描程序
}
}

下载文档原格式

  / 2
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。