1、基于 51 单片机的 lcd 1602 液晶显示的计算器/* 阿斌独家制作:计算器,1602 液晶显示 可计算 10 以下数加减乘除 可连续运算,最大显示结果数值 65536 */键盘设置:液晶初始显示:运算显示:主程序:#include#include #define uint unsigned int#define uchar unsigned charsbit lcden=P22;sbit lcdrw=P21;sbit lcdrs=P20;uchar num,temp,jia=0,jian=0,cheng=0,chu=0,qing=0;uint key,key1,shu;uchar fu
2、hao,flag1,flag=0;uchar table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x0;void delay(uint z)uint x,y;for(x=z;x0;x-)for(y=110;y0;y-);bit lcd_bz()bit result;lcdrs=0;lcdrw=1;lcden=1;_nop_();_nop_();_nop_();_nop_();result=(bit)(P0 lcden=0;return result;void write_com(uchar com)while(lcd_bz();lc
3、drs=0;lcden=0;lcdrw=0;P0=com;delay(5);lcden=1;delay(5);lcden=0;void write_date(uchar date)while(lcd_bz();lcdrs=1;lcden=0;lcdrw=0;P0=date;delay(5);lcden=1;delay(5);lcden=0;void lcd_init()lcden=0;write_com(0x38);write_com(0x0c);write_com(0x06);write_com(0x01);void keyscan()P3=0xfe;temp=P3;temp=tempwhi
4、le(temp!=0xf0)delay(5);temp=P3;temp=tempwhile(temp!=0xf0)temp=P3;switch(temp)case 0xee:key=1;num=0;break;case 0xde:key=2;num=0;break;case 0xbe:key=3;num=0;break;case 0x7e:num=1;break; /加号while(temp!=0xf0)temp=P3;temp=tempP3=0xfd;temp=P3;temp=tempwhile(temp!=0xf0)delay(5);temp=P3;temp=tempwhile(temp!
5、=0xf0)temp=P3;switch(temp)case 0xed:key=4;num=0;break;case 0xdd:key=5;num=0;break;case 0xbd:key=6;num=0;break;case 0x7d:num=2;break; /减号while(temp!=0xf0)temp=P3;temp=tempP3=0xfb;temp=P3;temp=tempwhile(temp!=0xf0)delay(5);temp=P3;temp=tempwhile(temp!=0xf0)temp=P3;switch(temp)case 0xeb:key=7;num=0;bre
6、ak;case 0xdb:key=8;num=0;break;case 0xbb:key=9;num=0;break;case 0x7b:num=3;break; /乘号while(temp!=0xf0)temp=P3;temp=tempP3=0xf7;temp=P3;temp=tempwhile(temp!=0xf0)delay(5);temp=P3;temp=tempwhile(temp!=0xf0)temp=P3;switch(temp)case 0xe7:key=0;num=0;break;case 0xd7:num=6;break; /清除case 0xb7:num=5;break;
7、 /等于号case 0x77:num=4;break; /除号while(temp!=0xf0)temp=P3;temp=tempvoid display0(uint shu)uint ge;ge=shu;write_date(0x30+ge);void display1(uint shu)uint shi,ge;shi=shu/10;ge=shu%10;write_date(0x30+shi);write_date(0x30+ge);void display2(uint shu)uint bai,shi,ge;bai=shu/100;shi=shu%100/10;ge=shu%10;writ
8、e_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);void display3(uint shu)uint qian,bai,shi,ge;qian=shu/1000;bai=shu%1000/100;shi=shu%100/10;ge=shu%10;write_date(0x30+qian);write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);void display4(uint shu)uint wan,qian,bai,shi,ge;wan=shu/10
9、000;qian=shu%10000/1000;bai=shu%1000/100;shi=shu%100/10;ge=shu%10;write_date(0x30+wan);write_date(0x30+qian);write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);void display_key1(uint result)if(flag1=1)write_com(0x01);flag1=0;shu=result;write_com(0x80);if(result=10void dis_key(result)if(re
10、sult=10void display_key(uint result)if(shu=10dis_key(result); void display_fuhao()switch(fuhao)case 1: write_date(0x2b);break;case 2: write_date(0x2d);break;case 3: write_date(0x2a);break;case 4: write_date(0x2f);break;fuhao=0;void fuhao_pan()if(flag1=1)flag1=0;write_com(0x01);write_com(0x80+1);disp
11、lay_fuhao();if(shu=10display_fuhao();flag=1;void fuhao_deng()write_com(0x80+0x40);write_date(0x3d);flag1=1;flag=0;void display_result(uint result)write_com(0x80+0x40+1);if(result=10void jia1()jia=1;jian=cheng=chu=qing=0;key1=key;fuhao=1;fuhao_pan();void jian1()jian=1;jia=cheng=chu=qing=0;key1=key;fu
12、hao=2;fuhao_pan();void cheng1()cheng=1;jia=jian=chu=qing=0;key1=key;fuhao=3;fuhao_pan();void chu1()chu=1;jia=jian=cheng=qing=0;key1=key;fuhao=4;fuhao_pan();void qing1()qing=1;jia=jian=cheng=chu=0;key1=key=0;void deng1()if(jia)key=key1+key;if(jian)key=key1-key;if(cheng)key=key1*key;if(chu)key=key1/ke
13、y;fuhao_deng();display_result(key);jia=jian=cheng=chu=qing=0;void deal()switch(num)case 0:switch(flag)case 0: display_key1(key);break;case 1: display_key(key);break; break; case 1: jia1();break;case 2: jian1();break;case 3: cheng1();break;case 4: chu1();break;case 5: deng1();break;case 6: qing1();break;void main()lcd_init();while(1)keyscan();deal();