资源描述:
《基于51单片机的计算器(附Proteus仿真图且程序有详细注释).doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、#include#defineuintunsignedint#defineucharunsignedchar#definelintunsignedlongintucharSMG[10]={0x3f,0x06,0x5b,0x4f,//数码管取模0x66,0x6d,0x7d,0x07,0x7f,0x6f};ucharDATA1[8]={0,0,0,0,0,0,0,0};//第一个数ucharDATA2[8]={0,0,0,0,0,0,0,0};//第二个数ucharRESUIT[8]={0,0,0,0,0,0,0,0};/
2、/结果ucharpx1=0,px2=0,px3=0,flag=0,flag1=0,flag2=0,flag3=0,f1=0,f2=0,f3=0,f4=0;//各全局变量uchartemp,key;lintx1=0,x2=0,y=0;//输入的数及其结果voidInit();//系统初始化voidkeyscan();//键盘检测voiddelay(uint);//延时voidDataOper();//数据运算voidDataHandle();//数据接收voidDisplayHandle();//显示处理voiddisplay(uchar
3、,uchar);//数码管显示函数voidmain(){P2=0x00;while(1){keyscan();DataHandle();DisplayHandle();}}voidInit()//初始化,所有数据归零{uchari;px1=0;px2=0;px3=0;flag=0;flag1=0;flag2=0;flag3=0;f1=0;f2=0;f3=0;f4=0;x1=0;x2=0;y=0;for(i=0;i<8;i++){DATA1[i]=0;DATA2[i]=0;RESUIT[i]=0;}}voiddelay(msx)//msx
4、为延时毫秒数{uinti,j;for(i=0;i<=msx;i++)for(j=0;j<=110;j++);}voiddisplay(ucharn,ucharm)//n是第几只数码管,m是显示的数字是多少{switch(n){case0:P2=0xfe;break;case1:P2=0xfd;break;case2:P2=0xfb;break;case3:P2=0xf7;break;case4:P2=0xef;break;case5:P2=0xdf;break;case6:P2=0xbf;break;case7:P2=0x7f;brea
5、k;}P3=SMG[m];}voidkeyscan()//矩阵键盘扫描子函数{P1=0xfe;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;switch(temp){case0xee:key=0;flag=1;break;case0xde:key=1;flag=1;break;case0xbe:key=2;flag=1;break;case0x7e:key=3;flag=1;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}
6、}P1=0xfd;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;switch(temp){case0xed:key=4;flag=1;break;case0xdd:key=5;flag=1;break;case0xbd:key=6;flag=1;break;case0x7d:key=7;flag=1;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}P1=0xfb;temp=P1;temp=temp&0xf0;if(tem
7、p!=0xf0){delay(10);temp=P1;switch(temp){case0xeb:key=8;flag=1;break;case0xdb:key=9;flag=1;break;case0xbb:key=10;flag1=1;display(7,0);break;case0x7b:key=11;flag1=1;display(7,0);break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}P1=0xf7;temp=P1;temp=temp&0xf0;if(temp!=0xf0)
8、{delay(10);temp=P1;switch(temp){case0xe7:key=12;flag2=1;Init();break;case0xd7:key=13;flag2=1;DataOper()