|
尝试了n次,轮换了三种代码方案,没有达到想要的效果,求大佬回帖
HX711模块+应变片 读数不精准问题
方案一
#include <Q2HX711.h> //引用库文件
const byte hx711_data_pin = 7;
const byte hx711_clock_pin = 6;
Q2HX711 hx711(hx711_data_pin, hx711_clock_pin);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(String((hx711.read()/100000.00)*277.77-23040)+String("g")); //手动校准此参数
delay(500);
}
问题:每次重新上传程序后读数都会发生变化,移动也会发生变化
方案二
uint8_t HX_SCK = 6;
uint8_t HX_DT = 7;
long count0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(HX_SCK, OUTPUT);
pinMode(HX_DT, INPUT);
ReadCount();
delay(100);
count0 = 0;
for(int i=0; i<8; i++)
count0 += ReadCount();
count0 /= 8;
}
void loop() {
// put your main code here, to run repeatedly:
long count = ReadCount() - count0;
Serial.println(count/400);
delay(100);
}
long ReadCount()
{
digitalWrite(HX_SCK, LOW);
while(digitalRead(HX_DT));
unsigned long count = 0;
for(int i=0; i<24; i++)
{
digitalWrite(HX_SCK, HIGH);
count <<= 1;
digitalWrite(HX_SCK, LOW);
if(digitalRead(HX_DT)) count |= 1;
}
digitalWrite(HX_SCK, HIGH);
if(count & 0x00800000) count |= 0xFF000000;
digitalWrite(HX_SCK, LOW);
return (long)count;
}
问题:可自动校准,但无论是改变应变片的角度还是平移位置都会使读数发生改变
方案三
/////以下为函数声明
extern unsigned long HX711_Read(void);
extern long Get_Weight();
///变量定义
double Weight = 0; //float Weight = 0;
int HX711_SCK =6; /// 作为输出口
int HX711_DT= 7; /// 作为输入口
long HX711_Buffer = 0;
long Weight_Maopi = 0, Weight_Shiwu = 0;
#define GapValue 285 ///该值需校准 每个传感器都有所不同 测量值偏小则减小此值
void setup()
{
//初始化HX711的两个io口
pinMode(HX711_SCK, OUTPUT); ///SCK 为输出口 ---输出脉冲
pinMode(HX711_DT, INPUT); /// DT为输入口 ---读取数据
Serial.begin(9600);
Serial.print("Welcome to use!\n");
delay(3000); ///延时3秒
//获取毛皮重量
Weight_Maopi = HX711_Read();
}
void loop() /// 一直循环{}内容 ----- 同while(1){xxx}
{
Weight = Get_Weight(); //计算放在传感器上的重物重量
Serial.print(float(Weight/1000),3); //串口显示重量,3意为保留三位小数
Serial.print(" kg\n"); //显示单位
Serial.print("\n"); //显示单位
delay(2000); //延时2s 两秒读取一次传感器所受压力
}
//称重函数
long Get_Weight()
{
HX711_Buffer = HX711_Read(); ///读取此时的传感器输出值
Weight_Shiwu = HX711_Buffer; ///将传感器的输出值储存
Weight_Shiwu = Weight_Shiwu - Weight_Maopi; //获取实物的AD采样数值。
Weight_Shiwu = (long)((float)Weight_Shiwu/GapValue); //AD值转换为重量(g)
return Weight_Shiwu;
}
unsigned long HX711_Read(void) //选择芯片工作方式并进行数据读取
{
unsigned long count; ///储存输出值
unsigned char i;
////high--高电平 1 low--低电平 0
digitalWrite(HX711_DT, HIGH); //// digitalWrite作用: DT=1;
delayMicroseconds(1); ////延时 1微秒
digitalWrite(HX711_SCK, LOW); //// digitalWrite作用: SCK=0;
delayMicroseconds(1); ////延时 1微秒
count=0;
while(digitalRead(HX711_DT)); //当DT的值为1时,开始ad转换
for(i=0;i<24;i++) ///24个脉冲,对应读取24位数值
{
digitalWrite(HX711_SCK, HIGH); //// digitalWrite作用: SCK=0;
/// 利用 SCK从0--1 ,发送一次脉冲,读取数值
delayMicroseconds(1); ////延时 1微秒
count=count<<1; ///用于移位存储24位二进制数值
digitalWrite(HX711_SCK, LOW); //// digitalWrite作用: SCK=0;为下次脉冲做准备
delayMicroseconds(1);
if(digitalRead(HX711_DT)) ///若DT值为1,对应count输出值也为1
count++;
}
digitalWrite(HX711_SCK, HIGH); ///再来一次上升沿 选择工作方式 128增益
count ^= 0x800000; //按位异或 不同则为1 0^0=0; 1^0=1;
///对应二进制 1000 0000 0000 0000 0000 0000 作用为将最高位取反,其他位保留原值
delayMicroseconds(1);
digitalWrite(HX711_SCK, LOW); /// SCK=0;
delayMicroseconds(1); ////延时 1微秒
return(count); ///返回传感器读取值
}
问题:同上一个问题
程序方面行不通
还试图通过供电的问题来解决
学生电源 板载电源 外接电脑电源 均无法解决读数不精准问题
无论是更换hx711模块还是应变片,问题亦无法解决
|
|