Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HX711.h
00001 /* 00002 * Copyright (c) 2020 Zoltan Hudak <hudakz@outlook.com> 00003 * All rights reserved. 00004 * 00005 * This program is free software: you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation, either version 3 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 */ 00018 00019 #ifndef _HX711_H_ 00020 #define _HX711_H_ 00021 #include "mbed.h" 00022 00023 /** 00024 * Class for communication with the HX711 24-Bit Analog-to-Digital converter (ADC) 00025 * (Developed for Weigh Scales by AVIA Semiconductor.) 00026 * This library is based on the library at https://github.com/bogde/HX711 00027 */ 00028 class HX711 00029 { 00030 public: 00031 /** 00032 * Creates an HX711 object 00033 * @param avdd Analog power supply voltage [V] 00034 * @param sck PinName of the clock pin (digital output) 00035 * @param dout PinName of the data pin (digital input) 00036 * @param drift Chip's input offset drift [V] 00037 * @param gain Channel selection is made by passing the associated gain: 00038 * 128 or 64 for channel A, 32 for channel B 00039 */ 00040 HX711(float avdd, PinName sck, PinName dout, uint8_t gain, float drift = 0) : 00041 _avdd(avdd), 00042 _sck(sck), 00043 _dout(dout, PullUp), 00044 _drift(drift) 00045 { 00046 setGain(gain); 00047 } 00048 00049 /** 00050 * Check if the sensor is ready. 00051 * From the datasheet: When output data is not ready for retrieval, 00052 * digital output pin _dout is high. Serial clock input _sck should be low. 00053 * When _dout goes low, it indicates data is ready for retrieval. 00054 * @return true if _dout.read() == 0 00055 */ 00056 bool isReady() { return _dout.read() == 0; } 00057 00058 /** 00059 * Returns a raw int reading 00060 * @return int sensor output value 00061 */ 00062 uint32_t readInt(); 00063 00064 /** 00065 * Obtains input voltage in mV biased by chip's input offset drift 00066 * @return float 00067 */ 00068 float read(); 00069 00070 /** 00071 * Puts the chip into power down mode 00072 */ 00073 void powerDown() { _sck.write(0); _sck.write(1); } 00074 00075 /** 00076 * Wakes up the chip after power down mode 00077 */ 00078 void powerUp() { _sck.write(0); } 00079 00080 /** 00081 * Set the gain factor; takes effect only after a call to read() 00082 * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain 00083 * depending on the parameter, the channel is also set to either A or B 00084 * Ensures that gain_ = 128, 64 or 32 00085 * @param gain 128, 64 or 32 00086 */ 00087 void setGain(uint8_t gain = 128); 00088 00089 /** 00090 * Obtain current gain 00091 * @return gain_ 00092 */ 00093 uint8_t getGain() { return _mode; } 00094 00095 /** 00096 * Get sensor's analog power supply voltage 00097 * @return power supply voltage 00098 */ 00099 float getAvdd() { return _avdd; } 00100 00101 /** 00102 * Sets the chip's input offset drift 00103 * @param drift The input offset drift [mV] 00104 */ 00105 void setDrift(float drift = 0) { _drift = drift; } 00106 00107 /** 00108 * Gets chip's input offset drift 00109 * @return Chip's input offset drift [mV] 00110 */ 00111 int getDrift() { return(_drift); } 00112 00113 private: 00114 float _avdd; // analog power supply voltage [V] 00115 DigitalOut _sck; // HX711 clock input line 00116 DigitalIn _dout; // HX711 data output line 00117 uint8_t _gain; // amplification factor of HX711 00118 uint8_t _mode; // operating mode of HX711 00119 float _drift; // chip's input offset drift [mV] 00120 00121 /** 00122 * @brief Converts a 32-bit signed fixed point integer to floating point value 00123 * @note The 32-bit unsigned integer argument represnts actually a 24-bit signed fixed point value: 00124 * two’s complement and the LSB of the 24-bit binary number represents 1/8,388,608th of a unit). 00125 * @param 32-bit unsigned integer 00126 * @retval Floating point value ranging from -1 to +1 00127 */ 00128 float _toFloat(uint32_t dword); 00129 00130 /** 00131 * Converts floating point value to input voltage 00132 * @param val Floating point value ranging from -1.0 to +1.0 00133 * @return Voltage at input [V] 00134 */ 00135 float toVolt(float val); 00136 }; 00137 #endif
Generated on Tue Jul 12 2022 16:32:48 by
1.7.2