找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7353|回复: 9
打印 上一主题 下一主题
收起左侧

51HX711+单片机10kg电子秤源程序+上线报警设置(加继电器)

  [复制链接]
跳转到指定楼层
楼主
电子秤的制作,能够去皮,范围10KG

单片机源程序如下:
  1. /************************************************
  2. 功能:
  3.      按键K1为去皮功能,K2和K3是修改报警参数值。K2加,K3减。当所测重量大于设置的报警值时,P3.0驱动继电器工作,继电器可以接报警装置等。
  4.          校正方法:如果所测重量偏小。则按住K3键不放,再按下K1键。 直到显示重量为正确重量时再放开K1和K3按键。
  5.                         如果所测重量偏大。则按住K2键不放,再按下K1键。 直到显示重量为正确重量时再放开K1和K2按键。。 校正系数具有断电保存功能。
  6. 时间:2018.4.25
  7. 作者:Obligation
  8. *************************************************/

  9. #include "main.h"
  10. #include "LCD1602.h"
  11. #include "HX711.h"
  12. #define uchar unsigned char  //无符号字符型 宏定义        变量范围0~255
  13. #define uint  unsigned int         //无符号整型 宏定义        变量范围0~65535


  14. #include "eeprom52.h"
  15. //定义变量
  16. unsigned char KEY_NUM = 0;   //用来存放按键按下的键值
  17. unsigned long HX711_Buffer = 0;  //用来存放HX711读取出来的数据
  18. unsigned long Weight_Maopi = 0; //用来存放毛皮数据
  19. long Weight_Shiwu = 0;          //用来存放实物重量
  20. unsigned char Max_Value;             //用来存放设置最大值

  21. unsigned char state = 0;    //用来存放设置状态
  22. unsigned char Blink_Speed = 0;
  23. #define Blink_Speed_Max 6          //该值可以改变设置指针闪烁频率


  24. uint GapValue= 210;           //传感器曲率

  25. unsigned char num1,num2,times; //num1 和 num2  是存储在单片机eeprom内部的。

  26. sbit JDQ=P3^0;

  27. //报警值,单位是g
  28. #define AlarmValue 10000                       



  29. /******************把数据保存到单片机内部eeprom中******************/
  30. void write_eeprom()
  31. {  
  32.     num1=GapValue/256;
  33.         num2=GapValue%256;

  34.     SectorErase(0x2000);
  35.         byte_write(0x2001,Max_Value);
  36.         byte_write(0x2002,  num1);
  37.         byte_write(0x2003,  num2);

  38.     byte_write(0x2060, a_a);       
  39. }

  40. /******************把数据从单片机内部eeprom中读出来*****************/
  41. void read_eeprom()
  42. {
  43.         Max_Value = byte_read(0x2001);
  44.         num1   = byte_read(0x2002);
  45.         num2   = byte_read(0x2003);
  46.        
  47.     a_a      = byte_read(0x2060);
  48.         GapValue= num1*256+num2;
  49. }

  50. /**************开机自检eeprom初始化*****************/
  51. void init_eeprom()
  52. {
  53.         read_eeprom();                //先读
  54.         if(a_a != 3)                //新的单片机初始单片机内问eeprom
  55.         {
  56.            a_a =3;
  57.            Max_Value = 10;
  58.            GapValue= 210;
  59.        write_eeprom();
  60.         }       
  61.        
  62.        
  63.        
  64. }


  65. //扫描按键
  66. void Scan_Key()
  67. {
  68.         if(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 )
  69.         {
  70.                 Delay_ms(5);
  71.                 if(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 )
  72.                 {
  73.                         while(KEY1 == 0 && KEY2 ==1 && KEY3 ==1 );
  74.                     Get_Maopi();
  75.                         Buzzer_Di();
  76.                 }       
  77.         }

  78.         if(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 )
  79.         {
  80.                 Delay_ms(5);
  81.                 if(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 )
  82.                 {
  83.                         while(KEY2 == 0 && KEY1 ==1 && KEY3 ==1 );
  84.                     if(Max_Value<10)         { Max_Value++;}
  85.                     write_eeprom();       //保存数据
  86.                         Buzzer_Di();
  87.                         LCD1602_write_com(0x80+0x40);
  88.             LCD1602_write_word("JDQ=");
  89.             LCD1602_write_data(Max_Value/100 + 0x30);
  90.             LCD1602_write_data(Max_Value%100/10 + 0x30);
  91.             LCD1602_write_data(Max_Value%10 + 0x30);
  92.                 }       
  93.         }

  94.         if(KEY3 == 0 && KEY1 ==1 && KEY2 ==1 )
  95.         {
  96.                 Delay_ms(5);
  97.                 if(KEY3 == 0 && KEY1 ==1 && KEY2 ==1)
  98.                 {
  99.                         while(KEY3 == 0 && KEY1 ==1 && KEY2 ==1);
  100.                     if(Max_Value>1)         { Max_Value--; }
  101.                          write_eeprom();       //保存数据
  102.                          Buzzer_Di();
  103.                          LCD1602_write_com(0x80+0x40);
  104.             LCD1602_write_word("JDQ=");
  105.             LCD1602_write_data(Max_Value/100 + 0x30);
  106.             LCD1602_write_data(Max_Value%100/10 + 0x30);
  107.             LCD1602_write_data(Max_Value%10 + 0x30);
  108.                 }       
  109.         }


  110.    //=======================对实物进行重量校正================================
  111.         if( KEY1 == 0 && KEY2 ==0)
  112.         {
  113.                 Delay_ms(25);
  114.                 if( KEY1 == 0 && KEY2 ==0)
  115.                 {
  116.                     times=0;
  117.                         Buzzer =0;
  118.                         Delay_ms(25);
  119.                         Buzzer =1;
  120.                     if(GapValue<999)  { GapValue++; }
  121.                         write_eeprom();       //保存数据
  122.                         LCD1602_write_com(0x80+0x40+11);
  123.             LCD1602_write_word("K=");
  124.                 LCD1602_write_data(GapValue/100 + 0x30);
  125.             LCD1602_write_data(GapValue%100/10 + 0x30);
  126.             LCD1602_write_data(GapValue%10 + 0x30);

  127.                 }       
  128.         }

  129.         if(KEY1 == 0 && KEY3 ==0)
  130.         {
  131.                 Delay_ms(25);
  132.                 if(KEY1 == 0 && KEY3 ==0)
  133.                 {       
  134.                     times=0;
  135.                         Buzzer =0;
  136.                         Delay_ms(25);
  137.                         Buzzer =1;
  138.                     if(GapValue>1)  {         GapValue--; }
  139.                          write_eeprom();       //保存数据
  140.                         LCD1602_write_com(0x80+0x40+11);
  141.             LCD1602_write_word("K=");
  142.                 LCD1602_write_data(GapValue/100 + 0x30);
  143.             LCD1602_write_data(GapValue%100/10 + 0x30);
  144.             LCD1602_write_data(GapValue%10 + 0x30);

  145.                 }       
  146.         }

  147. }



  148. //****************************************************
  149. //主函数
  150. //****************************************************
  151. void main()
  152. {
  153.         Init_LCD1602();                                                //初始化LCD1602
  154.         init_eeprom();                              //读eeprom数据
  155.         LCD1602_write_com(0x80);                        //设置LCD1602指针
  156.         LCD1602_write_word("Welcome to use!");
  157.        
  158.         Get_Maopi();
  159.         Get_Maopi();
  160.         Delay_ms(2000);                 //延时2s
  161.         Get_Maopi();
  162.         Get_Maopi();                                //称毛皮重量        //多次测量有利于HX711稳定
  163.     LCD1602_write_com(0x01);    //清屏
  164.    
  165.         LCD1602_write_com(0x80+0x40);
  166.     LCD1602_write_word("JDQ=");
  167.         LCD1602_write_data(Max_Value/100 + 0x30);
  168.     LCD1602_write_data(Max_Value%100/10 + 0x30);
  169.     LCD1602_write_data(Max_Value%10 + 0x30);
  170.     LCD1602_write_word("Kg");
  171.                    Buzzer =0;
  172.                         Delay_ms(25);
  173.                         Buzzer =1;

  174.         while(1)
  175.         {
  176.           
  177.             Scan_Key();
  178.         Get_Weight();               
  179.        
  180.                 //显示当前重量
  181.                 LCD1602_write_com(0x80);
  182.         LCD1602_write_word("Weight=");
  183.         LCD1602_write_data(Weight_Shiwu/10000 + 0x30);
  184.                 LCD1602_write_data(Weight_Shiwu%10000/1000 + 0x30);
  185.                 LCD1602_write_data('.');
  186.                 LCD1602_write_data(Weight_Shiwu%1000/100 + 0x30);
  187.                 LCD1602_write_data(Weight_Shiwu%100/10 + 0x30);
  188.                 LCD1602_write_data(Weight_Shiwu%10 + 0x30);       
  189.         
  190.                 LCD1602_write_word("Kg");

  191.        
  192.         
  193.         //超限报警
  194.         if(Weight_Shiwu/1000 >= Max_Value || Weight_Shiwu >= AlarmValue)                //超过设置最大值或者传感器本身量程最大值报警       
  195.                 {
  196.                         JDQ = 0; Buzzer=0;       
  197.                 }
  198.                 else
  199.                 {
  200.                         JDQ = 1; Buzzer=1;
  201.                 }

  202.             times++;
  203.                 if(times>=50)
  204.                 {
  205.                     times=0;
  206.                     LCD1602_write_com(0x80+0x40+11);
  207.             LCD1602_write_word("     ");
  208.                
  209.                 }
  210.         }
  211. }




  212. //****************************************************
  213. //称重
  214. //****************************************************
  215. void Get_Weight()
  216. {
  217.         Weight_Shiwu = HX711_Read();
  218.         Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                //获取净重
  219.         if(Weight_Shiwu >= 0)                       
  220.         {       
  221.                 Weight_Shiwu = (unsigned long)((float)Weight_Shiwu/GapValue);         //计算实物的实际重量
  222.         }
  223.         else
  224.         {
  225.                 Weight_Shiwu = 0;
  226.         }
  227.        
  228. }
  229. ……………………

  230. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
