基于C51单片机和MAX9219的简易计算器程序Word格式.docx
《基于C51单片机和MAX9219的简易计算器程序Word格式.docx》由会员分享,可在线阅读,更多相关《基于C51单片机和MAX9219的简易计算器程序Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
b++)
for(c=0;
c<
120;
c++)
;
}
//--------按键扫描子程序-------------
//“1--9”号键的key值分别为1--9;
“+、-、*、/”的key值分别为13、14、15、16
//“=”的key值为10
//“on/c”的key值为11
voidKeyScan(void)
P0=0xFF;
P0_0=0;
temp=P0;
temp&
=0xF0;
if(temp!
=0xF0)
delay10ms
(1);
switch(temp)
{
case0xE0:
key=7;
P3_4=1;
delay10ms(30);
P3_4=0;
}
break;
case0xd0:
key=8;
case0xb0:
key=9;
case0x70:
key=16;
//“除号”
P0_1=0;
key=4;
key=5;
key=6;
key=15;
//“乘号”
P0_2=0;
key=1;
key=2;
key=3;
key=14;
//减号
P0_3=0;
key=11;
//复位ON/C
key=10;
//0
key=12;
delay10ms(50);
delay10ms();
//等号
key=13;
//加号
//-------数据发送子程序----------------
voidsend(unsignedcharaddr,unsignedchardat)
unsignedcharADS,i,j;
LOAD=0;
i=0;
while(i<
16)
if(i<
8)ADS=addr;
else
ADS=dat;
for(j=8;
j>
=1;
j--)
DIN=ADS&
0x80;
ADS<
<
CLK=1;
CLK=0;
i+=8;
LOAD=1;
//--------数据显示子程序--------------------
voiddisplay(unsignedlongintn)
unsignedcharn0;
unsignedcharn1;
unsignedcharn2;
unsignedcharn3;
unsignedcharn4;
unsignedcharn5;
unsignedcharn6;
unsignedcharn7;
n0=n/10000000;
n1=(n/1000000)%10;
n2=(n/100000)%10;
n3=(n/10000)%10;
n4=(n/1000)%10;
n5=(n/100)%10;
n6=(n/10)%10;
n7=n%10;
if(n0==0)
n0=0x0f;
if(n1==0)
n1=0x0f;
if(n2==0)
{
n2=0x0f;
if(n3==0)
{
n3=0x0f;
if(n4==0)
{
n4=0x0f;
if(n5==0)
{
n5=0x0f;
if(n6==0)
n6=0x0f;
}
}
}
}
send(0x0c,0x01);
//掉电控制寄存器设置为正常操作
send(0x0b,0x07);
//扫描边界,显示8位
send(0x0a,0xf5);
//亮度控制
send(0x09,0xff);
//译码方式
send(0x01,n0);
//最大显示结果65535;
最高位不显示,灭零;
send(0x02,n1);
send(0x03,n2);
send(0x04,n3);
send(0x05,n4);
send(0x06,n5);
send(0x07,n6);
send(0x08,n7);
//-----------主函数-----------
voidmain(void)
unsignedlongintfirst;
unsignedlongintsecond;
unsignedlongintresult;
//最终运算结果
unsignedcharop;
//用来存储+、-、*、/操作符键的值
unsignedcharflag,flag1;
LOOP:
first=0;
second=0;
result=0;
op=0;
flag=1;
flag1=0;
display(0);
while
(1)
KeyScan();
if(key<
11&
&
key>
0&
flag==1)
if(key==10)
key=0;
first=first*10+key;
key=0;
display(first);
if(key>
12&
key<
=16)
op=key;
flag=2;
flag==2)
second=second*10+key;
display(second);
flag1=1;
if(key==12&
flag1==1)
switch(op)
case13:
result=first+second;
break;
case14:
result=first-second;
case15:
result=first*second;
case16:
result=first/second;
second=0;
display(result);
key=0;
flag=1;
flag1=0;
if(key==11)
gotoLOOP;