找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 5828|回復(fù): 1
收起左側(cè)

基于51單片機(jī)HX711的數(shù)碼電子秤Proteus仿真程序

[復(fù)制鏈接]
ID:436996 發(fā)表于 2020-2-15 15:26 | 顯示全部樓層 |閱讀模式
制作出來(lái)的實(shí)物圖如下:
0.jpg 0.jpg

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png

電子秤元件清單

1)    7*9萬(wàn)用板
2)    STC89C51單片機(jī)
3)    40腳IC座
4)    12MHz晶振
5)    30pF瓷片電容*2
6)    10uF電解電容
7)    100uF電解電容
8)    0.36四位一體共陽(yáng)數(shù)碼管
9)    5V有源蜂鳴器
10)    9012三極管*5
11)    5mm紅發(fā)紅LED
12)    按鍵*4
13)    10k電阻*3
14)    2.2k電阻*6
15)    HX711模塊
16)    4P單排母座
17)    6P單排母座
18)    壓力傳感器(含支架)
19)    自鎖開(kāi)關(guān)
20)    DC電源插座
21)    USB電源線
22)    導(dǎo)線
23)    焊錫

單片機(jī)源程序如下:
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <string.h>
  4. #include "main.h"
  5. #include "HX711.h"
  6. #include "eeprom52.h"

  7. #define uchar unsigned char
  8. #define uint  unsigned int

  9. unsigned long HX711_Buffer = 0;
  10. unsigned long Weight_Maopi = 0;
  11. unsigned long Weight_Maopi_0 = 0;
  12. long Weight_Shiwu = 0;
  13. unsigned int qupi=0;
  14. unsigned char p=0;
  15. //鍵盤(pán)處理變量
  16. unsigned char keycode;
  17. unsigned char key_press_num=0;

  18. uint GapValue,GapValue1;
  19. bit flag_cz=0;

  20. uchar code  LEDData[]={0x5F,0x44,0x9D,0xD5,0xC6,0xD3,0xDB,0x47,0xDF,0xD7,0xCF,0xDA,0x9B,0xDC,0x9B,0x8B}; //數(shù)碼管顯示碼表
  21. //定義標(biāo)識(shí)
  22. volatile bit FlagTest = 0;                //定時(shí)測(cè)試標(biāo)志,每0.5秒置位,測(cè)完清0
  23. volatile bit FlagKeyPress = 0;  //有鍵按下標(biāo)志,處理完畢清0
  24. //校準(zhǔn)參數(shù)
  25. //因?yàn)椴煌膫鞲衅魈匦郧不是很一致,因此,每一個(gè)傳感器需要矯正這里這個(gè)參數(shù)才能使測(cè)量值更準(zhǔn)確。
  26. //當(dāng)發(fā)現(xiàn)測(cè)試出來(lái)的重量偏大時(shí),增加該數(shù)值。
  27. //如果測(cè)試出來(lái)的重量偏小時(shí),減小改數(shù)值。
  28. //該值可以為小數(shù)
  29. sbit LED=P1^1;
  30. sbit ROW1=P3^0;
  31. sbit ROW2=P3^1;
  32. sbit ROW3=P3^2;
  33. sbit DIAN = P0^5;                        //小數(shù)點(diǎn)

  34. volatile bit ClearWeighFlag = 0; //傳感器調(diào)零標(biāo)志位,清除0漂

  35. /******************把數(shù)據(jù)保存到單片機(jī)內(nèi)部eeprom中******************/
  36. void write_eeprom()
  37. {
  38.         SectorErase(0x2000);
  39.         GapValue1=GapValue&0x00ff;
  40.         byte_write(0x2000, GapValue1);
  41.         GapValue1=(GapValue&0xff00)>>8;
  42.         byte_write(0x2001, GapValue1);
  43.         byte_write(0x2060, a_a);        
  44. }

  45. /******************把數(shù)據(jù)從單片機(jī)內(nèi)部eeprom中讀出來(lái)*****************/
  46. void read_eeprom()
  47. {
  48.         GapValue   = byte_read(0x2001);
  49.         GapValue   = (GapValue<<8)|byte_read(0x2000);
  50.         a_a      = byte_read(0x2060);
  51. }

  52. /**************開(kāi)機(jī)自檢eeprom初始化*****************/
  53. void init_eeprom()
  54. {
  55.         read_eeprom();                //先讀
  56.         if(a_a != 1)                //新的單片機(jī)初始單片機(jī)內(nèi)問(wèn)eeprom
  57.         {
  58.                 GapValue  = 3500;
  59.                 a_a = 1;
  60.                 write_eeprom();           //保存數(shù)據(jù)
  61.         }        
  62. }
  63. /*****顯示開(kāi)機(jī)初始化等待畫(huà)面*****/
  64. void Disp_init(void)   
  65. {
  66.         P0 = ~0x80;      //顯示----
  67.         P2 = 0xBF;                 //依次打開(kāi)各位
  68.         Delay_ms(1);                 //延時(shí)
  69.         P2 = 0xEF;
  70.         Delay_ms(1);   
  71.         P2 = 0xFB;
  72.         Delay_ms(1);
  73.         P2 = 0xFE;
  74.         Delay_ms(1);
  75.         P2 = 0xFF;         //關(guān)閉顯示
  76. }

  77. //顯示重量,單位kg,兩位整數(shù),三位小數(shù)
  78. void Display_Weight()
  79. {
  80.     P0 = ~LEDData[Weight_Shiwu%10];      //
  81.         P2 = 0xBF;                 //打開(kāi)位
  82.         Delay_ms(1);                 //延時(shí)
  83.         P2 = 0xff;         //關(guān)閉顯示
  84.         P0=~LEDData[Weight_Shiwu%100/10];    //顯示個(gè)位
  85.         P2 = 0xEF;
  86.         Delay_ms(1);
  87.         P2 = 0xff;         //關(guān)閉顯示
  88.         P0 =~LEDData[Weight_Shiwu%1000/100];    //顯示十位
  89.         P2 = 0xFB;
  90.         Delay_ms(1);
  91.         P2 = 0xff;         //關(guān)閉顯示
  92.         P0 =~LEDData[Weight_Shiwu%10000/1000];    //顯示百位
  93.         DIAN = 0;         //顯示小數(shù)點(diǎn)
  94.         P2 = 0xFE;
  95.         Delay_ms(1);
  96.         P2 = 0xff;         //關(guān)閉顯示
  97. }

  98. //定時(shí)器0初始化
  99. void Timer0_Init()
  100. {
  101.         ET0 = 1;        //允許定時(shí)器0中斷
  102.         TMOD = 1;       //定時(shí)器工作方式選擇
  103.         TL0 = 0xb0;     
  104.         TH0 = 0x3c;     //定時(shí)器賦予初值
  105.         TR0 = 1;        //啟動(dòng)定時(shí)器
  106. }

  107. //定時(shí)器0中斷
  108. void Timer0_ISR (void) interrupt 1 using 0
  109. {
  110. uchar Counter;
  111.         TL0 = 0xb0;
  112.         TH0 = 0x3c;     //定時(shí)器賦予初值

  113.         //每0.5秒鐘刷新重量
  114.     Counter ++;
  115.     if (Counter >= 10)
  116.     {
  117.        FlagTest = 1;
  118.            Counter = 0;
  119.     }
  120. }

  121. void KeyPress()
  122. {
  123.         if(ROW1==0)   //清零鍵
  124.         {
  125.                 Display_Weight();
  126.                 if(ROW1==0)
  127.                 {
  128.                         if(qupi==0)
  129.                         qupi=Weight_Shiwu;
  130.                         else
  131.                         qupi=0;
  132.                         Buzzer=0;
  133.                         for(p=0;p<10;p++)
  134.                         Display_Weight();
  135.                         Buzzer=1;        
  136.                         while(ROW1==0) Display_Weight();
  137.                 }
  138.         }
  139.         if(ROW2==0)           //加
  140.         {
  141.                 Display_Weight();
  142.                 if(ROW2==0)
  143.                 {
  144.                         while(!ROW2)
  145.                         {
  146.                                 key_press_num++;
  147.                                 if(key_press_num>=100)
  148.                                 {
  149.                                         key_press_num=0;
  150.                                         while(!ROW2)
  151.                                         {
  152.                                                 if(GapValue<10000)
  153.                                                 GapValue+=10;
  154.                                                 Buzzer=0;
  155.                                                 for(p=0;p<2;p++)
  156.                                                 Display_Weight();
  157.                                                 Buzzer=1;
  158.                                                 for(p=0;p<2;p++)
  159.                                                 Display_Weight();
  160.                                                 Get_Weight();
  161.                                         }
  162.                                 }
  163.                                 Display_Weight();
  164.                                 Delay_ms(8);
  165.                         }
  166.                         if(key_press_num!=0)
  167.                         {
  168.                                 key_press_num=0;
  169.                                 if(GapValue<10000)
  170.                                 GapValue++;
  171.                                 Buzzer=0;
  172.                                 for(p=0;p<10;p++)
  173.                                 Display_Weight();
  174.                                 Buzzer=1;
  175.                         }
  176.                         write_eeprom();               
  177.                 }
  178.         }
  179.         if(ROW3==0)  //減
  180.         {
  181.                 Display_Weight();
  182.                 if(ROW3==0)
  183.                 {
  184.                         while(!ROW3)
  185.                         {
  186.                                 key_press_num++;
  187.                                 if(key_press_num>=100)
  188.                                 {
  189.                                         key_press_num=0;
  190.                                         while(!ROW3)
  191.                                         {
  192.                                                 if(GapValue>1)
  193.                                                 GapValue-=10;
  194.                                                 Buzzer=0;
  195.                                                 for(p=0;p<2;p++)
  196.                                                 Display_Weight();
  197.                                                 Buzzer=1;
  198.                                                 for(p=0;p<2;p++)
  199.                                                 Display_Weight();
  200.                                                 Get_Weight();
  201.                                         }
  202.                                 }
  203.                                 Display_Weight();
  204.                                 Delay_ms(8);
  205.                         }
  206.                         if(key_press_num!=0)
  207.                         {
  208.                                 key_press_num=0;
  209.                                 if(GapValue>1)
  210.                                 GapValue--;
  211.                                 Buzzer=0;
  212.                                 for(p=0;p<10;p++)
  213.                                 Display_Weight();
  214.                                 Buzzer=1;
  215.                         }
  216.                         write_eeprom();                           //保存數(shù)?
  217.                 }
  218.         }
  219. }
  220. //****************************************************
  221. //主函數(shù)
  222. //****************************************************
  223. void main()
  224. {
  225.         init_eeprom();  //開(kāi)始初始化保存的數(shù)據(jù)
  226.    EA = 0;
  227.    Timer0_Init();
  228.    //初中始化完成,開(kāi)斷
  229.    EA = 1;
  230.    Get_Maopi();

  231.         while(1)
  232.         {
  233. //每0.5秒稱重一次
  234.                 if (FlagTest==1)
  235.                 {
  236.                         Get_Weight();
  237.                         FlagTest = 0;
  238.                 }
  239.                 if(flag_cz==1)
  240.                 Disp_init();
  241.                 else
  242.                 Display_Weight();         
  243.                 KeyPress();
  244.         }
  245. }
  246. //****************************************************
  247. //稱重
  248. //****************************************************
  249. void Get_Weight()
  250. {
  251.         Weight_Shiwu = HX711_Read();
  252.         Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                //獲取凈重
  253.         
  254.         Weight_Shiwu = (unsigned int)((float)(Weight_Shiwu*10)/GapValue)-qupi;         //計(jì)算實(shí)物的實(shí)際重量                                                                                                                                
  255.         if(Weight_Shiwu >= 10000)                //超重報(bào)警
  256.         {
  257.                 Buzzer = !Buzzer;        
  258.                 LED=!LED;
  259.                 flag_cz=1;
  260.         }
  261.         else
  262.         {
  263.                 if(Weight_Shiwu==0)
  264.                 LED=0;
  265.                 else if(Weight_Shiwu>0)
  266.                 LED=1;
  267.                 Buzzer = 1;
  268.                 flag_cz=0;
  269.         }
  270. }

  271. //****************************************************
  272. //獲取毛皮重量
  273. //****************************************************
  274. void Get_Maopi()
  275. {
  276.         unsigned char clear,f;
  277. mm:        Weight_Maopi_0 = HX711_Read();
  278.         for(clear=0;clear<10;clear++)
  279.         {
  280.                 Buzzer=1;
  281.                 LED=0;
  282.                 for(f=0;f<40;f++)
  283.                 Disp_init();
  284.                 LED=1;
  285.                 for(f=0;f<40;f++)
  286.                 Disp_init();        
  287.         }
  288.         Weight_Maopi = HX711_Read();
  289.         if((Weight_Maopi*10)/GapValue!=(Weight_Maopi_0*10)/GapValue)
  290.         goto mm;
  291.         Buzzer=0;
  292.         for(f=0;f<10;f++)
  293.         Disp_init();
  294.         Buzzer=1;
  295. }

  296. //****************************************************
  297. //MS延時(shí)函數(shù)(12M晶振下測(cè)試)
  298. //****************************************************
  299. void Delay_ms(unsigned int n)
  300. {
  301.         unsigned int  i,j;
  302.         for(i=0;i<n;i++)
  303.                 for(j=0;j<121;j++);
  304. }
復(fù)制代碼

所有資料51hei提供下載:
電子稱(數(shù)碼管).7z (3.52 MB, 下載次數(shù): 166)




評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:220599 發(fā)表于 2020-4-29 22:56 | 顯示全部樓層
有仿真的源程序嗎?
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表