Skip to content

Commit 3c4f007

Browse files
committed
0.3.7 HX711
1 parent dc5efd3 commit 3c4f007

File tree

13 files changed

+607
-139
lines changed

13 files changed

+607
-139
lines changed

libraries/HX711/CHANGELOG..md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,21 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [0.3.7] - 2023-06-27
9+
- add example to measure noise level
10+
- moved code to .cpp
11+
- reorder .cpp to match .h
12+
- removed **callibrate_scale()** (typo ll)
13+
- add scale == 0 in **set_scale(scale)**
14+
- changed return type to **bool set_scale(scale)**
15+
- add example is_ready
16+
- add example pulse-length decoder (morse)
17+
- update readme.md
18+
19+
820
## [0.3.6] - 2023-03-11
921
- update readme.md to reference HX711_MP
1022

11-
1223
## [0.3.5] - 2023-03-10
1324
- update readme.md
1425
- update GitHub actions

libraries/HX711/HX711.cpp

Lines changed: 204 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: HX711.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.6
4+
// VERSION: 0.3.7
55
// PURPOSE: Library for load cells for UNO
66
// URL: https://github.com/RobTillaart/HX711
77

@@ -15,7 +15,9 @@ HX711::HX711()
1515
}
1616

1717

18-
HX711::~HX711() {}
18+
HX711::~HX711()
19+
{
20+
}
1921

2022

2123
void HX711::begin(uint8_t dataPin, uint8_t clockPin)
@@ -49,6 +51,42 @@ bool HX711::is_ready()
4951
}
5052

5153

54+
void HX711::wait_ready(uint32_t ms)
55+
{
56+
while (!is_ready())
57+
{
58+
delay(ms);
59+
}
60+
}
61+
62+
63+
bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
64+
{
65+
while (retries--)
66+
{
67+
if (is_ready()) return true;
68+
delay(ms);
69+
}
70+
return false;
71+
}
72+
73+
74+
bool HX711::wait_ready_timeout(uint32_t timeout, uint32_t ms)
75+
{
76+
uint32_t start = millis();
77+
while (millis() - start < timeout)
78+
{
79+
if (is_ready()) return true;
80+
delay(ms);
81+
}
82+
return false;
83+
}
84+
85+
86+
///////////////////////////////////////////////////////////
87+
//
88+
// READ
89+
//
5290
// From datasheet page 4
5391
// When output data is not ready for retrieval,
5492
// digital output pin DOUT is HIGH.
@@ -111,78 +149,6 @@ float HX711::read()
111149
}
112150

113151

114-
// note: if parameter gain == 0xFF40 some compilers
115-
// will map that to 0x40 == HX711_CHANNEL_A_GAIN_64;
116-
// solution: use uint32_t or larger parameters everywhere.
117-
// note that changing gain/channel may take up to 400 ms (page 3)
118-
bool HX711::set_gain(uint8_t gain, bool forced)
119-
{
120-
if ( (not forced) && (_gain == gain)) return true;
121-
switch(gain)
122-
{
123-
case HX711_CHANNEL_B_GAIN_32:
124-
case HX711_CHANNEL_A_GAIN_64:
125-
case HX711_CHANNEL_A_GAIN_128:
126-
_gain = gain;
127-
read(); // next user read() is from right channel / gain
128-
return true;
129-
}
130-
return false; // unchanged, but incorrect value.
131-
}
132-
133-
134-
uint8_t HX711::get_gain()
135-
{
136-
return _gain;
137-
}
138-
139-
140-
// assumes tare() has been set.
141-
void HX711::calibrate_scale(uint16_t weight, uint8_t times)
142-
{
143-
_scale = (1.0 * weight) / (read_average(times) - _offset);
144-
}
145-
146-
147-
// OBSOLETE 0.4.0 (LL is wrong)
148-
void HX711::callibrate_scale(uint16_t weight, uint8_t times)
149-
{
150-
calibrate_scale(weight, times);
151-
};
152-
153-
154-
void HX711::wait_ready(uint32_t ms)
155-
{
156-
while (!is_ready())
157-
{
158-
delay(ms);
159-
}
160-
}
161-
162-
163-
bool HX711::wait_ready_retry(uint8_t retries, uint32_t ms)
164-
{
165-
while (retries--)
166-
{
167-
if (is_ready()) return true;
168-
delay(ms);
169-
}
170-
return false;
171-
}
172-
173-
174-
bool HX711::wait_ready_timeout(uint32_t timeout, uint32_t ms)
175-
{
176-
uint32_t start = millis();
177-
while (millis() - start < timeout)
178-
{
179-
if (is_ready()) return true;
180-
delay(ms);
181-
}
182-
return false;
183-
}
184-
185-
186152
float HX711::read_average(uint8_t times)
187153
{
188154
if (times < 1) times = 1;
@@ -252,22 +218,44 @@ float HX711::read_runavg(uint8_t times, float alpha)
252218
}
253219

254220

