單片機做的8位計算器
CODE:本文引用地址:http://m.butianyuan.cn/article/201612/324178.htm
#include
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar LA[8];
uchar code wetable[]={
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe};
uchar code dutable[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f};//段選0~9
longint num1,num2,num;
uchar temp;//按鍵需要的中間變量
uchar flag;//標記是否為數(shù)字鍵
uchar ch;//保存當(dāng)前按下的符號標記按鍵的次數(shù),按下“+-*/”
uchar date,count;//date保存按鍵得到的數(shù)字,count標記是第幾個數(shù)字
uchar p;//p為指針,為了消除數(shù)字之前多余的0
uchar mark;
void delay(uint z)
{
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()//判斷按下那個鍵
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖動
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P1;
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(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖動
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P1;
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(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖動
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P1;
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(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(5);//消除抖動
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//證明有鍵按下,while語句是為了讓鍵盤松開
{
temp=P1;
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(5);
temp=P1;
temp=temp&0xf0;
while(temp!=0xf0)//等待按鍵松開
{
temp=P1;
temp=temp&0xf0;
}
}
}
}
評論