#include <reg51.h> sbit wei = P2^1; int key = 0, num1 = 0, num2 = 0; int i,j,display_flag; //共阴数码管表 unsigned char code table[] = {0x3f, 0x06 , 0x5b , 0x4f , 0x66 , 0x6d, 0x7d , 0x07 , 0x7f , 0x6f }; void delay(int x) { for(j = 0; j < x; j++) { for (i = 0; i < 15; i++); } } void delay1(int x) { for(j = 0; j < x; j++) { for (i = 0; i < 200; i++); } } void KeyScan() { P1 = 0xf0; //判断哪一列被按下 if(P1 != 0xf0) { switch(P1) { case 0xe0: key = 0; break; case 0xd0: key = 1; break; case 0xb0: key = 2; break; case 0x70: key = 3; break; } P1 = 0x0f;//判断哪一行被按下 switch(P1) { case 0x0e: key += 1; break; case 0x0d: key += 5; break; case 0x0b: key += 9; break; case 0x07: key += 13; break; } while (P1 != 0x0f); } } void display(int n) { if (n >= 0 && n <= 9) { P2 = 0xfe; P0 = table[n]; } else if(n >= 10 && n <= 99) { P2 = 0xfe; P0 = table[n / 10]; delay(20); P2 = ~(1 << 1); P0 = table[n % 10]; delay(20); } else if (n >= 100 && n <= 999) { P2 = 0xfe; P0 = table[n / 100]; n = n % 100; delay(20); P2 = ~(1 << 1); P0 = table[n / 10]; delay(20); P2 = ~(1 << 2); P0 = table[n % 10]; delay(20); } } int main() { while (1) { key = 0; KeyScan(); //检测按键 switch(key) { case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: num1 = num1 * 10 + key; break; case 10: num1 = num1 * 10 + 0; break; case 11: //+ { num2 = num1 + num2; display_flag = 1; break; } case 12: //- { num2 = num1 - num2; display_flag = 2; break; } case 13: //* { if(num2 == 0) { num2 = 1; } num2 = num1 * num2; display_flag = 3; break; } case 14: // / { num2 = num2 / num1; if(num2 == 0) { num2 = num1; } display_flag = 4; break; } case 15: display_flag = 5; break; case 16: display_flag = 6; break; } if (1 == display_flag) { display(num2); // delay(50); num1 = 0; } else if(2 == display_flag) { display(num2); // delay(50); num1 = 0; } else if(3 == display_flag) { display(num2); // delay(50); num1 = 0; } else if(4 == display_flag) { display(num2); // delay(50); num1 = 0; } else if(5 == display_flag) { P0 = 0x00; //num1 = 0; } else if(6 == display_flag) { P2 = 0xfe; P0 = table[0]; if(num1 != 0 && num2 != 0) { num1 = 0; num2 = 0; } //delay(20); } } return 0; } |