|
单片机计算器仿真图:
源代码:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit leden=P2^7;
- sbit rs=P2^5;
- sbit wr=P2^6;
- //sbit busy=P0^7;
- uchar i,j,num,num_1,temp;
- long a,b, c;
- float a_c,b_c;
- uchar flag,fuhao;
- uchar code table[]={7,8,9,0,
- 4,5,6,0,
- 1,2,3,0};
- uchar code table1[]={7,8,9,0x2f-0x30,
- 4,5,6,0x2a-0x30,
- 1,2,3,0x2d-0x30,
- 0x01-0x30,0,0x3d-0x30,0x2b-0x30};
- void delay(uchar n)
- {
- uchar i,j;
- for(i=n;i>0;i--)
- for(j=200;j>0;j--) ;
- }
- /*
- void check()
- {
- do{P0=0xff;
- rs=0;wr=1;leden=0;delay(5);leden=1;}while(busy==1);
- }
- */
- void write(uint w)
- {
- rs=0;
- wr=0;
- leden=0;
- P0=w;
- leden=1;
- delay(10);//check();
- leden=0;
- }
- void write_data(uint q)
- {
- wr=0;
- leden=0;
- P0=q;
- leden=1;
- delay(10);
- leden=0;
- }
- void init()
- {
- num=1;
- leden=1;
- write(0x38);
- write(0x08);
- write(0x01);
- write(0x06);
- write(0x0f);
- num_1=0;
- i=0;j=0;a=0;b=0;c=0;flag=0;fuhao=0;
- }
- void keyscan()
- {
- P1=0Xfe;
- if(P1!=0xfe)
- {
- delay(10);
- if(P1!=0xfe)
- {
- temp=P1&0xf0;
- switch(temp)
- {
- case 0xe0:num=0;break;
- case 0xd0:num=1;break;
- case 0xb0:num=2;break;
- case 0x70:num=3;break;
- }
- }
- while(P1!=0xfe);
- if(num==0||num==2||num==1)
- {
- if(j!=0)
- {
- write(0x01);
- j=0;
- }
- if(flag==0)
- {
- a=a*10+table[num];
- }
- else
- {
- b=b*10+table[num];
- }
- }
- else
- {
- flag=1;
- fuhao=4;//????;
- }
- i=table1[num];
- write_data(0x30+i);
- }
- P1=0xfd;
- if(P1!=0xfd)
- {
- delay(10);
- if(P1!=0xfd)
- {
- temp=P1&0xf0;
- switch(temp)
- {
- case 0xe0:num=4;break;
- case 0xd0:num=5;break;
- case 0xb0:num=6;break;
- case 0x70:num=7;break;
- }
- }
- while(P1!=0xfd);
- if(num==4||num==5||num==6)
- {
- if(j!=0)
- {
- write(0x01);
- j=0;
- }
- if(flag==0)
- {
- a=a*10+table[num];
- }
- else
- {
- b=b*10+table[num];
- }
- }
- else
- {
- flag=1;
- fuhao=3;//????;
- }
- i=table1[num];
- write_data(0x30+i);
- }
- P1=0xfb;
- if(P1!=0xfb)
- {
- delay(10);
- if(P1!=0xfb)
- {
- temp=P1&0xf0;
- switch(temp)
- {
- case 0xe0:num=8;break;
- case 0xd0:num=9;break;
- case 0xb0:num=10;break;
- case 0x70:num=11;break;
- }
- }
- while(P1!=0xfb);
- if(num==8||num==9||num==10)
- {
- if(j!=0)
- {
- write(0x01);
- j=0;
- }
- if(flag==0)
- {
- a=a*10+table[num];
- }
- else
- {
- b=b*10+table[num];
- }
- }
- else if(num==11)
- {
- flag=1;
- fuhao=2;//jian???;
- }
- i=table1[num];
- write_data(0x30+i);
- }
- P1=0xf7;
- if(P1!=0xf7)
- {
- delay(10);
- if(P1!=0xf7)
- {
- temp=P1&0xf0;
- switch(temp)
- {
- case 0xe0:num=12;break;
- case 0xd0:num=13;break;
- case 0xb0:num=14;break;
- case 0x70:num=15;break;
- }
- }
- while(P1!=0xf7);
- switch(num)
- {
- case 12:{write(0x01);a=0;b=0;flag=0;fuhao=0;}break;
- case 13:
- {
- if(flag==0)
- {a=a/10;write_data(0x30);}
- else if(flag==1)
- {b=b*10;write_data(0x30);}
- } break;
- case 14: {j=1;
- if(fuhao==1)
- {write(0x80+0x4f);write(0x04);c=a+b;
- while(c!=0)
- {
- write_data(0x30+c%10);
- c=c/10;
- }
- write_data(0x3d);a=0;b=0;flag=0;fuhao=0;}
- else if(fuhao==2)
- {write(0x80+0x4b);write(0x04);
- if(a-b>0)
- {c=a-b;}
- else { c=b-a; }
- while(c!=0)
- {
- write_data(0x30+c%10);
- c=c/10;
- }
- if(a-b<0)
- write_data(0x2d);write_data(0x3d);a=0;b=1;flag=0;fuhao=0;}
- else if(fuhao==3)
- {write(0x80+0x4f);write(0x04);c=a*b;
- while(c!=0)
- {
- write_data(0x30+c%10);
- c=c/10;
- }
- write_data(0x3d);a=0;b=0;flag=0;fuhao=0;}
- else if(fuhao==4)
- {
- write(0x80+0x4f);write(0x04); i=0;
- c=a/b/c;
- while(c!=0)
- {
- write_data(0x30+c%10);
- c=c/10;
- i++;
- if(i==3)
- write(0x2e);
- }
- if(a/b<=0)
- write_data(0x30);write_data(0x3d);a=0;b=0;flag=0;fuhao=0;
- }
- } break;
- case 15:{write_data(0x30+table1[num]);flag=1;fuhao=1;} break;
- }
- }
- P1=0xf0;
- }
- void main()
- {
- init();
- while(1)
- {
- keyscan()
- }
- }
复制代码 |
评分
-
查看全部评分
|