51單片機電子秤資料分享給51hei的朋友們
3具體要求
(1)能用簡易鍵盤設置單價,加重后能同時顯示重量、金額和單價;
(2)重量顯示:單位為公斤;最大稱重為9.999公斤,重量誤差不大于±0.005公斤;
(3)單價金額及總價金額顯示:單價金額和總價金額的單位為元,最大金額數值為9999.99元,總價金額誤差不大于0.01元;
(4)具有去皮功能和總額累加計算功能。
設計步驟:擬定設計方案,寫出必要的設計步驟和參數計算,畫出邏輯電路圖,仿真與分析、寫出課程設計說明書。
4完成后應上交的材料
1.一張整體電路原理圖。
2.課程設計說明書。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (50.75 KB, 下載次數: 80)
下載附件
2018-4-20 01:38 上傳
原理圖(工程文件可在本帖附件中下載):
0.jpg (96.96 KB, 下載次數: 69)
下載附件
2018-4-20 01:39 上傳
實用電子稱單片機源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- extern void ADC();
- extern uint qian,bai,shi,ge;
- uchar table[]="P:000.00";
- uchar code table1[]="$:0000.00RMB";
- uchar code table2[]="W:0.000kg";
- uchar tab[]="welcome to use!" ;
- sbit lcden=P3^3; //LCD1602的en引腳段
- sbit lcdrs=P3^4;// rs引腳端
- sbit cs=P3^2; //ADC0804的片選信號
- uchar num;
- uint a,b,c,d,e;
- double weight,price,comulation,total,scale;
- uchar bt=0,key,clear,complete,sum,pot,temp,delete,flag,clean;
- void delay(uint z) //延時函數
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void write_com(uchar com)//寫指令的地址
- {
- lcdrs=0;//寫指令
- P0=com;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_data(uchar date) //寫數據
- {
- lcdrs=1;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void init() //LCD1602的初始化
- {
- lcden=0;
- write_com(0x38);//顯示模式設置
- write_com(0x0c); //開顯示,不顯示光標,光標不閃爍
- write_com(0x06);//當讀或寫一個字符后地址指針加1,且光標加1,整屏顯示不移動
- write_com(0x01); //清屏
- }
- void display(double a) //顯示價格 兩個變量分別為單價和重量
- {
- long tep; //定義中間變量
- uint a1,a2,a3,a4,a5,a6; //中間變量
- tep=(long)(a*100); //temp=temp*b相當于價格乘上重量
- a1=tep/100000; //千位上的值
- a2=tep%100000/10000; //百位上的值
- a3=tep%100000%10000/1000; //十位上的值
- a4=tep%100000%10000%1000/100; //個位上的值
- a5=tep%100000%10000%1000%100/10; //小數點后一位
- a6=tep%100000%10000%1000%100%10; //小數點后兩位
- write_com(0xc2); //寫入地址第二行第三個開始
- write_data(0x30+a1); //寫入數據由于LED的情況
- write_com(0xc3); //第四個開始
- write_data(0x30+a2);
- write_com(0xc4);
- write_data(0x30+a3);
- write_com(0xc5);
- write_data(0x30+a4);
- write_com(0xc6);
- write_data('.');
- write_com(0xc7);
- write_data(0x30+a5);
- write_com(0xc8);
- write_data(0x30+a6);
- }
- void keyscan() //鍵盤掃描
- {
- P2=0xfe;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0) //第一次確認
- {
- delay(10);
- if(temp!=0xf0) //第二次確認
- {
- temp=P2;
- switch(temp)
- {
- case 0xee:
- key=1;
- bt++;
- break;
- case 0xde:
- key=2;
- bt++;
- break;
- case 0xbe:
- key=3;
- bt++;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- }
- }
- P2=0xfd;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xed:
- key=4;
- bt++;
- break;
- case 0xdd:
- key=5;
- bt++;
- break;
- case 0xbd:
- key=6;
- bt++;
- break;
- case 0x7d:
- pot=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- }
- }
- P2=0xfb;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xeb:
- key=7;
- bt++;
- break;
- case 0xdb:
- key=8;
- bt++;
- break;
- case 0xbb:
- key=9;
- bt++;
- break;
- case 0x7b: //去皮
- delete=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- }
- }
- P2=0xf7;
- temp=P2;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P2;
- switch(temp)
- {
- case 0xe7: //計算
- complete=1;
- break;
- case 0xd7:
- key=0;
- bt++;
- break;
- case 0xb7: //清0
- clear=1;
- break;
- case 0x77: //累加
- sum=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P2;
- temp=temp&0xf0;
- }
- }
- }
- }
- void getprice()
- {
- if(bt<=5)
- {
- switch(bt)
- {
- case 1:
- a=key;
- break;
- case 2:
- b=key;
- break;
- case 3:
- c=key;
- break;
- case 4:
- d=key;
- break;
- case 5:
- e=key;
- break;
- }
- }
- price=a*100+b*10+c+d*0.1+e*0.01;//獲得價格
- }
- void showprice(double b)
- {
- long j;
- uint o,p,q,r,s;
- j=(long)(b*100);
- o=j/10000;
- p=j%10000/1000;
- q=j%10000%1000/100;
- r=j%10000%1000%100/10;
- s=j%10000%1000%100%10;
- if(bt<=5)
- {
- switch(bt)
- {
- case 1:
- write_com(0x80+2);
- write_data(0x30+o);
- break;
- case 2:
- write_com(0x80+3);
- write_data(0x30+p);
- break;
- case 3:
- write_com(0x80+4);
- write_data(0x30+q);
- break;
- case 4:
- write_com(0x80+6);
- write_data(0x30+r);
- break;
- case 5:
- write_com(0x80+7);
- write_data(0x30+s);
- break;
- }
- }
- }
- void getweight()
- {
- ADC(); //通過ADC獲得個十百的大小
- weight=qian+bai*0.1+shi*0.01+ge*0.001-0.625;
- }
- void showweight(double a)
- {
- long i;
- i=(long)(a*1000);
- write_com(0x80+10);
- write_data(0x30+i/1000);
- write_com(0x80+12); //顯示重量的地方從第一行第十一個開始
- write_data(0x30+(i%1000)/100);
- write_com(0x80+13);
- write_data(0x30+((i%1000)%100)/10);
- write_com(0x80+14);
- write_data(0x30+((i%1000)%100)%10);
- }
- void main()
- {
- init(); //初始化
- write_com(0x80);
- for(num=0;num<16;num++)
- {
- write_data(tab[num]);
- }
- delay(1000);
- write_com(0x80); //地址為第一行第一個
- for(num=0;num<8;num++) //在num為0到8的范圍內調用寫函數寫下字符
- {
- write_data(table[num]);
- delay(20);
- }
- write_com(0x80+8); //在第一行的第九個地址處寫下十個長度的字符
- for(num=0;num<9;num++)
- {
- write_data(table2[num]);
- delay(20);
- }
- write_com(0x80+0x40); //地址為第二行第一個
- for(num=0;num<13;num++) //在num為0~12時寫下字符
- {
- write_data(table1[num]);
- delay(20);
- }
- while(1) //程序運行時調keyscan函數進行顯示以及其他判斷
- {
- keyscan();
- getprice();
- getweight();
- if(clear==1)
- {
- write_com(0x80); //地址為第一行第一個
- for(num=0;num<8;num++) //在num為0到8的范圍內調用寫函數寫下字符
- {
- write_data(table[num]);
- delay(10);
- }
- write_com(0x80+8); //在第一行的第九個地址處寫下十個長度的字符
- for(num=0;num<9;num++)
- {
- write_data(table2[num]);
- delay(10);
- }
- write_com(0x80+0x40); //地址為第二行第一個
- for(num=0;num<13;num++) //在num為0~12時寫下字符
- {
- write_data(table1[num]);
- delay(10);
- }
- bt=0;
- key=0;
- price=0;
- weight=0;
- delete=0;
- sum=0;
- complete=0;
- comulation=0;
- total=0;
- scale=0;
- flag=0;
- pot=0;
- clean=1;
- clear=0;
- }
- if(pot==1)
- {
- write_com(0x80+5);
- write_data('.');
- pot=0;
- }
- if(sum==1)
- {
- write_com(0x80); //地址為第一行第一個
- for(num=0;num<8;num++) //在num為0到8的范圍內調用寫函數寫下字符
- {
- write_data(table[num]);
- delay(10);
- }
- write_com(0x80+8); //在第一行的第九個地址處寫下十個長度的字符
- for(num=0;num<9;num++)
- {
- write_data(table2[num]);
- delay(10);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
0.png (47.29 KB, 下載次數: 68)
下載附件
2018-4-20 01:38 上傳
所有資料51hei提供下載:
電子秤.zip
(207.97 KB, 下載次數: 157)
2018-4-19 16:15 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|