模拟计算器程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include
#include
void lcd_init(void); //初始化lcm
void clr_lcd(void); //清屏
void send_com(unsigned char cmd);
void send_data(unsigned char dat);
void chek_busy(void); //检测忙
void set_xy(unsigned char xpos,unsigned char ypos); //光标位置
void print(unsigned char x,unsigned char y,char* str); //
void printstr(unsigned char xpos,unsigned char ypos,unsigned char str[],unsigned char k);
//unsigned char code shu[10] ={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
//unsigned char code fuh[5] ={0x2b/*+*/,0x2d/*-*/,0x2a/***/,0x2f/*chu*/,0x2e/*.*/};
#define DATA P0
#define CONTROL P2
#define E 2 //并行的使能信号 sbit E = P0^7
#define RW 1 //并行的读写信号 sbit RW = P0^6
#define RS 0 //并行的指令数据选择信号 sbit RS = P0^5
#define PSB 3 //并/串行接口选择 sbit PSB = P0^4
#define RST 5 //复位 低电平有效 sbit RST = P0^3
#define SETB(x,y) (x|=(1<
#define uchar unsigned char
#define uint unsigned int
uchar fu,ql,m,m2,t;
sbit key1= P3^2; //按键K1
sbit key2= P3^3; //按键K2
sbit key3= P3^4; //按键K3
sbit key4= P3^5; //按键K4
sbit key5= P3^6; //按键K5
sbit key6= P3^7; //按键K6
/********************测忙碌**********************/
//测忙碌子程序
//RS=0,RW=1,E=H,D0-D7=状态字
/************************************************/
void chek_busy(void)
{ unsigned char temp1;//状态信息(判断是否忙)
CLRB(CONTROL,RS); // RS = 0;
SETB(CONTROL,RW); // RW = 1;
SETB(CONTROL,E); // E = 1;
do{temp1 = DATA;DATA=0xFF;} while(temp1&0x80);
SETB(CONTROL,E); // E = 1;
DATA=0xFF;
}
/********************写命令**********************/
//写命令子程序
//
/************************************************/
void send_com(unsigned char cmd)/*写命令*/
{
chek_busy();
CLRB(CONTROL,RS); //RS = 0;
CLRB(CONTROL,RW); //RW = 0;
DATA = cmd;
SETB(CONTROL,E); //E = 1;
CLRB(CONTROL,E); //E = 0;
}
/********************写数据**********************/
//写数据子程序
//
/************************************************/
void send_data(unsigned char dat)
{
chek_busy();
SETB(CONTROL,RS); //RS = 1;
CLRB(CONTROL,RW); //RW = 0;
DATA = dat;
SETB(CONTROL,E); //E = 1;
CLRB(CONTROL,E); //E = 0;
}
/********************初始化**********************/
//复位、通讯方式选择
/************************************************/
void lcd_init(void)
{
SETB(CONTROL,RST);//复位RST=1
SETB(CONTROL,PSB); //通讯方式为并口PSB = 1
send_com(0x30);//功能设置,一次送8位数据,基本指令集
send_com(0x0C);//0000,1100 整体显示,游标off,游标位置off
send_com(0x01);//0000,0001 清DDRAM
send_com(0x02);//0000,0010 DDRAM地址归位
send_com(0x80);//1000,0000 设定DDRAM 7位地址000,0000到地址计数器AC
}
/*******************************************************************/
// 设置显示位置 xpos(1~16),tpos(1~4)
/*******************************************************************/
void set_xy(unsigned char xpos,unsigned char ypos)
{
switch(ypos)
{
case 1:
send_com(0X80|xpos);break;
case 2:
send_com(0X90|xpos);break;
case 3:
send_com(0X88|xpos);break;
case 4:
send_com(0X98|xpos);break;
default:break;
}
}
/*******************************************************************/
// 在指定位置显示字符串
/*******************************************************************/
void print(unsigned char x,unsigned char y,char* str)
{
unsigned char lcd_temp;
set_xy(x,y);
lcd_temp=*str;
while(lcd_temp != 0x00)
{
send_data(lcd_temp);
lcd_temp=*(++str);
}
}
/********************写字符串******************/
//写字符串子程序
//xpos1取0~7共八列,ypos1取0~3共四行。
/**********************************************/
void printstr(unsigned char xpos,unsigned char ypos,unsigned char
str[],unsigned char k)
{ unsigned char n;
switch (ypos)
{ case 1: xpos |= 0x80;break; //第一行
case 2: xpos |= 0x90;break; //第二行
case 3: xpos |= 0x88;break; //第三行
case 4: xpos |= 0x98;break; //第四行
default: break;
}
send_com(xpos);
//此处的Xpos已转换为LCM的显示寄存器实际地址
for(n=0;n < k;n++)
{
send_data(str[n]);//显示汉字时注意码值,连续两个码表示一个汉字
}
}
/********************清屏************************/
//清屏
/************************************************/
void clr(void)
{
print(0,1," ");
print(0,2," ");
print(0,3," ");
print(0,4," ");
}
/*********************延时***********************
/
//延时
/************************************************/
void delay_50us(uint t)
{
uchar j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
/********************清屏************************/
//键盘
/************************************************/
unsigned char Keyscan() //键盘扫描
{ char keynum;
unsigned char temp,Flag;
keynum=0;
temp=0xfe;
P1=temp; //令P1.0=0
if((P1&0xf0)!=0xf0)
{
delay_50us(10);
if((P1&0xf0)!=0xf0) //确认是否按下
{
Flag=1;
switch(P1)
{
case 0xee:keynum=12;break;//对应显示"11"
case 0xde:keynum=11;break;//对应显示"10"
case 0xbe:keynum=10;break;//对应显示"9"
}
}
}
temp=0xfd;
P1=temp;//令P1.1=0
if((P1&0xf0)!=0xf0)
{
delay_50us(100);
if((P1&0xf0)!=0xf0) //确认是否按下
{ Flag=1;
switch(P1)
{
case 0xed:keynum=9;break;//对应显示"8"
case 0xdd:keynum=8;break;//对应显示"7"
case 0xbd:keynum=7;break;//对应显示"6"
}
}
}
temp=0xfb;
P1=temp;//令P1.2=0
if((P1&0xf0)!=0xf0)
{
delay_50us(1000);
if((P1&0xf0)!=0xf0) //确认是否按下
{ Flag=1;
switch(P1)
{
case 0xeb:keynum=6;break;//对应显示"5"
case 0xdb:keynum=5;break;//对应显示"4"
case 0xbb:keynum=4;break;//对应显示"3"
}
}
}
temp=0xf7;
P1=temp;//令P1.3=0
if((P1&0xf0)!=0xf0)
{
delay_50us(1000);
if((P1&0xf0)!=0xf0) //确认是否按下
{ Flag=1;
switch(P1)
{
case 0xe7:keynum=3;break;//对应显示"2"
case 0xd7:keynum=2;break;//对应显示"1"
case 0xb7:keynum=1;break;//对应显示"0"
}
}
}
if(key1==0) { delay_50us(10);
if(key1==0){keynum=13;} }
if(key2==0) { delay_50us(10);
if(key2==0){keynum=14;} } //
if(key3==0) { delay_50us(10);
if(key3==0){keynum=15;} }
if(key4==0) { delay_50us(10);
if(key4==0){keynum=16;} }
if(key5==0) { delay_50us(10);
if(key5==0){keynum=17;} }
if(key6==0) { delay_50us(10);
if(key6==0){keynum=18;} }
return(keynum);
}
void shux1(uchar tmp,uchar a,uchar b)
{
switch(tmp)
{
case 1: {print(a,b,"0");};break;//对应
显示"0"
case 2: {print(a,b,"1");};break;//对应显示"1"
case 3: {print(a,b,"2");};break;//对应显示"2"
case 4: {print(a,b,"3");};break;//对应显示"3"
case 5: {print(a,b,"4");};break;//对应显示"4"
case 6: {print(a,b,"5");};break;//对应显示"5"
case 7: {print(a,b,"6");};break;//对应显示"6"
case 8: {print(a,b,"7");};break;//对应显示"7"
case 9: {print(a,b,"8");};break;//对应显示"8"
case 10: {print(a,b,"9");};break;//对应显示"9"
}
}
int cifang(uchar j) //10的次方
{ int s=1;
uchar i;
for(i=0;i
}
float cifang2(uchar j) //0.1的次方
{ float s=1;
uchar i;
for(i=0;i
}
void xianshi (float s,uchar a1,uchar b1)
{ uchar k,pd=1,su,aa,bb;
long int j,s1,s2;
char i;
aa=a1;bb=b1;k=0;
if(s<0.0){s=s*(-1);print(aa,bb,"-");}
s1=s*1000;
s1=s1/1000;
if(s<1.0){print(aa,bb,"0");
aa++;}else{
while(pd==1)
{
j=cifang(k);
su=s1/j%10;
if(su==0){ if(s
k++;
}
k=k-2;
for(i=k;i>-1;i--)
{
j=cifang(i);
su=s1/j%10;
su++;shux1(su,aa,bb);
aa++;
}
}
print(aa,bb,".");
aa++;
s1=s*1000;
s1=s1/100;
s1=s1%10+1;
shux1(s1,aa,bb);
aa++;
s1=s*1000;
s1=s1/10;
s1=s1%10+1;
shux1(s1,aa,bb);
}
void xianshi2 (float s,uchar a1,uchar b1,uchar d)
{ uchar k,pd=1,su,aa,bb;
long int j,s1,s2,s3;
char i;
aa=a1;bb=b1;k=0;
if(s<0.0){s=s*(-1);print(aa,bb,"-");}
s1=s*1000;
s1=s1/1000;
if(s<1.0){print(aa,bb,"0");
aa++;}else{
while(pd==1)
{
j=cifang(k);
su=s1/j%10;
if(su==0){ if(s
k++;
}
k=k-2;
for(i=k;i>-1;i--)
{
j=cifang(i);
su=s1/j%10;
su++;
shux1(su,aa,bb);
aa++;
}
}
if(d==1){
print(aa,bb,".");
aa++;
s1=s*10000;
s1=s1/1000;
s1=s1%10+1;
s2=s*10000;
s2=s2/100;
s2=s2%10+1;
s3=s*10000;
s3=s3/10;
s3=s3%10+1;
if((s1!=1)||(s2!=1)){
shux1(s1,aa,bb);
aa++; }
if(s2!=1){
shux1(s2,aa,bb); aa++;
}
if(s3!=1){
// shux1(s3,aa,bb);
}
}
}
void mas(float s)
{uchar tmp1;
float a;
clr();
a=s;
xianshi(a,0,1);
print(0,2,"sin cos log");
print(0,3,"sinh exp tan");
tmp1=0;
while(tmp1==0){
tmp1 = Keyscan();
while(Keyscan()>0);
if(tmp1>8){tmp1=0;}
}
shux1(tmp1,0,4);
switch(tmp1)
{
case 1: {a=cos(s);}break;//对应显示"cos"
case 2: {a=sin(s);}break;//对应显示"sin
"
case 3: {a=log(s);}break;//对应显示"log"
case 4: {a=sinh(s);}break;//对应显示"sinh"
case 5: {a=cosh(s);}break;//对应显示"cosh"
case 6: {a=tan(s);}break;//对应显示"tan"
}
while(tmp1!=12){tmp1 = Keyscan();
while(Keyscan()>0);}
clr();
print(0,1,"结果");
xianshi(a,0,2);
tmp1=0;
while(tmp1==0){tmp1 = Keyscan();
while(Keyscan()>0);}
ql=1;
}
float shuzi(uchar a,uchar b)
{ uchar d=0,tmp=0,biao=1,b2=0,aa,bb;
float l1=0,l=0,b21=0;
biao=1;aa=a;
while(biao==1){
tmp = Keyscan();
while(Keyscan()>0);
if(tmp<11&&tmp>0)
{ t=1;
if(d==0)
{l=tmp-1;
l1=l1*10+l;
//shux1(tmp,aa,b);
aa++;
xianshi2(l1,0,b,d);
}
if(d==1)
{
if(b2<3){ l=tmp-1;
b21=cifang2(b2);
l1=l1+l*b21;
xianshi2(l1,0,b,d);
//shux1(tmp,aa,b);
aa++;
b2++; }
}
}
if(tmp==11) {
//if(l1==0){print(a,b,"0");}
if(d==0){
b2=1;
print(aa,b,".");
aa++;
d=1;
}
}
if(tmp==12){biao=0;}
if(tmp>14&&tmp<19) {biao=0;
switch(tmp)
{
case 15: {fu=1;}break;//对应显示"+"
case 16: {fu=2;}break;//对应显示"-"
case 17: {fu=3;}break;//对应显示"*"
case 18: {fu=4;}break;//对应显示"/"
}
}
if(tmp==13){ql=1;m2=1;biao=0;}
if(tmp==14){
if((t==1)&&(l1-l>=0)) {
if(d==0)
{
l1=l1-l;
l1=l1*0.1;
print(0,b," ");
xianshi2(l1,0,b,d);
aa--;
}
if(d==1)
{
l1=l1-l*b21;
print(0,b," ");
xianshi2(l1,0,b,d);
b2--;
aa--;
}
t=0;
}
}
}
return l1;
}
/**************主函数***********************
/
void main ()
{
uchar biao=1,f,tmp;
float l=0,l2,l3;
lcd_init(); //设定液晶工作方式
m=2;
while(1){
clr();m2=0;
ql=0; t=1;
l2=shuzi(0,1);
if(m2==1){mas(l2);ql=1;}
if(ql==0){
switch(fu)
{
case 1: {print(0,2,"+");}break;//对应显示"+"
case 2: {print(0,2,"-");}break;//对应显示"-"
case 3: {print(0,2,"*");}break;//对应显示"*"
case 4: {print(0,2,"/");}break;//对应显示"/"
}
t=1;
l=shuzi(0,3);
}
if(ql==0){
switch(fu)
{
case 1: l3=l+l2;break;//对应显示"+"
case 2: {
if(l2
}else{l3=l2-l;}
};break;//对应显示"-"
case 3: l3=l2*l;break;//对应显示"*"
case 4: l3=l2/l;break;//对应显示"/"
}
if(f==1){print(0,4,"= -");
f=0;
}else{print(0,4,"=");}
xianshi(l3,2,4);tmp=0;
while(tmp==0){tmp = Keyscan();
while(Keyscan()>0);}
}
}
}