-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathQ2HX711.cpp
58 lines (48 loc) · 1.06 KB
/
Q2HX711.cpp
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
#include <Arduino.h>
#include "Q2HX711.h"
Q2HX711::Q2HX711(byte output_pin, byte clock_pin) {
CLOCK_PIN = clock_pin;
OUT_PIN = output_pin;
GAIN = 1;
pinsConfigured = false;
}
Q2HX711::~Q2HX711() {
}
bool Q2HX711::readyToSend() {
if (!pinsConfigured) {
// We need to set the pin mode once, but not in the constructor
pinMode(CLOCK_PIN, OUTPUT);
pinMode(OUT_PIN, INPUT);
pinsConfigured = true;
}
return digitalRead(OUT_PIN) == LOW;
}
void Q2HX711::setGain(byte gain) {
switch (gain) {
case 128:
GAIN = 1;
break;
case 64:
GAIN = 3;
break;
case 32:
GAIN = 2;
break;
}
digitalWrite(CLOCK_PIN, LOW);
read();
}
long Q2HX711::read() {
while (!readyToSend());
byte data[3];
for (byte j = 3; j--;) {
data[j] = shiftIn(OUT_PIN,CLOCK_PIN, MSBFIRST);
}
// set gain
for (int i = 0; i < GAIN; i++) {
digitalWrite(CLOCK_PIN, HIGH);
digitalWrite(CLOCK_PIN, LOW);
}
data[2] ^= 0x80;
return ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8) | (uint32_t) data[0];
}