Libery for HX711 sensor. This is for measureing the weigth of the keg
Revision 0:52c805a049c4, committed 2016-02-21
- Comitter:
- gert_lauritsen
- Date:
- Sun Feb 21 11:01:00 2016 +0000
- Commit message:
- A lib for hx711 SG sensor
Changed in this revision
Hx711.cpp | Show annotated file Show diff for this revision Revisions of this file |
Hx711.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 52c805a049c4 Hx711.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hx711.cpp Sun Feb 21 11:01:00 2016 +0000 @@ -0,0 +1,102 @@ +#include "Hx711.h" + +HX711::HX711(PinName pin_dout, PinName pin_slk): _pin_dout(pin_dout), _pin_slk(pin_slk) +{ + _pin_slk = 1; + wait_ms(100); + _pin_slk = 0; + + set_gain(128); +} + +HX711::~HX711() +{ + +} + + +void HX711::set_gain(char gain) +{ + switch (gain) { + case 128: // channel A, gain factor 128 + GAIN = 1; + break; + case 64: // channel A, gain factor 64 + GAIN = 3; + break; + case 32: // channel B, gain factor 32 + GAIN = 2; + break; + } + _pin_slk = 0; + read(); +} + +long HX711::read() +{ + long value = 0; + + while (_pin_dout) + ; + + for (int i=0;i<24;i++) + { + _pin_slk = 1; + value=value<<1; + _pin_slk = 0; + if(_pin_dout) value++; + } + + _pin_slk = 1; + _pin_slk = 0; + + value=value^0x800000; + + return value; +} + +long HX711::read_average(char times) +{ + long sum = 0; + for (char i = 0; i < times; i++) { + sum += read(); + } + return sum / times; +} + +double HX711::get_value(char times) +{ + return read_average(times) - OFFSET; +} + +float HX711::get_units(char times) +{ + return get_value(times) / SCALE; +} + +void HX711::tare(char times) +{ + double sum = read_average(times); + set_offset(sum); +} + +void HX711::set_scale(float scale) +{ + SCALE = scale; +} + +void HX711::set_offset(long offset) +{ + OFFSET = offset; +} + +void HX711::power_down() +{ + _pin_slk = 0; + _pin_slk = 1; +} + +void HX711::power_up() +{ + _pin_slk = 0; +} \ No newline at end of file
diff -r 000000000000 -r 52c805a049c4 Hx711.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hx711.h Sun Feb 21 11:01:00 2016 +0000 @@ -0,0 +1,62 @@ +#ifndef HX711_h +#define HX711_h +//Translatet module from arduino to mbed https://codebender.cc/library/HX711#HX711.h + +#include "mbed.h" + +class HX711 +{ + private: + DigitalOut _pin_slk; // Power Down and Serial Clock Input Pin + DigitalIn _pin_dout; // Serial Data Output Pin + char GAIN; // amplification factor + long OFFSET; // used for tare weight + float SCALE; // used to return weight in grams, kg, ounces, whatever + public: + // define clock and data pin, channel, and gain factor + // channel selection is made by passing the appropriate gain: 128 or 64 for channel A, 32 for channel B + // gain: 128 or 64 for channel A; channel B works with 32 gain factor only + HX711(PinName pin_dout, PinName pin_slk); + + virtual ~HX711(); + + // check if HX711 is ready + // from the datasheet: When output data is not ready for retrieval, digital output pin DOUT is high. Serial clock + // input PD_SCK should be low. When DOUT goes to low, it indicates data is ready for retrieval. + + + // set the gain factor; takes effect only after a call to read() + // channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain + // depending on the parameter, the channel is also set to either A or B + void set_gain(char gain = 128); + + // waits for the chip to be ready and returns a reading + long read(); + + // returns an average reading; times = how many times to read + long read_average(char times = 10); + + // returns (read_average() - OFFSET), that is the current value without the tare weight; times = how many readings to do + double get_value(char times = 1); + + // returns get_value() divided by SCALE, that is the raw value divided by a value obtained via calibration + // times = how many readings to do + float get_units(char times = 1); + + // set the OFFSET value for tare weight; times = how many times to read the tare value + void tare(char times = 10); + + // set the SCALE value; this value is used to convert the raw data to "human readable" data (measure units) + void set_scale(float scale = 1.f); + + // set OFFSET, the value that's subtracted from the actual reading (tare weight) + void set_offset(long offset = 0); + + // puts the chip into power down mode + void power_down(); + + // wakes up the chip after power down mode + void power_up(); +}; + +#endif /* HX711_h */ \ No newline at end of file