電子秤1.JPG (102.51 KB, 下載次數(shù): 61)
下載附件
2018-12-18 16:28 上傳
電子秤2.JPG (62.2 KB, 下載次數(shù): 46)
下載附件
2018-12-18 16:28 上傳
連線圖.JPG (36.58 KB, 下載次數(shù): 44)
下載附件
2018-12-18 16:28 上傳
代碼:
//電子秤
#include "LCD12864RSPI.h"
#define AR_SIZE( a ) sizeof( a ) / sizeof( a[0] )
#include "HX711.h"
int Weight = 0;
unsigned char show0[]={0xB5, 0xE7, 0xD7, 0xD3, 0xB3, 0xD3}; //電子秤
unsigned char show1[]="================";//橫隔線
unsigned char show2[]={ 0xD6, 0xD8, 0xC1, 0xBF, 0x3D, 0x00}; //重量=
unsigned char show3[]={0xBF, 0xCB}; //[克]
unsigned char show4[]={0xCC, 0xBD, 0xCB, 0xF7, 0xC8, 0xED, 0xBC, 0xFE, 0x20, 0x00, 0xD6, 0xC6}; //探索軟件 制
char str[4]; //定義重量值存儲數(shù)組,5位,其中4位為數(shù)字,1位為小數(shù)點
int temp =0; //定義重量值中間變量,,用于將獲取的float型重量值轉變?yōu)閡nsigned char數(shù)組
void setup(){
Serial.begin(9600);
LCDA.Initialise(); // 屏幕初始化
delay(100);
LCDA.DisplayString(0,2,show0,AR_SIZE(show0)); //第1行第2格開始,顯示文字"電子秤"
LCDA.DisplayString(1,0,show1,AR_SIZE(show1));//第2行第2格開始,顯示橫隔線
LCDA.DisplayString(2,1,show2,AR_SIZE(show2));//第4行第2格開始,顯示文字濕度
LCDA.DisplayString(2,7,show3,AR_SIZE(show3));
LCDA.DisplayString(3,1,show4,AR_SIZE(show4));
Init_Hx711(); //初始化HX711模塊連接的IO設置
Get_Maopi(); //獲取毛皮
delay(3000);
Get_Maopi(); //獲取毛皮
}
void loop(){
Weight = Get_Weight(); //計算放在傳感器上的重物重量
temp =Get_Weight();
if(temp<0){ temp =0; }
dtostrf(temp,4,0,str);
LCDA.DisplayString(2,4,(unsigned char *)str,AR_SIZE(str));//第3行第4格開始值
//Serial.print("W="); //串口顯示重量
//Serial.print(Weight); //串口顯示重量
//Serial.print(" g\n"); //顯示單位
delay(1000); //延時1s
}
HX711.h
#ifndef __HX711__H__
#define __HX711__H__
#include <Arduino.h>
#define HX711_SCK 6
#define HX711_DT 7
extern void Init_Hx711();
extern unsigned long HX711_Read(void);
extern unsigned int Get_Weight();
extern void Get_Maopi();
#endif
HX711.cpp
#include "hx711.h"
#define ValueGap 630
//該數(shù)值可用砝碼校準
long HX711_Buffer = 0;
long Weight_Maopi = 0,Weight_Shiwu = 0;
//****************************************************
//初始化HX711
//****************************************************
void Init_Hx711()
{
pinMode(HX711_SCK, OUTPUT);
pinMode(HX711_DT, INPUT);
}
//****************************************************
//獲取毛皮重量
//****************************************************
void Get_Maopi()
{
Weight_Maopi = HX711_Read();
}
//****************************************************
//稱重
//****************************************************
unsigned int Get_Weight()
{
HX711_Buffer = HX711_Read();
Weight_Shiwu = HX711_Buffer;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //獲取實物的AD采樣數(shù)值。
Weight_Shiwu = (int)((float)Weight_Shiwu/ValueGap+0.05);
return Weight_Shiwu;
}
//****************************************************
//讀取HX711
//****************************************************
unsigned long HX711_Read(void) //增益128
{
unsigned long count;
unsigned char i;
bool Flag = 0;
digitalWrite(HX711_DT, HIGH);
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
count=0;
while(digitalRead(HX711_DT));
for(i=0;i<24;i++)
{
digitalWrite(HX711_SCK, HIGH);
delayMicroseconds(1);
count=count<<1;
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
if(digitalRead(HX711_DT))
count++;
}
digitalWrite(HX711_SCK, HIGH);
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW);
delayMicroseconds(1);
count ^= 0x800000;
return(count);
}
|