您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 高中教育 > 51单片机计算器(包括三角函数指数函数等)
#includeREG51.h#includeoled.h#includemath.h#includestring.h#definepi3.141592654#definee2.718281828#defineKeyPortP0//¶¨Òå±äÁ¿unsignedcharcodetables[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb,0xbb,0x7b,0xe7,0xd7,0xb7,0x77};charcodenub[]={'1','2','3','+','4','5','6','-','7','8','9','*','0','.','=','/'};chartemp[105];charac[105];chartxt[20];doubleans;charflg;unsignedchartmp;staticchartable[]={'0','1','2','3','4','5','6','7','8','9'};unsignedlongatof_A(char*sptr);unsignedlongatof_C(char*sptr);unsignedlongarr(inta,intb);unsignedlongcrr(inta,intb);doubleabst(doublenb);doublemy_(charstr[]);voidnum2char(charstr[],floatnumber,u8g,u8l);intisdigit(charc);intstrlen(charstr[]);intpriority(charc);floatatof(char*sptr);doublecal(charstr[],intlen);intkeyscan();voidDelayUs2x(unsignedchart);voidDelayMs(unsignedchart);voidmain(void){//floattt;//intpre;doubleans_pe[10];intlens,i,j,nn,count=0,f,cls,as,jisuan,k=0,TO;OLED_Init();//³õʼ»¯OLEDdelay_ms(100);OLED_Clear();for(i=0;i10;i++)ans_pe[i]=0;while(1){/*nn=keyscan();if(nn=0&&nn=15){ac[count++]=nub[nn-1];ac[count]='\0';}*/nn=keyscan();//pre=count;//-----------------------ÊäÈëËãʽ----------------------------------------if(nn0&&nn=36){if(cls==1){OLED_Clear();cls=0;}TO=1;switch(nn){case1:ac[count++]='1';break;//1case2:ac[count++]='2';break;//2case3:ac[count++]='3';break;//3case4:ac[count++]='+';break;//+case5:ac[count++]='s';ac[count++]='i';ac[count++]='n';break;//sincase6:ac[--count]='\0';OLED_Clear();break;//DELcase7:ac[count++]='4';break;//4case8:ac[count++]='5';break;//5case9:ac[count++]='6';break;//6case10:ac[count++]='-';break;//-case11:ac[count++]='c';ac[count++]='o';ac[count++]='s';break;//coscase12:ac[0]='\0';OLED_Clear();break;//rstcase13:ac[count++]='7';break;//7case14:ac[count++]='8';break;//8case15:ac[count++]='9';break;//9case16:ac[count++]='*';break;//*case17:ac[count++]='t';ac[count++]='a';ac[count++]='n';break;//tancase18:ac[count++]='l';ac[count++]='n';break;//lncase19:ac[count++]='0';break;//0case20:ac[count++]='.';break;//.case21:ac[count++]='p';ac[count++]='i';break;//picase22:ac[count++]='/';break;//¡Âcase23:ac[count++]='a';ac[count++]='s';ac[count++]='i';ac[count++]='n';break;//asincase24:ac[count++]='l';ac[count++]='o';ac[count++]='g';break;//logcase25:ac[count++]='e';break;//ecase26:ac[count++]='(';break;//)case27:ac[count++]='s';ac[count++]='q';ac[count++]='r';ac[count++]='t';break;//¡Ìcase28:ac[count++]='^';break;//^case29:ac[count++]='a';ac[count++]='c';ac[count++]='o';ac[count++]='s';break;//acoscase30:ac[count++]='%';break;//%case31:ac[count++]='1';ac[count++]='0';ac[count++]='^';break;//10^case32:ac[count++]=')';break;//)case33:ac[count++]='A';break;//Acase34:ac[count++]='C';break;//Ccase35:ac[count++]='a';ac[count++]='t';ac[count++]='a';ac[count++]='n';break;//atancase36:ac[count++]='=';break;//=default:break;}}ac[count]='\0';if(ac[count-1]=='A'&&TO){k++;TO=0;if(k10)k=0;num2char(txt,ans_pe[k],10,5);for(i=0;istrlen(txt);i++){OLED_ShowChar(i*6,4,txt[i]);//OLED_ShowString(0,4,txt););}}if(ac[count-1]=='C'&&T0){TO=0;k--;if(k0)k=9;num2char(txt,ans_pe[k],10,5);for(i=0;istrlen(txt);i++){OLED_ShowChar(i*6,4,txt[i]);//OLED_ShowString(0,4,txt););}}lens=strlen(ac);if(ac[count-1]=='=')jisuan=1;if(jisuan){//ac[count]='\0';count=0;jisuan=0;//ac[0]='l';ac[1]='o';ac[2]='g';ac[3]='(';ac[4]='e';ac[5]='+';ac[6]='2';ac[7]='*';ac[8]='3';ac[9]='-';ac[10]='4';ac[11]='+';ac[12]='e';ac[13]=')';//ac[14]='=';ac[15]='\0';ans=cal(ac,lens);as=1;cls=1;for(i=1;i10;i++)ans_pe[i]=ans_pe[i-1];ans_pe[0]=ans;}//-----------------ÏÔʾ´ð°¸-----------------------//ans=456-(double)41*8/6+3*59*0.54;num2char(txt,ans,10,5);f=1;if(as=1){as=0;for(i=0;istrlen(txt);i++)OLED_ShowChar(i*6,6,'');}for(i=0;istrlen(txt);i++){if(flg=='-'&&txt[i+1]!='0'){OLED_ShowChar(i*6,6,'-');flg='+';continue;}if(txt[i]=='.'){if(txt[i-1]=='0')OLED_ShowChar((i-1)*6,6,'0');}if(f==1&&txt[i]=='0')continue;else{OLED_ShowChar(i*6,6,txt[i]);//OLED_ShowString(0,4,txt););f=0;}}//-----------------ÏÔʾËãʽ-----------------------for(i=0;ilens;i++){j=i/20;OLED_ShowChar(i*6%120,j,ac[i]);}//num2char(txt,lens,8,5);delay_ms(70);//}}intisdigit(charc){if(c='0'&&c='9')return1;elsereturn0;}intstrlen(charstr[]){intlen=0;while(*(str+len)!='\0')len++;returnlen;}intpriority(charc){if(c=='=')return0;if(c=='+')return1;if(c=='-')return1;if(c=='*')return2;if(c=='/')return2;if(c=='^')return3;return0;}doubleatof(char*sptr){doubletem=10.0;//charispnum=1;intflg_p=1;doubleans=0;while(*sptr!='\0')//Ñ°ÕÒСÊýµã֮ǰµÄÊý{if(*sptr=='.'){sptr++;flg_p=0;//break;continue;}if(*sptr'0'||*sptr'9')break;if(flg_p)ans=ans*10+(*sptr-'0');else{ans=ans+(double)(*sptr-'0')/(double)tem;tem=tem*10;}sptr++;}/*while(*sptr!='\0')//Ñ°ÕÒСÊýµãÖ®ºóµÄÊý{if(*sptr'0'||*sptr'9')break;ans=ans+(float)(*sptr-'0')/tem;tem=tem*10;sptr++;}num2char(txt,ans,12,6);OLED_ShowString(0,3,txt);*/returnans;}doublecal(charstr[],intlen){inti;intnumk=0,opj=0;floatnum[50];intleft,right;floata=0,b=0,n=0;charop[20];intk,te;;//intlen=4;for(i=0;ilen;i++){k=0;te=0;if(str[i]=='e')n
本文标题:51单片机计算器(包括三角函数指数函数等)
链接地址:https://www.777doc.com/doc-4228976 .html