電子秤是基于STC89C51單片機,以及HX711稱重模塊,利用單片機控制A/D轉換,屏幕數據顯示,鍵盤實時輸入,設計方案能夠易于各種場合使用。
單片機源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <string.h>
- #include "main.h"
- #include "HX711.h"
- #include "eeprom52.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned long HX711_Buffer = 0;
- unsigned long Weight_Maopi = 0;
- unsigned long Weight_Maopi_0 = 0;
- long Weight_Shiwu = 0;
- unsigned int qupi=0;
- unsigned char p=0;
- //鍵盤處理變量
- unsigned char keycode;
- unsigned char key_press_num=0;
- uint GapValue,GapValue1;
- bit flag_cz=0;
- uchar code LEDData[]={0x5F,0x44,0x9D,0xD5,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B}; //數碼管顯示碼表
- //定義標識
- volatile bit FlagTest = 0; //定時測試標志,每0.5秒置位,測完清0
- volatile bit FlagKeyPress = 0; //有鍵按下標志,處理完畢清0
- //校準參數
- //因為不同的傳感器特性曲線不是很一致,因此,每一個傳感器需要矯正這里這個參數才能使測量值很準確。
- //當發現測試出來的重量偏大時,增加該數值。
- //如果測試出來的重量偏小時,減小改數值。
- //該值可以為小數
- //#define GapValue 349
- sbit LED=P1^1;
- sbit ROW1=P3^0;
- sbit ROW2=P3^1;
- sbit ROW3=P3^2;
- sbit ROW4=P3^3;
- sbit DIAN = P0^5; //小數點
- volatile bit ClearWeighFlag = 0; //傳感器調零標志位,清除0漂
- /******************把數據保存到單片機內部eeprom中******************/
- void write_eeprom()
- {
- SectorErase(0x2000);
- GapValue1=GapValue&0x00ff;
- byte_write(0x2000, GapValue1);
- GapValue1=(GapValue&0xff00)>>8;
- byte_write(0x2001, GapValue1);
- byte_write(0x2060, a_a);
- }
- /******************把數據從單片機內部eeprom中讀出來*****************/
- void read_eeprom()
- {
- GapValue = byte_read(0x2001);
- GapValue = (GapValue<<8)|byte_read(0x2000);
- a_a = byte_read(0x2060);
- }
- /**************開機自檢eeprom初始化*****************/
- void init_eeprom()
- {
- read_eeprom(); //先讀
- if(a_a != 1) //新的單片機初始單片機內問eeprom
- {
- GapValue = 3500;
- a_a = 1;
- write_eeprom(); //保存數據
- }
- }
- /*****顯示開機初始化等待畫面*****/
- void Disp_init(void)
- {
- P0 = ~0x80; //顯示----
- P2 = 0xBF; //依次打開各位
- Delay_ms(1); //延時
- P2 = 0xEF;
- Delay_ms(1);
- P2 = 0xFB;
- Delay_ms(1);
- P2 = 0xFE;
- Delay_ms(1);
- P2 = 0xFF; //關閉顯示
- }
- //顯示重量,單位kg,兩位整數,三位小數
- void Display_Weight()
- {
- P0 = ~LEDData[Weight_Shiwu%10]; //
- P2 = 0xBF; //打開位
- Delay_ms(1); //延時
- P2 = 0xff; //關閉顯示
- P0=~LEDData[Weight_Shiwu%100/10]; //顯示個位
- P2 = 0xEF;
- Delay_ms(1);
- P2 = 0xff; //關閉顯示
- P0 =~LEDData[Weight_Shiwu%1000/100]; //顯示十位
- P2 = 0xFB;
- Delay_ms(1);
- P2 = 0xff; //關閉顯示
- P0 =~LEDData[Weight_Shiwu%10000/1000]; //顯示百位
- DIAN = 0; //顯示小數點
- P2 = 0xFE;
- Delay_ms(1);
- P2 = 0xff; //關閉顯示
- }
- //定時器0初始化
- void Timer0_Init()
- {
- ET0 = 1; //允許定時器0中斷
- TMOD = 1; //定時器工作方式選擇
- TL0 = 0xb0;
- TH0 = 0x3c; //定時器賦予初值
- TR0 = 1; //啟動定時器
- }
- //定時器0中斷
- void Timer0_ISR (void) interrupt 1 using 0
- {
- uchar Counter;
- TL0 = 0xb0;
- TH0 = 0x3c; //定時器賦予初值
- //每0.5秒鐘刷新重量
- Counter ++;
- if (Counter >= 10)
- {
- FlagTest = 1;
- Counter = 0;
- }
- }
- //按鍵響應程序,參數是鍵值
- //返回鍵值:
- // 7 8 9 10(清0)
- // 4 5 6 11(刪除)
- // 1 2 3 12(未定義)
- // 14(未定義) 0 15(.) 13(確定價格)
- void KeyPress()
- {
- if(ROW1==0) //清零鍵
- {
- Display_Weight();
- if(ROW1==0)
- {
- if(qupi==0)
- qupi=Weight_Shiwu;
- else
- qupi=0;
- Buzzer=0;
- for(p=0;p<10;p++)
- Display_Weight();
- Buzzer=1;
- while(ROW1==0) Display_Weight();
- }
- }
- if(ROW2==0) //加
- {
- Display_Weight();
- if(ROW2==0)
- {
- while(!ROW2)
- {
- key_press_num++;
- if(key_press_num>=100)
- {
- key_press_num=0;
- while(!ROW2)
- {
- if(GapValue<10000)
- GapValue+=10;
- Buzzer=0;
- for(p=0;p<2;p++)
- Display_Weight();
- Buzzer=1;
- for(p=0;p<2;p++)
- Display_Weight();
- Get_Weight();
- }
- }
- Display_Weight();
- Delay_ms(8);
- }
- if(key_press_num!=0)
- {
- key_press_num=0;
- if(GapValue<10000)
- GapValue++;
- Buzzer=0;
- for(p=0;p<10;p++)
- Display_Weight();
- Buzzer=1;
- }
- write_eeprom();
- }
- }
- if(ROW3==0) //減
- {
- Display_Weight();
- if(ROW3==0)
- {
- while(!ROW3)
- {
- key_press_num++;
- if(key_press_num>=100)
- {
- key_press_num=0;
- while(!ROW3)
- {
- if(GapValue>1)
- GapValue-=10;
- Buzzer=0;
- for(p=0;p<2;p++)
- Display_Weight();
- Buzzer=1;
- for(p=0;p<2;p++)
- Display_Weight();
- Get_Weight();
- }
- }
- Display_Weight();
- Delay_ms(8);
- }
- if(key_press_num!=0)
- {
- key_press_num=0;
- if(GapValue>1)
- GapValue--;
- Buzzer=0;
- for(p=0;p<10;p++)
- Display_Weight();
- Buzzer=1;
- }
- write_eeprom(); //保存數?
- }
- }
- }
- //****************************************************
- //主函數
- //****************************************************
- void main()
- {
- init_eeprom(); //開始初始化保存的數據
- EA = 0;
- Timer0_Init();
- //初中始化完成,開斷
- EA = 1;
- Get_Maopi();
- while(1)
- {
- //每0.5秒稱重一次
- if (FlagTest==1)
- {
- Get_Weight();
- FlagTest = 0;
- }
- if(flag_cz==1)
- Disp_init();
- else
- Display_Weight();
- KeyPress();
- }
- }
- //****************************************************
- //稱重
- //****************************************************
- void Get_Weight()
- {
- Weight_Shiwu = HX711_Read();
- Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取凈重
-
- Weight_Shiwu = (unsigned int)((float)(Weight_Shiwu*10)/GapValue)-qupi; //計算實物的實際重量
- if(Weight_Shiwu >= 5000) //超重報警
- {
- Buzzer = !Buzzer;
- LED=!LED;
- flag_cz=1;
- }
- else
- {
- if(Weight_Shiwu==0)
- LED=0;
- else if(Weight_Shiwu>0)
- LED=1;
- Buzzer = 1;
- flag_cz=0;
- }
- }
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
1-程序5kg.rar
(65.57 KB, 下載次數: 129)
2020-12-5 13:34 上傳
點擊文件名下載附件
|