找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于单片机+HX711的20kg电子秤的相关程序

[复制链接]
跳转到指定楼层
楼主
ID:433582 发表于 2018-12-3 23:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
51单片机设计的电子秤
20kg电子秤程序+上线报警设置(加继电器)

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

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


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

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


  22. uint GapValue= 104;           //传感器曲率

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

  24. sbit JDQ=P3^0;

  25. //报警值,单位是g
  26. #define AlarmValue 20000                        



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

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

  36.     byte_write(0x2060, a_a);        
  37. }

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

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


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

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

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


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

  125.                 }        
  126.         }

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

  143.                 }        
  144.         }

  145. }



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

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

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

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




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

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

所有资料51hei提供下载:
附件2:20kg的压力传感器例程.rar (79.21 KB, 下载次数: 66)


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

使用道具 举报

沙发
ID:1 发表于 2018-12-4 04:04 | 只看该作者
补全原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

板凳
ID:1062594 发表于 2023-3-26 22:02 | 只看该作者
大哥,补全原理图或者详细说明一下电路连接呗
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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