标题: HX711 5K压力传感器配套程序 [打印本页]

作者: TTCC    时间: 2017-7-28 16:51
标题: HX711 5K压力传感器配套程序

单片机源程序如下:
  1. #include "main.h"
  2. #include "LCD1602.h"
  3. #include "HX711.h"

  4. unsigned long HX711_Buffer = 0;
  5. unsigned long Weight_Maopi = 0,Weight_Shiwu = 0;
  6. char Price_Count = 0;
  7. unsigned char KEY_NUM = 0;
  8. unsigned char Price_Buffer[3] = {0x00,0x00,0x00};
  9. unsigned long Money = 0;
  10. bit Flag_OK = 0;

  11. //****************************************************
  12. //主函数
  13. //****************************************************
  14. void main()
  15. {
  16.         Init_LCD1602();                                                                        //初始化LCD1602

  17.         LCD1602_write_com(0x80);                                                //指针设置
  18.         LCD1602_write_word("Welcome to use! ");                        //开机画面第一行


  19.         Delay_ms(2000);                 //延时2s


  20. loop:Price_Count = 0;
  21.         Price_Buffer[0] = 0;
  22.         Price_Buffer[1] = 0;
  23.         Price_Buffer[2] = 0;
  24.         Flag_OK = 0;

  25.         LCD1602_write_com(0x80);                                                //指针设置
  26.         LCD1602_write_word("+WEI |PRI | MON ");
  27.         LCD1602_write_com(0x80+0x40);                                //指针设置
  28.         LCD1602_write_word("0.000|  . |   . ");

  29.        

  30.        
  31.         Get_Maopi();                                //称毛皮重量

  32.         while(1)
  33.         {
  34.                 if( Flag_OK == 0)
  35.                 {
  36.                         Get_Weight();                        //称重
  37.        
  38.                         //显示当前重量
  39.                         LCD1602_write_com(0x80+0x40);
  40.                         LCD1602_write_data(Weight_Shiwu/1000 + 0x30);
  41.                         LCD1602_write_data('.');
  42.                         LCD1602_write_data(Weight_Shiwu%1000/100 + 0x30);
  43.                         LCD1602_write_data(Weight_Shiwu%100/10 + 0x30);
  44.                         LCD1602_write_data(Weight_Shiwu%10 + 0x30);
  45.                 }

  46.                 KEY_NUM = KEY_Scan();

  47.                 if( KEY_NUM != 0x55)                        //当返回的不是初值时候,确认按键按下。
  48.                 {
  49.                         if(KEY_NUM == 16)                        //数字A键,去皮功能
  50.                         {
  51.                                 Get_Maopi();                        //去皮       
  52.                         }

  53.                         if(KEY_NUM == 15)                        //数字B键清除键,二次测量
  54.                         {
  55.                                 goto loop;       
  56.                         }

  57.                         if(KEY_NUM == 12)                        //数字C输入单价错误时返回上一步
  58.                         {
  59.                                 Price_Count--;
  60.                                 if( Price_Count < 0)
  61.                                 {
  62.                                         Price_Count = 0;
  63.                                 }
  64.                                
  65.                                 Price_Buffer[Price_Count] = 0;                           //清除上一个输入的数据

  66.                                 switch(Price_Count)
  67.                                 {
  68.                                         case 0:
  69.                                                         LCD1602_write_com(0x80+0x40+6);
  70.                                                         LCD1602_write_data(' ');
  71.                                                         break;
  72.                                         case 1:
  73.                                                         LCD1602_write_com(0x80+0x40+7);
  74.                                                         LCD1602_write_data(' ');
  75.                                                         break;
  76.                                         case 2:
  77.                                                         LCD1602_write_com(0x80+0x40+9);
  78.                                                         LCD1602_write_data(' ');
  79.                                                         break;
  80.                                         default : break;
  81.                                 }       
  82.                         }

  83.                         if(KEY_NUM == 13)                        //数字D键,计算总价
  84.                         {
  85.                                 Money = Price_Buffer[0] * 100 + Price_Buffer[1] * 10 + Price_Buffer[2];       
  86.                                 Money = Money * Weight_Shiwu / 1000;
  87.                                 LCD1602_write_com(0x80+0x40+11);
  88.                                 LCD1602_write_data(Money/1000 + 0x30);                       
  89.                                 LCD1602_write_data(Money%1000/100 + 0x30);
  90.                                 LCD1602_write_data(Money%100/10 + 0x30);
  91.                                 LCD1602_write_data('.');
  92.                                 LCD1602_write_data(Money%10 + 0x30);
  93.                                
  94.                                 Flag_OK = 1;                                       
  95.                         }

  96.                         if(KEY_NUM >= 0 && KEY_NUM <= 9)                                          //显示输入的价格值
  97.                         {
  98.                                 Price_Buffer[Price_Count] = KEY_NUM;
  99.        
  100.                                 switch(Price_Count)
  101.                                 {
  102.                                         case 0:
  103.                                                         LCD1602_write_com(0x80+0x40+6);
  104.                                                         LCD1602_write_data(Price_Buffer[0] + 0x30);
  105.                                                         break;
  106.                                         case 1:
  107.                                                         LCD1602_write_com(0x80+0x40+7);
  108.                                                         LCD1602_write_data(Price_Buffer[1] + 0x30);
  109.                                                         break;
  110.                                         case 2:
  111.                                                         LCD1602_write_com(0x80+0x40+9);
  112.                                                         LCD1602_write_data(Price_Buffer[2] + 0x30);
  113.                                                         break;
  114.                                         default : break;
  115.                                 }
  116.        
  117.                                 Price_Count++;
  118.        
  119.                                 if( Price_Count >= 3)
  120.                                 {
  121.                                         Price_Count = 3;
  122.                                 }
  123.                         }

  124.                 }

  125.         }
  126. }

  127. //****************************************************
  128. //矩阵键盘扫描
  129. //****************************************************
  130. unsigned char KEY_Scan()
  131. {
  132.         unsigned char temp = 0;
  133.         unsigned char com = 0x55 , com1 = 0 , com2 = 0;
  134.         P3=0xf0;
  135.         if(P3!=0xf0)
  136.         {
  137.                 com1=P3;
  138.                 P3=0x0f;
  139.                 com2=P3;        
  140.         }
  141.         P3=0xf0;
  142.     while(P3!=0xf0);          
  143.         temp=com1|com2;
  144.         if(temp==0xee)com=1;//数字1
  145.         if(temp==0xed)com=4;//数字4
  146.         if(temp==0xeb)com=7;//数字7
  147.         if(temp==0xe7)com=11;//备用键*号键
  148.         if(temp==0xd7)com=0;//数字0
  149.         if(temp==0xb7)com=14;//备用键*号键
  150.        

  151.         if(temp==0xde)com=2; //数字2
  152.         if(temp==0xdd)com=5; //数字5
  153.         if(temp==0xdb)com=8;//数字8

  154.         if(temp==0xbe)com=3;//数字3
  155.         if(temp==0xbd)com=6; //数字6
  156.         if(temp==0xbb)com=9;//数字9

  157.         if(temp==0x7e)com=16;//数字A键,去皮功能         
  158.         if(temp==0x7d)com=15;//数字B键清除键,二次测量
  159.         if(temp==0x7b)com=12;//数字C输入单价错误时返回上一步
  160.         if(temp==0x77)com=13;//数字D键,计算总价         
  161.         return(com);
  162. }

  163. //****************************************************
  164. //称重
  165. //****************************************************
  166. void Get_Weight()
  167. {
  168.         HX711_Buffer = HX711_Read();
  169.         HX711_Buffer = HX711_Buffer/100;
  170.         if(HX711_Buffer > Weight_Maopi)                       
  171.         {
  172.                 Weight_Shiwu = HX711_Buffer;
  173.                 Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                                //获取实物的AD采样数值。
  174.        
  175.                 Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/4.22+0.05);         //计算实物的实际重量
  176.                                                                                                                                                 //因为不同的传感器特性曲线不一样,因此,每一个传感器需要矫正这里的4.30这个除数。
  177.                                                                                                                                                 //当发现测试出来的重量偏大时,增加该数值。
  178.                                                                                                                                                 //如果测试出来的重量偏小时,减小改数值。
  179.                                                                                                                                                 //该数值一般在4.0-5.0之间。因传感器不同而定。
  180.                                                                                                                                                 //+0.05是为了四舍五入百分位
  181.                 Buzzer = 1;                                //关闭警报
  182.         }
  183. //        else if(HX711_Buffer < Weight_Maopi - 30)
  184. //        {
  185. //                Buzzer = 0;                                //负重量报警
  186. //        }
  187. //        else if(HX711_Buffer > Weight_Maopi + 24970)                //大于5Kg的最大量程,报警       
  188. //        {
  189. //                Buzzer = 0;
  190. //        }

  191.         if(Weight_Shiwu > 5000 || HX711_Buffer < Weight_Maopi - 30)
  192.         {
  193. ……………………

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

所有资料51hei提供下载:
5K压力传感器配套程序V2.5(最新).rar (45.11 KB, 下载次数: 32)



作者: dream602    时间: 2019-4-3 10:56
感谢楼主




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1