点第一个数后 点符号键 前一个数会清0
单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- uint num1,num2,result;
- uchar symbol1,symbol2,flag;
- uchar codetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- void delayms(uint x) //延时函数
- {
- uint i;
- while(x--) for(i=0;i<120;i++);
- }
- void CLR()//清零
- {
- result=0;
- num1=0;
- num2=0;
- symbol1=0;
- symbol2=0;
- flag=0;
- }
- void display(uint result) //显示函数
- {
- ucharqian,bai,shi,ge;
- qian= result/1000;
- bai= result%1000/100;
- shi= result%100/10;
- ge= result%10;
- P0= 0;
- P1= 0xfe; //千位
- P0= table[qian];
- delayms(3);
- P1= 0xfd; //百位
- P0= table[bai];
- delayms(3);
-
- P1= 0xfb; //十位
- P0= table[shi];
- delayms(3);
- P1= 0xf7; //个位
- P0= table[ge];
- delayms(3);
- }
- void keybord() //反转法
- { uchar firstnum,nextnum,keynum;
- flag = result = num1 = num2 = 0;
- while(1)
- { P2=0x0f;
- firstnum=P2;
- P2=0xf0;
- nextnum=P2;
- keynum=nextnum|firstnum;
- switch(keynum)
- {
- case 0xee: delayms(200);
- if(flag< 2) //1
- {
- num1= num1*10 + 1;
- result= num1;
- flag++;
-
- };break;
- case 0xde:delayms(200); //2
- if(flag < 2)
- {
- num1= num1*10 + 2;
- result= num1;
- flag++;
- };break;
- case 0xbe: delayms(200); //3
- if(flag < 2)
- {
- num1= num1*10 + 3;
- result= num1;
- flag++;
- };break;
- case 0x7e: delayms(200); //4
- if(flag < 2)
- {
- num1= num1*10 + 4;
- result= num1;
- flag++;
- };break;
- case 0xed: delayms(200); //5
- if(flag < 2)
- {
- num1= num1*10 + 5;
- result= num1;
- flag++;
- };break;
- case 0xdd: delayms(200); //6
- if(flag < 2)
- {
- num1= num1*10 + 6;
- result= num1;
- flag++;
- };break;
- case 0xbd: delayms(200); //7
- if(flag < 2)
- {
- num1= num1*10 + 7;
- result= num1;
- flag++;
- };break;
- case 0x7d: delayms(200); //8
- if(flag < 2)
- {
- num1= num1*10 + 8;
- result= num1;
- flag++;
- };break;
- case 0xeb: delayms(200); //9
- if(flag < 2)
- {
- num1= num1*10 + 9;
- result= num1;
- flag++;
- };break;
- case 0xdb: delayms(200); //0
- if(flag < 2)
- {
- num1= num1*10 + 0;
- result= num1;
- flag++;
- };break;
- case 0xbb: symbol1 = '+'; //+
- symbol2= symbol1;
- result= num2 = num1;
- num1= 0;
- flag= 0;
- break;
- case 0x7b: symbol1 = '-'; //-
- symbol2= symbol1;
- result= num2 = num1;
- num1= 0;
- flag= 0;
- break;
- case 0xe7: symbol1 = '*'; //*
- symbol2= symbol1;
- result= num2 = num1;
- num1= 0;
- flag= 0;
- break;
- case 0xd7: symbol1 = '/'; //÷
- symbol2= symbol1;
- result= num2 = num1;
- num1= 0;
- flag= 0;
- break;
- case 0xb7: CLR();break;
- case 0x77: delayms(200); //=
- symbol1= '=';
- break;
-
- default:break;
- }
- if(symbol1=='=')
- {switch(symbol2)
- {
- case '+':
- result = num2 + num1;
- break;
-
- case '-':
- result = num2 - num1;
- break;
-
- case '*':
- result = num2 * num1;
- break;
-
- case'/':
- result = num2 / num1;
- break; }
- }display(result);
- }
-
- }
- void main()
- {
- keybord();
-
- }
复制代码
|