Library to read out HX711 24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales by AVIA Semiconductor. Tested with K22F with SCK pint at D13 and DT pin at D12

Dependents:   SmartCrutches

Committer:
megrootens
Date:
Thu Jun 23 14:16:26 2016 +0000
Revision:
5:16627e96c8de
Parent:
3:9bbfb7f8c751
removed TODO; should not be done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
megrootens 0:5fa72cacd94b 1 #ifndef _HX711_H_
megrootens 0:5fa72cacd94b 2 #define _HX711_H_
megrootens 0:5fa72cacd94b 3
megrootens 0:5fa72cacd94b 4 /**
megrootens 0:5fa72cacd94b 5 * Class for communication with the HX711 24-Bit Analog-to-Digital
megrootens 0:5fa72cacd94b 6 * Converter (ADC) for Weigh Scales by AVIA Semiconductor.
megrootens 0:5fa72cacd94b 7 * This library is a port of the Arduino library at
megrootens 0:5fa72cacd94b 8 * https://github.com/bogde/HX711
megrootens 0:5fa72cacd94b 9 * It works with the FRDM K22F.
megrootens 0:5fa72cacd94b 10 */
megrootens 0:5fa72cacd94b 11 class Hx711 {
megrootens 1:197ef68ad6de 12
megrootens 0:5fa72cacd94b 13 public:
megrootens 1:197ef68ad6de 14
megrootens 0:5fa72cacd94b 15 /**
megrootens 0:5fa72cacd94b 16 * Create an Hx711 ADC object
megrootens 0:5fa72cacd94b 17 * @param pin_sck PinName of the clock pin (digital output)
megrootens 0:5fa72cacd94b 18 * @param pin_dt PinName of the data pin (digital input)
megrootens 0:5fa72cacd94b 19 * @param offset offset for sensor values
megrootens 0:5fa72cacd94b 20 * @param scale scale factor to obtain real values
megrootens 0:5fa72cacd94b 21 * @param gain channel selection is made by passing the appropriate gain:
megrootens 0:5fa72cacd94b 22 * 128 or 64 for channel A, 32 for channel B
megrootens 0:5fa72cacd94b 23 */
megrootens 0:5fa72cacd94b 24 Hx711(PinName pin_sck, PinName pin_dt, int offset, float scale, uint8_t gain = 128) :
megrootens 0:5fa72cacd94b 25 sck_(pin_sck),
megrootens 0:5fa72cacd94b 26 dt_(pin_dt) {
megrootens 0:5fa72cacd94b 27 set_offset(offset);
megrootens 0:5fa72cacd94b 28 set_scale(scale);
megrootens 0:5fa72cacd94b 29 set_gain(gain);
megrootens 0:5fa72cacd94b 30 }
megrootens 0:5fa72cacd94b 31
megrootens 0:5fa72cacd94b 32 /**
megrootens 1:197ef68ad6de 33 * Create an Hx711 ADC object with zero offset and unit scaling
megrootens 0:5fa72cacd94b 34 * @param pin_sck PinName of the clock pin (digital output)
megrootens 0:5fa72cacd94b 35 * @param pin_dt PinName of the data pin (digital input)
megrootens 0:5fa72cacd94b 36 * @param gain channel selection is made by passing the appropriate gain:
megrootens 0:5fa72cacd94b 37 * 128 or 64 for channel A, 32 for channel B
megrootens 1:197ef68ad6de 38 * TODO: constructor overloading is not allowed?
megrootens 0:5fa72cacd94b 39 */
megrootens 0:5fa72cacd94b 40 Hx711(PinName pin_sck, PinName pin_dt, uint8_t gain = 128) :
megrootens 0:5fa72cacd94b 41 sck_(pin_sck),
megrootens 0:5fa72cacd94b 42 dt_(pin_dt) {
megrootens 0:5fa72cacd94b 43 set_offset(0);
megrootens 0:5fa72cacd94b 44 set_scale(1.0f);
megrootens 0:5fa72cacd94b 45 set_gain(gain);
megrootens 0:5fa72cacd94b 46 }
megrootens 0:5fa72cacd94b 47
megrootens 0:5fa72cacd94b 48 /**
megrootens 0:5fa72cacd94b 49 * Check if the sensor is ready
megrootens 0:5fa72cacd94b 50 * from the datasheet: When output data is not ready for retrieval,
megrootens 0:5fa72cacd94b 51 * digital output pin DOUT is high. Serial clock input PD_SCK should be low.
megrootens 0:5fa72cacd94b 52 * When DOUT goes to low, it indicates data is ready for retrieval.
megrootens 0:5fa72cacd94b 53 * @return true if dt_.read() == LOW
megrootens 1:197ef68ad6de 54 * TODO: this is not ideal; the programm will hang if the chip never
megrootens 1:197ef68ad6de 55 * becomes ready...
megrootens 0:5fa72cacd94b 56 */
megrootens 0:5fa72cacd94b 57 bool is_ready() {
megrootens 0:5fa72cacd94b 58 return dt_.read() == LOW;
megrootens 0:5fa72cacd94b 59 }
megrootens 0:5fa72cacd94b 60
megrootens 0:5fa72cacd94b 61 /**
megrootens 0:5fa72cacd94b 62 * Waits for the chip to be ready and returns a raw int reading
megrootens 0:5fa72cacd94b 63 * @return int sensor output value
megrootens 0:5fa72cacd94b 64 */
megrootens 2:f118f8c456a4 65 uint32_t readRaw();
megrootens 0:5fa72cacd94b 66
megrootens 0:5fa72cacd94b 67 /**
megrootens 0:5fa72cacd94b 68 * Obtain offset and scaled sensor output; i.e. a real value
megrootens 0:5fa72cacd94b 69 * @return float
megrootens 0:5fa72cacd94b 70 */
megrootens 0:5fa72cacd94b 71 float read() {
megrootens 3:9bbfb7f8c751 72 return convert_to_real(readRaw());
megrootens 2:f118f8c456a4 73 }
megrootens 2:f118f8c456a4 74
megrootens 2:f118f8c456a4 75 /**
megrootens 2:f118f8c456a4 76 * Convert integer value from chip to offset and scaled real value
megrootens 2:f118f8c456a4 77 * @param val integer value
megrootens 2:f118f8c456a4 78 * @return (val - get_offset()) * get_scale()
megrootens 2:f118f8c456a4 79 */
megrootens 2:f118f8c456a4 80 float convert_to_real(int val) {
megrootens 2:f118f8c456a4 81 return ((float)(val - get_offset())) * get_scale();
megrootens 0:5fa72cacd94b 82 }
megrootens 0:5fa72cacd94b 83
megrootens 0:5fa72cacd94b 84 /**
megrootens 0:5fa72cacd94b 85 * Puts the chip into power down mode
megrootens 0:5fa72cacd94b 86 */
megrootens 0:5fa72cacd94b 87 void power_down() {
megrootens 0:5fa72cacd94b 88 sck_.write(LOW);
megrootens 0:5fa72cacd94b 89 sck_.write(HIGH);
megrootens 0:5fa72cacd94b 90 }
megrootens 0:5fa72cacd94b 91
megrootens 0:5fa72cacd94b 92 /**
megrootens 0:5fa72cacd94b 93 * Wakes up the chip after power down mode
megrootens 0:5fa72cacd94b 94 */
megrootens 0:5fa72cacd94b 95 void power_up() {
megrootens 0:5fa72cacd94b 96 sck_.write(LOW);
megrootens 0:5fa72cacd94b 97 }
megrootens 0:5fa72cacd94b 98
megrootens 0:5fa72cacd94b 99 /**
megrootens 0:5fa72cacd94b 100 * Set the gain factor; takes effect only after a call to read()
megrootens 0:5fa72cacd94b 101 * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
megrootens 0:5fa72cacd94b 102 * depending on the parameter, the channel is also set to either A or B
megrootens 0:5fa72cacd94b 103 * Ensures that gain_ = 128, 64 or 32
megrootens 0:5fa72cacd94b 104 * @param gain 128, 64 or 32
megrootens 0:5fa72cacd94b 105 */
megrootens 0:5fa72cacd94b 106 void set_gain(uint8_t gain = 128);
megrootens 0:5fa72cacd94b 107
megrootens 0:5fa72cacd94b 108 /**
megrootens 0:5fa72cacd94b 109 * Obtain current gain
megrootens 0:5fa72cacd94b 110 * @return gain_
megrootens 0:5fa72cacd94b 111 */
megrootens 0:5fa72cacd94b 112 uint8_t get_gain() {
megrootens 0:5fa72cacd94b 113 return gain_;
megrootens 0:5fa72cacd94b 114 }
megrootens 0:5fa72cacd94b 115
megrootens 0:5fa72cacd94b 116 /**
megrootens 0:5fa72cacd94b 117 * Set the scale factor
megrootens 0:5fa72cacd94b 118 * @param scale desired scale
megrootens 0:5fa72cacd94b 119 */
megrootens 2:f118f8c456a4 120 void set_scale(float scale = 1.0f) {
megrootens 0:5fa72cacd94b 121 scale_ = scale;
megrootens 0:5fa72cacd94b 122 };
megrootens 0:5fa72cacd94b 123
megrootens 0:5fa72cacd94b 124 /**
megrootens 0:5fa72cacd94b 125 * Get sensor scale factor
megrootens 0:5fa72cacd94b 126 * @return scale_
megrootens 0:5fa72cacd94b 127 */
megrootens 0:5fa72cacd94b 128 float get_scale() {
megrootens 0:5fa72cacd94b 129 return scale_;
megrootens 0:5fa72cacd94b 130 }
megrootens 0:5fa72cacd94b 131
megrootens 0:5fa72cacd94b 132 /**
megrootens 0:5fa72cacd94b 133 * Set the sensor offset
megrootens 0:5fa72cacd94b 134 * @param offset the desired offset
megrootens 0:5fa72cacd94b 135 */
megrootens 0:5fa72cacd94b 136 void set_offset(int offset = 0) {
megrootens 0:5fa72cacd94b 137 offset_ = offset;
megrootens 0:5fa72cacd94b 138 }
megrootens 0:5fa72cacd94b 139
megrootens 0:5fa72cacd94b 140 /**
megrootens 0:5fa72cacd94b 141 * Get current sensor offset
megrootens 0:5fa72cacd94b 142 * @return offset_
megrootens 0:5fa72cacd94b 143 */
megrootens 0:5fa72cacd94b 144 int get_offset() { return offset_; }
megrootens 0:5fa72cacd94b 145
megrootens 0:5fa72cacd94b 146
megrootens 0:5fa72cacd94b 147 private:
megrootens 1:197ef68ad6de 148
megrootens 1:197ef68ad6de 149 static const uint8_t LOW = 0; // digital low
megrootens 1:197ef68ad6de 150 static const uint8_t HIGH = 1; // digital high
megrootens 1:197ef68ad6de 151
megrootens 0:5fa72cacd94b 152 DigitalOut sck_; // clock line
megrootens 0:5fa72cacd94b 153 DigitalIn dt_; // data line
megrootens 0:5fa72cacd94b 154
megrootens 0:5fa72cacd94b 155 uint8_t gain_; // amplification factor at chip
megrootens 1:197ef68ad6de 156 int offset_; // offset chip value
megrootens 1:197ef68ad6de 157 float scale_; // scale output after offset
megrootens 0:5fa72cacd94b 158
megrootens 0:5fa72cacd94b 159 /**
megrootens 0:5fa72cacd94b 160 * Port of the Arduino shiftIn function; shifts a byte one bit at a time
megrootens 0:5fa72cacd94b 161 * @return incoming but
megrootens 0:5fa72cacd94b 162 */
megrootens 3:9bbfb7f8c751 163 uint8_t shiftInMsbFirst();
megrootens 0:5fa72cacd94b 164 };
megrootens 0:5fa72cacd94b 165
megrootens 0:5fa72cacd94b 166 #endif