多功能计算器源代码.docx

多功能计算器系统设计

收藏

压缩包内文档预览:(预览前20页/共92页)
预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图 预览图
编号:36008701    类型:共享资源    大小:2.68MB    格式:ZIP    上传时间:2019-12-31 上传人:遗**** IP属地:湖北
30
积分
关 键 词:
多功能 计算器 系统 设计
资源描述:
多功能计算器系统设计,多功能,计算器,系统,设计
内容简介:
/注:从keil中赋值过来的代码会有格式不对齐的状况/*main.c*/#include stm32f10x.h#include delay.h#include lcd.h#include myfloat.h#include token.h#include keyboard.h#include at24c02.h#include stdio.h#include calculator.hint main(void)RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE); /关闭SWJDelay_Init();AT_Init();LCD_Init();Token_Init();mfconst_init();while(1)Get_Token();/*Error.h*/#ifndef ERROR_H_#define ERROR_H_typedef enum _ErrI_Exp,E_Overflow,Div_Zero,tan_Err,OK,factor_Err,ln_Err,lg_Err,log_Err,Mod_Zero,Exp_Err,ASin_Err,ACos_Err,EQ_Err,Error;#endif/*at24c02.h*/#ifndef _AT24C02_H#define _AT24C02_H#include stm32f10x.h#include myfloat.h#defineBITBAND(addr,bitnum)(addr&0xF0000000)+0x2000000+(addr&0xFFFFF)5)+(bitnum2) #define MEM_ADDR(addr) *(volatile unsigned long *)(addr) #define BIT_ADDR(addr, bitnum)MEM_ADDR(BITBAND(addr, bitnum) #define GPIOB_ODR_Addr (GPIOB_BASE+12)#define GPIOB_IDR_Addr (GPIOB_BASE+8)#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) #define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n)#define SDAoutPBout(12)#define SCLoutPBout(11)#define SDAinPBin(12)#define AT_SDAGPIO_Pin_12#define AT_SCLGPIO_Pin_11voidAT_Init(void);void AT_ACK(void);void SDA_I(void);void SDA_O(void);void AT_Send_Byte(u8 _data);u8 AT_Read_Byte(void);void Write_Byte(u8 _data,u8 address);u8 Read_Byte(u8 address);void Write_Bits(void* data,int len,u8 address);void Read_Bits(void* data,int len,u8 address);#endif/*at24c02.c*/#include at24c02.h#include stm32f10x.h#include stm32f10x_gpio.h#include delay.h#include myfloat.h#include lcd.h#include string.h/reference : datasheetvoid AT_Init(void)GPIO_InitTypeDef GPIOStruct;GPIOStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIOStruct.GPIO_Pin=AT_SDA|AT_SCL;GPIOStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIOStruct);SDAout=1;Delay_us(2);SCLout=1;Delay_us(2);void SDA_I(void)GPIO_InitTypeDef GPIOStruct;GPIOStruct.GPIO_Mode=GPIO_Mode_IPU;GPIOStruct.GPIO_Pin=AT_SDA;GPIOStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIOStruct);GPIO_SetBits(GPIOB,AT_SDA);void SDA_O(void)GPIO_InitTypeDef GPIOStruct;GPIOStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIOStruct.GPIO_Pin=AT_SDA;GPIOStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIOStruct);void AT_ACK(void)u8 i;SDA_I();SCLout=1;Delay_us(2);while(SDAin&i250)i+;SCLout=0;Delay_us(2);void AT_Start(void)SDA_O();SDAout=1;Delay_us(5);SCLout=1;Delay_us(5);SDAout=0;Delay_us(5);void AT_Stop(void)SDAout=0;Delay_us(5);SCLout=1;Delay_us(5);SDAout=1;Delay_us(5);void AT_Send_Byte(u8 _data)u8i;SDA_O();SCLout=0;for(i=0;i7;_data=1;Delay_us(5);SCLout=1;Delay_us(5);SCLout=0;Delay_us(5);SDAout=1;Delay_us(5);u8 AT_Read_Byte(void)u8 ret=0,i;SDA_I();for(i=0;i8;i+)ret=1;SCLout=0;Delay_us(5);SCLout=1;Delay_us(5);if(SDAin)ret+;SCLout=0;return ret;void Write_Byte(u8 _data,u8 address)AT_Start();AT_Send_Byte(0xa0);AT_ACK();AT_Send_Byte(address);AT_ACK();AT_Send_Byte(_data);AT_ACK();AT_Stop();Delay_ms(6);u8 Read_Byte(u8 address)u8 _data;AT_Start();AT_Send_Byte(0xa0);AT_ACK();AT_Send_Byte(address);AT_ACK();AT_Start();AT_Send_Byte(0xa1);AT_ACK();_data=AT_Read_Byte();AT_Stop();return _data;void Write_Bits(void* data,int len,u8 address)u8 i;u8* start=(u8*)data;for(i=0;ilen;i+)Write_Byte(starti,address+i);void Read_Bits(void* _data,int len,u8 address)u8 i;u8* start=(u8*)_data;for(i=0;iBSRR=0xbe00; /A- 1GPIOB-BSRR=(u32)0x000003f0 0void COL_test(void)GPIO_InitTypeDef GPIOStruct;GPIOStruct.GPIO_Mode=GPIO_Mode_Out_PP;GPIOStruct.GPIO_Pin=KROW;GPIOStruct.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIOStruct);GPIOStruct.GPIO_Mode=GPIO_Mode_IPU;GPIOStruct.GPIO_Pin=KCOL;GPIO_Init(GPIOB,&GPIOStruct);GPIOA-BSRR=(u32)0x0000be00 0GPIOB-BSRR=0x03f0;/B- 1u8 Keyscan(void)u16 temp;u8 loc=0;/*First digit - rowSecond digit - column*/ROW_test();while(1)if(GPIO_ReadInputData(GPIOA)&0xbe00)!=0xbe00)Delay_ms(5);if(temp=GPIO_ReadInputData(GPIOA)&0xbe00)!=0xbe00)switch(temp)case 0x3e00:loc+=10;break;case 0x9e00:loc+=20;break;case 0xae00:loc+=30;break;case 0xb600:loc+=40;break;case 0xba00:loc+=50;break;case 0xbc00:loc+=60;break;default :loc=0 ;break;COL_test();Delay_ms(5);temp=GPIO_ReadInputData(GPIOB)&0x03f0;switch(temp)case 0x03e0:loc+=1;break;case 0x03d0:loc+=2;break;case 0x03b0:loc+=3;break;case 0x0370:loc+=4;break;case 0x02f0:loc+=5;break;case 0x01f0:loc+=6;break;default :loc=0;break;while(GPIO_ReadInputData(GPIOB)&0x03f0)!=0x03f0);Delay_ms(5);break;return loc;/*Indentify the loc of Keyscan*/Token K_identify(void)Token token;u8 loc=Keyscan();if(CMode=STD)if(CShift)switch(loc)case 11:token.type=Function;token.value=f_acos;break;case 12:token.type=Function;token.value=f_asin;break;case 13:token.type=Function;token.value=f_atan;break;case 14:token.type=Operator;token.value=comma;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=AC;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=const_e;break;case 32:token.type=Number;token.value=const_pi;break;case 33:token.type=Operation;token.value=_m10;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operation;token.value=_sq;break;case 36:token.type=Operator;token.value=degree;break;case 41:token.type=Number;token.value=vx;break;case 42:token.type=Number;token.value=vy;break;case 43:token.type=Number;token.value=vz;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operation;token.value=_sqrt;break;case 46:token.type=Operation;token.value=option;break;case 51:token.type=Operation;token.value=x_eq;break;case 52:token.type=Operation;token.value=y_eq;break;case 53:token.type=Operation;token.value=z_eq;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;elseswitch(loc)case 11:token.type=Function;token.value=f_cos;break;case 12:token.type=Function;token.value=f_sin;break;case 13:token.type=Function;token.value=f_tan;break;case 14:token.type=Operator;token.value=comma;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=delete;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=7;break;case 32:token.type=Number;token.value=8;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operator;token.value=factor;break;case 36:token.type=Operator;token.value=degree;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operator;token.value=_pow;break;case 46:token.type=Operation;token.value=mode;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;else if(CMode=N_BASE)if(CShift=0)switch(loc)case 11:token.type=Number;token.value=10;break;case 12:token.type=Number;token.value=11;break;case 13:token.type=Number;token.value=12;break;/case 14:token.type=Operator;token.value=_neg;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=delete;break;case 21:token.type=Number;token.value=13;break;case 22:token.type=Number;token.value=14;break;case 23:token.type=Number;token.value=15;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=7;break;case 32:token.type=Number;token.value=8;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operator;token.value=_and;break;case 36:token.type=Operator;token.value=_not;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operator;token.value=_or;break;case 46:token.type=Operation;token.value=mode;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=_mod;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;elseswitch(loc)case 11:token.type=Operation;token.value=I_B;break;case 12:token.type=Operation;token.value=I_O;break;case 13:token.type=Operation;token.value=I_H;break;/case 14:token.type=Operator;token.value=_neg;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=AC;break;case 21:token.type=Operation;token.value=O_B;break;case 22:token.type=Operation;token.value=O_O;break;case 23:token.type=Operation;token.value=O_H;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Operation;token.value=I_D;break;case 32:token.type=Operation;token.value=O_D;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operator;token.value=_xor;break;case 36:token.type=Operator;token.value=_not;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operator;token.value=_xnor;break;case 46:token.type=Operation;token.value=mode;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=_mod;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;else if(CMode=L_EQ)if(CShift)switch(loc)case 11:token.type=Function;token.value=f_acos;break;case 12:token.type=Function;token.value=f_asin;break;case 13:token.type=Function;token.value=f_atan;break;case 14:token.type=Operator;token.value=comma;break;case 15:token.type=Operation;token.value=Sup;break;case 16:token.type=Operation;token.value=AC;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=Sleft;break;case 25:token.type=Operation;token.value=Sdown;break;case 26:token.type=Operation;token.value=Sright;break;case 31:token.type=Number;token.value=const_e;break;case 32:token.type=Number;token.value=const_pi;break;case 33:token.type=Operation;token.value=_m10;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operation;token.value=_sq;break;case 36:token.type=Operator;token.value=degree;break;case 41:token.type=Number;token.value=vx;break;case 42:token.type=Number;token.value=vy;break;case 43:token.type=Number;token.value=vz;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operation;token.value=_sqrt;break;case 46:token.type=Operation;token.value=option;break;case 51:token.type=Operation;token.value=x_eq;break;case 52:token.type=Operation;token.value=y_eq;break;case 53:token.type=Operation;token.value=z_eq;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Operation;token.value=_size;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=_solve;break;default:token.type=Invalid;break;elseswitch(loc)case 11:token.type=Function;token.value=f_cos;break;case 12:token.type=Function;token.value=f_sin;break;case 13:token.type=Function;token.value=f_tan;break;case 14:token.type=Operator;token.value=comma;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=delete;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=7;break;case 32:token.type=Number;token.value=8;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operator;token.value=factor;break;case 36:token.type=Operator;token.value=degree;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operator;token.value=_pow;break;case 46:token.type=Operation;token.value=mode;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;else if(CMode=CMPLX)if(CShift)switch(loc)case 11:token.type=Function;token.value=f_acos;break;case 12:token.type=Function;token.value=f_asin;break;case 13:token.type=Function;token.value=f_atan;break;case 14:token.type=Operator;token.value=comma;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=AC;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=const_e;break;case 32:token.type=Number;token.value=const_pi;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operation;token.value=_sq;break;case 36:token.type=Operator ;token.value=_arg;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operation;token.value=_sqrt;break;case 46:token.type=Operation;token.value=option;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operator;token.value=enter;break;default:token.type=Invalid;break;elseswitch(loc)case 11:token.type=Function;token.value=f_cos;break;case 12:token.type=Function;token.value=f_sin;break;case 13:token.type=Function;token.value=f_tan;break;case 14:token.type=Number;token.value=_i;break;case 15:token.type=Operation;token.value=up;break;case 16:token.type=Operation;token.value=delete;break;case 21:token.type=Function;token.value=f_ln;break;case 22:token.type=Function;token.value=f_log;break;case 23:token.type=Function;token.value=f_lg;break;case 24:token.type=Operation;token.value=left;break;case 25:token.type=Operation;token.value=down;break;case 26:token.type=Operation;token.value=right;break;case 31:token.type=Number;token.value=7;break;case 32:token.type=Number;token.value=8;break;case 33:token.type=Number;token.value=9;break;case 34:token.type=Operator;token.value=_add;break;case 35:token.type=Operator;token.value=factor;break;case 36:token.type=Operator;token.value=degree;break;case 41:token.type=Number;token.value=4;break;case 42:token.type=Number;token.value=5;break;case 43:token.type=Number;token.value=6;break;case 44:token.type=Operator;token.value=_sub;break;case 45:token.type=Operator;token.value=_pow;break;case 46:token.type=Operation;token.value=mode;break;case 51:token.type=Number;token.value=1;break;case 52:token.type=Number;token.value=2;break;case 53:token.type=Number;token.value=3;break;case 54:token.type=Operator;token.value=_mul;break;case 55:token.type=Operator;token.value=Lbrac;break;case 56:token.type=Operation;token.value=shift;break;case 61:token.type=Number;token.value=ans;break;case 62:token.type=Number;token.value=0;break;case 63:token.type=Operator;token.value=point;break;case 64:token.type=Operator;token.value=_div;break;case 65:token.type=Operator;token.value=Rbrac;break;case 66:token.type=Operation;token.value=enter;break;default:token.type=Invalid;break;return token;/*token.h*/*token.h*/#ifndef TOKEN_H#define TOKEN_H#include stm32f10x.h#define MAX_T102/*Value of Type - Function*/ #definef_sin 0#define f_cos 1#define f_tan 2#define f_asin3#define f_acos4#define f_atan5#define f_ln6#define f_lg7#definef_log8/*Value of Type - Operator*/#define _add0#define _sub1#define_mul2#define _div3#define _pow4#define factor5#define Lbrac6/bracket#define Rbrac7#define degree8#define _cursor 9#define Func10#define comma11#define Func2 12#define _arg 13#define point15#define semi14#define _and4#define _or5#define _not8#define _xor10#define _xnor11#define _mod12/*Value of Type - Operation*/#define left0#define right1#define up2#define down3#define delete4#define shift5#define enter6#define mode7#define option8#define AC9#define I_B10/ bin input#define I_O11/ oct input#define I_H12/ hex input#define I_D13/ dec input#define O_B14/ bin output#define O_O15/ oct output#define O_H16/ hex output#define O_D17/ dec output#define x_eq18#define y_eq19#define z_eq20#define _sq21#define _m1022#define _sqrt23#define Sright24#define Sup25#define Sdown26#define Sleft27#define _solve28#define _size29/*Value of Type - Number*/#define const_pi22#define const_e23#define vx24#define vy25#define vz26#define ans 27#define _i28typedef enumNumber,Function,Operator,Operation,Cursor,Invalid,Type;typedef struct _Mlocint x;int y; Mloc;typedef struct _TokenTypetype;u8value;Token;extern TokengtokenMAX_T;extern intCursor_LCD;extern intBegin;extern intNum_Token;extern intLoc_Cursor;extern intfinish;extern Mlocmloc;extern Mlocmloc_old;u8 Token_Length(Token token);void Token_Init(void);void Token_Add(Token token);void Mul_Add(Token token);void Token_Del(void);void Get_Token(void);void CursorL(void);void CursorR(void);void CursorHome(void);void CursorEnd(void);#endif/*token.c*/*token.c*/#includelcd.h#includekeyboard.h#includetoken.h#includecalculator.h#include stm32f10x.h#include at24c02.hToken gtokenMAX_T;int Num_Token=0;intLoc_Cursor=0;/Location of cursor in tokensintCursor_LCD=1;/Location of cursor in LCD ( 1 - 16 )intBegin=0;intfinish=0;Mlocmloc;Mlocmloc_old;u8 Token_Length(Token token)if(token.type=Number)if(token.valueMAX_T-3)return;for(i=Num_Token-1;i=Loc_Cursor;i-)gtokeni+1=gtokeni;gtokenLoc_Cursor=token;Cursor_LCD+=Token_Length(token);Loc_Cursor+;Num_Token+;void Mul_Add(Token token)Token temp;if(token.value=_m10)temp.type=Operator;temp.value=_mul;Token_Add(temp);temp.type=Number;temp.value=1;Token_Add(temp);temp.value=0;Token_Add(temp);temp.type=Operator;temp.value=_pow;Token_Add(temp);else if(token.value=_sq)temp.type=Operator;temp.value=_pow;Token_Add(temp);temp.type=Number;temp.value=2;Token_Add(temp);elsetemp.type=Operator;temp.value=_pow;Token_Add(temp);temp.value=Lbrac;Token_Add(temp);temp.type=Number;temp.value=1;Token_Add(temp);temp.type=Operator;temp.value=_div;Token_Add(temp);temp.type=Number;temp.value=2;Token_Add(temp);temp.type=Operator;temp.value=Rbrac;Token_Add(temp);void Token_Del(void)int i;if(Loc_Cursor1)return;Num_Token-;Loc_Cursor-;Cursor_LCD-=Token_Length(gtokenLoc_Cursor);for(i=Loc_Cursor;iMAX_T)return;token=K_identify();if(token.type=Operation)switch(token.value)case delete:Token_Del();break;case left:CursorL();break;case right:CursorR();break;case up:CursorHome();break;case down:CursorEnd();break;case shift:CShift=!CShift;break;case mode:SetMode();break;case enter:Cal_test();break;case option :Cal_Option();break;case AC:All_Clean();break;case _solve:EQ_test();break;case _size:SizeReset();break;case x_eq:;case y_eq:;case z_eq:;vassignment(token.value);break;case _m10:;case _sq:;case _sqrt:Mul_Add(token);break;case I_B:Base_I=2;Write_Byte(Base_I,0x85);break;case I_O:Base_I=8;Write_Byte(Base_I,0x85);break;case I_H:Base_I=16;Write_Byte(Base_I,0x85);break;case I_D:Base_I=10;Write_Byte(Base_I,0x85);break;case O_B:Base_O=2;Write_Byte(Base_O,0x86);break;case O_O:Base_O=8;Write_Byte(Base_O,0x86);break;case O_H:Base_O=16;Write_Byte(Base_O,0x86);break;case O_D:Base_O=10;Write_Byte(Base_O,0x86);break;case Sup:if(mloc.x1)mloc_old.x=-mloc.x;mloc_old.y=mloc.y;break;case Sleft:if(mloc.y1)mloc_old.y=-mloc.y;mloc_old.x=mloc.x;break;case Sright:if(mloc.yMax_Ecol)mloc_old.y=+mloc.y;mloc_old.x=mloc.x;break;case Sdown:if(mloc.x0;i-)gtokeni=gtokeni-1;Cursor_LCD=1;Begin=0;Loc_Cursor=0;gtokenLoc_Cursor.type=Cursor;void CursorEnd(void)int i;for(i=Loc_Cursor;iNum_Token-1;i+)gtokeni=gtokeni+1;Cursor_LCD+=Token_Length(gtokeni);Loc_Cursor=Num_Token-1;gtokenLoc_Cursor.type=Cursor;void CursorL(void)Token t;if(Loc_Cursor=Num_Token-1)return;Cursor_LCD+=Token_Length(gtokenLoc_Cursor+1);t=gtokenLoc_Cursor;gtokenLoc_Cursor=gtokenLoc_Cursor+1;gtokenLoc_Cursor+1=t;Loc_Cursor+;/*myfloat.h*/#ifndef _MYFLOAT_H#define _MYFLOAT_H#define Max_bit 20/each bit-0-9#include error.htypedef struct _myfloatchar mbitMax_bit;int sign;int power; myfloat;typedef struct _complexmyfloat real;myfloat image; complex;extern myfloat mf_pi;extern myfloat mf_e;extern myfloat mf_ln10;void mfconst_init(void);void str_myfloat(myfloat* mf,int power,char* str);voidmyfloat_str_sc(char* str,const myfloat * mf);void myfloat_str(char* str,const myfloat * mf);void myfloat_str_hp(char* str,char* str2,const myfloat* mf);void m_sub_p(const myfloat * mf1,const myfloat * mf2,myfloat * res);void m_add_p(const myfloat * mf1,const myfloat * mf2,myfloat * res);void m_sub(const myfloat * mf1,const myfloat * mf2,myfloat * res);void m_add(const myfloat * mf1,const myfloat * mf2,myfloat * res);void m_mul(const myfloat * mf1,const myfloat * mf2,myfloat * res);void m_div(const myfloat * mf1,const myfloat * mf2,myfloat * res);voidm_div2(const myfloat * mf1,myfloat * res);void m_sin(const myfloat * mf,myfloat * res);void m_cos(const myfloat* mf,myfloat* res);Error m_tan(const myfloat* mf,myfloat* res);void m_div_int(const myfloat * mf1,int a,myfloat * res);void m_mul_int(const myfloat * mf1,int a,myfloat * res);void int_myfloat(int a,myfloat * mf);voidto_degree(const myfloat* mf,myfloat* res);void bithandler(const myfloat* mf,myfloat* res,int digit);Errorm_factor(const myfloat* mf,myfloat* res);void m_sub_int(const myfloat* mf1,int i,myfloat* res);void m_add_int(const myfloat* mf1,int a,myfloat* res);Error m_ln(const myfloat* mf,myfloat* res);Error m_lg(const myfloat* mf,myfloat* res);Error m_log(const myfloat* mf1,const myfloat* mf2,myfloat* res);void m_int_div(int a,const myfloat* mf1,myfloat* res);void add_atan(myfloat* value,const myfloat* atanx,myfloat* sum,int power);void m_atan(const myfloat* mf,myfloat* res);Error m_exp(const myfloat* power,myfloat* res);Error m_power_int(const myfloat* num,const myfloat* power,myfloat* res);Error m_power(const myfloat* num,const myfloat* power,myfloat* res);void m_sqrt(const myfloat* mf,myfloat* res);void m_int_sub(int i,const myfloat* mf1,myfloat* res);Error m_asin(const myfloat* mf,myfloat* res);Error m_acos(const myfloat* mf,myfloat* res);void complex_add(const complex* cp1,const complex* cp2,complex* res);void complex_sub(const complex* cp1,const complex* cp2,complex* res);void complex_mul(const complex* cp1,const complex* cp2,complex* res);void complex_conj(const complex* cp,complex* res);void complex_div(const complex* cp1,const complex* cp2,complex* res);Error complex_power (const complex* cp1,const complex* cp2,complex* res);Error m_power_int_C(const complex* num,const myfloat* power,complex* res);voidm_arg(const complex* num,constmyfloat* a,complex* res);#endif/*myfloat.c*/#includemyfloat.h#includelcd.h#include#include error.h#include stm32f10x.hmyfloat mf_pi;myfloat mf_e;myfloat mf_ln10;myfloat mf_atan1;myfloat mf_atan101;myfloat mf_atan201;myfloat mf_atan301;void mfconst_init()str_myfloat(&mf_pi,0,314159265358979323846264338327);str_myfloat(&mf_e,0,271828182845904523536028747135);str_myfloat(&mf_ln10,0,230258509299404568401799145468);str_myfloat(&mf_atan1, -1,785398163397448309615660845819);str_myfloat(&mf_atan101,-2,996686524911620273784461198780);str_myfloat(&mf_atan201,-3,999966668666523820634011620928);str_myfloat(&mf_atan301,-4,999999666666866666523809634920);void str_myfloat(myfloat* mf,int power,char* str)int i;memmove(mf-mbit,str,Max_bit);for(i=0;imbiti-=0;mf-sign=1;mf-power=power;void myfloat_str_sc(char* str,const myfloat* mf)int i,power;myfloat mf0;bithandler(mf,&mf0,7);power=mf0.power;if(mf-mbit0=0)memset(str+1, ,15);str0=0;str16=0;return;if(mf0.sign)str0= ;elsestr0=-;str1=mf0.mbit0+0;str2=.;for(i=3;i9;i+)stri=mf0.mbiti-2+0;stri=*;i+;stri=1;i+;stri=0;i+;stri=;i+;if(powermbit0=0)memset(str+1, ,15);str0=0;str16=0;return;if(mf-powerpower6)myfloat_str_sc(str,mf);return;else if(mf-powerpower);i=1;str1=0;str2=.;while(i-mf0.power)stri+2=0;i+;for(j=0;i+j8;j+)stri+j+2=mf0.mbitj+0;i=9;while(stri=0)stri= ;i-;for(i=10;i16;i+)stri= ;str16=0;elsebithandler(mf,&mf0,7);for(i=0;i=mf0.power;i+)stri+1=mf0.mbiti+0;if(i7)stri+1=.;for(;i7;i+)stri+2=mf0.mbiti+0;i=8;while(stri=0)stri= ;i-;if(stri=.)stri= ;else str8= ;for(i=9;imbit0=0)memset(str, ,16);str0=0;str16=0;return;if(mf0.sign)str0= ;elsestr0=-;str1=mf0.mbit0+0;str2=.;for(i=3;i16;i+)stri=mf0.mbiti-2+0;memset(str2, ,16);if(powerpower+1-digit;temp.sign=mf-sign;memset(temp.mbit,0,Max_bit);temp.mbit0=1;if(mf-mbitdigit=5)m_add(mf,&temp,res);else*res=*mf;void m_add_p(const myfloat* mf1,const myfloat* mf2,myfloat* res)/正加正 const myfloat* t;int pow_diff;int i,carry=0;if(mf1-powerpower)t=mf1;mf1=mf2;mf2=t;pow_diff=mf1-power-mf2-power;res-power=mf1-power;for(i=Max_bit-1;i=pow_diff;i-)res-mbiti=mf2-mbiti-pow_diff;for(i=0;imbiti=0;for(i=Max_bit-1;i=0;i-)res-mbiti+=mf1-mbiti+carry;if(res-mbiti=10)res-mbiti%=10;carry=1;else carry=0;if(carry)for(i=Max_bit-1;i0;i-)res-mbiti=res-mbiti-1;res-mbit0=1;res-power+;void m_sub_p(const myfloat* mf1,const myfloat* mf2,myfloat* res)/正减正,大减小 int pow_diff=mf1-power-mf2-power;int carry=0,i;res-power=mf1-power;for(i=Max_bit-1;i=pow_diff;i-)res-mbiti=mf2-mbiti-pow_diff;for(i=0;imbiti=0;for(i=Max_bit-1;i=0;i-)res-mbiti+=carry;if(mf1-mbiti=res-mbiti)res-mbiti=mf1-mbiti-res-mbiti;carry=0;elseres-mbiti=mf1-mbiti+10-res-mbiti;carry=1;if(res-mbit0=0)for(i=0;imbiti) break;if(i!=Max_bit)memmove(res-mbit,res-mbit+i,Max_bit-i);memset(res-mbit+Max_bit-i,0,i);res-power-=i;void m_sub(const myfloat* mf1,const myfloat* mf2,myfloat* res)myfloat temp_result;int i;if(mf1-mbit0=0|mf2-power-mf1-power=Max_bit)*res=*mf2;res-sign=!mf2-sign;return;else if(mf2-mbit0=0|mf1-power-mf2-power=Max_bit)*res=*mf1;return;if(mf1-sign=mf2-sign)if(mf1-powermf2-power)/大减小 (绝对值) m_sub_p(mf1,mf2,&temp_result);temp_result.sign=mf1-sign;else if(mf1-powerpower)/小减大 m_sub_p(mf2,mf1,&temp_result);temp_result.sign=!mf1-sign;elsefor(i=0;imbiti!=mf2-mbiti)break;if(i=Max_bit)memset(temp_result.mbit,0,Max_bit);temp_result.power=0;temp_result.sign=1;else if(mf1-mbitimf2-mbiti)m_sub_p(mf1,mf2,&temp_result);temp_result.sign=mf1-sign;elsem_sub_p(mf2,mf1,&temp_result);temp_result.sign=!mf1-sign;elsem_add_p(mf1,mf2,&temp_result);if(mf1-sign=1)temp_result.sign=1;elsetemp_result.sign=0;*res=temp_result;void m_add(const myfloat* mf1,const myfloat* mf2,myfloat* res)myfloat temp_mf,temp_result;if(mf1-mbit0=0|mf2-power-mf1-power=Max_bit)*res=*mf2;return;if(mf2-mbit0=0|mf1-power-mf2-power=Max_bit)*res=*mf1;return;if(mf1-sign=mf2-sign)m_add_p(mf1,mf2,&temp_result);temp_result.sign=mf1-sign;elseif(mf1-sign=1)temp_mf=*mf2;temp_mf.sign=1;m_sub(mf1,&temp_mf,&temp_result);elsetemp_mf=*mf1;temp_mf.sign=1;m_sub(mf2,&temp_mf,&temp_result);*res=temp_result;void m_mul(const myfloat* mf1,const myfloat* mf2,myfloat* res)int i,j,carry=0;char temp_result2*Max_bit;memset(temp_result,0,Max_bit*2);for(i=Max_bit-1;i=0;i-)for(j=Max_bit-1;j=0;j-)temp_resulti+j+1+=mf1-mbiti*mf2-mbitj;for(carry=0,j=2*Max_bit-1;j=Max_bit+1;j-)/后 Max_bit+1位 temp_resultj+=carry;carry=temp_resultj/10;temp_resultj%=10;for(;j=0;j-)/前 Max_bit位 temp_resultj+=carry;carry=temp_resultj/10;temp_resultj%=10;res-power=mf1-power+mf2-power;if(temp_result0=0)memmove(res-mbit,temp_result+1,Max_bit);elseres-power+;memmove(res-mbit,temp_result,Max_bit);if(mf1-sign=mf2-sign) res-sign=1;else res-sign=0;/* *mitemp_result = mi * mf2-mbit * - *mf2-mbit | temp_mf1compare temp_result and temp_mf1 * */void m_div(const myfloat* mf1,const myfloat* mf2,myfloat* res)int hi,lo,mi,i,j,carry;/二分法上商 char temp_resultMax_bit+1,temp_mf1Max_bit+1;char t_resultMax_bit;memmove(temp_mf1+1,mf1-mbit,Max_bit);temp_mf10=0;for(i=0;ilo+1)|(mi=hi)/未逼近到极限,或者上商的依然大了 carry=0;mi=(hi+lo)/2;for(j=Max_bit-1;j=0;j-)temp_resultj+1=mf2-mbitj*mi+carry;carry=temp_resultj+1/10;temp_resultj+1%=10;temp_result0=carry;for(j=0;jtemp_mf1j)hi=mi;/上商大了 else lo=mi;/上商小了 t_resulti=mi;/上商成功for(j=Max_bit,carry=0;j=0;j-)/取余 temp_mf1-=temp_resulttemp_resultj+=carry;if(temp_mf1j=temp_resultj)temp_mf1j-=temp_resultj;carry=0;elsetemp_mf1j=temp_mf1j+10-temp_resultj;carry=1;memmove(temp_mf1,temp_mf1+1,Max_bit);temp_mf1Max_bit=0;res-power=mf1-power-mf2-power;if(t_result0=0)memmove(res-mbit,t_result+1,Max_bit-1);res-mbitMax_bit-1=0;res-power-;elsememmove(res-mbit,t_result,Max_bit);if(mf1-sign=mf2-sign) res-sign=1;else res-sign=0;/*myfloat / 2时间复杂度大幅度减少,用于某些函数的计算*/void m_div2(const myfloat* mf1,myfloat* res)int i,carry=0,temp=0;myfloat temp_result;res-sign=mf1-sign;for(i=0;imbiti%2;temp_result.mbiti=(mf1-mbiti+carry*10)/2;carry=temp;if(temp_result.mbit0=0)memmove(res-mbit,temp_result.mbit+1,Max_bit-1);res-mbitMax_bit-1=0;res-power=mf1-power-1;elsememmove(res-mbit,temp_result.mbit,Max_bit);res-power=mf1-power;void int_myfloat(int a,myfloat* mf)/-99999999memset(mf-mbit,0,Max_bit);if(asign=0;else mf-sign=1;if(a=0)mf-mbit0=0;else if(ambit0=a;mf-power=0;else if(ambit0=a/10;mf-mbit1=a%10;mf-power=1;else if(ambit0=a/100;mf-mbit1=(a%100)/10;mf-mbit2=a%10;mf-power=2;elsemf-mbit0=a/1000;mf-mbit1=(a%1000)/100;mf-mbit2=(a%100)/10;mf-mbit3=a%10;mf-power=3;void m_div_int(const myfloat* mf1,int a,myfloat* res)myfloat temp;int_myfloat(a,&temp);m_div(mf1,&temp,res);void m_mul_int(const myfloat* mf1,int a,myfloat* res)myfloat temp;int_myfloat(a,&temp);m_mul(mf1,&temp,res);void m_int_sub(int i,const myfloat* mf1,myfloat* res)myfloat temp;int_myfloat(i,&temp);m_sub(&temp,mf1,res);/利用泰勒公式求高精度正弦 void m_sin(const myfloat* mf,myfloat* res)myfloat sum,num,x2,temp,temp_mf=*mf;int i;memset(&sum,0,sizeof(myfloat);sum.sign=1;memset(&temp,0,sizeof(myfloat);temp.sign=1;/* -把 mf 减至 2pi以内- */m_div(&temp_mf,&mf_pi,&temp);/temp=mf/pim_div2(&temp,&temp);/temp/=2if(temp.power=0)/sin(x)=sin(pi-x)if(temp_mf.sign=1)m_sub(&mf_pi,&temp_mf,&temp_mf);elsetemp=mf_pi;temp.sign=0;m_sub(&temp,&temp_mf,&temp_mf);/* -泰勒公式求和- */m_mul(&temp_mf,&temp_mf,&x2);for(i=1,num=temp_mf;num.mbit0!=0&(sum.power-num.power)=Max_bit;i+=4)m_add(&sum,&num,&sum);/sum += numm_mul(&num,&x2,&num);/num *= x2m_div_int(&num,(i+1)*(i+2),&num);/num /= (i+1)*(i+2)m_sub(&sum,&num,&sum);/sum -= numm_mul(&num,&x2,&num);/num *= x2m_div_int(&num,(i+3)*(i+4),&num);/num /= (i+3)*(i+4)if(sum.powersign=1;return;*res=sum;void m_cos(const myfloat* mf,myfloat* res)myfloat temp=mf_pi;m_div_int(&mf_pi,2,&temp);m_sub(&temp,mf,&temp);m_sin(&temp,res);/ tanx=sinx/cosxError m_tan(const myfloat* mf,myfloat* res)myfloat temps,tempc;m_sin(mf,&temps);m_cos(mf,&tempc);if(tempc.mbit0=0)return tan_Err;m_div(&temps,&tempc,res);return OK;void to_degree(const myfloat* mf,myfloat* res)myfloat temp;temp=*mf;m_div_int(&temp,180,&temp);m_mul(&temp,&mf_pi,res);Error m_factor(const myfloat* mf,myfloat* res)/ sign=0)return factor_Err;if(mf-mbit0=0)*res=temp;return OK;else if(mf-power=0)for(i=Max_bit-1;i0;i-)if(mf-mbiti)return factor_Err;for(i=1;imbit0;i+)m_mul_int(&temp,i,&temp);else if(mf-power=1)for(i=Max_bit-1;i1;i-)if(mf-mbiti)return factor_Err;max=mf-mbit0*10+mf-mbit1;if(max60)return factor_Err;for(i=1;isign=0|mf-mbit0=0)return ln_Err;memset(&sum,0,sizeof(myfloat);sum.power=0;sum.sign=1;temp=*mf;temp.power=0;while(temp.power=0)/ temp *= ejm_mul(&temp,&mf_e,&temp);j+;temp.power=0;/ temp /= 10m_sub_int(&temp,1,&y);/ y = temp-1m_add_int(&temp,1,&temp);/ temp += 1m_div(&y,&temp,&y);/ y = y/tempm_mul(&y,&y,&y2);/ y2 = y*ynum=y;/ num = yfor(i=3;num.mbit0!=0&sum.power-num.powerpower+1,&temp);/ temp =( power+1)*ln10m_add(&sum,&temp,res);/ res = sum + tempreturn OK;Error m_lg(const myfloat* mf,myfloat* res)myfloat temp;if(mf-sign=0|mf-mbit0=0)return lg_Err;m_ln(mf,&temp);m_div(&temp,&mf_ln10,res);return OK;Error m_log(const myfloat* mf1,const myfloat* mf2,myfloat* res)myfloat temp1,temp2;if(mf1-sign=0|mf2-sign=0|mf1-mbit0=0|mf2-mbit0=0)return log_Err;m_ln(mf1,&temp1);m_ln(mf2,&temp2);if(temp2.mbit0=0)return log_Err;m_div(&temp1,&temp2,res);return OK;/* * *atan x=atan y+atan (x-y)/(x*y+1) * */void add_atan(myfloat* value,const myfloat* atanx,myfloat* sum,int power)myfloat temp;int i;char flag=1;while(flag&value-power=power)temp=*value;temp.power+=power;m_add_int(&temp,1,&temp);/temp += 1value-mbit0-;/value -= yif(!value-mbit0)for(i=0;imbiti)break;if(i!=Max_bit)memmove(value-mbit,value-mbit+i,Max_bit-i);memset(value-mbit+Max_bit-i,0,i);value-power-=i;flag=0;m_div(value,&temp,value);/value /= tempm_add(sum,atanx,sum);/sum += atanx/* *atan x= *x-1/3*x3+1/5*x5-1/7*x7+1/9*x9 . * */void m_atan(const myfloat* mf,myfloat* res)myfloat temp,sum,num,x2;int i,j=0,k=0;if(mf-mbit0=0)memset(res-mbit,0,Max_bit);res-power=0;res-sign=1;if(mf-power0|(mf-power=0&mf-mbit01)m_int_div(1,mf,&temp);j=1;elsetemp=*mf;if(mf-sign=0)temp.sign=1;k=1;memset(sum.mbit,0,Max_bit);sum.power=0;sum.sign=1;if(temp.power=0)add_atan(&temp,&mf_atan1,&sum,0);if(temp.power=-1)add_atan(&temp,&mf_atan101,&sum,-1);if(temp.power=-2)add_atan(&temp,&mf_atan201,&sum,-2);if(temp.power=-3)add_atan(&temp,&mf_atan301,&sum,-3);/*x 0.0001 */m_mul(&temp,&temp,&x2);/x2 = temp*tempnum=temp;for(i=3;num.mbit0&sum.power-num.powerpower2)return E_Overflow;else if(power-power=2)if(power-mbit02)return E_Overflow;else if(power-mbit0=2&power-mbit1=3)return E_Overflow;memset(pw.mbit,0,Max_bit-1);pw.mbit0=1;pw.sign=1;pw.power=0;if(power-power=0)/ calculate for int sectionif(power-power=0)xp=power-mbit0;else if(power-power=1)xp=power-mbit1+power-mbit0*10;elsexp=power-mbit2+power-mbit1*10+power-mbit0*100;num=mf_e;while(xp)if(xp&1) m_mul(&pw,&num,&pw);/ if (bit n) pw *= e(2n)m_mul(&num,&num,&num);/ num *= numxp=1; if(power-sign=0)m_int_div(1,&pw,&pw);memmove(num.mbit,power-mbit+power-power+1,Max_bit-power-power-1);memset(num.mbit+Max_bit-power-power,0,power-power);num.power=-1;elsenum=*power;x=num;memset(sum.mbit+1,0,Max_bit-1);sum.mbit0=1;sum.power=0;sum.sign=1;for(i=2;num.mbit0&sum.power-num.powerpower=0)xp=power-mbit0;else if(power-power=1)xp=power-mbit1+power-mbit0*10;elsexp=power-mbit2+power-mbit1*10+power-mbit0*100;temp=*num;while(xp)if(xp&1) if(pw.power99)return E_Overflow;m_mul(&pw,&temp,&pw);/ if (bit n) pw *= num(2n)m_mul(&temp,&temp,&temp);/ temp *= tempxp=1; if(power-sign=0)m_int_div(1,&pw,&pw);*res=pw;return OK;Error m_power(const myfloat* num,const myfloat* power,myfloat* res)myfloat temp=*num;int i;Error err;if(power-power=3)return E_Overflow;else if(power-mbit0=0)if(num-mbit0=0)return Exp_Err;elsememset(res-mbit+1,0,Max_bit-1);res-mbit0=1;res-sign=1;res-power=0;return OK;elsefor(i=Max_bit-1;ipower-power;i-)/ for xn (int)if(power-mbiti) break;if(i=power-power)m_power_int(num,power,res);return OK;else if(i=0&power-power=-1&power-mbit0=5)if(num-sign=0)return Exp_Err;m_sqrt(num,res);if(power-sign=0)m_int_div(1,res,res);return OK;if(num-mbit0=0)memset(res-mbit,0,Max_bit);res-sign=1;res-power=0;return OK;if(num-sign=0)return Exp_Err;if(err=m_ln(&temp,&temp)!=OK)return err;/ temp = ln(temp)m_mul(power,&temp,&temp);/ temp *= powerif(err=m_exp(&temp,&temp)!=OK)return err;/ temp = exp(temp)*res=temp;return OK;void m_sqrt(const myfloat* mf,myfloat* res)int power=0;myfloat temp,temp2,x,mftemp=*mf;if(mf-mbit0=0)memset(res-mbit,0,Max_bit);res-power=0;res-sign=1;return;memset(x.mbit+1,0,Max_bit-1);x.power=0;x.sign=1;x.mbit0=1;power=mftemp.power/2;mftemp.power=mftemp.power%2;dotemp2=x;m_mul(&x,&x,&temp);/ temp = x*xm_sub(&temp,&mftemp,&temp);/ temp -= mfm_div2(&temp,&temp);/ temp /= 2m_div(&temp,&x,&temp);/ temp /= xm_sub(&x,&temp,&x);/ x = x-tempm_sub(&x,&temp2,&temp2);while(temp2.mbit0);x.power+=power;*res=x;Error m_asin(const myfloat* mf,myfloat* res)myfloat temp;m_mul(mf,mf,&temp);m_int_sub(1,&temp,&temp);if(temp.sign=0) return ASin_Err;m_sqrt(&temp,&temp);m_div(mf,&temp,&temp);m_atan(&temp,res);return OK;Error m_acos(const myfloat* mf,myfloat* res)myfloat temp;m_mul(mf,mf,&temp);m_int_sub(1,&temp,&temp);if(temp.sign=0) return ACos_Err;m_sqrt(&temp,&temp);m_div(&temp,mf,&temp);m_atan(&temp,&temp);if(temp.sign=0)m_add(&temp,&mf_pi,res);else *res=temp;return OK;void complex_add(const complex* cp1,const complex* cp2,complex* res)m_add(&cp1-real,&cp2-real,&res-real);m_add(&cp1-image,&cp2-image,&res-image);void complex_sub(const complex* cp1,const complex* cp2,complex* res)m_sub(&cp1-real,&cp2-real,&res-real);m_sub(&cp1-image,&cp2-image,&res-image);void complex_mul(const complex* cp1,const complex* cp2,complex* res)myfloat temp1,temp2;complex temp;m_mul(&cp1-real,&cp2-real,&temp1);m_mul(&cp1-image,&cp2-image,&temp2);m_sub(&temp1,&temp2,&temp.real);m_mul(&cp1-real,&cp2-image,&temp1);m_mul(&cp1-image,&cp2-real,&temp2);m_add(&temp2,&temp1,&temp.image);*res=temp;void complex_conj(const complex* cp,complex* res)res-real=cp-real;res-image=cp-image;res-image.sign=!res-image.sign;void complex_div(const complex* cp1,const complex* cp2,complex* res)myfloat temp1,temp2;complex temp;complex_conj(cp2,&temp);complex_mul(cp1,&temp,&temp);m_mul(&cp2-real,&cp2-real,&temp1);m_mul(&cp2-image,&cp2-image,&temp2);m_add(&temp1,&temp2,&temp1);m_div(&temp.real,&temp1,&res-real);m_div(&temp.image,&temp1,&res-image);Error complex_power(const complex* cp1,const complex* cp2,complex* res)int i;if(cp2-image.mbit0)return Exp_Err;if(cp1-image.mbit0=0)/ cp1-image=0m_power(&cp1-real,&cp2-real,&res-real);return OK;else/ cp1-image!=0 but cp2 is a integerfor(i=Max_bit-1;icp2-real.power;i-)/ for xn (int)if(cp2-real.mbiti) break;if(i=cp2-real.power)m_power_int_C(cp1,&cp2-real,res);return OK;return Exp_Err;Error m_power_int_C(const complex* num,const myfloat* power,complex* res)complex temp,pw;int xp;memset(&pw,0,sizeof(complex);pw.real.mbit0=1;pw.real.sign=1;if(power-power=0)xp=power-mbit0;else if(power-power=1)xp=power-mbit1+power-mbit0*10;elsexp=power-mbit2+power-mbit1*10+power-mbit0*100;temp=*num;while(xp)if(xp&1) if(pw.real.power99|pw.image.power99)return E_Overflow;complex_mul(&pw,&temp,&pw);/ if (bit n) pw *= num(2n)complex_mul(&temp,&temp,&temp);/ temp *= tempxp=1; if(power-sign=0)myfloat temp1,temp2;complex_conj(&pw,&pw);m_mul(&pw.image,&pw.image,&temp1);m_mul(&pw.real,&pw.real,&temp2);m_add(&temp1,&temp2,&temp1);m_div(&pw.real,&temp1,&pw.real);m_div(&pw.image,&temp1,&pw.image);*res=pw;return OK;void m_arg(const complex* num,constmyfloat* a,complex* res)complex temp;m_sin(a,&temp.image);m_cos(a,&temp.real);complex_mul(num,&temp,res);/*calculator.h*/#ifndef _CALCULATOR_H#define _CALCULATOR_H#include stm32f10x.h#include token.h#include myfloat.h#include error.h#define Num_Op14#define Num_Opb13#define Max_St30typedef enum _Cal_ModeSTD,N_BASE,L_EQ,CMPLX, Cal_Mode;extern Cal_Mode CMode;extern u8CShift;extern myfloatResult;extern myfloatsave;externu8Hprecision;externu8Scientific;externu8Degree;extern u8Base_I;extern u8Base_O;extern u8Base_bit;extern u32Result_base;extern u8Max_Erow;extern u8Max_Ecol;extern myfloatEQ_M78;extern complexResultc;extern u8complex_disp;extern myfloat_A_;extern myfloat_H_;voidSetMode(void);Error Calculate(void);Error Calculate_Base(void);Error Calculate_CMPLX(void);void Complex_test(void);int Token_fNumber(Token * begin,myfloat * mf);int Token_fNumber_c(Token* begin,complex* mf);Error Calo1(int op,myfloat * mf);Error Calo2(int op,myfloat * mf);voidCal_test(void);Error Cal_f(int _func,myfloat* mf);Error Cal_f2(int _func,myfloat* mf1,myfloat* mf);void Cal_Option(void);voidAll_Clean(void);int Token_data(Token* token,u32* data);Error Cal_base_o1(int op,u32* data);void Cal_base_o2(int op,u32* data);void vassignment(int v);void EQ_M_Init(void);Error Solve_EQ(void);void EQ_test(void);void SizeReset(void);Error Calo_CMPLX(int op,complex * cp);#endif/*calculator.c*/#include lcd.h#include keyboard.h#include token.h#include calculator.h#include myfloat.h#include #include delay.h#include error.h#include stm32f10x.h#include error.h#include at24c02.hCal_ModeCMode=STD;u8CShift=0;myfloatResult,save;u8Hprecision=0;u8Scientific=0;u8complex_disp=0;u8Degree=0;u8Base_I=10;u8Base_O=10;u32Result_base=0;myfloatEQ_M78;u8Max_Erow=2;u8Max_Ecol=3;complexResultc;myfloat_A_;myfloat_H_;/* priority of operator * From 邓俊辉*/const int priNum_OpNum_Op=/Current/ + - * / ! ( ) d_Func , Func2 arg/*Top*/*add*/,/*Lbrac*/,=, ,/*cursor*/, ,=, ,/*comma*/, , ,/*Func2 */ , , , , , ,/*arg*/,/*div*/,/*and*/,/*or*/,/*Lbrac*/,=, ,/*cursor*/, ,=,/*xor*/,/*xnor*/,;void EQ_M_Init(void)int i,j;for(i=0;i5;i+)for(j=0;j=1&select.value stack is empty)intstack_opMax_St;int*otop=stack_op;int*obuttom=stack_op;intstack_funcMax_St;int*fctop=stack_func;Error err;CursorEnd();Show_Tokens(gtoken,Num_Token);ShowMode();gtokenLoc_Cursor.value=_cursor;otop+;*otop=_cursor;while(otop!=obuttom)if(gtokeni.type=Number)count=Token_fNumber(>okeni,ftop+1);if(count)ftop+;/pushi+=count;elsereturn I_Exp;else if(gtokeni.type=Operator|gtokeni.type=Cursor|gtokeni.type=Function)/ 处于第一位 / 前面是双目运算符if(gtokeni.type=Operator&/ sub- minusgtokeni.value=_sub&(i=0|(gtokeni-1.type=Operator &(gtokeni-1.valuesign=!ftop-sign;i+=count;else return I_Exp;else if(otop=obuttom)return I_Exp;else if(gtokeni.type=Function)fctop+;/push*fctop=gtokeni.value;otop+;if(gtokeni.value=f_log)*otop=Func2;else *otop=Func;i+;else if(pri*otopgtokeni.value=)if(*otop=4)/双目运算符 if(ftopfbuttom+2)return I_Exp;elseif(err=Calo1(*otop,ftop-1)!=OK)return err;otop-;/popftop-; else if(*otopFunc)/单目运算符if(ftopfbuttom+1)return I_Exp;elseif(Calo2(*otop,ftop)=factor_Err)return factor_Err;otop-; /popelse if(*otop=comma)/Function 二元if(ftopfbuttom+2)return I_Exp;if(err=Cal_f2(*fctop,ftop-1,ftop)!=OK)return err;otop-=3;fctop-;ftop-;i+;else/Function 一元if(ftoppower99|ftop-power-99)return E_Overflow;else if(pri*otopgtokeni.value= )return I_Exp;elseotop-;/popi+;if(ftop!=fbuttom+1)return I_Exp;save=Result=*ftop;Write_Bits(ftop,sizeof(myfloat),0x00);if(CMode=L_EQ)Token_Init();LCDw_Com(0x01);mloc_old.x=mloc.x;mloc_old.y=mloc.y;EQ_Mmloc.x-1mloc.y-1=Result;if(mloc.yMax_Ecol)mloc.y+;elseif(mloc.xmbit0=0)return Div_Zero;m_div(mf,mf+1,mf);break;return OK;Error Calo2(int op,myfloat * mf)switch(op)case degree:to_degree(mf,mf);break;case factor:if(m_factor(mf,mf)!=OK)return factor_Err;break;return OK;Error Cal_f(int _func,myfloat* mf)switch(_func)case f_sin:m_sin(mf,mf);break;case f_cos:m_cos(mf,mf);break;case f_atan:m_atan(mf,mf);break;case f_asin:if(m_asin(mf,mf)=ASin_Err)return ASin_Err;break;case f_acos:if(m_acos(mf,mf)=ACos_Err)return ACos_Err;break;case f_tan:if(m_tan(mf,mf)=tan_Err)return tan_Err;break;case f_ln:if(m_ln(mf,mf)=ln_Err)return ln_Err;break;case f_lg:if(m_lg(mf,mf)=ln_Err)return lg_Err;break;return OK;Error Cal_f2(int _func,myfloat* mf1,myfloat* mf)switch(_func)case f_log: if(m_log(mf,mf1,mf1)=log_Err)return log_Err;break;default:return I_Exp;return OK;/* - N-BASE MODE - */*you can calculate though the data overflow */Error Calculate_Base(void)u32 data_stackMax_St;u32*dtop=data_stack;u32*dbuttom=data_stack;inti=0,count=0;intstack_opMax_St;int* otop=stack_op;int*obuttom=stack_op;Error err;CursorEnd();Show_Tokens(gtoken,Num_Token);ShowMode();gtokenLoc_Cursor.value=_cursor;otop+;*otop=_cursor;while(otop!=obuttom)if(gtokeni.type=Number)dtop+;count=Token_data(>okeni,dtop);if(!count)return I_Exp;else i+=count;else if(gtokeni.type=Operator|gtokeni.type=Cursor)if(gtokeni.type=Operator>okeni.value=_sub&(i=0 | (gtokeni-1.type=Operator & gtokeni-1.value!=Rbrac) )i+;dtop+;count=Token_data(>okeni,dtop);if(count)i+=count;*dtop=-*dtop;else return I_Exp;else if(bpri*otopgtokeni.value=)if(*otop!=_not)/双目运算符if(dtopdbuttom+2)return I_Exp;elseif(err=Cal_base_o1(*otop,dtop)!=OK)return err;otop-;/popdtop-; else/单目运算符if(dtop=Base_I)return 0;*data*=Base_I;*data+=tokeni+.value;count+;return count;Error Cal_base_o1(int op,u32* data)switch(op)case _add:*(data-1)+=*data;break;case _sub:*(data-1)-=*data;break;case _mul:*(data-1)*=*data;break;case _and:*(data-1)&=*data;break;case _or:*(data-1)|=*data;break;case _xor:*(data-1)=*data;break;case _xnor:*(data-1)=(*(data-1)*data);break;case _mod:if(*(u32*)data=0)return Mod_Zero;else *(data-1)%=*data;break;case _div:if(*(u32*)data=0)return Div_Zero;else *(data-1)/=*data;break;default:return I_Exp;return OK;void Cal_base_o2(int op,u32* data)*data= *data;/* - L-EQ - */Error Solve_EQ(void)int i,j,k;myfloat temp;for(i=0;iMax_Ecol-1;i+)if(EQ_Mii.mbit0=0)/div 0for(k=i+1;kMax_Erow;k+)if(k!=i&EQ_Mki.mbit0!=0)/exchange between row(k) and row(i)for(j=i;jMax_Ecol;j+)temp=EQ_Mij;EQ_Mij=EQ_Mkj;EQ_Mkj=temp;break;if(k=Max_Erow)return EQ_Err;for(j=i+1;jMax_Ecol;j+)m_div(&EQ_Mij,&EQ_Mii,&EQ_Mij);/ row(i)/=EQ_Miifor(j=0;jMax_Erow;j+)if(j!=i)for(k=i+1;k1)i-;break;case down:if(i stack is empty )intstack_opMax_St;int*otop=stack_op;int*obuttom=stack_op;intstack_funcMax_St;int*fctop=stack_func;Error err;CursorEnd();Show_Tokens(gtoken,Num_Token);ShowMode();gtokenLoc_Cursor.value=_cursor;otop+;*otop=_cursor;while(otop!=obuttom)if(gtokeni.type=Number)count=Token_fNumber_c(>okeni,ctop+1);if(count)ctop+;/pushi+=count;elsereturn I_Exp;else if(gtokeni.type=Operator|gtokeni.type=Cursor|gtokeni.type=Function)if(gtokeni.type=Operator&/ sub- minusgtokeni.value=_sub&/ 处于第一位 / 前面是双目运算符(i=0|(gtokeni-1.type=Operator & (gtokeni-1.valueimage.sign=!ctop-image.sign;ctop-real.sign=!ctop-real.sign;i+=count;else return I_Exp;else if(otop=obuttom)return I_Exp;else if(gtokeni.type=Function)fctop+;/push*fctop=gtokeni.value;otop+;if(gtokeni.value=f_log)*otop=Func2;else *otop=Func;i+;else if(pri*otopgtokeni.value=)if(*otop=4|*otop=_arg)/双目运算符 if(ctopcbuttom+2)return I_Exp;elseif(err=Calo_CMPLX(*otop,ctop-1)!=OK)return err;otop-;/popctop-; else if(*otopFunc)/单目运算符if(ctopimage.mbit0)return I_Exp;elseif(Calo2(*otop,&ctop-real)=factor_Err)return factor_Err;otop-; /popelse if(*otop=comma)/Function 二元if(ctopimage.mbit0|(ctop-1)-real.mbit0)return I_Exp;if(err=Cal_f2(*fctop,&(ctop-1)-real,&ctop-real)!=OK)return err;otop-=3;fctop-;ctop-;i+;else/Function 一元if(ctopimage.mbit0)return I_Exp;if(err=Cal_f(*fctop,&ctop-real)!=OK)return err;otop-;fctop-;if(ctop-real.power99|ctop-real.powerimage.power99|ctop-image.powervalue=ans)Read_Bits(mf,sizeof(complex),0x90);return 1;else if(begin-value=_i)memset(mf,0,sizeof(complex);mf-image.mbit0=1;mf-image.sign=1;return 1;count=Token_fNumber(begin,&temp);if(begincount.type=Number&begincount.value=_i)mf-image=temp;memset(&mf-real,0,sizeof(myfloat);mf-real.sign=1;count+;elsemf-real=temp;memset(&mf-image,0,sizeof(myfloat);mf-image.sign=1;return count;Error Calo_CMPLX(int op,complex * cp)Error err;switch(op)case _add:complex_add(cp,cp+1,cp);break;case _sub:complex_sub(cp,cp+1,cp);break;case _mul:complex_mul(cp,cp+1,cp);break;case _pow:if(err=complex_power(cp,cp+1,cp)!=OK)return err;break;case _arg:if(cp+1)-image.mbit0)return I_Exp;else m_arg(cp,&(cp+1)-real,cp);break;case _div:if(cp+1)-image.mbit0=0&(cp+1)-real.mbit0=0)return Div_Zero;complex_div(cp,cp+1,cp);break;return OK;/* - Others - */int Token_fNumber(Token * begin,myfloat * mf)u8 pflag=0;int count=0;int i=0;memset(mf-mbit,0,Max_bit);mf-power=-1;mf-sign=1;if(begin-value=const_pi)*mf=mf_pi;return 1;else if(begin-value=const_e)*mf=mf_e;return 1;else if(begin-value=vx)switch(begin-value)case ans:Read_Bits(mf,sizeof(myfloat),0x00);break;case vx:Read_Bits(mf,sizeof(myfloat), sizeof(myfloat);break;case vy:Read_Bits(mf,sizeof(myfloat),2*sizeof(myfloat);break;case vz:Read_Bits(mf,sizeof(myfloat),3*sizeof(myfloat);break;return 1;else if(begin-value=0)if(begin+1)-type=Operator&(begin+1)-value=point&(begin+2)-type=Number)begin+=2;count+;while(begin-type=Number&begin-value=0)mf-power-;begin+;count+;pflag=1;else if(begin+1)-type=Operator&(begin+1)-value=point&(begin+2)-type!=Number)return 0;else/zeromf-power=0;mf-mbit0=0;mf-sign=1;return 1;while(begin-type=Number&begin-valuetype=Operator&begin-value=point)if(pflag=0&begin-valueMax_bit)mf-power+;elsemf-mbiti=begin-value;mf-power+;i+;count+;begin+;else if(begin-value=point)if(pflag)return 0;/encounter another pointpflag=1;begin+;else if(pflag=1&begin-valueMax_bit);elsemf-mbiti=begin-value;i+;count+;begin+;return count+pflag;void All_Clean(void)Token_Init();LCDw_Com(0x01);CShift=0;if(CMode=L_EQ)
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
提示  人人文库网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:多功能计算器系统设计
链接地址:https://www.renrendoc.com/p-36008701.html

官方联系方式

2:不支持迅雷下载,请使用浏览器下载   
3:不支持QQ浏览器下载,请用其他浏览器   
4:下载后的文档和图纸-无水印   
5:文档经过压缩,下载后原文更清晰   
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

网站客服QQ:2881952447     

copyright@ 2020-2025  renrendoc.com 人人文库版权所有   联系电话:400-852-1180

备案号:蜀ICP备2022000484号-2       经营许可证: 川B2-20220663       公网安备川公网安备: 51019002004831号

本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知人人文库网,我们立即给予删除!