目前只能實現加減乘除的簡單運算,想加函數運算和開方進去,用獨立按鍵來操作,但是不知道函數運算和開方的代碼該怎么寫,目前只寫了部分代碼,有大神可以發些資料參考一下嗎?
- #include<reg52.h>
- #include<stdlib.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define lcd P0
- #define key P3
- void write_com(uchar com);
- void write_dat(uchar dat);
- void delay(uchar z);
- void Init_lcd();
- void keyscan1();
- void keyscan2();
- void keyscan3();
- void keyscan4();
- void count_add();
- void count_sub();
- void count_mul();
- void count_div();
- void show_int();
- void show_float();
- sbit RS=P1^0;
- sbit RW=P1^1;
- sbit E=P2^5;
- uchar temp,i=0,a=0,b=0,c=0,d=0,e=0,f=0,g=0;
- long num,num1,num2,num3,value,value1;
- uchar table[10];
- uchar table2[10];
- uchar table3[10];
- uchar table4[10];
- uchar code table1[]="0123456789";
- void main()
- {
- Init_lcd();
- while(1)
- {
- keyscan1();
- keyscan2();
- keyscan3();
- keyscan4();
- }
- }
- void keyscan1()
- {
- key=0xfe;
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- switch(temp)
- {
- case 0xe0:
- if(f==0)
- {
- if(e==0)
- table='1';
- if(e==1)
- table2='1';
- }
- if(f==1)
- table3='1';
- if(f==2)
- table4='1';
- write_dat('1');
- break;
- case 0xd0:
- if(f==0)
- {
- if(e==0)
- table='2';
- if(e==1)
- table2='2';
- }
- if(f==1)
- table3='2';
- if(f==2)
- table4='2';
- write_dat('2');
- break;
- case 0xb0:
- if(f==0)
- {
- if(e==0)
- table='3';
- if(e==1)
- table2='3';
- }
- if(f==1)
- table3='3';
- if(f==2)
- table4='3';
- write_dat('3');
- break;
- case 0x70:
- if(f==0)
- {
- if(e==0)
- table='4';
- if(e==1)
- table2='4';
- }
- if(f==1)
- table3='4';
- if(f==2)
- table4='4';
- write_dat('4');
- break;
- }
- i++;
- while(temp!=0xf0)
- {
- temp=key;
- temp=temp&0xf0;
- }
- }
- }
- }
- void keyscan2()
- {
- key=0xfd;
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- switch(temp)
- {
- case 0xe0:
- if(f==0)
- {
- if(e==0)
- table='5';
- if(e==1)
- table2='5';
- }
- if(f==1)
- table3='5';
- if(f==2)
- table4='5';
- write_dat('5');
- break;
- case 0xd0:
- if(f==0)
- {
- if(e==0)
- table='6';
- if(e==1)
- table2='6';
- }
- if(f==1)
- table3='6';
- if(f==2)
- table4='6';
- write_dat('6');
- break;
- case 0xb0:
- if(f==0)
- {
- if(e==0)
- table='7';
- if(e==1)
- table2='7';
- }
- if(f==1)
- table3='7';
- if(f==2)
- table4='7';
- write_dat('7');
- break;
- case 0x70:
- if(f==0)
- {
- if(e==0)
- table='8';
- if(e==1)
- table2='8';
- }
- if(f==1)
- table3='8';
- if(f==2)
- table4='8';
- write_dat('8');
- break;
- }
- i++;
- while(temp!=0xf0)
- {
- temp=key;
- temp=temp&0xf0;
- }
- }
- }
- }
- void keyscan3()
- {
- key=0xfb;
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- switch(temp)
- {
- case 0xe0:
- if(f==0)
- {
- if(e==0)
- table='9';
- if(e==1)
- table2='9';
- }
- if(f==1)
- table3='9';
- if(f==2)
- table4='9';
- i++;
- write_dat('9');
- break;
- case 0xd0:
- if(f==0)
- {
- if(e==0)
- table='0';
- if(e==1)
- table2='0';
- }
- if(f==1)
- table3='0';
- if(f==2)
- table4='0';
- i++;
- write_dat('0');
- break;
- case 0xb0:
- f++;
- g++;
- if(g==2)
- f++;
- i=0;
- write_dat('.');
- break;
- case 0x70:
- write_dat('=');
- num=atol(table);
- num1=atol(table2);
- num2=atol(table3);
- num3=atol(table4);
- if(a==1)
- count_add();
- if(b==1)
- count_sub();
- if(c==1)
- count_mul();
- if(d==1)
- count_div();
- break;
- }
- while(temp!=0xf0)
- {
- temp=key;
- temp=temp&0xf0;
- }
- }
- }
- }
- void keyscan4()
- {
- key=0xf7;
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- temp=key;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- switch(temp)
- {
- case 0xe0:
- write_dat('+');
- a++;
- break;
- case 0xd0:
- write_dat('-');
- b++;
- break;
- case 0xb0:
- write_dat('*');
- c++;
- break;
- case 0x70:
- write_dat('/');
- d++;
- break;
- }
- i=0;
- f=0;
- e++;
- while(temp!=0xf0)
- {
- temp=key;
- temp=temp&0xf0;
- }
- }
- }
- }
- void Init_lcd()
- {
- write_com(0x38);
- write_com(0x0e);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80);
- }
- void write_com(uchar com)
- {
- RS=0;
- RW=0;
- E=1;
- lcd=com;
- delay(10);
- E=0;
- }
- void write_dat(uchar dat)
- {
- RS=1;
- RW=0;
- E=1;
- lcd=dat;
- delay(10);
- E=0;
- }
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=112;y>0;y--);
- }
- void count_add()
- {
- value=num+num1;
- value1=num2+num3;
- if(value1>=10)
- {
- value++;
- value1=value1-10;
- }
- write_com(0x80+0x49);
- show_int();
- show_float();
- while(1);
- }
- void count_sub()
- {
- value=num-num1;
- value1=num2-num3;
- if(value1<0)
- {
- value--;
- value1=num2-num3+10;
- }
- write_com(0x80+0x49);
- show_int();
- show_float();
- while(1);
- }
- void count_mul()
- {
- value=num*num1;
- write_com(0x80+0x49);
- show_int();
- while(1);
- }
- void count_div()
- {
- value=num/num1;
- write_com(0x80+0x49);
- show_int();
- while(1);
- }
|