From 22ce88aa9babf477845e5f63fda88f28ba754b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Sat, 12 Nov 2016 15:27:08 +0100 Subject: [PATCH 1/2] Add two new functions; powerDown() and powerUp(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The HX711 will enter power down when PD_SCK is high for more then 60μs. On my demo board powerDown() lowers the current consumption from ~15mA to less than 0.01mA. --- src/Q2HX711.cpp | 21 ++++++++++++++++++++- src/Q2HX711.h | 3 +++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Q2HX711.cpp b/src/Q2HX711.cpp index 1c82bb5..e309d03 100644 --- a/src/Q2HX711.cpp +++ b/src/Q2HX711.cpp @@ -12,6 +12,25 @@ 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; } @@ -29,7 +48,7 @@ void Q2HX711::setGain(byte gain) { break; } - digitalWrite(CLOCK_PIN, LOW); + powerUp(); read(); } diff --git a/src/Q2HX711.h b/src/Q2HX711.h index a8a4c4b..f55b85e 100644 --- a/src/Q2HX711.h +++ b/src/Q2HX711.h @@ -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(); }; From e9385676938add56e5db3475b60f58d2f08fa806 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Thing=20Andersen?= Date: Sat, 12 Nov 2016 16:15:03 +0100 Subject: [PATCH 2/2] Fix two's complement calculation --- src/Q2HX711.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Q2HX711.cpp b/src/Q2HX711.cpp index e309d03..2ae583b 100644 --- a/src/Q2HX711.cpp +++ b/src/Q2HX711.cpp @@ -37,13 +37,13 @@ bool Q2HX711::readyToSend() { 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; } @@ -52,21 +52,31 @@ void Q2HX711::setGain(byte gain) { 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; }