Skip to content

Commit

Permalink
Merge pull request #92 from medlor/master
Browse files Browse the repository at this point in the history
Allow settings in config.h to be redefined
  • Loading branch information
olkal authored Jan 30, 2022
2 parents 3d624af + f6aee7f commit fb40144
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
52 changes: 47 additions & 5 deletions src/HX711_ADC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -513,4 +555,4 @@ bool HX711_ADC::getSignalTimeoutFlag()
//tare/zero-offset must be re-set after calling this.
void HX711_ADC::setReverseOutput() {
reverseVal = true;
}
}
6 changes: 5 additions & 1 deletion src/HX711_ADC.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -116,7 +119,8 @@ class HX711_ADC
unsigned long lastDoutLowTime = 0;
bool signalTimeoutFlag = 0;
bool reverseVal = 0;
bool dataWaiting = 0;
};

#endif


4 changes: 4 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit fb40144

Please sign in to comment.