-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHX711.java
89 lines (74 loc) · 2.22 KB
/
HX711.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package kiosk;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
/**
*
* @author Z002VDJE
*/
public class HX711 {
private final GpioPinDigitalOutput pinCLK;
private final GpioPinDigitalInput pinDAT;
private int gain;
public long emptyValue = 0;
public double emptyWeight = 0.0d;
public long calibrationValue = 0;
public double calibrationWeight = 0.0d;
public double fullCupWeight = 200.0d;
public double weight = 0.0d;
public long value = 0;
public HX711(GpioPinDigitalInput pinDAT, GpioPinDigitalOutput pinSCK, int gain) {
this.pinCLK = pinSCK;
this.pinDAT = pinDAT;
setGain(gain);
}
public void read() {
pinCLK.setState(PinState.LOW);
while (!isReady()) {
sleep(1);
}
long count = 0;
for (int i = 0; i < this.gain; i++) {
pinCLK.setState(PinState.HIGH);
count = count << 1;
pinCLK.setState(PinState.LOW);
if (pinDAT.isHigh()) {
count++;
}
}
pinCLK.setState(PinState.HIGH);
count = count ^ 0x800000;
pinCLK.setState(PinState.LOW);
value = count;
weight = (value - emptyValue)*((calibrationWeight - emptyWeight)/(calibrationValue - emptyValue));
}
public void setGain(int gain) {
switch (gain) {
case 128: // channel A, gain factor 128
this.gain = 24;
break;
case 64: // channel A, gain factor 64
this.gain = 26;
break;
case 32: // channel B, gain factor 32
this.gain = 25;
break;
}
pinCLK.setState(PinState.LOW);
read();
}
public boolean isReady() {
return (pinDAT.isLow());
}
private void sleep(long delay) {
try {
Thread.sleep(delay);
} catch (Exception ex) {
}
}
}