255-
void HX711::_insertSort(float * array, uint8_t size)
221+
///////////////////////////////////////////////////////
222+
//
223+
// MODE
224+
//
225+
void HX711::set_raw_mode()
256226
{
257-
uint8_t t, z;
258-
float temp;
259-
for (t = 1; t < size; t++)
260-
{
261-
z = t;
262-
temp = array[z];
263-
while( (z > 0) && (temp < array[z - 1] ))
264-
{
265-
array[z] = array[z - 1];
266-
z--;
267-
}
268-
array[z] = temp;
269-
yield();
270-
}
227+
_mode = HX711_RAW_MODE;
228+
}
229+
230+
231+
void HX711::set_average_mode()
232+
{
233+
_mode = HX711_AVERAGE_MODE;
234+
}
235+
236+
237+
void HX711::set_median_mode()
238+
{
239+
_mode = HX711_MEDIAN_MODE;
240+
}
241+
242+
243+
void HX711::set_medavg_mode()
244+
{
245+
_mode = HX711_MEDAVG_MODE;
246+
}
247+
248+
249+
// set_run_avg will use a default alpha of 0.5.
250+
void HX711::set_runavg_mode()
251+
{
252+
_mode = HX711_RUNAVG_MODE;
253+
}
254+
255+
256+
uint8_t HX711::get_mode()
257+
{
258+
return _mode;
271259
}
272260

273261

@@ -304,6 +292,99 @@ float HX711::get_units(uint8_t times)
304292
};
305293

306294

295+
///////////////////////////////////////////////////////
296+
//
297+
// TARE
298+
//
299+
void HX711::tare(uint8_t times)
300+
{
301+
_offset = read_average(times);
302+
}
303+
304+
305+
float HX711::get_tare()
306+
{
307+
return -_offset * _scale;
308+
}
309+
310+
311+
bool HX711::tare_set()
312+
{
313+
return _offset != 0;
314+
}
315+
316+
317+
///////////////////////////////////////////////////////
318+
//
319+
// GAIN
320+
//
321+
// note: if parameter gain == 0xFF40 some compilers
322+
// will map that to 0x40 == HX711_CHANNEL_A_GAIN_64;
323+
// solution: use uint32_t or larger parameters everywhere.
324+
// note that changing gain/channel may take up to 400 ms (page 3)
325+
bool HX711::set_gain(uint8_t gain, bool forced)
326+
{
327+
if ( (not forced) && (_gain == gain)) return true;
328+
switch(gain)
329+
{
330+
case HX711_CHANNEL_B_GAIN_32:
331+
case HX711_CHANNEL_A_GAIN_64:
332+
case HX711_CHANNEL_A_GAIN_128:
333+
_gain = gain;
334+
read(); // next user read() is from right channel / gain
335+
return true;
336+
}
337+
return false; // unchanged, but incorrect value.
338+
}
339+
340+
341+
uint8_t HX711::get_gain()
342+
{
343+
return _gain;
344+
}
345+
346+
347+
///////////////////////////////////////////////////////
348+
//
349+
// CALIBRATION AND SETUP
350+
//
351+
bool HX711::set_scale(float scale)
352+
{
353+
if (scale == 0) return false;
354+
_scale = 1.0 / scale;
355+
return true;
356+
}
357+
358+
359+
float HX711::get_scale()
360+
{
361+
return 1.0 / _scale;
362+
}
363+
364+
365+
void HX711::set_offset(long offset)
366+
{
367+
_offset = offset;
368+
}
369+
370+
371+
long HX711::get_offset()
372+
{
373+
return _offset;
374+
}
375+
376+
377+
// assumes tare() has been set.
378+
void HX711::calibrate_scale(uint16_t weight, uint8_t times)
379+
{
380+
_scale = (1.0 * weight) / (read_average(times) - _offset);
381+
}
382+
383+
384+
///////////////////////////////////////////////////////
385+
//
386+
// POWER
387+
//
307388
void HX711::power_down()
308389
{
309390
// at least 60 us HIGH
@@ -318,6 +399,40 @@ void HX711::power_up()
318399
}
319400

320401

402+
///////////////////////////////////////////////////////
403+
//
404+
// MISC
405+
//
406+
uint32_t HX711::last_read()
407+
{
408+
return _lastRead;
409+
}
410+
411+
412+
/////////////////////////////////////////////////////////
413+
//
414+
// PRIVATE
415+
//
416+
417+
void HX711::_insertSort(float * array, uint8_t size)
418+
{
419+
uint8_t t, z;
420+
float temp;
421+
for (t = 1; t < size; t++)
422+
{
423+
z = t;
424+
temp = array[z];
425+
while( (z > 0) && (temp < array[z - 1] ))
426+
{
427+
array[z] = array[z - 1];
428+
z--;
429+
}
430+
array[z] = temp;
431+
yield();
432+
}
433+
}
434+
435+
321436
// MSB_FIRST optimized shiftIn
322437
// see datasheet page 5 for timing
323438
uint8_t HX711::_shiftIn()

0 commit comments

Comments
 (0)