Skip to content

Commit d25517e

Browse files
committed
1.2.4
1 parent 2e7ec40 commit d25517e

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=HX711_ADC
2-
version=1.2.3
2+
version=1.2.4
33
author=Olav Kallhovd
44
maintainer=Olav Kallhovd
55
sentence=Library for the HX711 24-bit ADC for weight scales.

src/HX711_ADC.cpp

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void HX711_ADC::begin(uint8_t gain)
4444
/* start(t):
4545
* will do conversions continuously for 't' +400 milliseconds (400ms is min. settling time at 10SPS).
4646
* Running this for 1-5s in setup() - before tare() seems to improve the tare accuracy */
47-
int HX711_ADC::start(unsigned int t)
47+
void HX711_ADC::start(unsigned int t)
4848
{
4949
t += 400;
5050
lastDoutLowTime = millis();
@@ -60,7 +60,7 @@ int HX711_ADC::start(unsigned int t)
6060
/* start(t, dotare) with selectable tare:
6161
* will do conversions continuously for 't' +400 milliseconds (400ms is min. settling time at 10SPS).
6262
* Running this for 1-5s in setup() - before tare() seems to improve the tare accuracy. */
63-
int HX711_ADC::start(unsigned int t, bool dotare)
63+
void HX711_ADC::start(unsigned int t, bool dotare)
6464
{
6565
t += 400;
6666
lastDoutLowTime = millis();
@@ -96,7 +96,7 @@ int HX711_ADC::startMultiple(unsigned int t)
9696
}
9797
isFirst = 0;
9898
}
99-
if(millis() < startMultipleTimeStamp + startMultipleWaitTime) {
99+
if(millis() - startMultipleTimeStamp > startMultipleWaitTime) {
100100
update(); //do conversions during stabilization time
101101
yield();
102102
return 0;
@@ -145,7 +145,7 @@ int HX711_ADC::startMultiple(unsigned int t, bool dotare)
145145
}
146146
isFirst = 0;
147147
}
148-
if(millis() < startMultipleTimeStamp + startMultipleWaitTime) {
148+
if(millis() - startMultipleTimeStamp > startMultipleWaitTime) {
149149
update(); //do conversions during stabilization time
150150
yield();
151151
return 0;
@@ -210,6 +210,7 @@ void HX711_ADC::tareNoDelay()
210210
void HX711_ADC::setCalFactor(float cal)
211211
{
212212
calFactor = cal;
213+
calFactorRecip = 1/calFactor;
213214
}
214215

215216
//returns 'true' if tareNoDelay() operation is complete
@@ -241,7 +242,8 @@ uint8_t HX711_ADC::update()
241242
}
242243
else
243244
{
244-
if (millis() > (lastDoutLowTime + SIGNAL_TIMEOUT))
245+
//if (millis() > (lastDoutLowTime + SIGNAL_TIMEOUT))
246+
if (millis() - lastDoutLowTime > SIGNAL_TIMEOUT)
245247
{
246248
signalTimeoutFlag = 1;
247249
}
@@ -252,14 +254,10 @@ uint8_t HX711_ADC::update()
252254

253255
float HX711_ADC::getData() // return fresh data from the moving average dataset
254256
{
255-
//long k = 0;
256257
long data = 0;
257-
//data = smoothedData() - tareOffset;
258258
lastSmoothedData = smoothedData();
259-
data = (lastSmoothedData >> divBit) - tareOffset ;
260-
//data = (data >> divBit);
261-
262-
float x = (float)data / calFactor;
259+
data = lastSmoothedData - tareOffset ;
260+
float x = (float)data * calFactorRecip;
263261
return x;
264262
}
265263

@@ -268,19 +266,28 @@ long HX711_ADC::smoothedData()
268266
long data = 0;
269267
long L = 0xFFFFFF;
270268
long H = 0x00;
271-
//for (uint8_t r = 0; r < DATA_SET; r++) {
272269
for (uint8_t r = 0; r < (samplesInUse + IGN_HIGH_SAMPLE + IGN_LOW_SAMPLE); r++)
273270
{
271+
#if IGN_LOW_SAMPLE
274272
if (L > dataSampleSet[r]) L = dataSampleSet[r]; // find lowest value
273+
#endif
274+
#if IGN_HIGH_SAMPLE
275275
if (H < dataSampleSet[r]) H = dataSampleSet[r]; // find highest value
276+
#endif
276277
data += dataSampleSet[r];
277278
}
278-
if(IGN_LOW_SAMPLE) data -= L; //remove lowest value
279-
if(IGN_HIGH_SAMPLE) data -= H; //remove highest value
280-
return data;
279+
#if IGN_LOW_SAMPLE
280+
data -= L; //remove lowest value
281+
#endif
282+
#if IGN_HIGH_SAMPLE
283+
data -= H; //remove highest value
284+
#endif
285+
//return data;
286+
return (data >> divBit);
287+
281288
}
282289

283-
uint8_t HX711_ADC::conversion24bit() //read 24 bit data, store in dataset and start the next conversion
290+
void HX711_ADC::conversion24bit() //read 24 bit data, store in dataset and start the next conversion
284291
{
285292
conversionTime = micros() - conversionStartTime;
286293
conversionStartTime = micros();
@@ -290,24 +297,25 @@ uint8_t HX711_ADC::conversion24bit() //read 24 bit data, store in dataset and s
290297
if(SCK_DISABLE_INTERRUPTS) noInterrupts();
291298
for (uint8_t i = 0; i < (24 + GAIN); i++)
292299
{ //read 24 bit data + set gain and start next conversion
293-
if(SCK_DELAY) delayMicroseconds(1); // could be required for faster mcu's, set value in config.h
300+
if(SCK_DELAY) delayMicroseconds(3); // could be required for faster mcu's, set value in config.h
294301
digitalWrite(sckPin, 1);
295-
if(SCK_DELAY) delayMicroseconds(1); // could be required for faster mcu's, set value in config.h
302+
if(SCK_DELAY) delayMicroseconds(3); // could be required for faster mcu's, set value in config.h
296303
digitalWrite(sckPin, 0);
297304
if (i < (24))
298305
{
299306
dout = digitalRead(doutPin);
300-
data = data << 1;
301-
if (dout)
302-
{
303-
data++;
304-
}
307+
data = (data << 1) | dout;
305308
}
306309
}
307310
if(SCK_DISABLE_INTERRUPTS) interrupts();
308-
//convert range from 0x800000 > 0x7FFFFF to 0x000000 > 0xFFFFFF:
309-
data = data ^ 0x800000; // if the 24th bit is '1', change 24th bit to 0
310-
if ((data < 0x000000) || (data > 0xFFFFFF))
311+
/*
312+
The HX711 output range is min. 0x800000 and max. 0x7FFFFF (the value rolls over).
313+
In order to convert the range to min. 0x000000 and max. 0xFFFFFF,
314+
the 24th bit must be changed from 0 to 1 or from 1 to 0.
315+
*/
316+
data = data ^ 0x800000; // flip the 24th bit
317+
318+
if (data > 0xFFFFFF)
311319
{
312320
dataOutOfRange = 1;
313321
//Serial.println("dataOutOfRange");
@@ -332,7 +340,7 @@ uint8_t HX711_ADC::conversion24bit() //read 24 bit data, store in dataset and s
332340
}
333341
else
334342
{
335-
tareOffset = (smoothedData() >> divBit);
343+
tareOffset = smoothedData();
336344
tareTimes = 0;
337345
doTare = 0;
338346
tareStatus = 1;
@@ -433,7 +441,7 @@ void HX711_ADC::setSamplesInUse(int samples)
433441
{
434442
for (uint8_t r = 0; r < samplesInUse + IGN_HIGH_SAMPLE + IGN_LOW_SAMPLE; r++)
435443
{
436-
dataSampleSet[r] = (lastSmoothedData >> old_divbit);
444+
dataSampleSet[r] = lastSmoothedData;
437445
}
438446
readIndex = 0;
439447
}

src/HX711_ADC.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ class HX711_ADC
5454
void setGain(uint8_t gain = 128); //value must be 32, 64 or 128*
5555
void begin(); //set pinMode, HX711 gain and power up the HX711
5656
void begin(uint8_t gain); //set pinMode, HX711 selected gain and power up the HX711
57-
int start(unsigned int t); //start HX711 and do tare
58-
int start(unsigned int t, bool dotare); //start HX711, do tare if selected
57+
void start(unsigned int t); //start HX711 and do tare
58+
void start(unsigned int t, bool dotare); //start HX711, do tare if selected
5959
int startMultiple(unsigned int t); //start and do tare, multiple HX711 simultaniously
6060
int startMultiple(unsigned int t, bool dotare); //start and do tare if selected, multiple HX711 simultaniously
6161
void tare(); //zero the scale, wait for tare to finnish (blocking)
@@ -84,12 +84,13 @@ class HX711_ADC
8484
bool getSignalTimeoutFlag(); //returns 'true' if it takes longer time then 'SIGNAL_TIMEOUT' for the dout pin to go low after a new conversion is started
8585

8686
protected:
87-
uint8_t conversion24bit(); //if conversion is ready: returns 24 bit data and starts the next conversion
87+
void conversion24bit(); //if conversion is ready: returns 24 bit data and starts the next conversion
8888
long smoothedData(); //returns the smoothed data value calculated from the dataset
8989
uint8_t sckPin; //HX711 pd_sck pin
9090
uint8_t doutPin; //HX711 dout pin
9191
uint8_t GAIN; //HX711 GAIN
92-
float calFactor = 1.0; //calibration factor, the raw data is divided with this number
92+
float calFactor = 1.0; //calibration factor as given in function setCalFactor(float cal)
93+
float calFactorRecip = 1.0; //reciprocal calibration factor (1/calFactor), the HX711 raw data is multiplied by this value
9394
volatile long dataSampleSet[DATA_SET + 1]; // dataset, make voltile if interrupt is used
9495
long tareOffset;
9596
int readIndex = 0;

src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Note that you can also overide (reducing) the number of samples in use at any ti
3030
#define IGN_HIGH_SAMPLE 1 //default value: 1
3131
#define IGN_LOW_SAMPLE 1 //default value: 1
3232

33-
//1 microsecond delay after writing sck pin high or low. This delay could be required for faster mcu's.
33+
//microsecond delay after writing sck pin high or low. This delay could be required for faster mcu's.
3434
//So far the only mcu reported to need this delay is the ESP32 (issue #35), both the Arduino Due and ESP8266 seems to run fine without it.
3535
//Change the value to '1' to enable the delay.
3636
#define SCK_DELAY 0 //default value: 0

0 commit comments

Comments
 (0)