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

QQ登錄

只需一步,快速開始

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

HX711稱重模塊電路+單片機(jī)程序(測(cè)試通過)

  [復(fù)制鏈接]
ID:257389 發(fā)表于 2017-12-4 20:37 | 顯示全部樓層 |閱讀模式
HX711芯片采用了海芯科技集成電路專利技術(shù),是一款專為高精度壓力測(cè)量而設(shè)計(jì)的24 A/D 轉(zhuǎn)換器芯片。該芯片與單片機(jī)的接口電路設(shè)計(jì)和程序編寫非常簡單,所有控制信號(hào)由管腳驅(qū)動(dòng),無需對(duì)芯片內(nèi)部的寄存器編程。輸入選擇開關(guān)可任意選取通道A 或通道B,與其內(nèi)部的低噪聲可編程放大器相連。通道A 的可編程增益為128 64,對(duì)應(yīng)的滿額度差分輸入信號(hào)電壓值分別為±20mV±40mV。通道B 則為固定的32 增益,用于系統(tǒng)參數(shù)檢測(cè)。芯片內(nèi)提供的穩(wěn)壓電源可以直接向外部傳感器和芯片內(nèi)的A/D 轉(zhuǎn)換器提供電源,系統(tǒng)板上無需另外的模擬電。芯片內(nèi)的時(shí)鐘振蕩器不需要任何外接器件。上電自動(dòng)復(fù)位功能簡化了開機(jī)的初始化過程。

原理圖:



                                     總體電路


電源+串口通訊



單片機(jī)最小系統(tǒng):


存儲(chǔ)模塊+下載模塊+蜂鳴器+矩陣鍵盤


