#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char KeyValue;
uint count;
uchar a,b,c,d,symbol,amount,KEY;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
void Delay(uint nms)
{
uint i,j;
for(i=nms;i>0;i--)
for(j=110;j>0;j--)
;
}
void Display()
{
P0=table[a];
a=0;
Delay(5);
a=1;
P0=table[b];
b=0;
Delay(5);
b=1;
P0=table[c];
c=0;
Delay(5);
c=1;
P0=table[d];
d=0;
Delay(5);
d=1;
}
void Key()
{
KEY=0x0f;
if(KEY!=0x0f)//讀取按鍵是否按下
{
Delay(1);//延時10ms進行消抖
if(KEY!=0x0f)//再次檢測鍵盤是否按下
{
//測試列
KEY=0X0F;//10000
switch(KEY)
{
case(0X07): KeyValue=0;break;
case(0X0b): KeyValue=1;break;
case(0X0d): KeyValue=2;break;
case(0X0e): KeyValue=3;break;
}
//測試行
KEY=0XF0;
switch(KEY)
{
case(0X70): KeyValue=KeyValue+12;break;
case(0Xb0): KeyValue=KeyValue+8;break;
case(0Xd0): KeyValue=KeyValue+4;break;
case(0Xe0): KeyValue=KeyValue+0;break;
}
while (KEY!=0xf0); //檢測按鍵松手檢測
}
}
}
void main()
{
KeyValue=13;
a=b=c=d=10;
while(1)
{
while(count==0)
{
Display();
Key();
if(KeyValue<=9)
{
a=KeyValue;
count++;
KeyValue=13;
}
}
while(count==1)
{
Display();
Key();
while(KeyValue!=13)
{
if(KeyValue>9)
{
if(KeyValue==10)
{
symbol=0;
count++;
KeyValue=13;
}
if(KeyValue==11)
{
symbol=1;
count++;
KeyValue=13;
}
}
}
}
while(count==2)
{
Display();
Key();
while(KeyValue!=13)
{
if(KeyValue<=9)
{
b=KeyValue;
count++;
KeyValue=13;
}
}
}
while(count==3)
{
Display();
if(symbol==0)
{
amount=a+b;
}
if(symbol==1)
{
amount=a-b;
}
c=amount/10;
d=amount%10;
}
}
}
|