大家幫忙檢查一下那里出了問題,調試很長時間都沒檢查出來:
以下是源碼:
/*---------------------------------------------*/
/* 簡單計算器數碼管顯示,用于兩個整數的加減乘除,其中兩個數及其結果都不能超過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;
} |