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
Hx711.h
00001 #ifndef _HX711_H_ 00002 #define _HX711_H_ 00003 00004 /** 00005 * Class for communication with the HX711 24-Bit Analog-to-Digital 00006 * Converter (ADC) for Weigh Scales by AVIA Semiconductor. 00007 * This library is a port of the Arduino library at 00008 * https://github.com/bogde/HX711 00009 * It works with the FRDM K22F. 00010 */ 00011 class Hx711 { 00012 00013 public: 00014 00015 /** 00016 * Create an Hx711 ADC object 00017 * @param pin_sck PinName of the clock pin (digital output) 00018 * @param pin_dt PinName of the data pin (digital input) 00019 * @param offset offset for sensor values 00020 * @param scale scale factor to obtain real values 00021 * @param gain channel selection is made by passing the appropriate gain: 00022 * 128 or 64 for channel A, 32 for channel B 00023 */ 00024 Hx711(PinName pin_sck, PinName pin_dt, int offset, float scale, uint8_t gain = 128) : 00025 sck_(pin_sck), 00026 dt_(pin_dt) { 00027 set_offset(offset); 00028 set_scale(scale); 00029 set_gain(gain); 00030 } 00031 00032 /** 00033 * Create an Hx711 ADC object with zero offset and unit scaling 00034 * @param pin_sck PinName of the clock pin (digital output) 00035 * @param pin_dt PinName of the data pin (digital input) 00036 * @param gain channel selection is made by passing the appropriate gain: 00037 * 128 or 64 for channel A, 32 for channel B 00038 * TODO: constructor overloading is not allowed? 00039 */ 00040 Hx711(PinName pin_sck, PinName pin_dt, uint8_t gain = 128) : 00041 sck_(pin_sck), 00042 dt_(pin_dt) { 00043 set_offset(0); 00044 set_scale(1.0f); 00045 set_gain(gain); 00046 } 00047 00048 /** 00049 * Check if the sensor is ready 00050 * from the datasheet: When output data is not ready for retrieval, 00051 * digital output pin DOUT is high. Serial clock input PD_SCK should be low. 00052 * When DOUT goes to low, it indicates data is ready for retrieval. 00053 * @return true if dt_.read() == LOW 00054 * TODO: this is not ideal; the programm will hang if the chip never 00055 * becomes ready... 00056 */ 00057 bool is_ready() { 00058 return dt_.read() == LOW; 00059 } 00060 00061 /** 00062 * Waits for the chip to be ready and returns a raw int reading 00063 * @return int sensor output value 00064 */ 00065 uint32_t readRaw(); 00066 00067 /** 00068 * Obtain offset and scaled sensor output; i.e. a real value 00069 * @return float 00070 */ 00071 float read() { 00072 return convert_to_real(readRaw()); 00073 } 00074 00075 /** 00076 * Convert integer value from chip to offset and scaled real value 00077 * @param val integer value 00078 * @return (val - get_offset()) * get_scale() 00079 */ 00080 float convert_to_real(int val) { 00081 return ((float)(val - get_offset())) * get_scale(); 00082 } 00083 00084 /** 00085 * Puts the chip into power down mode 00086 */ 00087 void power_down() { 00088 sck_.write(LOW); 00089 sck_.write(HIGH); 00090 } 00091 00092 /** 00093 * Wakes up the chip after power down mode 00094 */ 00095 void power_up() { 00096 sck_.write(LOW); 00097 } 00098 00099 /** 00100 * Set the gain factor; takes effect only after a call to read() 00101 * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain 00102 * depending on the parameter, the channel is also set to either A or B 00103 * Ensures that gain_ = 128, 64 or 32 00104 * @param gain 128, 64 or 32 00105 */ 00106 void set_gain(uint8_t gain = 128); 00107 00108 /** 00109 * Obtain current gain 00110 * @return gain_ 00111 */ 00112 uint8_t get_gain() { 00113 return gain_; 00114 } 00115 00116 /** 00117 * Set the scale factor 00118 * @param scale desired scale 00119 */ 00120 void set_scale(float scale = 1.0f) { 00121 scale_ = scale; 00122 }; 00123 00124 /** 00125 * Get sensor scale factor 00126 * @return scale_ 00127 */ 00128 float get_scale() { 00129 return scale_; 00130 } 00131 00132 /** 00133 * Set the sensor offset 00134 * @param offset the desired offset 00135 */ 00136 void set_offset(int offset = 0) { 00137 offset_ = offset; 00138 } 00139 00140 /** 00141 * Get current sensor offset 00142 * @return offset_ 00143 */ 00144 int get_offset() { return offset_; } 00145 00146 00147 private: 00148 00149 static const uint8_t LOW = 0; // digital low 00150 static const uint8_t HIGH = 1; // digital high 00151 00152 DigitalOut sck_; // clock line 00153 DigitalIn dt_; // data line 00154 00155 uint8_t gain_; // amplification factor at chip 00156 int offset_; // offset chip value 00157 float scale_; // scale output after offset 00158 00159 /** 00160 * Port of the Arduino shiftIn function; shifts a byte one bit at a time 00161 * @return incoming but 00162 */ 00163 uint8_t shiftInMsbFirst(); 00164 }; 00165 00166 #endif
Generated on Tue Jul 12 2022 23:49:57 by 1.7.2