电子秤:10kg的压力传感器.rar (83.54 KB, 下载次数: 199)



评分

参与人数 2黑币 +60 收起 理由
zkele + 10 共享资料的黑币奖励!
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶1 踩
回复

使用道具 举报

10#
ID:895304 发表于 2021-3-25 07:34 | 只看该作者
num1=GapValue/256;
        num2=GapValue%256;
是什么意思呢
回复

使用道具 举报

9#
ID:120677 发表于 2020-12-22 16:02 | 只看该作者
我也来学学 试试 提高一下
回复

使用道具 举报

8#
ID:855668 发表于 2020-12-22 10:59 | 只看该作者
Obligation 发表于 2018-5-19 15:02
原理图,即一个压力传感器配上单片机最小系统就可以了。

没有ad转换器和hx711吗
回复

使用道具 举报

7#
ID:612219 发表于 2019-12-17 11:00 | 只看该作者
为什么LCD液晶屏只显示Wellcome to use!就没清屏显示其它内容了。
回复

使用道具 举报

6#
ID:568729 发表于 2019-6-21 11:29 | 只看该作者
需要下载学习,谢谢!
回复

使用道具 举报

5#
ID:224412 发表于 2018-5-19 15:02 | 只看该作者
原理图,即一个压力传感器配上单片机最小系统就可以了。

123.png (134.74 KB, 下载次数: 51)

123.png
回复

使用道具 举报

地板
ID:329634 发表于 2018-5-14 23:33 | 只看该作者
请问原理图在哪
回复

使用道具 举报

板凳
ID:224412 发表于 2018-5-4 13:34 | 只看该作者
应管理员要求特发电路原理图,其实买一个压力传感器配上单片机最小系统就可以了。
回复

使用道具 举报

沙发
ID:1 发表于 2018-5-2 01:01 | 只看该作者
能分享个原理图吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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