From 028913fc10fd61f12d95330ecbb2204432772c1c Mon Sep 17 00:00:00 2001 From: Tobias Sandhaas <21081031+medlor@users.noreply.github.com> Date: Thu, 27 Jan 2022 21:18:24 +0100 Subject: [PATCH 1/3] Update config.h --- src/config.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/config.h b/src/config.h index 84704db..1ca410a 100644 --- a/src/config.h +++ b/src/config.h @@ -23,6 +23,9 @@ Note that you can also overide (reducing) the number of samples in use at any ti */ +#ifndef HX711_ADC_config_h +#define HX711_ADC_config_h + //number of samples in moving average dataset, value must be 1, 2, 4, 8, 16, 32, 64 or 128. #define SAMPLES 16 //default value: 16 @@ -39,3 +42,4 @@ Note that you can also overide (reducing) the number of samples in use at any ti //if required you can change the value to '1' to disable interrupts when writing to the sck pin. #define SCK_DISABLE_INTERRUPTS 0 //default value: 0 +#endif From 354da3ea454b9950139a1a5cb905a2d3c4687225 Mon Sep 17 00:00:00 2001 From: Tobias Sandhaas <21081031+medlor@users.noreply.github.com> Date: Fri, 28 Jan 2022 22:43:47 +0100 Subject: [PATCH 2/3] Update HX711_ADC.cpp add async functions --- src/HX711_ADC.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/src/HX711_ADC.cpp b/src/HX711_ADC.cpp index 0e486fa..26021c2 100644 --- a/src/HX711_ADC.cpp +++ b/src/HX711_ADC.cpp @@ -254,6 +254,45 @@ uint8_t HX711_ADC::update() return convRslt; } +// call the function dataWaitingAsync() in loop or from ISR to check if new data is available to read +// if conversion is ready, just call updateAsync() to read out 24 bit data and add to dataset +// returns 1 if data available , else 0 +bool HX711_ADC::dataWaitingAsync() +{ + if (dataWaiting) { lastDoutLowTime = millis(); return 1; } + byte dout = digitalRead(doutPin); //check if conversion is ready + if (!dout) + { + dataWaiting = true; + lastDoutLowTime = millis(); + signalTimeoutFlag = 0; + return 1; + } + else + { + //if (millis() > (lastDoutLowTime + SIGNAL_TIMEOUT)) + if (millis() - lastDoutLowTime > SIGNAL_TIMEOUT) + { + signalTimeoutFlag = 1; + } + convRslt = 0; + } + return 0; +} + +// if data is available call updateAsync() to convert it and add it to the dataset. +// call getData() to get latest value +bool HX711_ADC::updateAsync() +{ + if (dataWaiting) { + conversion24bit(); + dataWaiting = false; + return true; + } + return false; + +} + float HX711_ADC::getData() // return fresh data from the moving average dataset { long data = 0; @@ -297,19 +336,22 @@ void HX711_ADC::conversion24bit() //read 24 bit data, store in dataset and star uint8_t dout; convRslt = 0; if(SCK_DISABLE_INTERRUPTS) noInterrupts(); + for (uint8_t i = 0; i < (24 + GAIN); i++) - { //read 24 bit data + set gain and start next conversion - if(SCK_DELAY) delayMicroseconds(3); // could be required for faster mcu's, set value in config.h + { //read 24 bit data + set gain and start next conversion digitalWrite(sckPin, 1); - if(SCK_DELAY) delayMicroseconds(3); // could be required for faster mcu's, set value in config.h + if(SCK_DELAY) delayMicroseconds(1); // could be required for faster mcu's, set value in config.h digitalWrite(sckPin, 0); if (i < (24)) { dout = digitalRead(doutPin); data = (data << 1) | dout; + } else { + if(SCK_DELAY) delayMicroseconds(1); // could be required for faster mcu's, set value in config.h } } - if(SCK_DISABLE_INTERRUPTS) interrupts(); + if(SCK_DISABLE_INTERRUPTS) interrupts(); + /* The HX711 output range is min. 0x800000 and max. 0x7FFFFF (the value rolls over). In order to convert the range to min. 0x000000 and max. 0xFFFFFF, @@ -513,4 +555,4 @@ bool HX711_ADC::getSignalTimeoutFlag() //tare/zero-offset must be re-set after calling this. void HX711_ADC::setReverseOutput() { reverseVal = true; -} \ No newline at end of file +} From f6aee7fd029dbe6678524085a7e85ea3d727c405 Mon Sep 17 00:00:00 2001 From: Tobias Sandhaas <21081031+medlor@users.noreply.github.com> Date: Fri, 28 Jan 2022 22:44:08 +0100 Subject: [PATCH 3/3] Update HX711_ADC.h create async functions --- src/HX711_ADC.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/HX711_ADC.h b/src/HX711_ADC.h index 5a56c07..1ba6c2e 100644 --- a/src/HX711_ADC.h +++ b/src/HX711_ADC.h @@ -64,6 +64,7 @@ class HX711_ADC void setCalFactor(float cal); //set new calibration factor, raw data is divided by this value to convert to readable data float getCalFactor(); //returns the current calibration factor float getData(); //returns data from the moving average dataset + int getReadIndex(); //for testing and debugging float getConversionTime(); //for testing and debugging float getSPS(); //for testing and debugging @@ -75,6 +76,8 @@ class HX711_ADC long getTareOffset(); //get the tare offset (raw data value output without the scale "calFactor") void setTareOffset(long newoffset); //set new tare offset (raw data value input without the scale "calFactor") uint8_t update(); //if conversion is ready; read out 24 bit data and add to dataset + bool dataWaitingAsync(); //checks if data is available to read (no conversion yet) + bool updateAsync(); //read available data and add to dataset void setSamplesInUse(int samples); //overide number of samples in use int getSamplesInUse(); //returns current number of samples in use void resetSamplesIndex(); //resets index for dataset @@ -116,7 +119,8 @@ class HX711_ADC unsigned long lastDoutLowTime = 0; bool signalTimeoutFlag = 0; bool reverseVal = 0; + bool dataWaiting = 0; }; #endif - \ No newline at end of file +