From 92f3ae64d66e954c7035bf65b88a117780e00a31 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sun, 2 Jun 2019 14:05:04 +0200 Subject: [PATCH] Fix HX711 --- esphome/components/hx711/hx711.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/esphome/components/hx711/hx711.cpp b/esphome/components/hx711/hx711.cpp index efa9e7b264..bc692c1a20 100644 --- a/esphome/components/hx711/hx711.cpp +++ b/esphome/components/hx711/hx711.cpp @@ -1,5 +1,6 @@ #include "hx711.h" #include "esphome/core/log.h" +#include "esphome/core/helpers.h" namespace esphome { namespace hx711 { @@ -10,6 +11,7 @@ void HX711Sensor::setup() { ESP_LOGCONFIG(TAG, "Setting up HX711 '%s'...", this->name_.c_str()); this->sck_pin_->setup(); this->dout_pin_->setup(); + this->sck_pin_->digital_write(false); // Read sensor once without publishing to set the gain this->read_sensor_(nullptr); @@ -39,10 +41,11 @@ bool HX711Sensor::read_sensor_(uint32_t *result) { this->status_clear_warning(); uint32_t data = 0; + disable_interrupts(); for (uint8_t i = 0; i < 24; i++) { this->sck_pin_->digital_write(true); delayMicroseconds(1); - data |= uint32_t(this->dout_pin_->digital_read()) << (24 - i); + data |= uint32_t(this->dout_pin_->digital_read()) << (23 - i); this->sck_pin_->digital_write(false); delayMicroseconds(1); } @@ -50,10 +53,16 @@ bool HX711Sensor::read_sensor_(uint32_t *result) { // Cycle clock pin for gain setting for (uint8_t i = 0; i < this->gain_; i++) { this->sck_pin_->digital_write(true); + delayMicroseconds(1); this->sck_pin_->digital_write(false); + delayMicroseconds(1); + } + enable_interrupts(); + + if (data & 0x800000ULL) { + data |= 0xFF000000ULL; } - data ^= 0x800000; if (result != nullptr) *result = data; return true;