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.cpp
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 #include "mbed.h" 00020 #include "HX711.h" 00021 00022 /** 00023 * Class for communication with the HX711 24-Bit Analog-to-Digital converter (ADC) 00024 * (Developed for Weigh Scales by AVIA Semiconductor but can be used generally.) 00025 * This library is based on the library at https://github.com/bogde/HX711 00026 */ 00027 00028 /** 00029 * @brief Sets gain 00030 * @note 00031 * @param gain Selected gain (128, 64 or 32) 00032 * @retval 00033 */ 00034 void HX711::setGain(uint8_t gain) 00035 { 00036 _gain = gain; 00037 00038 switch (gain) { 00039 case 128: // channel A, gain factor 128 00040 _mode = 1; 00041 break; 00042 00043 case 64: // channel A, gain factor 64 00044 _mode = 3; 00045 break; 00046 00047 case 32: // channel B, gain factor 32 00048 _mode = 2; 00049 break; 00050 } 00051 00052 _sck.write(0); 00053 } 00054 00055 /** 00056 * @brief Obtains digital value of measured analog input voltage. 00057 * @note It's a 24-bit two's complement integer. 00058 * @retval Digital value of measured analog input voltage. 00059 */ 00060 uint32_t HX711::readInt() 00061 { 00062 uint32_t value = 0; 00063 for (uint8_t i = 0; i < 24; ++i) { 00064 _sck.write(1); 00065 value = value << 1; 00066 value |= _dout.read(); 00067 _sck.write(0); 00068 } 00069 00070 // set the channel and the gain factor for the next reading using the clock pin 00071 for (unsigned int i = 0; i < _mode; i++) { 00072 _sck.write(1); 00073 _sck.write(0); 00074 } 00075 00076 return value; 00077 } 00078 00079 /** 00080 * @brief Converts a 32-bit signed fixed point integer to floating point value 00081 * @note The 32-bit unsigned integer argument represnts actually a 24-bit signed fixed point value: 00082 * two’s complement and the LSB of the 24-bit binary number represents 1/8,388,608th of a unit). 00083 * @param 32-bit unsigned integer 00084 * @retval Floating point value ranging from -1 to +1 00085 */ 00086 float HX711::_toFloat(uint32_t dword) 00087 { 00088 if (dword & 0x800000) 00089 return(-float((~dword + 1) & 0xFFFFFF) / 8388608.0f); 00090 else 00091 return(float(dword) / 8388607.0f); 00092 } 00093 00094 /** 00095 * @brief Converts normalised value to voltage. 00096 * @note 00097 * @param val Normalised measured value (ranging from -1.0f to + 1.0f) 00098 * @retval Input voltage in Volts [V] 00099 */ 00100 float HX711::toVolt(float val) 00101 { 00102 return((val * _avdd) / _gain / 2); 00103 } 00104 00105 /** 00106 * @brief Obtains HX711's input voltage in mV. 00107 * @note 00108 * @retval Measured voltage in mV. 00109 */ 00110 float HX711::read() 00111 { 00112 return(toVolt(_toFloat(readInt())) * 1000 - _drift); 00113 }
Generated on Tue Jul 12 2022 16:32:48 by
1.7.2