《单片机简易计算器.doc》由会员分享,可在线阅读,更多相关《单片机简易计算器.doc(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、51单片机简易计算器#include#include#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 display(long a);void init();void dealerror
2、(); 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 error5=error; char overflow8=ov
3、erflow;char welcome_16=welcome to use !;void delay(uint x)/延时 uint a,b; for(a=x;a0;a-) for(b=10;b0;b-); void delay_ms(uint x)/延时 uint a,b; for(a=x;a0;a-) for(b=110;b0;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=d
4、ate; 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
5、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;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;vo
6、id init()/LCD初始化 rw=0; dula=0; wela=0; write_com(0x38); /显示模式设置:162显示,57点阵,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;display(num);els
7、edataoverflow();break;case 2:x=x-y;num=x;display(num);elsedataoverflow();break;case 3:x=x*y;num=x/100;display(num);elsedataoverflow();break;case 4:if (y=0)dealerror();elseif(y100)x=x*100;y=y*100;y=y/100;x=x/y;num=x; display(num);elsedataoverflow();break;y=0;void dealerror()/除数为0int i=0;write_com(0x0
8、1);for(i=0;i5;i+)write_data(errori);void dataoverflow()/数值溢出int i=0;write_com(0x01);for(i=0;i8;i+)write_data(overflowi);void welcome()/欢送界面int i=0;write_com(0x01);for(i=0;i16;i+)write_data(welcome_i);void keyinput(char s) /键盘输入if(s=0) /判断按下的键是否为数值 if(flag3=0) num=num*10+100*(s-0); else num=num*10+10
9、*(s-0); if(flag2=1) num=num/10; flag3=1; if (operators0) y=num; iny=1; else x=num; display(num); else dataoverflow(); else switch(s) case .: /*iny=0; operators=0;*/ flag2=1; break; case =: write_data(table214); delay(10); yunsuan(); iny=0; operators=0; num=0; flag2=0; flag3=0; break; case +: if (ope
10、rators) yunsuan(); operators=1; write_data(table215); num=0; flag2=0; flag3=0; break; case -: if (operators) yunsuan(); operators=2; write_data(table211); num=0; flag2=0; flag3=0; break; case *: if (operators) yunsuan(); operators=3; write_data(table27); num=0; flag2=0;flag3=0;break; case /: if (ope
11、rators) 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;i10;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;i16;i+) if(temp=table1i) m=table2i; flag0=0; if(flag0=0) keyinput(m); flag0=1;第 12 页