您好,欢迎访问三七文档
#include<reg52.h>#include<intrins.h>#defineucharunsignedchar#defineuintunsignedintsbitOE=P2^7;sbitclk=P2^2;sbitEOC=P2^1;sbitSTART=P2^0;sbitsclk=P2^5;//使能//串行同步时钟sbitsid=P2^4;//1读0写//串行数据口sbitcs=P2^3;//1数据0命令//串行片选sbitled=P2^6;//超重报警指示灯ucharcodedis1[]={"毕业设计"};ucharcodeaa[]={"."};ucharcodedan[]={"元"};ucharcodejia[]={"单价:"};ucharcodemg1[]={"重量:"};ucharcodezjia[]={"价格:"};ucharcodezji[]={"总计:"};ucharcodekg[]={"kg"};ucharcodea1[]={"毕业设计"};ucharcodea2[]={"作品;"};ucharcodea3[]={"作者;"};ucharcodea4[]={"欢迎使用"};ucharkey;uchara;uchars;//固定单价标志uchardanjia;bitp=0;uchark,shiwei,gewei,jiao,feng;uintZmoney;money,mg,mg3;voiddelay(uintz)//ms{uintx,y;for(x=z;x>0;x--);for(y=110;y>0;y--);}voidwrite_cmd(uchardat)//写指令,分为三个字节{uchari;ucharcom;com=0xf8;//第一字节cs=1;sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}com=dat;//第二字节****0000com&=0xf0;//sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}com=dat;//第三字节移位后****0000com<<=4;//sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}cs=0;delay(10);}voidwrite_dat(uchardat)//写数据,同指令{uchari;ucharcom;com=0xfa;cs=1;//sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}com=dat;com&=0xf0;//sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}com=dat;com<<=4;//sclk=0;for(i=0;i<8;i++){sid=(bit)(com&0x80);sclk=0;sclk=1;com=com<<1;}cs=0;delay(10);}voidlcd_init(){//lcd_psb=1;write_cmd(0x30);delay(10);write_cmd(0x0C);delay(10);write_cmd(0x01);delay(10);}voiddisplay(){uchari;write_cmd(0x82);//第一行1for(i=0;i<8;i++){write_dat(a1[i]);delay(5);}write_cmd(0x90);//第2行1for(i=0;i<12;i++){write_dat(a2[i]);delay(5);}write_cmd(0x88);//第3行1for(i=0;i<16;i++){write_dat(a3[i]);delay(5);}write_cmd(0x9a);//第4行1for(i=0;i<8;i++){write_dat(a4[i]);delay(5);}}voiddisplay6(){ucharzshiwei,zgewei,zjiao,j;write_cmd(0x80);//第4行for(j=0;j<6;j++){write_dat(zji[j]);delay(5);}zshiwei=Zmoney/10000;zgewei=Zmoney%10000/1000;zjiao=Zmoney%10000%1000/100;write_cmd(0x83);write_dat(0x30+zshiwei);write_dat(0x30+zgewei);write_dat(aa[0]);write_dat(0x30+zjiao);write_dat(dan[0]);write_dat(dan[1]);}voiddisplay1(uintkey)//保存键值{if(k==0){gewei=key;}if(k==1){jiao=key;}//if(k==3){feng=key;}k++;if(k==2)k=0;}voiddanjiaa()//单价显示{ucharj;write_cmd(0x88);for(j=0;j<6;j++){write_dat(jia[j]);delay(5);}write_cmd(0x8b);//write_dat(0x30+shiwei);write_dat(0x30+gewei);write_dat(aa[0]);write_dat(0x30+jiao);//write_dat(0x30+feng);write_cmd(0x8e);write_dat(dan[0]);write_dat(dan[1]);}voiddisplay2(){ucharbai,shi,ge,j;floatbb;write_cmd(0x90);//第2行1for(j=0;j<6;j++){write_dat(mg1[j]);delay(5);}write_cmd(0x98);//第4行for(j=0;j<6;j++){write_dat(zjia[j]);delay(5);}write_cmd(0x93);bb=500/255;if(a<7){mg=0;}else{mg=(a-7)*1.96;if(p==0){mg3=mg;}if(p==1){mg=mg-mg3;if(mg<3)mg=0;}//去皮}//write_cmd(0x9b);扩大100if(mg>300){led=1;}else{led=0;}bai=mg/100;shi=mg%100/10;ge=mg%10;write_dat(0x30+bai);write_dat(aa[0]);write_dat(0x30+shi);write_dat(0x30+ge);write_dat(kg[0]);write_dat(kg[1]);}voiddisplay4()//固定显示{ucharshi,ge,j;write_cmd(0x88);for(j=0;j<6;j++){write_dat(jia[j]);delay(5);}shi=danjia%100/10;ge=danjia%10;write_cmd(0x8b);write_dat(0x30+shi);write_dat(aa[0]);write_dat(0x30+ge);//write_dat(0x30+feng);write_cmd(0x8e);write_dat(dan[0]);write_dat(dan[1]);}voiddisplay3()//disihang{ucharzshiwei,zgewei,zjiao;money=mg*(gewei*10+jiao);//kuoda10Zmoney+=money;//累加zshiwei=money/10000;zgewei=money%10000/1000;zjiao=money%10000%1000/100;write_cmd(0x9b);write_dat(0x30+zshiwei);write_dat(0x30+zgewei);write_dat(aa[0]);write_dat(0x30+zjiao);write_dat(dan[0]);write_dat(dan[1]);}voidad(){START=0;START=1;START=0;//启动AD转换。while(EOC==0);//eoc为0表示正在转化OE=1;a=P1;display2();OE=0;}voidkeyscan()//键盘扫描{uchartemp,DELETE;P0=0xfe;temp=P0;temp=temp&0xf0;if(temp!=0xf0){delay(10);//消抖temp=P0;temp=temp&0xf0;if(temp!=0xf0)//有键按下{temp=P0;switch(temp){case0xee:danjia=20;gewei=2;jiao=0;display4();DELETE=1;s=1;break;//ccase0xde:key=8;DELETE=0;s=0;break;case0xbe:key=4;DELETE=0;s=0;break;case0x7e:key=0;DELETE=0;s=0;break;}while(temp!=0xf0)//等待释放{temp=P0;temp=temp&0xf0;}if(DELETE==0){display1(key);}}}P0=0xfd;temp=P0;temp=temp&0xf0;if(temp!=0xf0){delay(10);//消抖temp=P0;temp=temp&0xf0;if(temp!=0xf0)//有键按下{temp=P0;switch(temp){case0xed:danjia=30;gewei=3;jiao=0;display4();DELETE=1;s=1;break;//dcase0xdd:key=9;DELETE=0;s=0;break;case0xbd:key=5;DELETE=0;s=0;break;case0x7d:key=1;DELETE=0;s=0;break;}while(temp!=0xf0)//等待释放{temp=P0;temp=temp&0xf0;}if(DELETE==0){display1(key);}}}P0=0xfb;temp=P0;temp=temp&0xf0;if(temp!=0xf0){delay(10);//消抖temp=P0;temp=temp&0xf0;if(temp!=0xf0)//有键按下{temp=P0;switch(temp){case0xeb:p=~p;DELETE=1;s=1;break;//ecase0xdb:shiwei=0;gewei=0;jiao=0;DELETE=1;k=0;danjia=0;s=0;break;//acase0xbb:key=6;DELETE=0;s=0;break;case0x7b:key=2;DELE
本文标题:电子秤51程序
链接地址:https://www.777doc.com/doc-7320337 .html