大家帮忙检查一下那里出了问题,调试很长时间都没检查出来:
以下是源码:
/*---------------------------------------------*/
/* 简单计算器数码管显示,用于两个整数的加减乘除,其中两个数及其结果都不能超过8位数,否则显示“-ERROR-”*/
/*芯片:STC89C52RC */
/*---------------------------------------------*/
#include<reg52.h>//头包含
#include<intrins.h>//头文件包含,_crol_()函数移位函数,类似C语言中"<<"操作符
#define unit unsigned int//宏定义
#define uchar unsigned char
//硬件:
sbit wela=P2^7;//LED位选
sbit dula=P2^6;//LED段选
uchar stack[8];uchar top;//用于存放待显示数字的栈 红色的分号改成半角字符(西文标点)
long int num1,num2,reasult;
uchar key=255,i,opsignal,error;
uchar code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数码管数字显示十六进制编码
uchar code errorCode[]={0xbf,0x86,0x88,0x88,0xc0,0x88,0xbf};//"ERROR"英文字母编码
uchar lenth(long int n);//计算数字的位数用于将待显示数存进stack[]中
long int power(uchar n);//幂函数10的n次方
void delayms(unit ms);延时函数 //加上注释符号,下面2行同
void display(uchar mode);显示函数
uchar keyscan();键盘扫描函数返回按键键值0~15,矩阵按键
//主函数
void main()
{
while(1)
{
key=keyscan();
if(key<10)
{
stack[top]=key;
top++;
}
else if(key>11)
{
for(i=0;i<top;i++)num1=num1+stack*power(top-i-1);
if(key==12)opsignal=1;
else if(key==13)opsignal=2;
else if(key==14)opsignal=3;
else if(key==15)opsignal=4;
top=0;
error=0;
}
else if(key==10){top=0;error=0;}
else if(key==11)
{
for(i=0;i<top;i++)num2=num2+stack*power(top-i-1);
switch(opsignal)
{
case 1:reasult=num1+num2;break;
case 2:reasult=num1-num2;break;
case 3:reasult=num1*num2;break;
case 4:reasult=num1/num2;break;
}
}
for(i=0;i<top;i++)reasult=reasult+stack*power(top-i-1);
if(top>8)error=1;
display(reasult);
}
}
//
uchar lenth(long int n)
{
uchar i,l;
for(i=0;i<8;i++)if(n/power(i)==0){l=i;break;}
return l;
}
//
long int power(uchar n)
{
uchar i;
long int p=1;
for(i=0;i<n;i++)p=p*10;
return p;
}
//延时函数(毫秒)
void delayms(unit ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
//数码管动态扫描
void display(long int number)// 这里的参数类型和前面声明中的不一致
{
uchar i,l,aa=0x01;
l=lenth(number);
for(i=0;i<l;i++)
{
stack=number/power(l-i-1);
number=number%power(l-i-1);
}
if(error==0)
{
for(i=0;i<l;i++)
{
wela=1;
P0=aa;
wela=0;
aa=_crol_(aa,1);
dula=1;
P0=table[stack];
dula=0;
P0=0xff;
delayms(10);
}
}
else if(error==1)
{
aa=0x01;
for(i=0;i<7;i++)
{
wela=1;
P0=aa;
wela=0;
aa=_crol_(aa,1);
dula=1;
P0=errorCode;
dula=0;
P0=0xff;
delayms(10);
}
}
}
//扫描函数(I/O)并返回按键
uchar keyscan()
{
uchar key=255,temp;
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 0x7e:key=15;break;//"/"除
case 0xbe:key=14;break;//"*"乘
case 0xde:key=13;break;//"-"减
case 0xee:key=12;break;//"+"加
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&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:key=11;break;//"="
case 0xdd:key=10;break;//"CLR"
case 0xbd:key=9;break;
case 0x7d:key=8;break;
}
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:key=7;break;
case 0xdb:key=6;break;
case 0xbb:key=5;break;
case 0x7b:key=4;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:key=3;break;
case 0xd7:key=2;break;
case 0xb7:key=1;break;
case 0x77:key=0;break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return key;
}
改过后应如下: (void display(uchar number 参数类型的问题自己斟酌这样是否合适)
/* 简单计算器数码管显示,用于两个整数的加减乘除,其中两个数及其结果都不能超过8位数,否则显示"-ERROR-"*/
/*芯片:STC89C52RC */
/*---------------------------------------------*/
#include<reg52.h>//头包含
#include<intrins.h>//头文件包含,_crol_()函数移位函数,类似C语言中"<<"操作符
#define unit unsigned int//宏定义
#define uchar unsigned char
//硬件:
sbit wela=P2^7;//LED位选
sbit dula=P2^6;//LED段选
uchar stack[8];
uchar top;//用于存放待显示数字的栈
long int num1,num2,reasult;
uchar key=255,i,opsignal,error;
uchar code table[]={0xc0,0xcf,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//数码管数字显示十六进制编码
uchar code errorCode[]={0xbf,0x86,0x88,0x88,0xc0,0x88,0xbf};//"ERROR"英文字母编码
uchar lenth(long int n);//计算数字的位数用于将待显示数存进stack[]中
long int power(uchar n);//幂函数10的n次方
void delayms(unit ms);//延时函数
void display(uchar number);//显示函数
uchar keyscan();//键盘扫描函数返回按键键值0~15,矩阵按键
//主函数
void main()
{
while(1)
{
key=keyscan();
if(key<10)
{
stack[top]=key;
top++;
}
else if(key>11)
{
for(i=0;i<top;i++)num1=num1+stack*power(top-i-1);
if(key==12)opsignal=1;
else if(key==13)opsignal=2;
else if(key==14)opsignal=3;
else if(key==15)opsignal=4;
top=0;
error=0;
}
else if(key==10){top=0;error=0;}
else if(key==11)
{
for(i=0;i<top;i++)num2=num2+stack*power(top-i-1);
switch(opsignal)
{
case 1:reasult=num1+num2;break;
case 2:reasult=num1-num2;break;
case 3:reasult=num1*num2;break;
case 4:reasult=num1/num2;break;
}
}
for(i=0;i<top;i++)reasult=reasult+stack*power(top-i-1);
if(top>8)error=1;
display(reasult);
}
}
//
uchar lenth(long int n)
{
uchar i,l;
for(i=0;i<8;i++)if(n/power(i)==0){l=i;break;}
return l;
}
//
long int power(uchar n)
{
uchar i;
long int p=1;
for(i=0;i<n;i++)p=p*10;
return p;
}
//延时函数(毫秒)
void delayms(unit ms)
{
uchar i,j;
for(i=0;i<ms;i++)
for(j=0;j<110;j++);
}
//数码管动态扫描
//void display(long int number)//
void display(uchar number)
{
uchar i,l,aa=0x01;
l=lenth(number);
for(i=0;i<l;i++)
{
stack=number/power(l-i-1);
number=number%power(l-i-1);
}
if(error==0)
{
for(i=0;i<l;i++)
{
wela=1;
P0=aa;
wela=0;
aa=_crol_(aa,1);
dula=1;
P0=table[stack];
dula=0;
P0=0xff;
delayms(10);
}
}
else if(error==1)
{
aa=0x01;
for(i=0;i<7;i++)
{
wela=1;
P0=aa;
wela=0;
aa=_crol_(aa,1);
dula=1;
P0=errorCode;
dula=0;
P0=0xff;
delayms(10);
}
}
}
//扫描函数(I/O)并返回按键
uchar keyscan()
{
uchar key=255,temp;
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 0x7e:key=15;break;//"/"除
case 0xbe:key=14;break;//"*"乘
case 0xde:key=13;break;//"-"减
case 0xee:key=12;break;//"+"加
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&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:key=11;break;//"="
case 0xdd:key=10;break;//"CLR"
case 0xbd:key=9;break;
case 0x7d:key=8;break;
}
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:key=7;break;
case 0xdb:key=6;break;
case 0xbb:key=5;break;
case 0x7b:key=4;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:key=3;break;
case 0xd7:key=2;break;
case 0xb7:key=1;break;
case 0x77:key=0;break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
return key;
} |