單片機(jī)源程序如下:



  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. //主函數(shù)
  13. //****************************************************
  14. void main()
  15. {
  16.               Init_LCD1602();                                                                                                                              //初始化LCD1602

  17.               LCD1602_write_com(0x80);                                                                                    //指針設(shè)置
  18.               LCD1602_write_word("Welcome to use! ");                                          //開機(jī)畫面第一行


  19.               Delay_ms(2000);                            //延時(shí)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);                                                                                    //指針設(shè)置
  26.               LCD1602_write_word("+WEI |PRI | MON ");
  27.               LCD1602_write_com(0x80+0x40);                                                        //指針設(shè)置
  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.                                           //顯示當(dāng)前重量
  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)                                          //當(dāng)返回的不是初值時(shí)候,確認(rèn)按鍵按下。
  48.                             {
  49.                                           if(KEY_NUM == 16)                                          //數(shù)字A鍵,去皮功能
  50.                                           {
  51.                                                         Get_Maopi();                                          //去皮            
  52.                                           }

  53.                                           if(KEY_NUM == 15)                                          //數(shù)字B鍵清除鍵,二次測(cè)量
  54.                                           {
  55.                                                         goto loop;            
  56.                                           }

  57.                                           if(KEY_NUM == 12)                                          //數(shù)字C輸入單價(jià)錯(cuò)誤時(shí)返回上一步
  58.                                           {
  59.                                                         Price_Count--;
  60.                                                         if( Price_Count < 0)
  61.                                                         {
  62.                                                                       Price_Count = 0;
  63.                                                         }
  64.                                                       
  65.                                                         Price_Buffer[Price_Count] = 0;                                             //清除上一個(gè)輸入的數(shù)據(jù)

  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)                                          //數(shù)字D鍵,計(jì)算總價(jià)
  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)                                                                        //顯示輸入的價(jià)值
  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;//數(shù)字1
  145.               if(temp==0xed)com=4;//數(shù)字4
  146.               if(temp==0xeb)com=7;//數(shù)字7
  147.               if(temp==0xe7)com=11;//備用鍵*號(hào)鍵
  148.               if(temp==0xd7)com=0;//數(shù)字0
  149.               if(temp==0xb7)com=14;//備用鍵*號(hào)鍵
  150.             

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

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

  157.               if(temp==0x7e)com=16;//數(shù)字A鍵,去皮功能            
  158.               if(temp==0x7d)com=15;//數(shù)字B鍵清除鍵,二次測(cè)量
  159.               if(temp==0x7b)com=12;//數(shù)字C輸入單價(jià)錯(cuò)誤時(shí)返回上一步
  160.               if(temp==0x77)com=13;//數(shù)字D鍵,計(jì)算總價(jià)            
  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;              //獲取實(shí)物的AD采樣數(shù)值。
  174.               Weight_Shiwu = (unsigned int)((float)Weight_Shiwu/4.22+0.05); //計(jì)算實(shí)物的實(shí)際重量
  175. //因?yàn)椴煌膫鞲衅魈匦郧不一樣,因此,每一個(gè)傳感器需要矯正這里的4.30這個(gè)除數(shù)。
  176. //當(dāng)發(fā)現(xiàn)測(cè)試出來的重量偏大時(shí),增加該數(shù)值。
  177. //如果測(cè)試出來的重量偏小時(shí),減小改數(shù)值。
  178. //該數(shù)值一般在4.0-5.0之間。因傳感器不同而定。
  179. //+0.05是為了四舍五入百分位
  180.                             Buzzer = 1;                                                        //關(guān)閉警報(bào)
  181.               }
  182. //              else if(HX711_Buffer < Weight_Maopi - 30)
  183. //              {
  184. //                            Buzzer = 0;                                                        //負(fù)重量報(bào)警
  185. //              }
  186. //              else if(HX711_Buffer > Weight_Maopi + 24970)                            //大于5Kg的最大量程,報(bào)警            
  187. //              {
  188. //                            Buzzer = 0;
  189. //              }

  190.               if(Weight_Shiwu > 5000 || HX711_Buffer < Weight_Maopi - 30)
  191.               {
  192.                             Buzzer = 0;            
  193.               }
  194.               else
  195.               {
  196.                             Buzzer = 1;
  197.               }

  198.             
  199. }

  200. //****************************************************
  201. //獲取毛皮重量
  202. //****************************************************
  203. void Get_Maopi()
  204. {
  205.               HX711_Buffer = HX711_Read();
  206.               Weight_Maopi = HX711_Buffer/100;                           
  207. }

  208. //****************************************************
  209. //MS延時(shí)函數(shù)(12M晶振下測(cè)試)
  210. //****************************************************
  211. void Delay_ms(unsigned int n)
  212. {
  213.               unsigned int  i,j;
  214.               for(i=0;i<n;i++)
  215.                             for(j=0;j<123;j++);
  216. }





  217. 1602.H文件
  218. #ifndef __LCD1602_H__
  219. #define __LCD1602_H__


  220. #include <reg52.h>

  221. //LCD1602 IO設(shè)置
  222. #define LCD1602_PORT P0
  223. sbit LCD1602_RS = P2^5;
  224. sbit LCD1602_RW = P2^6;
  225. sbit LCD1602_EN = P2^7;

  226. //函數(shù)或者變量聲明
  227. extern void LCD1602_delay_ms(unsigned int n);
  228. extern void LCD1602_write_com(unsigned char com);
  229. extern void LCD1602_write_data(unsigned char dat);
  230. extern void LCD1602_write_word(unsigned char *s);
  231. extern void Init_LCD1602();

  232. #endif


  233. 1602.C程序 :

  234. #include "LCD1602.h"

  235. //****************************************************
  236. //MS延時(shí)函數(shù)(12M晶振下測(cè)試)
  237. //****************************************************
  238. void LCD1602_delay_ms(unsigned int n)
  239. {
  240.               unsigned int  i,j;
  241.               for(i=0;i<n;i++)
  242.                             for(j=0;j<123;j++);
  243. }

  244. //****************************************************
  245. //寫指令
  246. //****************************************************
  247. void LCD1602_write_com(unsigned char com)
  248. {
  249.               LCD1602_RS = 0;
  250.               LCD1602_delay_ms(1);
  251.               LCD1602_EN = 1;
  252.               LCD1602_PORT = com;
  253.               LCD1602_delay_ms(1);
  254.               LCD1602_EN = 0;
  255. }

  256. //****************************************************
  257. //寫數(shù)據(jù)
  258. //****************************************************
  259. void LCD1602_write_data(unsigned char dat)
  260. {
  261.               LCD1602_RS = 1;
  262.               LCD1602_delay_ms(1);            
  263.               LCD1602_PORT = dat;
  264.               LCD1602_EN = 1;
  265.               LCD1602_delay_ms(1);
  266.               LCD1602_EN = 0;
  267. }

  268. //****************************************************
  269. //連續(xù)寫字符
  270. //****************************************************
  271. void LCD1602_write_word(unsigned char *s)
  272. {
  273.               while(*s>0)
  274.               {
  275.                             LCD1602_write_data(*s);
  276.                             s++;
  277.               }
  278. }

  279. void Init_LCD1602()
  280. {
  281.               LCD1602_EN = 0;
  282.               LCD1602_RW = 0;                                                                                    //設(shè)置為寫狀態(tài)
  283.               LCD1602_write_com(0x38);                                          //顯示模式設(shè)定
  284.               LCD1602_write_com(0x0c);                                          //開關(guān)顯示、光標(biāo)有無設(shè)置、光標(biāo)閃爍設(shè)置
  285.               LCD1602_write_com(0x06);                                          //寫一個(gè)字符后指針加一
  286.               LCD1602_write_com(0x01);                                          //清屏指令
  287. }




  288. 稱重模塊.H文件:

  289. #ifndef __HX711_H__
  290. #define __HX711_H__
  291. #include <reg52.h>
  292. #include <intrins.h>

  293. //IO設(shè)置
  294. sbit HX711_DOUT=P2^0;
  295. sbit HX711_SCK=P2^1;

  296. //函數(shù)或者變量聲明
  297. extern void Delay__hx711_us(void);
  298. extern unsigned long HX711_Read(void);

  299. #endif
  300. ……………………
  301. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:

