找回密码
 注册

QQ登录

只需一步,快速开始

搜索

51单片机的电子称设计(可设浮动) 含代码仿真文件原理图

[复制链接]
coolice 发表于 2020-2-1 17:22:31 | 显示全部楼层 |阅读模式
电子秤主要由电源、称重传感器、A/D转换器、单片机、键盘/开关、LCD显示器等部分组成。主要技术指标为:称量范围0~15kg;分度值为0.005kg;精度等级Ⅲ级。仪器主要功能有自检、去皮、计价、单价设定、过载报警等。仪器若不进行称量操作,5分钟后自动进入休眠模式,降低电源消耗。
电子秤的构成
便携式电子秤硬件系统由应变式称重传感器、放大器、A/D转换器、单片机系统、键盘/开关、LCD显示器、打印机等组成
234316tgnhcrv78k7rqgxc.png 234044m30mww3limesqdze.png
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)

1.jpg

单片机源程序如下:
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <string.h>

  4. bit bdata flag_key;
  5. #include "main.h"
  6. #include "LCD1602.h"
  7. #include "HX711.h"
  8. #include "keyboard.h"
  9. #include "eeprom52.h"

  10. #define uchar unsigned char
  11. #define uint  unsigned int

  12. unsigned long HX711_Buffer = 0;
  13. unsigned long Weight_Maopi = 0;
  14. unsigned long Weight_Maopi_0 = 0;
  15. unsigned int qupi=0;
  16. long Weight_Shiwu = 0;
  17. //键盘处理变量
  18. unsigned char keycode;
  19. unsigned char DotPos;                                   //小数点标志及位置

  20. uint GapValue,GapValue1;

  21. unsigned char idata price;     //单价,长整型值,单位为分   
  22. unsigned char idata money;     //总价,长整型值,单位为分
  23. //定义标识
  24. volatile bit FlagTest = 0;                //定时测试标志,每0.5秒置位,测完清0
  25. volatile bit FlagKeyPress = 0;  //有键按下标志,处理完毕清0
  26. //校准参数
  27. //因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
  28. //当发现测试出来的重量偏大时,增加该数值。
  29. //如果测试出来的重量偏小时,减小改数值。
  30. //该值可以为小数
  31. //#define GapValue 349
  32. sbit LED=P1^1;

  33. volatile bit ClearWeighFlag = 0; //传感器调零标志位,清除0漂

  34. /******************把数据保存到单片机内部eeprom中******************/
  35. void write_eeprom()
  36. {
  37.         SectorErase(0x1000);
  38.         GapValue1=GapValue&0x00ff;
  39.         byte_write(0x2000, GapValue1);
  40.         GapValue1=(GapValue&0xff00)>>8;
  41.         byte_write(0x2001, GapValue1);
  42.         byte_write(0x2060, a_a);        
  43. }

  44. /******************把数据从单片机内部eeprom中读出来*****************/
  45. void read_eeprom()
  46. {
  47.         GapValue   = byte_read(0x2001);
  48.         GapValue   = (GapValue<<8)|byte_read(0x2000);
  49.         a_a      = byte_read(0x2060);
  50. }

  51. /**************开机自检eeprom初始化*****************/
  52. void init_eeprom()
  53. {
  54.         read_eeprom();                //先读
  55.         if(a_a != 1)                //新的单片机初始单片机内问eeprom
  56.         {
  57.                 GapValue  = 3500;
  58.                 a_a = 1;
  59.                 write_eeprom();           //保存数据
  60.         }        
  61. }
  62.                                                                                                                                        
  63. //显示单价,单位为元,四位整数,两位小数
  64. void Display_Price()
  65. {
  66.             LCD1602_write_com(0x8c);
  67.                         LCD1602_write_data(price/100 + 0x30);
  68.                         LCD1602_write_data(price%100/10 + 0x30);
  69.                         LCD1602_write_data('.');
  70.                         LCD1602_write_data(price%10 + 0x30);
  71. }

  72. //显示重量,单位kg,两位整数,三位小数
  73. void Display_Weight()
  74. {
  75.             LCD1602_write_com(0x83);
  76.                         LCD1602_write_data(Weight_Shiwu/1000 + 0x30);
  77.                         LCD1602_write_data('.');
  78.                         LCD1602_write_data(Weight_Shiwu%1000/100 + 0x30);
  79.                         LCD1602_write_data(Weight_Shiwu%100/10 + 0x30);
  80.                         LCD1602_write_data(Weight_Shiwu%10 + 0x30);
  81. }

  82. //显示总价,单位为元,四位整数,两位小数
  83. void Display_Money()
  84. {
  85.   // unsigned int i,j;

  86.    if (money>9999)         //超出显示量程
  87.    {
  88.      LCD1602_write_com(0x80+0x40+6);
  89.      LCD1602_write_word("---.-");
  90.       return;      
  91.    }
  92.    if (money>=1000)
  93.    {
  94.        LCD1602_write_com(0x80+0x40+6);
  95.            LCD1602_write_data(money/1000 + 0x30);
  96.            LCD1602_write_data(money%1000/100 + 0x30);
  97.            LCD1602_write_data(money%100/10 + 0x30);
  98.            LCD1602_write_data('.');
  99.            LCD1602_write_data(money%10 + 0x30);
  100.    }
  101.    else if (money>=100)
  102.    {
  103.        LCD1602_write_com(0x80+0x40+6);
  104.            LCD1602_write_data(0x20);
  105.            LCD1602_write_data(money%1000/100 + 0x30);
  106.            LCD1602_write_data(money%100/10 + 0x30);
  107.            LCD1602_write_data('.');
  108.            LCD1602_write_data(money%10 + 0x30);
  109.    }
  110.     else if(money>=10)
  111.    {
  112.        LCD1602_write_com(0x80+0x40+6);
  113.           LCD1602_write_data(0x20);
  114.             LCD1602_write_com(0x80+0x40+7);
  115.            LCD1602_write_data(0x20);
  116.            LCD1602_write_data(money%100/10 + 0x30);
  117.            LCD1602_write_data('.');
  118.            LCD1602_write_data(money%10+ 0x30);
  119.    }   
  120.    else
  121.      {
  122.        LCD1602_write_com(0x80+0x40+6);
  123.            LCD1602_write_data(0x20);
  124.            LCD1602_write_com(0x80+0x40+7);
  125.            LCD1602_write_data(0x20);
  126.            LCD1602_write_com(0x80+0x40+8);
  127.            LCD1602_write_data(0 + 0x30);
  128.            LCD1602_write_data('.');
  129.            LCD1602_write_data(money%10 + 0x30);
  130.    }         
  131. }

  132. //数据初始化
  133. void Data_Init()
  134. {
  135.    price = 0;
  136.    DotPos = 0;
  137. }
  138. //定时器0初始化
  139. void Timer0_Init()
  140. {
  141.         ET0 = 1;        //允许定时器0中断
  142.         TMOD = 1;       //定时器工作方式选择
  143.         TL0 = 0xb0;     
  144.         TH0 = 0x3c;     //定时器赋予初值
  145.         TR0 = 1;        //启动定时器
  146. }

  147. //定时器0中断
  148. void Timer0_ISR (void) interrupt 1 using 0
  149. {
  150. uchar Counter;
  151.         TL0 = 0xb0;
  152.         TH0 = 0x3c;     //定时器赋予初值

  153.         //每0.5秒钟刷新重量
  154.     Counter ++;
  155.     if (Counter >= 10)
  156.     {
  157.        FlagTest = 1;
  158.            Counter = 0;
  159.     }
  160. }


  161. //按键响应程序,参数是键值
  162. //返回键值:
  163. //         1    2    3    10           //10:清零重量,兼去皮功能        
  164. //         4    5    6    11           //11:清除单价
  165. //         7    8    9    12           //12:显示数值偏大调节按键  
  166. //         14   0    15   13           //14:无功能(用于扩展)15:小数点   13:显示数值偏小调节按键

  167. void KeyPress(uchar keycode)
  168. {
  169.         switch (keycode)
  170.         {
  171.                 case 0:
  172.                 case 1:
  173.                 case 2:
  174.                 case 3:
  175.                 case 4:
  176.                 case 5:
  177.                 case 6:
  178.                 case 7:
  179.                 case 8:
  180.                 case 9:        //目前在设置整数位,要注意price是整型,存储单位为分
  181.                         if (DotPos == 0)
  182.                         {                        //最多只能设置到千位
  183.                                 if (price<100)
  184.                                 {
  185.                                         price=price*10+keycode*10;
  186.                                         /*输入第一个数字时,price是等于0的,当第一次输入一个数字,例如输入5,那么计算:price=price*10+5*10=50
  187.                                         我们看到液晶上显示的是05.0,但是在程序里直接处理小数显示是比较麻烦的,所以我们是将这个数乘以10,扩大10倍后处理的
  188.                                         那么当第二次输入数字时,例如输入4,那么计算:price=50*10+4*10=540,液晶显示的就是54.0
  189.                                         */
  190.                                 }
  191.                         }//目前在设置小数位
  192.                         else if (DotPos==1)  //小数点后第一位
  193.                         {
  194.                                 price=price+keycode;
  195.                                 DotPos=2;
  196.                         }
  197.                         Display_Price();
  198.                         break;
  199.                 case 10:   //去皮键
  200.                         if(qupi==0)
  201.                                 qupi=Weight_Shiwu;
  202.                         else
  203.                                 qupi=0;
  204.                         Display_Price();
  205.                         //                 FlagSetPrice = 0;
  206.                         DotPos = 0;
  207.                         break;
  208.                 case 11:        //删除键
  209.                         price=0;
  210.                         DotPos=0;
  211.                         Display_Price();
  212.                         break;
  213.                 case 12:           //加
  214.                         if(GapValue<10000)
  215.                         GapValue++;        
  216.                         
  217.                         break;
  218.                 case 13:   //减
  219.                         if(GapValue>1)
  220.                         GapValue--;
  221.                         break;
  222.                 case 15:   //小数点按下
  223.                         DotPos = 1;      //小数点后第一位
  224.                         break;

  225.    }
  226. }
  227. //****************************************************
  228. //主函数
  229. //****************************************************
  230. void main()
  231. {
  232.         init_eeprom();  //开始初始化保存的数据
  233.         Init_LCD1602();                                                                        //初始化LCD1602
  234.    EA = 0;
  235.    Data_Init();
  236.    Timer0_Init();
  237.    //初中始化完成,开断
  238.    EA = 1;
  239.         
  240. //        Get_Maopi();
  241.         LCD1602_write_com(0x80);                                                //指针设置
  242.    LCD1602_write_word(" Welcome To Use ");        //  
  243.    LCD1602_write_com(0x80+0x40);                                                //指针设置
  244.    LCD1602_write_word("Electronic Scale");
  245. //   Delay_ms(2000);
  246.    Get_Maopi();
  247.    LCD1602_write_com(0x80);                                                //指针设置
  248.    LCD1602_write_word("WE:0.000 PR:00.0");
  249.    LCD1602_write_com(0x80+0x40);                                //指针设置
  250.    LCD1602_write_word("MONEY:  0.00    ");
  251.    Display_Price();
  252. //        Get_Maopi();                                //称毛皮重量

  253.         while(1)
  254.         {
  255. //每0.5秒称重一次
  256.           if (FlagTest==1)
  257.                 {
  258.                         Get_Weight();
  259.                         FlagTest = 0;
  260.                 }                        
  261.                   
  262.           keycode = Getkeyboard();
  263.           //有效键值0-15
  264.           if (keycode<16)
  265.           {
  266.                  KeyPress(keycode);
  267.                  Buzzer=0;
  268.                  Delay_ms(100);
  269.                  Buzzer=1;
  270.                  while(keycode<16)
  271.                  {
  272.                         if(keycode==12||keycode==13)
  273.                         {
  274.                                 Buzzer=0;
  275.                                  Delay_ms(10);
  276.                                  Buzzer=1;
  277.                                  KeyPress(keycode);
  278.                                  Get_Weight();
  279.                                  flag_key=1;
  280.                         }
  281.                         keycode = Getkeyboard();
  282.                  }
  283.                  write_eeprom();                           //保存数据
  284.           }
  285.         }
  286. }
  287. //****************************************************
  288. //称重
  289. //****************************************************
  290. void Get_Weight()
  291. {
  292.         Weight_Shiwu = HX711_Read();
  293.         Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                //获取净重
  294.         
  295.         Weight_Shiwu = (unsigned int)((float)Weight_Shiwu*10/GapValue)-qupi;         //计算实物的实际重量                                                                                                                                
  296.         if(Weight_Shiwu > 10000)                //超重报警
  297.         {
  298.                 Buzzer = !Buzzer;        
  299.                 LED=!LED;
  300.                 LCD1602_write_com(0x83);
  301.            LCD1602_write_word("-.---");
  302.         }
  303.         else
  304.         {
  305.                 if(Weight_Shiwu==0)
  306.                 LED=1;
  307.                 else if(Weight_Shiwu>0)
  308.                 LED=0;
  309.                 Buzzer = 1;
  310.                 Display_Weight();
  311.                 money = Weight_Shiwu*price/1000;  //money单位为分
  312.                //显示总金额
  313.                Display_Money();
  314.         }
  315. }

  316. //****************************************************
  317. //获取毛皮重量
  318. //****************************************************
  319. void Get_Maopi()
  320. {
  321.         unsigned char clear;
  322. mm:        Weight_Maopi_0 = HX711_Read();
  323.         for(clear=0;clear<10;clear++)
  324.         {
  325.                 Buzzer=1;
  326.                 LED=1;
  327.                 Delay_ms(100);
  328.                 LED=0;
  329.                 Delay_ms(100);        
  330.         }
  331.         Weight_Maopi = HX711_Read();
  332.         if(Weight_Maopi/GapValue!=Weight_Maopi_0/GapValue)
  333.         goto mm;
  334.         Buzzer=0;
  335.         Delay_ms(500);
  336.         Buzzer=1;
  337. }

  338. //****************************************************
  339. //MS延时函数(12M晶振下测试)
  340. //****************************************************
  341. void Delay_ms(unsigned int n)
  342. {
  343.         unsigned int  i,j;
  344.         for(i=0;i<n;i++)
  345.                 for(j=0;j<123;j++);
  346. }
复制代码

2.jpg
全部内容: 电子秤.rar (1007.05 KB, 售价: 2 E币)
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-25 12:36

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表