《51单片机简易计算器-(可算小数)(共17页).doc》由会员分享,可在线阅读,更多相关《51单片机简易计算器-(可算小数)(共17页).doc(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上51单片机简易计算器#include<reg51.h>#include<math.h>#define uchar unsigned char#define uint unsigned intsbit beep=P23;sbit dula=P26;sbit wela=P27;sbit rw=P11;sbit rs=P10;sbit lcden=P25;void yunsuan();void keyinput(char s);void write_data(char ddata);void write_com(char command);void
2、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 ope
3、rators,input,iny;char error5="error" char overflow8="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)/
4、写命令 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=;return m;break; case 8:m=;return m;break; case 7:m=;return m;break; case 6:m=;return m;break;
5、 case 5:m=;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=m
6、ing(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); /显示模
7、式设置:光标右移,字符不移 delay(20); write_com(0x01); /清屏幕指令,将以前的显示内容清除 delay(20); void yunsuan() / 运算if (iny)switch(operators)case 1:x=x+y;num=x;if(num<&&num>-)display(num);elsedataoverflow();break;case 2:x=x-y;num=x;if(num<&&num>-) display(num);elsedataoverflow();break;case 3:x=x*y
8、;num=x/100;if(num<&&num>-) display(num);elsedataoverflow();break;case 4:if (y=0)dealerror();elseif(y<100)x=x*100;y=y*100;y=y/100;x=x/y;num=x; if(num<&&num>-) display(num);elsedataoverflow(); break;y=0;void dealerror()/除数为0int i=0;write_com(0x01);for(i=0;i<5;i+)write
9、_data(errori);void dataoverflow()/数值溢出int i=0;write_com(0x01);for(i=0;i<8;i+)write_data(overflowi);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+10
10、0*(s-'0'); else num=num*10+10*(s-'0'); if(flag2=1) num=num/10; flag3=1; if (operators>0) y=num; iny=1; else x=num; if(num<&&num>-) /当前数值是否超出限定范围 display(num); else dataoverflow(); else switch(s) case '.': /*iny=0; operators=0;*/ flag2=1; break; case '=
11、9;: write_data(table214); delay(10); yunsuan(); iny=0; operators=0; num=0; flag2=0; flag3=0; break; case '+': if (operators) yunsuan(); operators=1; write_data(table215); num=0; flag2=0; flag3=0; break; case '-': if (operators) yunsuan(); operators=2; write_data(table211); num=0; fla
12、g2=0; flag3=0; break; case '*': if (operators) yunsuan(); operators=3; write_data(table27); num=0; flag2=0;flag3=0;break; case '/': if (operators) yunsuan(); operators=4; write_data(table23); 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=table1i) m=table2i; flag0=0; if(flag0=0) keyinput(m); flag0=1; 专心-专注-专业