代码:
//电子秤
#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]; //定义重量值存储数组,5位,其中4位为数字,1位为小数点
int temp =0; //定义重量值中间变量,,用于将获取的float型重量值转变为unsigned char数组
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
//该数值可用砝码校准
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采样数值。
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);
}
|