《msp430计算器设计(共13页).doc》由会员分享,可在线阅读,更多相关《msp430计算器设计(共13页).doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上#include msp430x14x.h#include double r;#include duoxiangshi.h#define RS_1 P3OUT|=BIT0#define RS_0 P3OUT&=BIT0#define RW_1 P3OUT|=BIT1#define RW_0 P3OUT&=BIT1#define E_1 P3OUT|=BIT2#define E_0 P3OUT&=BIT2#define DATA P4OUT#define data_dir P4DIR#define KEYDIR P5DIR#define KEYOUT P5OUT#def
2、ine KEYIN P5IN#define led P2OUT#define LED1 P2DIR=0XFF;#define uchar unsigned char#define uint unsigned intchar table116=1;char table216= ;char flag;int flag1;/*延时*/void delay(void) unsigned int i,j; for(i=0;i50;i+) for(j=0;j10;j+) ; /*消抖延时*/void delay_key(void) int i,j; for(i=0;i500;i+) for(j=0;j10
3、0;j+) ; void delayms(uchar ms) int i,j; for(i=0;ims;i+) for(j=0;j157;j+);int key_column_scan() int a; P5DIR = 0xff; P5OUT = 0x0f; P5DIR = 0X00; a = P5IN; switch(a) case 0x8f:return 4; case 0x4f:return 3; case 0x2f:return 2; case 0x1f:return 1; int key_line_scan() int a,column,key=-1,line; P5DIR = 0x
4、ff; P5OUT = 0xf0; P5DIR = 0X00; a = P5IN; if(a!=0xf0) column = key_column_scan(); switch(a) case 0xf8:line=4;break; case 0xf4:line=3;break; case 0xf2:line=2;break; case 0xf1:line=1;break; key=(line-1)*4+column; delay_key(); /延时消抖 for(;) P5DIR = 0xff; /软件消抖 P5OUT = 0xf0; P5DIR = 0X00; a = P5IN; if(a=
5、0xf0) break; return key;/给LCD1602写命令void write_com(uchar com) RS_0; RW_0; E_0; DATA=com; E_1; delayms(5); E_0; /给LCD1602写数据void write_data(uchar data) RS_1; RW_0; E_0; DATA=data; E_1; delayms(5); E_0; /初始化LCD1602void lcd_init() data_dir=0xff; P3DIR|=BIT0+BIT1+BIT2; write_com(0x38); /设置16*2显示,双行,5*7点
6、阵 write_com(0x01); /清屏 write_com(0x06); /字符进入模式;屏幕不动,字符后移 write_com(0x0f); /显示开,光标开,光标闪烁 write_com(0x80); /在指定位置显示字符串void lcd_string(uchar r,uchar c,uchar *str) /r表示行数,C表示在R行的第几位,*str表示输入的字符串 uchar i=0; uchar DDRAM=0x80,0xc0; /LCD1602两行的起始地址 write_com(DDRAMr|c); /设置显示的起始位置 for(i=0;stri&i16;i+) /输出字符
7、串 write_data(stri); for(;i16;i+) write_data( ); /一行不足时用空格填充int main( void ) / Stop watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; int key,m,i=0; flag=0; flag1=0; lcd_init(); while(1) key=key_line_scan(); if(key!=-1) switch(key) case 1:write_data(7);table1+i=7;break; case 2:write_
8、data(8);table1+i=8;break; case 3:write_data(9);table1+i=9;break; case 4: 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); else if(flag1%4=1) write_data(-);write_com(0x80+i); else if(flag1%4=2) write_data(*);wr
9、ite_com(0x80+i); else if(flag1%4=3) write_data(/);write_com(0x80+i); else if(key!=4) if(flag1%4=0) write_data(+);table1+i=+;flag1=0; else if(flag1%4=1)write_data(-);table1+i=-;flag1=0; else if(flag1%4=2) write_data(*);table1+i=*;flag1=0; else if(flag1%4=3) write_data(/);table1+i=/;flag1=0; switch(ke
10、y) case 1:write_data(7);table1+i=7;break; case 2:write_data(8);table1+i=8;break; case 3:write_data(9);table1+i=9;break; case 5:write_data(4);table1+i=4;break; case 6:write_data(5);table1+i=5;break; case 7:write_data(6);table1+i=6;break; case 8: if(flag=0)write_data(#);table1+i=#;break; else write_da
11、ta();table1+i=;break; case 9:write_data(1);table1+i=1;break; case 10:write_data(2);table1+i=2;break; case 11:write_data(3);table1+i=3;break; case 12: 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; case 13: if(flag=0)write_data(0
12、);table1+i=0;break; else write_data();table1+i=(;break; case 14: if(flag=0)write_data(.);table1+i=.;break; else write_data();table1+i=);break; case 15:write_data(=);table1+i=;break; case 16: if(flag=0)write_com(0x80+i-1);i-; else WDTCTL=0X00; if(table1i=) cal(table1); sprintf(table2,%lf,r); lcd_stri
13、ng(1,6,table2); break; break; case 5:write_data(4);table1+i=4;break; case 6:write_data(5);table1+i=5;break; case 7:write_data(6);table1+i=6;break; case 8: if(flag=0)write_data(#);table1+i=#;break; else write_data();table1+i=;break; case 9:write_data(1);table1+i=1;break; case 10:write_data(2);table1+
14、i=2;break; case 11:write_data(3);table1+i=3;break; case 12: 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; case 13: if(flag=0)write_data(0);table1+i=0;break; else write_data();table1+i=(;break; case 14: if(flag=0)write_data(.);t
15、able1+i=.;break; else write_data();table1+i=);break; case 15:write_data(=);table1+i=;break; case 16: if(flag=0)write_com(0x80+i-1);i-; else WDTCTL=0X00; if(table1i=) cal(table1); sprintf(table2,%g,r); lcd_string(1,6,table2); duoxiangshi.h/*=*函数名:多项式运算*函数功能描述:实现多项式运算*参数:char型数组*返回值:double结果=*/#includ
16、e#include#includevoid cal(char a) int i=1,j,k,m,cnt=0,t1=0,t2=0,t3=0; char nibo20=0,zhan215=0;/存放运算符 double x,n,l,z=0,zhan315=0,0;/暂存结果 typedef struct double d1; int d2; dd; typedef struct dd data20; int top; zhan1;/存储数据 zhan1 *num; num=(zhan1 *)malloc(sizeof(zhan1); num-top=0; while(ai!=) if(ai=0)&
17、(ai=0&aj=9) j+; j-; for(k=i;k=0&aj=9) j+;/转出小数点后的连续数字 j-; for(k=i;kdata+num-top.d1=x;/将x保存在zhan1里的结构体组中,根据zhan1里的top值决定结构体组中的哪一个,保存在结构体的d1里,top+1 num-datanum-top.d2=+cnt;/根据栈中的top值,将datatop中的d2赋值cnt,cnt加1cnt决定为第几个数 nibo+t1=0+num-datanum-top.d2; nibot1+1=0; /如果是数字那么就保存到结构体里 的dd数组里的 第二个成员里 else if(ai=
18、() zhan2+t2=ai;/存储( i+;/读下一个字符 else if(ai=) j=t2;/j位于当前。栈2中未存储运算符的位置 while(zhan2j!=()/查找前半个括号 nibo+t1=zhan2j;/记录()之间所有的运算符 nibot1+1=0; j-; t2=j-1;/t2位于前半个括号的前一个位置 i+; else if(ai=+) while(t20&zhan2t2!=()/将+前的运算符存入nibo nibo+t1=zhan2t2; nibot1+1=0; t2-; zhan2+t2=ai; i+; else if(ai=-) if(ai-1=$) a0=0; i
19、=0; else if(ai-1=() ai-1=0; ai-2=(; i=i-2; t2-; else while(t20&zhan2t2!=() nibo+t1=zhan2t2; nibot1+1=0; t2-; zhan2+t2=ai; i+; else if(ai=*|ai=/) while(zhan2t2=*|zhan2t2=/|zhan2t2=|zhan2t2=#) nibo+t1=zhan2t2; nibot1+1=0; t2-; zhan2+t2=ai; i+; else if(ai=|ai=#) while(zhan2t2=|zhan2t2=#) nibo+t1=zhan2t
20、2; nibot1+1=0; t2-; zhan2+t2=ai; i+; /开始计算 while(t20) /将栈2中所有的运算符存入nibo中 nibo+t1=zhan2t2; nibot1+1=0; t2-; j=1;t3=0; while(j=0&niboj!=&niboj!=#)/ for(i=1;itop;i+) if(int)(niboj-0)=num-datai.d2) m=i; break; zhan3+t3=num-datam.d1; else if(niboj=+) zhan3t3-1=zhan3t3-1+zhan3t3; t3-; else if(niboj=-) zhan3t3-1=zhan3t3-1-zhan3t3; t3-; else if(niboj=*) zhan3t3-1=zhan3t3-1*zhan3t3; t3-; else if(niboj=/) zhan3t3-1=zhan3t3-1/zhan3t3; t3-; else if(niboj=) zhan3t3-1=pow(zhan3t3-1,zhan3t3); t3-; else if(niboj=#)