# include "reg52. H"
Typedef unsigned char u8;
typedef unsigned int u16;
U8 Keyvalue;
Int n=0;
Int number=0;
Int mathsign=0;
Int sign1=0, sign2=0, sign3=0, sign4=0;
Int num1=0, num2=0; Num3=0; Num4=0;
Int show1 show2, show3 show4, show5=16;
U8 mark=0;
U8 k;
Int flag=0;
Reset=0;
# define DigDisplay P0
# define Key P1
Sbit LSA=P2 ^ 2;//dynamic digital tube a selected IO mouth
Sbit LSB=P2 ^ 3;
sbit LSC=P2^4;
U8 code smgduan [17]={x5b x3f 0, 0 x06, 0, 0, x4f x66 0, 0 x6d, 0 x7d, 0 x07,//a total of Yin digital tube array
X6f 0 x7f, 0, 0 x77, 0 x7c, 0 x39, 0 x5e, 0 x79, 0 x71, 0 x00};
Void delay (under-16 I)//delay function
{
while(i--);
}
Void Appear ()//dynamic digital tube display
{
If ((number<10000) & amp; & (number> 1000))//remove to calculate the end result is a 1000 digits
{
Show1=number/1000;
Show2 number %=1000/100;
Show3 number %=100/10;
Show4 %=number 10;
}
If ((number<1000) & amp; & (number> 100))
{
Show1=number/100;
Show2 number %=100/10;
Show3 %=number 10;
Show4=16;
}
If ((number<100) & amp; & (number> 10))
{
Show1=number/10;
Show2 %=number 10;
Show3=16;
Show4=16;//0 xf0
}
If (number<10)
{
Show1=number;
Show2=16;
Show3=16;
Show4=16;
}
If (number==0)
{
Show1=16;
Show2=16;
Show3=16;
Show4=16;
}
for(k=0; k<8; K++)//dynamic digital tube a loop, to achieve at the same time show
{
The switch (k)
{
Case (0) :
LSA=0; LSB=0; LSC=0; break;//show a 0
Case (1) :
LSA=1; LSB=0; LSC=0; break;//show the first bit
Case (2) :
LSA=0; LSB=1; LSC=0; break;//show the second
Case (3) :
LSA=1; LSB=1; LSC=0; break;//show the third
Case (4) :
LSA=0; LSB=0; LSC=1; break;//show 4
Case (5) :
LSA=1; LSB=0; LSC=1; break;//show the fifth
Case (6) :
LSA=0; LSB=1; LSC=1; break;//show the sixth
Case (7) :
LSA=1; LSB=1; LSC=1; break;//show the 7th
}
If (k==0)
{
DigDisplay=smgduan [show1];
delay(100);
DigDisplay=0 x00;
}
If (k==1)
{
DigDisplay=smgduan [show2];
delay(100);
DigDisplay=0 x00;
}
If (k==2)
{
DigDisplay=smgduan [show3];
delay(100);
DigDisplay=0 x00;
}
If (k==3)
{
DigDisplay=smgduan [show4];
delay(100);
DigDisplay=0 x00;
}
If (k> 3)
{
DigDisplay=smgduan [show5];
delay(100);
DigDisplay=0 x00;
}
}
}
Void Keydown (void)//matrix keys
{
Char a=0;
Key=0 x0f;//testing column level confirm by pressing the button
If (Key!=0 x0f)
{
Delay (1000);
If (Key!=0 x0f)
{
Key=0 x0f;
The switch (Key)
{
Case 0 x07:keyvalue=https://bbs.csdn.net/topics/0; break;
Case 0 x0b:keyvalue=https://bbs.csdn.net/topics/1; break;
Case 0 x0d:keyvalue=https://bbs.csdn.net/topics/2; break;
Case 0 x0e: mathsign=1; break;//the last as a mathematical operators
}
Key=0 xf0;//detection line level, confirm by pressing buttons and row
If (mathsign==0)
{
The switch (Key)//to press the number keys a number
{
Case 0 x70:keyvalue=https://bbs.csdn.net/topics/keyvalue; break;//0 2 + 1
Case 0 xb0:keyvalue=https://bbs.csdn.net/topics/keyvalue+3; break;//3, 4, 5 -
Case 0 xd0:keyvalue=https://bbs.csdn.net/topics/keyvalue+6; break;//6 7 8 *
Case 0 xe0:keyvalue=https://bbs.csdn.net/topics/keyvalue+9; break;//9 C=/
}
If (Keyvalue=https://bbs.csdn.net/topics/=11)//press for 11 key is equivalent to pressing the equals sign, flag logo position 1
{
flag=1;
}
n++;//every press a key, the n + 1, when n is an odd number of press Numbers, even press operator
}
If (Keyvalue=https://bbs.csdn.net/topics/=10)//press 10 key is equivalent to press the reset button, the reset also marked a
{
Reset=1;
}
If (mathsign==1)//specified in the first four buttons, sign on behalf of each operator
{
The switch (Key)
{
Case 0 x70: sign1=1; break;
Case 0 xb0: sign2=1; break;
Case 0 xd0: sign3=1; break;
Case 0 xe0-0xfc: sign4=1; break;
}
n++;
}
}
If (n % 2==1)//will press the numeric value is assigned to a variable num
{
The switch (n)
{
Case (1) :
Num1=Keyvalue; break;
Case (3) :
Num2=Keyvalue; break;
Case (5) :
Num3=Keyvalue; break;
Case (7) :
Num4=Keyvalue; break;
}
Keyvalue=https://bbs.csdn.net/topics/0;
}
}
While ((a<50) & amp; & (the Key!=0 xf0))//press automatic exit for a period of time, according to the next is only a number
{
delay(100);
a++;
}
}
Void InitInt0 ()//interrupt to allow an open
{
IT0=1;
EX0=1;
EA=1;
}
Void main ()
{
While (1)
{
Keydown ();
InitInt0 ();
If (flag==1)//press the equal sign shows the final result the
{
Appear ();
If reset (reset==1)//
{
N=0;
flag=0;
Show1=0;
Show2=0;
Show3=0;
Show4=0;
Num1=0;
Num2=0;
Num3=0;
Num4=0;
Reset=0;
}
}
}
}
Void Int0 () interrupt 0//external interrupt 0
{
Delay (1000);
If ((sign1==1) & amp; & (n % 2==0))
{
The switch (n)
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull