当前位置:文档之家› 51单片机简易计算器 (可算小数)

51单片机简易计算器 (可算小数)

51单片机简易计算器#include<reg51.h>#include<math.h>#define uchar unsigned char #define uint unsigned int sbit beep=P2^3;sbit dula=P2^6;sbit wela=P2^7;sbit rw=P1^1;sbit rs=P1^0;sbit lcden=P2^5;void yunsuan();void keyinput(char s);void write_data(char ddata);void write_com(char command);void display(long a);void init();void dealerror();void dataoverflow();void welcome();char code table1[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};char code table2[]="789/456*123-.0=+";char j,k,temp,temp1,temp2,key,mchar,m,flag2=0,flag3=0; long x,y,num;int operators,input,iny;char error[5]="error";char overflow[8]="overflow";char welcome_[16]="welcome to use !";void delay(uint x)//延时{uint a,b;for(a=x;a>0;a--)for(b=10;b>0;b--);}void delay_ms(uint x)//延时{uint a,b;for(a=x;a>0;a--)for(b=110;b>0;b--);}void write_com(uchar com)//写命令{P0=com;rs=0;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}void write_data(uchar date)//写数据{P0=date;rs=1;lcden=0;delay(10);lcden=1;delay(10);lcden=0;}long ming(int x){long m;switch(x){case 9:m=1000000000;return m;break; case 8:m=100000000;return m;break; case 7:m=10000000;return m;break; case 6:m=1000000;return m;break; case 5:m=100000;return m;break; case 4:m=10000;return m;break; case 3:m=1000;return m;break;case 2:m=100;return m;break;case 1:m=10;return m;break; case 0:m=1;return m;break; }}void display(long a){long d;int i,flag1=0,temp,c=-1;init();if(a<0){a=a*c;write_data('-');}for(i=9;i>=0;i--){d=ming(i);temp=a/d;a=a%d;if((temp==0)&&(flag1==0)) ;else{write_data(0x30+temp);flag1=1;}if(i==2){write_data('.');flag1=1;}}}void init()//LCD初始化{rw=0;dula=0;wela=0;write_com(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口delay(20);write_com(0x0e); //显示模式设置delay(20);write_com(0x06); //显示模式设置:光标右移,字符不移delay(20);write_com(0x01); //清屏幕指令,将以前的显示内容清除delay(20);}void yunsuan() // 运算{if (iny){switch(operators){case 1:x=x+y;num=x;if(num<10000000000&&num>-10000000000){display(num);}elsedataoverflow();break;case 2:x=x-y;num=x;if(num<10000000000&&num>-10000000000) {display(num);}elsedataoverflow();break;case 3:x=x*y;num=x/100;if(num<10000000000&&num>-10000000000) {display(num);}elsedataoverflow();break;case 4:if (y==0)dealerror();else{if(y<100){x=x*100;y=y*100;}y=y/100;x=x/y;num=x;if(num<10000000000&&num>-10000000000) {display(num);}elsedataoverflow();}break;}y=0;}}void dealerror()//除数为0{int i=0;write_com(0x01);for(i=0;i<5;i++)write_data(error[i]);}void dataoverflow()//数值溢出{int i=0;write_com(0x01);for(i=0;i<8;i++)write_data(overflow[i]);}void welcome()//欢迎界面{int i=0;write_com(0x01);for(i=0;i<16;i++)write_data(welcome_[i]);}void keyinput(char s) //键盘输入{if(s<='9'&&s>='0') //判断按下的键是否为数值{if(flag3==0)num=num*10+100*(s-'0');elsenum=num*10+10*(s-'0');if(flag2==1){num=num/10;flag3=1;}if (operators>0){y=num;iny=1;}elsex=num;if(num<10000000000&&num>-10000000000) //当前数值是否超出限定范围display(num);elsedataoverflow();}elseswitch(s){case '.':/*iny=0;operators=0;*/flag2=1;break;case '=':write_data(table2[14]); delay(10);yunsuan();iny=0;operators=0;num=0;flag2=0;flag3=0;break;case '+':if (operators) yunsuan();operators=1;write_data(table2[15]); num=0;flag2=0;flag3=0;break;case '-':if (operators) yunsuan(); operators=2;write_data(table2[11]); num=0;flag2=0;flag3=0;break;case '*':if (operators) yunsuan(); operators=3;write_data(table2[7]); num=0;flag2=0;flag3=0;break;case '/':if (operators) yunsuan(); operators=4;write_data(table2[3]); num=0;flag2=0;flag3=0;break;}}void main() //主函数{char i;char flag0=1;init();welcome();for(i=0;i<10;i++) delay_ms(100);init();while(1){P3=0xf0;temp1=P3;P3=0x0f;temp2=P3;temp=temp1|temp2; while(P3!=0x0f) {beep=0;}beep=1;for(i=0;i<16;i++) {if(temp==table1[i]) {m=table2[i];flag0=0;}}if(flag0==0) {keyinput(m); flag0=1;}}}。

相关主题