Skip to content

Commit

Permalink
Add fast processor delays (#47)
Browse files Browse the repository at this point in the history
* Update HX711.cpp

Added variable clock delay for fast processors

* Update HX711.h

Added fast processor code to begin() and a private variable.

* Update README.md

Updated for fast_processor
  • Loading branch information
palmerr23 authored Mar 2, 2024
1 parent 1cdaa2f commit 0290957
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
13 changes: 10 additions & 3 deletions HX711.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ HX711::~HX711()
}


void HX711::begin(uint8_t dataPin, uint8_t clockPin)
void HX711::begin(uint8_t dataPin, uint8_t clockPin, bool fastProcessor )
{
_dataPin = dataPin;
_clockPin = clockPin;
_fast_processor = fastProcessor;

pinMode(_dataPin, INPUT);
pinMode(_clockPin, OUTPUT);
Expand Down Expand Up @@ -134,7 +135,11 @@ float HX711::read()
{
// delayMicroSeconds(1) needed for fast processors?
digitalWrite(_clockPin, HIGH);
if(_fast_processor)
delayMicroseconds(1);
digitalWrite(_clockPin, LOW);
if(_fast_processor)
delayMicroseconds(1);
m--;
}

Expand Down Expand Up @@ -445,13 +450,15 @@ uint8_t HX711::_shiftIn()
while (mask > 0)
{
digitalWrite(clk, HIGH);
delayMicroseconds(1); // T2 >= 0.2 us
if(_fast_processor)
delayMicroseconds(1);
if (digitalRead(data) == HIGH)
{
value |= mask;
}
digitalWrite(clk, LOW);
delayMicroseconds(1); // keep duty cycle ~50%
if(_fast_processor)
delayMicroseconds(1);
mask >>= 1;
}
return value;
Expand Down
3 changes: 2 additions & 1 deletion HX711.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class HX711
~HX711();

// fixed gain 128 for now
void begin(uint8_t dataPin, uint8_t clockPin);
void begin(uint8_t dataPin, uint8_t clockPin, bool fastProcessor = false);

void reset();

Expand Down Expand Up @@ -164,6 +164,7 @@ class HX711

void _insertSort(float * array, uint8_t size);
uint8_t _shiftIn();
bool _fast_processor = false;
};


Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ Steps to take for calibration

- **HX711()** constructor.
- **~HX711()**
- **void begin(uint8_t dataPin, uint8_t clockPin)** sets a fixed gain 128 for now.
- **void begin(uint8_t dataPin, uint8_t clockPin, bool fastProcessor)** sets a fixed gain 128 for now.
The fastProcessor option adds a 1uS delay for each clock half-cycle to keep the time greater than 200nS
- **void reset()** set internal state to start condition.
Since 0.3.4 reset also does a power down / up cycle.
- **bool is_ready()** checks if load cell is ready to read.
Expand Down

0 comments on commit 0290957

Please # to comment.