天祥单片机数码管实现简易计算器问题
时间:10-02
整理:3721RD
点击:
矩阵键盘和数码管实现简易计算器,但是在烧录后只能显示第一次按下的数字,后边就没反应了不知道是哪里写作了求大神帮忙看看
#include<reg51.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6;
sbit wei=P2^7;
uchar temp;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code weima[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void display(unsigned long a);
uchar keyscan();
void delay(uint xms);
void main()
{
unsigned long num1=0,num2=0,dispbuf=0;
unsigned char key,flag=1,fuhao;
while(1)
{
if(flag==1)
{
key=keyscan();
if(key<10)
{
num1=num1*10+key;
dispbuf=num1;
}
else if(key==12)
{
fuhao='+';
flag=2;
}
else if(key==13)
{
fuhao='-';
flag=2;
}
else if(key==14)
{
fuhao='*';
flag=2;
}
else if(key==15)
{
fuhao='/';
flag=2;
}
}
else if(flag==2)
{
key=keyscan();
if(key<10)
{
num2=num2*10+key;
dispbuf=num2;
}
else if(key==11)
{
switch(fuhao)
{
case'+':dispbuf=num1+num2;break;
case'-':dispbuf=num1-num2;break;
case'*':dispbuf=num1*num2;break;
case'/':dispbuf=num1/num2;break;
}
}
}
display(dispbuf);
}
}
void maco(unsigned char du,unsigned char we)
{
duan=1;
P0=table[du];
duan=0;
P0=0xff;
wei=1;
P0=weima[we];
wei=0;
}
void display(unsigned long x)
{
int i;
unsigned char a[6];
if(x==0)
{
maco(0,1);
}
else
{
for(i=0;x!=0;i++)
{
a[i]=x%10;
x/=10;
}
switch(i)
{
case 1:
maco(a[0],1);
while(1);
case 2:
while(1)
{
maco(a[1],1);
delay(1);
maco(a[0],2);
delay(1);
}
case 3:
while(1)
{
maco(a[2],1);
delay(1);
maco(a[1],2);
delay(1);
maco(a[0],3);
delay(1);
}
case 4:
while(1)
{
maco(a[3],1);
delay(1);
maco(a[2],2);
delay(1);
maco(a[1],3);
delay(1);
maco(a[0],4);
delay(1);
}
case 5:
while(1)
{
maco(a[4],1);
delay(1);
maco(a[3],2);
delay(1);
maco(a[2],3);
delay(1);
maco(a[1],4);
delay(1);
maco(a[0],5);
delay(1);
}
case 6:
while(1)
{
maco(a[5],1);
delay(1);
maco(a[4],2);
delay(1);
maco(a[3],3);
delay(1);
maco(a[2],4);
delay(1);
maco(a[1],5);
delay(1);
maco(a[0],6);
delay(1);
}
}
}
}
#include<reg51.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
sbit duan=P2^6;
sbit wei=P2^7;
uchar temp;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code weima[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
void display(unsigned long a);
uchar keyscan();
void delay(uint xms);
void main()
{
unsigned long num1=0,num2=0,dispbuf=0;
unsigned char key,flag=1,fuhao;
while(1)
{
if(flag==1)
{
key=keyscan();
if(key<10)
{
num1=num1*10+key;
dispbuf=num1;
}
else if(key==12)
{
fuhao='+';
flag=2;
}
else if(key==13)
{
fuhao='-';
flag=2;
}
else if(key==14)
{
fuhao='*';
flag=2;
}
else if(key==15)
{
fuhao='/';
flag=2;
}
}
else if(flag==2)
{
key=keyscan();
if(key<10)
{
num2=num2*10+key;
dispbuf=num2;
}
else if(key==11)
{
switch(fuhao)
{
case'+':dispbuf=num1+num2;break;
case'-':dispbuf=num1-num2;break;
case'*':dispbuf=num1*num2;break;
case'/':dispbuf=num1/num2;break;
}
}
}
display(dispbuf);
}
}
void maco(unsigned char du,unsigned char we)
{
duan=1;
P0=table[du];
duan=0;
P0=0xff;
wei=1;
P0=weima[we];
wei=0;
}
void display(unsigned long x)
{
int i;
unsigned char a[6];
if(x==0)
{
maco(0,1);
}
else
{
for(i=0;x!=0;i++)
{
a[i]=x%10;
x/=10;
}
switch(i)
{
case 1:
maco(a[0],1);
while(1);
case 2:
while(1)
{
maco(a[1],1);
delay(1);
maco(a[0],2);
delay(1);
}
case 3:
while(1)
{
maco(a[2],1);
delay(1);
maco(a[1],2);
delay(1);
maco(a[0],3);
delay(1);
}
case 4:
while(1)
{
maco(a[3],1);
delay(1);
maco(a[2],2);
delay(1);
maco(a[1],3);
delay(1);
maco(a[0],4);
delay(1);
}
case 5:
while(1)
{
maco(a[4],1);
delay(1);
maco(a[3],2);
delay(1);
maco(a[2],3);
delay(1);
maco(a[1],4);
delay(1);
maco(a[0],5);
delay(1);
}
case 6:
while(1)
{
maco(a[5],1);
delay(1);
maco(a[4],2);
delay(1);
maco(a[3],3);
delay(1);
maco(a[2],4);
delay(1);
maco(a[1],5);
delay(1);
maco(a[0],6);
delay(1);
}
}
}
}
你认为这里死循环while(1);程序能够跑下去吗?
......
switch(i)
{
case 1:
maco(a[0],1);
while(1);
case 2:
while(1)
{
maco(a[1],1);
delay(1);
maco(a[0],2);
delay(1);
}.....