完整word版msp430计算器设计.docx
- 文档编号:5747075
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:17
- 大小:17.86KB
完整word版msp430计算器设计.docx
《完整word版msp430计算器设计.docx》由会员分享,可在线阅读,更多相关《完整word版msp430计算器设计.docx(17页珍藏版)》请在冰豆网上搜索。
完整word版msp430计算器设计
#include"msp430x14x.h"
#include
doubler;
#include"duoxiangshi.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
#defineuintunsignedint
chartable1[16]="1";chartable2[16]={''};
charflag;
intflag1;
/*延时*/
voiddelay(void){
unsignedinti,j;
for(i=0;i<50;i++){
for(j=0;j<10;j++){
;
}
}
}
/*消抖延时*/
voiddelay_key(void){
inti,j;
for(i=0;i<500;i++){
for(j=0;j<100;j++){
;
}
}
}
voiddelayms(ucharms)
{
inti,j;
for(i=0;i for(j=0;j<157;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; } //初始化LCD1602 voidlcd_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]&&i<16;i++)//输出字符串 write_data(str[i]); for(;i<16;i++) write_data('');//一行不足时用空格填充 } intmain(void) { //Stopwatchdogtimertopreventtimeoutreset WDTCTL=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结果 ========================================*/ #include #include #include voidcal(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)); num->top=0; while(a[i]! ='=') { if((a[i]>='0')&&(a[i]<='9'))//将连续的数字符转化为数字 { z=0; j=i+1; while(a[j]>='0'&&a[j]<='9')j++; j--; for(k=i;k<=j;k++) { z=z*10+a[k]-'0'; } j=j+1; x=z; if(a[j]=='.')//整合小数部分 { l=1; i=j+1; j=i+1; while(a[j]>='0'&&a[j]<='9')j++;//转出小数点后的连续数字 j--; for(k=i;k<=j;k++) { n=pow(0.1,l); l=l+1; x=x+n*(a[k]-'0'); } i=j+1; } elsei=j; num->data[++num->top].d1=x;//将x保存在zhan1里的结构体组中,根据zhan1里的top值决定结构体组中的哪一个,保存在结构体的d1里,top+1 num->data[num->top].d2=++cnt;//根据栈中的top值,将data[top]中的d2赋值cnt,cnt加1cnt决定为第几个数 nibo[++t1]='0'+num->data[num->top].d2; nibo[t1+1]='\0'; }//如果是数字那么就保存到结构体里的dd数组里的第二个成员里 elseif(a[i]=='(') { zhan2[++t2]=a[i];//存储( i++;//读下一个字符 } elseif(a[i]==')') { j=t2;//j位于当前。 栈2中未存储运算符的位置 while(zhan2[j]! ='(')//查找前半个括号 { nibo[++t1]=zhan2[j];//记录()之间所有的运算符 nibo[t1+1]='\0'; j--; } t2=j-1;//t2位于前半个括号的前一个位置 i++; } elseif(a[i]=='+') { while(t2>0&&zhan2[t2]! ='(')//将+前的运算符存入nibo { nibo[++t1]=zhan2[t2]; nibo[t1+1]='\0'; t2--; } zhan2[++t2]=a[i]; i++; } elseif(a[i]=='-') { if(a[i-1]=='$') { a[0]='0'; i=0; } elseif(a[i-1]=='(') { a[i-1]='0'; a[i-2]='('; i=i-2; t2--; } else { while(t2>0&&zhan2[t2]! ='(') { nibo[++t1]=zhan2[t2]; nibo[t1+1]='\0'; t2--; } zhan2[++t2]=a[i]; i++; } } elseif(a[i]=='*'||a[i]=='/') { while(zhan2[t2]=='*'||zhan2[t2]=='/'||zhan2[t2]=='^'||zhan2[t2]=='#') { nibo[++t1]=zhan2[t2]; nibo[t1+1]='\0'; t2--; } zhan2[++t2]=a[i]; i++; } elseif(a[i]=='^'||a[i]=='#') { while(zhan2[t2]=='^'||zhan2[t2]=='#') { nibo[++t1]=zhan2[t2]; nibo[t1+1]='\0'; t2--; } zhan2[++t2]=a[i]; i++; } } //开始计算 while(t2>0)//将栈2中所有的运算符存入nibo中 { nibo[++t1]=zhan2[t2]; nibo[t1+1]='\0'; t2--; } j=1;t3=0; while(j<=t1) { if(nibo[j]>='0'&&nibo[j]! ='^'&&nibo[j]! ='#')// { for(i=1;i<=num->top;i++) { if((int)(nibo[j]-'0')==num->data[i].d2) { m=i; break; } } zhan3[++t3]=num->data[m].d1; } elseif(nibo[j]=='+') { zhan3[t3-1]=zhan3[t3-1]+zhan3[t3]; t3--; } elseif(nibo[j]=='-') { zhan3[t3-1]=zhan3[t3-1]-zhan3[t3]; t3--; } elseif(nibo[j]=='*') { zhan3[t3-1]=zhan3[t3-1]*zhan3[t3]; t3--; } elseif(nibo[j]=='/') { zhan3[t3-1]=zhan3[t3-1]/zhan3[t3]; t3--; } elseif(nibo[j]=='^') { zhan3[t3-1]=pow(zhan3[t3-1],zhan3[t3]); t3--; } elseif(nibo[j]=='#') { zhan3[t3]=sqrt(zhan3[t3]); } j++; } r=zhan3[t3]; } malloc.h /*alloc.h memorymanagementfunctionsandvariables. Copyright(c)BorlandInternational1987,1988 AllRightsReserved. */ #if__STDC__ #define_Cdecl #else #define_Cdeclcdecl #endif #ifndef_STDDEF #define_STDDEF #ifndef_PTRDIFF_T #define_PTRDIFF_T #ifdefined(__LARGE__)||defined(__HUGE__)||defined(__COMPACT__) typedeflongptrdiff_t; #else typedefintptrdiff_t; #endif #endif #ifndef_SIZE_T #define_SIZE_T typedefunsignedsize_t; #endif #endif #ifndefNULL #ifdefined(__TINY__)||defined(__SMALL__)||defined(__MEDIUM__) #defineNULL0 #else #defineNULL0L #endif #endif int_Cdeclbrk(void*addr); void*_Cdeclcalloc(size_tnitems,size_tsize); #ifdefined(__COMPACT__)||defined(__LARGE__)||defined(__HUGE__) unsignedlong_Cdeclcoreleft(void); #else unsigned_Cdeclcoreleft(void); #endif void_Cdeclfree(void*block); void*_Cdeclmalloc(size_tsize); void*_Cdeclrealloc(void*block,size_tsize); void*_Cdeclsbrk(intincr); #if! __STDC__ voidfar*_Cdeclfarcalloc(unsignedlongnunits,unsignedlongunitsz); unsignedlong_Cdeclfarcoreleft(void); void_Cdeclfarfree(voidfar*block); voidfar*_Cdeclfarmalloc(unsignedlongnbytes); voidfar*_Cdeclfarrealloc(voidfar*oldblock,unsignedlongnbytes); #endif
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 完整 word msp430 计算器 设计