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