|
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char//宏替換 //段選和位選
uchar LA[8],j,m;
sbit M=P3^5; //數(shù)組8位LA
uchar code wetable[]={
0x80,0x40,0x20,0x10,
0x08,0x04,0x02,0x01}; //數(shù)組編碼(位選從高位向低位逐位送0)(為共陰數(shù)碼管)
uchar code dutable[]=
{
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90
};
//段選0~9 //段選編碼(0到9)
long int num1,num2,num,numi;
uchar temp;//按鍵需要的中間變量
uchar flag;//標(biāo)記是否為數(shù)字鍵
uchar ch;//保存當(dāng)前按下的符號標(biāo)記按鍵的次數(shù),按下"+-*/"
uchar date,count;//date保存按鍵得到的數(shù)字,count標(biāo)記是第幾個數(shù)字
uchar p;//p為指針,為了消除數(shù)字之前多余的0
uchar mark;
void delay(uint z) //延遲函數(shù)
{
uint x,y;
for(x=z;x>0;x--)
for(y=0;y<=112;y++);
}
void init() //定義初值{
{
count=0;
flag=0;
mark=0;
num1=0;
num2=0;
num=0;
}
uchar keyscan()//判斷按下那個鍵//掃鍵函數(shù)
{
P2=0xfe;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(20);//消除抖動
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P2;
switch(temp)
{
case 0xee:flag=1;date=7;
break;
case 0xde:flag=1;date=8;
break;
case 0xbe:flag=1;date=9;
break;
case 0x7e:flag=2;ch='/';
break;
}
if(temp!=0xf0)
{
delay(20);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
P2=0xfd;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(20);//消除抖動
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P2;
switch(temp)
{
case 0xed:flag=1;date=4;
break;
case 0xdd:flag=1;date=5;
break;
case 0xbd:flag=1;date=6;
break;
case 0x7d:flag=2;ch='*';
break;
}
if(temp!=0xf0)
{
delay(20);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
P2=0xfb;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(20);//消除抖動
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P2;
switch(temp)
{
case 0xeb:flag=1;date=1;
break;
case 0xdb:flag=1;date=2;
break;
case 0xbb:flag=1;date=3;
break;
case 0x7b:flag=2;ch='-';
break;
}
if(temp!=0xf0)
{
delay(20);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
P2=0xf7;
temp=P2;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(20);//消除抖動
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P2;
switch(temp)
{
case 0xe7:flag=4;ch='C';
break;
case 0xd7:flag=1;date=0;
break;
case 0xb7:flag=3;//ch='=';
break;
case 0x77:flag=2;ch='+';
break;
}
if(temp!=0xf0)
{
delay(20);
temp=P2;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P2;
temp=temp&0xf0;
}
}
}
}
return date;
}
void display(long int num) //(總)顯示函數(shù)(顯示到第一次位的值不為0)(666)(顯示一個完整數(shù))
{
char i;
LA[7]=num%100000000/10000000;
LA[6]=num%10000000/1000000;
LA[5]=num%1000000/100000;
LA[4]=num%100000/10000;
LA[3]=num%10000/1000;
LA[2]=num%1000/100;
LA[1]=num%100/10;
LA[0]=num%10;
if(num==0)
{
P1=wetable[0];
P0=dutable[0];
delay(2);
P0=0xff;
}
else
{
for(i=7;i>=0;i--)
{
if(LA!=0)
{
p=i;
break;
}
}
for(i=0;i<=p;i++)
{
P1=wetable;
P0=dutable[LA];
delay(2);
P0=0xff;
}
}
}
void calc(uchar date) //得數(shù)轉(zhuǎn)換成數(shù)值
{
if(flag==1)
{
if(count==0)//得到的第一個完整數(shù)字//第一個數(shù)連續(xù)使用count=0以得到數(shù)值;
{
num1=num1*10+date;
flag=0;
num=num1;
}
if(count==1)//得到的第二個完整數(shù)字//第二個數(shù)在使用flag2后開始連續(xù)使用,直至flag3的
{
num2=num2*10+date;
flag=0;
num=num2;
}
}
if(count>1&&mark==1)
{
num1=num;
flag=0;
count=1;//講num2置為第二個數(shù)
}
display(num);
if(flag==2)//如果得到符號位
{
count++;
flag=0;
}
if(flag==3)
{
num=0;
switch(ch)
{
case '+':num=num1+num2; break;
case '-':num=num1-num2; break;
case '*':num=num1*num2; break;
case '/':num=num1/num2; break;
}
num1=0;
num2=0;
flag=0;
mark=1;
M=0;
delay(100);
M=1;
}
if(flag==4)
{
num1=0;
num2=0;
num=0;
count=0;
flag=0;
mark=0;
}
}
void main()
{
init();
while(1)
{
calc(keyscan());
}
}
大佬,里面的void calc(uchar date) 函數(shù)為什么能調(diào)用 uchar keyscan()里面除了date以外的其他變量啊,如flag,和字符變量ch,不是只返回了一個date嗎 |
|