Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions src/Q2HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,71 @@ Q2HX711::Q2HX711(byte output_pin, byte clock_pin) {
Q2HX711::~Q2HX711() {
}

/**
* Puts the chip in power down mode by setting the clock signal high.
*
* The HX711 will enter power down when PD_SCK is high for more then 60μs.
*/
void Q2HX711::powerDown() {
digitalWrite(CLOCK_PIN, HIGH);
}

/**
* Wakes the chip from power down mode by setting the clock signal low.
*
* When PD_SCK returns to low, the HX711 will reset and enter normal operation mode.
* After a reset or power-down event, input selection is default to Channel A with a gain of 128.
*/
void Q2HX711::powerUp() {
digitalWrite(CLOCK_PIN, LOW);
}

bool Q2HX711::readyToSend() {
return digitalRead(OUT_PIN) == LOW;
}

void Q2HX711::setGain(byte gain) {
switch (gain) {
case 128:
case 128: // Input Channel A
GAIN = 1;
break;
case 64:
case 64: // Input Channel A
GAIN = 3;
break;
case 32:
case 32: // Input Channel B
GAIN = 2;
break;
}

digitalWrite(CLOCK_PIN, LOW);
powerUp();
read();
}

/**
* Reads a value from the chip and returns the raw value.
*/
long Q2HX711::read() {
while (!readyToSend());

// Read 24 bit data, most significant bit first.
byte data[3];

for (byte j = 3; j--;) {
data[j] = shiftIn(OUT_PIN,CLOCK_PIN, MSBFIRST);
data[j] = shiftIn(OUT_PIN, CLOCK_PIN, MSBFIRST);
}

// set gain
// Set gain and input for next conversion by pulsing the clock signal 1 to 3 times.
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];
// Collect the bytes in a signed integer
long value = ((uint32_t) data[2] << 16) | ((uint32_t) data[1] << 8) | (uint32_t) data[0];
// The value read from the HX711 is in two's complement.
// Normally the most significant bit of a 24 bit number represents 2^23.
// But for a 24 bit two's complement number, the bit represents -2^23 (a negative value).
if (value & (1L << 23)) {
value -= (1L << 24);
}
return value;
}
3 changes: 3 additions & 0 deletions src/Q2HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ class Q2HX711
byte OUT_PIN;
byte GAIN;
void setGain(byte gain = 128);

public:
Q2HX711(byte output_pin, byte clock_pin);
virtual ~Q2HX711();
void powerDown();
void powerUp();
bool readyToSend();
long read();
};
Expand Down