稱重壓力傳感器HX711AD模塊電路 程序.docx (691.56 KB, 下載次數(shù): 239)



回復(fù)

使用道具 舉報(bào)

ID:64017 發(fā)表于 2017-12-6 11:06 | 顯示全部樓層
要看看,就是刷新了一次。有多扣了一次。郁悶。
回復(fù)

使用道具 舉報(bào)

ID:329634 發(fā)表于 2018-5-15 19:23 | 顯示全部樓層
沒有main.h文件
回復(fù)

使用道具 舉報(bào)

ID:241242 發(fā)表于 2018-9-13 11:07 | 顯示全部樓層
不錯(cuò)的
回復(fù)

使用道具 舉報(bào)

ID:518547 發(fā)表于 2019-5-18 21:50 | 顯示全部樓層
真的的很有用了
回復(fù)

使用道具 舉報(bào)

ID:104846 發(fā)表于 2019-5-24 16:08 | 顯示全部樓層
確實(shí)少M(fèi)AIN.H文件,望樓主確認(rèn)回復(fù)
回復(fù)

使用道具 舉報(bào)

ID:578886 發(fā)表于 2019-7-7 21:11 | 顯示全部樓層
沒有main.h文件
回復(fù)

使用道具 舉報(bào)

ID:578886 發(fā)表于 2019-7-7 21:13 | 顯示全部樓層
一個(gè)錯(cuò)誤七個(gè)警告
回復(fù)

使用道具 舉報(bào)

ID:554109 發(fā)表于 2019-7-12 15:13 | 顯示全部樓層
程序試過了,但是一直處于負(fù)重量報(bào)警,檢查過就是負(fù)重量報(bào)警,不是超量程報(bào)警。例如0.000-0.001-0.002-0.003-0.004-0.003-0.002-0.001-0.000-【bibibi】   so,,,各種嘗試,檢查都不行。。唉。。兩天了,,,頭大。。。誰有同類經(jīng)歷或者經(jīng)驗(yàn)????
回復(fù)

使用道具 舉報(bào)

ID:582255 發(fā)表于 2019-8-8 14:58 | 顯示全部樓層
樓主能分享一下所有的文件嗎?main.h文件嗎?
謝謝了謝謝了
56003418@qq.com
回復(fù)

使用道具 舉報(bào)

ID:62514 發(fā)表于 2019-8-16 20:31 | 顯示全部樓層
真的很好。謝謝資料,正要做電子秤。
回復(fù)

使用道具 舉報(bào)

ID:624657 發(fā)表于 2019-10-15 19:35 | 顯示全部樓層
真的太好用了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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