For the electronic major, although I don't plan to eat this dinner later, I still have to do my homework at the end of the course-"A simple calculator program ". Because of the time, I have never been concerned about sorting it out. I will post it here today to share it with you. It is also a bit of memory for my beautiful youth ...... hee hee 1
The program code is as follows:
# Include <reg51.h>
# Define uchar unsigned char
# Define uintUnsigned int
BitNumsem; // mutex semaphores of the frontend and backend operands
BitEqu; // equal sign judgment
BitCLR; // clear the flag
BitAdd; // Add a flag
BitDec; // minus sign
BitMul; // multiplication mark
BitDiv; // remove the flag
BitOP; // arithmetic sign
UcharKeynum; // key value
UintResult; // Calculation Result
UintPrenum, latnum; // The first and last operands of the two operands.
UcharKeypos;
UcharDisled [] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, // LED digital seven-segment table
0x92,0x82, 0xf8, 0x80, 0x90 };
UcharNumbufe [] = {0, 0, 0 };
// Sbit P10 = p1 ^ 0;
// Sbit P11 = p1 ^ 1;
// Sbit p12 = p1 ^ 2;
// Sbit P13 = p1 ^ 3;
VoidT0intal (void); // timer 0 initialization program
Void display (void); // display program
Void resultnum (void); // calculation result of the calculator
Void clear (void); // clears the program
Void delay_510 (void); // compile the 510us program with the latency
Void changenum (uchar * P, uchar ncount); // numbufe [] shift Program
Void main (void)
{
T0intal ();
While (1)
{
If (numsem)
{
Prenum = numbufe [3] * 1000 + numbufe [2] * 100 + numbufe [1] * 10 + numbufe [0];
Numbufe [4] = 0;
Numsem = 0;
}
If (OP)
{
If (Prenum! = 0)
{
If (numbufe [4] = 0)
{
Numsem = 1;
}
If (equ)
{
If (numbufe [4]! = 0)
{
Latnum = numbufe [3] * 1000 + numbufe [2] * 100 + numbufe [1] * 10 + numbufe [0];
// Disled [] = 0;
Keypos = 0;
}
Resultnum (); // calculator Result Calculation
// Display (); // display the calculation result
}
}
Else clear ();
}
If (CLR)
{
Clear ();
}
Display (); // display the computing result
}
}
VoidT0intal (void)
{
Tmod = 0x01;
Th0 = (65536-20000)/256;
Tl0 = (65536-20000) % 256;
Ea = 1;
Et0 = 1;
Tr0 = 1;
}
Void display (void)
{
// Uchar one, two, three, four;
// One = Result/1000;
P2 = disled [numbufe [3];
P0 = 0xfe;
Delay_510 ();
// Two = (result-one * 1000)/100;
P2 = disled [numbufe [2];
P0 = 0xfd;
Delay_510 ();
// Three = (result-one * 1000-two * 100)/10;
P2 = disled [numbufe [1];
P0 = 0xfb;
Delay_510 ();
// Four = (result-one * 1000-two * 100) % 10;
P2 = disled [numbufe [0];
P0 = 0xf7;
Delay_510 ();
}
Void resultnum (void)
{
If (OP)
{
If (ADD)
{
Result = Prenum + latnum;
}
If (DEC)
{
Result = Prenum-latnum;
}
If (Mul)
{
Result = Prenum * latnum;
}
If (Div)
{
Result = Prenum/latnum;
}
}
// Return result;
// Unsigned char
// Uchar One, Two, Three;
// One = Result/1000;
Numbufe [3] = Result/1000;
Numbufe [2] = (result-numbufe [3] * 1000)/100;
// Numbufe [2] = two;
Numbufe [1] = (result-numbufe [3] * 1000-numbufe [2] * 100)/10;
// Numbufe [1] = three;
Numbufe [0] = (result-numbufe [3] * 1000-numbufe [2] * 100) % 10;
}
Void clear (void)
{
// If (clear)
//{
Numbufe [0] = 0;
Numbufe [1] = 0;
Numbufe [2] = 0;
Numbufe [3] = 0;
Prenum = 0;
Latnum = 0;
Result = 0;
Numsem = 0;
Keypos = 0;
CLR = 0;
Equ = 0;
Add = 0;
Dec = 0;
Mul = 0;
DIV = 0;
OP = 0;
//}
}
VoidChangenum (uchar * P, uchar ncount)
{
If (ncount <4)
{
Uchar ncount;
Ncount = ncount;
While (ncount> 0)
{
* (P + ncount) = * (p + nCount-1 );
Ncount --;
}
}
}
Void delay_510 (void) // The latency is 510 microseconds.
{
# Pragma ASM
MoV r0, # 7dh
MoV R1, #02 h
Tsr1:
Djnz r0, tsr1
MoV r0, # 7dh
Djnz R1, tsr1
# Pragma endasm
}
Void time0_isr_int (void)Interrupt 1 // timer 0 interrupt service subroutine
{
Th0 = (65536-20000)/256;
Tl0 = (65536-20000) % 256;
P1 = 0xf0;
If (P1 & 0xf0 )! = 0xf0)
{
Delay_510 ();
If (P1 & 0xf0 )! = 0xf0)
{
P1 = 0xfe;
If (P1 & 0xf0) = 0xe0) // number key 0
{
Keynum = 0;
Changenum (numbufe, keypos );
Keypos ++;
}
If (P1 & 0xf0) = 0xd0)
{
Keynum = 1;
Changenum (numbufe, keypos );
Keypos ++;
}
If (P1 & 0xf0) = 0xb0)
{
Keynum = 2;
Changenum (numbufe, keypos );
Keypos ++;
}
If (P1 & 0xf0) = 0x70)
{
Keynum = 3;
Changenum (numbufe, keypos );
Keypos ++;
}
P1 = 0xfd;
If (P1 & 0xf0) = 0xe0)
{
Keynum = 4;
Changenum (numbufe, keypos );
Keypos ++;
}
If (P1 & 0xf0) = 0xd0)
{
Keynum = 5;
Changenum (numbufe, keypos );
Keypos ++;
}
If (P1 & 0xf0) = 0xb0)