msp430计算器设计

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includemsp430x14x.h#includestdio.hdoubler;#includeduoxiangshi.h#defineRS_1P3OUT|=BIT0#defineRS_0P3OUT&=~BIT0#defineRW_1P3OUT|=BIT1#defineRW_0P3OUT&=~BIT1#defineE_1P3OUT|=BIT2#defineE_0P3OUT&=~BIT2#defineDATAP4OUT#definedata_dirP4DIR#defineKEYDIRP5DIR#defineKEYOUTP5OUT#defineKEYINP5IN#defineledP2OUT#defineLED1P2DIR=0XFF;#defineucharunsignedchar#defineuintunsignedintchartable1[16]=1;chartable2[16]={''};charflag;intflag1;/*延时*/voiddelay(void){unsignedinti,j;for(i=0;i50;i++){for(j=0;j10;j++){;}}}/*消抖延时*/voiddelay_key(void){inti,j;for(i=0;i500;i++){for(j=0;j100;j++){;}}}voiddelayms(ucharms){inti,j;for(i=0;ims;i++)for(j=0;j157;j++);}intkey_column_scan(){inta;P5DIR=0xff;P5OUT=0x0f;P5DIR=0X00;a=P5IN;switch(a){case0x8f:return4;case0x4f:return3;case0x2f:return2;case0x1f:return1;}}intkey_line_scan(){inta,column,key=-1,line;P5DIR=0xff;P5OUT=0xf0;P5DIR=0X00;a=P5IN;if(a!=0xf0){column=key_column_scan();switch(a){case0xf8:line=4;break;case0xf4:line=3;break;case0xf2:line=2;break;case0xf1:line=1;break;}key=(line-1)*4+column;delay_key();//延时消抖for(;;){P5DIR=0xff;//软件消抖P5OUT=0xf0;P5DIR=0X00;a=P5IN;if(a==0xf0){break;}}}returnkey;}//给LCD1602写命令voidwrite_com(ucharcom){RS_0;RW_0;E_0;DATA=com;E_1;delayms(5);E_0;}//给LCD1602写数据voidwrite_data(uchardata){RS_1;RW_0;E_0;DATA=data;E_1;delayms(5);E_0;}//初始化LCD1602voidlcd_init(){data_dir=0xff;P3DIR|=BIT0+BIT1+BIT2;write_com(0x38);//设置16*2显示,双行,5*7点阵write_com(0x01);//清屏write_com(0x06);//字符进入模式;屏幕不动,字符后移write_com(0x0f);//显示开,光标开,光标闪烁write_com(0x80);}//在指定位置显示字符串voidlcd_string(ucharr,ucharc,uchar*str)//r表示行数,C表示在R行的第几位,*str表示输入的字符串{uchari=0;ucharDDRAM[]={0x80,0xc0};//LCD1602两行的起始地址write_com(DDRAM[r]|c);//设置显示的起始位置for(i=0;str[i]&&i16;i++)//输出字符串write_data(str[i]);for(;i16;i++)write_data('');//一行不足时用空格填充}intmain(void){//StopwatchdogtimertopreventtimeoutresetWDTCTL=WDTPW+WDTHOLD;intkey,m,i=0;flag=0;flag1=0;lcd_init();while(1){key=key_line_scan();if(key!=-1){switch(key){case1:write_data('7');table1[++i]='7';break;case2:write_data('8');table1[++i]='8';break;case3:write_data('9');table1[++i]='9';break;case4:write_data('+');write_com(0x80+i);while(1){key=key_line_scan();if(key!=-1){if(key==4){flag1++;if(flag1%4==0){write_data('+');write_com(0x80+i);}elseif(flag1%4==1){write_data('-');write_com(0x80+i);}elseif(flag1%4==2){write_data('*');write_com(0x80+i);}elseif(flag1%4==3){write_data('/');write_com(0x80+i);}}elseif(key!=4){if(flag1%4==0){write_data('+');table1[++i]='+';flag1=0;}elseif(flag1%4==1){write_data('-');table1[++i]='-';flag1=0;}elseif(flag1%4==2){write_data('*');table1[++i]='*';flag1=0;}elseif(flag1%4==3){write_data('/');table1[++i]='/';flag1=0;}switch(key){case1:write_data('7');table1[++i]='7';break;case2:write_data('8');table1[++i]='8';break;case3:write_data('9');table1[++i]='9';break;case5:write_data('4');table1[++i]='4';break;case6:write_data('5');table1[++i]='5';break;case7:write_data('6');table1[++i]='6';break;case8:if(flag==0){write_data('#');table1[++i]='#';break;}else{write_data('^');table1[++i]='^';break;}case9:write_data('1');table1[++i]='1';break;case10:write_data('2');table1[++i]='2';break;case11:write_data('3');table1[++i]='3';break;case12:if(flag==0){lcd_string(1,0,shift);flag=1;write_com(0x80+i);break;}else{lcd_string(1,0,);flag=0;write_com(0x80+i);break;}case13:if(flag==0){write_data('0');table1[++i]='0';break;}else{write_data('(');table1[++i]='(';break;}case14:if(flag==0){write_data('.');table1[++i]='.';break;}else{write_data(')');table1[++i]=')';break;}case15:write_data('=');table1[++i]='=';break;case16:if(flag==0){write_com(0x80+i-1);i--;}else{WDTCTL=0X00;}}if(table1[i]=='='){cal(table1);sprintf(table2,%lf,r);lcd_string(1,6,table2);}break;}}}break;case5:write_data('4');table1[++i]='4';break;case6:write_data('5');table1[++i]='5';break;case7:write_data('6');table1[++i]='6';break;case8:if(flag==0){write_data('#');table1[++i]='#';break;}else{write_data('^');table1[++i]='^';break;}case9:write_data('1');table1[++i]='1';break;case10:write_data('2');table1[++i]='2';break;case11:write_data('3');table1[++i]='3';break;case12:if(flag==0){lcd_string(1,0,shift);flag=1;write_com(0x80+i);break;}else{lcd_string(1,0,);write_com(0x80+i);flag=0;break;}case13:if(flag==0){write_data('0');table1[++i]='0';break;}else{write_data('(');table1[++i]='(';break;}case14:if(flag==0){write_data('.');table1[++i]='.';break;}else{write_data(')');table1[++i]=')';break;}case15:write_data('=');table1[++i]='=';break;case16:if(flag==0){write_com(0x80+i-1);i--;}else{WDTCTL=0X00;}}if(table1[i]=='='){cal(table1);sprintf(table2,%g,r);lcd_string(1,6,table2);}}}}duoxiangshi.h/*========================================**函数名:多项式运算**函数功能描述:实现多项式运算**参数:char型数组***返回值:double结果========================================*/#includestdio.h#includemath.h#includemalloc.hvoidcal(chara[]){inti=1,j,k,m,cnt=0,t1=0,t2=0,t3=0;charnibo[20]={'0'},zhan2[15]={'0'};//存放运算符doublex,n,l,z=0,zhan3[15]={0,0};//暂存结果typedefstruct{doubled1;intd2;}dd;typedefstruct{dddata[20];inttop;}zhan1;//存储数据zhan1*num;num=(zhan1*)malloc(sizeof(zhan1))

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功