Library for communication with the HX711 24-Bit Analog-to-Digital converter.

Dependents:   HMC1501_Hello

Library for communication with the HX711 24-Bit Analog-to-Digital converter.

HX711

400

HX711 is a precision 24-bit analog-to-digital converter (ADC) designed for weigh scales and industrial control applications to interface directly with a bridge sensor. The input multiplexer selects either Channel A or B differential input to the low-noise programmable gain amplifier (PGA). Channel A can be programmed with a gain of 128 or 64, corresponding to a full-scale differential input voltage of ±20mV or ±40mV respectively, when a 5V supply is connected to AVDD analog power supply pin. Channel B has a fixed gain of 32. On-chip power supply regulator eliminates the need for an external supply regulator to provide analog power for the ADC and the sensor. Clock input is flexible. It can be from an external clock source, a crystal, or the on-chip oscillator that does not require any external component. On-chip power-on-reset circuitry simplifies digital interface initialization.

https://os.mbed.com/media/uploads/hudakz/hx711_sch.png

Example program:

Import programHMC1501_Hello

Example program for the HMC1501 magnetic 90° angle sensor connected to a HX711 high precision 24-bit programmable analog-to-digital converter (ADC) .

Committer:
hudakz
Date:
Sat Sep 19 17:54:17 2020 +0000
Revision:
0:72a9d7812a33
Library for communication with the HX711 24-Bit Analog-to-Digital converter.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:72a9d7812a33 1 /*
hudakz 0:72a9d7812a33 2 * Copyright (c) 2020 Zoltan Hudak <hudakz@outlook.com>
hudakz 0:72a9d7812a33 3 * All rights reserved.
hudakz 0:72a9d7812a33 4 *
hudakz 0:72a9d7812a33 5 * This program is free software: you can redistribute it and/or modify
hudakz 0:72a9d7812a33 6 * it under the terms of the GNU General Public License as published by
hudakz 0:72a9d7812a33 7 * the Free Software Foundation, either version 3 of the License, or
hudakz 0:72a9d7812a33 8 * (at your option) any later version.
hudakz 0:72a9d7812a33 9 *
hudakz 0:72a9d7812a33 10 * This program is distributed in the hope that it will be useful,
hudakz 0:72a9d7812a33 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:72a9d7812a33 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:72a9d7812a33 13 * GNU General Public License for more details.
hudakz 0:72a9d7812a33 14 *
hudakz 0:72a9d7812a33 15 * You should have received a copy of the GNU General Public License
hudakz 0:72a9d7812a33 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:72a9d7812a33 17 */
hudakz 0:72a9d7812a33 18
hudakz 0:72a9d7812a33 19 #include "mbed.h"
hudakz 0:72a9d7812a33 20 #include "HX711.h"
hudakz 0:72a9d7812a33 21
hudakz 0:72a9d7812a33 22 /**
hudakz 0:72a9d7812a33 23 * Class for communication with the HX711 24-Bit Analog-to-Digital converter (ADC)
hudakz 0:72a9d7812a33 24 * (Developed for Weigh Scales by AVIA Semiconductor but can be used generally.)
hudakz 0:72a9d7812a33 25 * This library is based on the library at https://github.com/bogde/HX711
hudakz 0:72a9d7812a33 26 */
hudakz 0:72a9d7812a33 27
hudakz 0:72a9d7812a33 28 /**
hudakz 0:72a9d7812a33 29 * @brief Sets gain
hudakz 0:72a9d7812a33 30 * @note
hudakz 0:72a9d7812a33 31 * @param gain Selected gain (128, 64 or 32)
hudakz 0:72a9d7812a33 32 * @retval
hudakz 0:72a9d7812a33 33 */
hudakz 0:72a9d7812a33 34 void HX711::setGain(uint8_t gain)
hudakz 0:72a9d7812a33 35 {
hudakz 0:72a9d7812a33 36 _gain = gain;
hudakz 0:72a9d7812a33 37
hudakz 0:72a9d7812a33 38 switch (gain) {
hudakz 0:72a9d7812a33 39 case 128: // channel A, gain factor 128
hudakz 0:72a9d7812a33 40 _mode = 1;
hudakz 0:72a9d7812a33 41 break;
hudakz 0:72a9d7812a33 42
hudakz 0:72a9d7812a33 43 case 64: // channel A, gain factor 64
hudakz 0:72a9d7812a33 44 _mode = 3;
hudakz 0:72a9d7812a33 45 break;
hudakz 0:72a9d7812a33 46
hudakz 0:72a9d7812a33 47 case 32: // channel B, gain factor 32
hudakz 0:72a9d7812a33 48 _mode = 2;
hudakz 0:72a9d7812a33 49 break;
hudakz 0:72a9d7812a33 50 }
hudakz 0:72a9d7812a33 51
hudakz 0:72a9d7812a33 52 _sck.write(0);
hudakz 0:72a9d7812a33 53 }
hudakz 0:72a9d7812a33 54
hudakz 0:72a9d7812a33 55 /**
hudakz 0:72a9d7812a33 56 * @brief Obtains digital value of measured analog input voltage.
hudakz 0:72a9d7812a33 57 * @note It's a 24-bit two's complement integer.
hudakz 0:72a9d7812a33 58 * @retval Digital value of measured analog input voltage.
hudakz 0:72a9d7812a33 59 */
hudakz 0:72a9d7812a33 60 uint32_t HX711::readInt()
hudakz 0:72a9d7812a33 61 {
hudakz 0:72a9d7812a33 62 uint32_t value = 0;
hudakz 0:72a9d7812a33 63 for (uint8_t i = 0; i < 24; ++i) {
hudakz 0:72a9d7812a33 64 _sck.write(1);
hudakz 0:72a9d7812a33 65 value = value << 1;
hudakz 0:72a9d7812a33 66 value |= _dout.read();
hudakz 0:72a9d7812a33 67 _sck.write(0);
hudakz 0:72a9d7812a33 68 }
hudakz 0:72a9d7812a33 69
hudakz 0:72a9d7812a33 70 // set the channel and the gain factor for the next reading using the clock pin
hudakz 0:72a9d7812a33 71 for (unsigned int i = 0; i < _mode; i++) {
hudakz 0:72a9d7812a33 72 _sck.write(1);
hudakz 0:72a9d7812a33 73 _sck.write(0);
hudakz 0:72a9d7812a33 74 }
hudakz 0:72a9d7812a33 75
hudakz 0:72a9d7812a33 76 return value;
hudakz 0:72a9d7812a33 77 }
hudakz 0:72a9d7812a33 78
hudakz 0:72a9d7812a33 79 /**
hudakz 0:72a9d7812a33 80 * @brief Converts a 32-bit signed fixed point integer to floating point value
hudakz 0:72a9d7812a33 81 * @note The 32-bit unsigned integer argument represnts actually a 24-bit signed fixed point value:
hudakz 0:72a9d7812a33 82 * two’s complement and the LSB of the 24-bit binary number represents 1/8,388,608th of a unit).
hudakz 0:72a9d7812a33 83 * @param 32-bit unsigned integer
hudakz 0:72a9d7812a33 84 * @retval Floating point value ranging from -1 to +1
hudakz 0:72a9d7812a33 85 */
hudakz 0:72a9d7812a33 86 float HX711::_toFloat(uint32_t dword)
hudakz 0:72a9d7812a33 87 {
hudakz 0:72a9d7812a33 88 if (dword & 0x800000)
hudakz 0:72a9d7812a33 89 return(-float((~dword + 1) & 0xFFFFFF) / 8388608.0f);
hudakz 0:72a9d7812a33 90 else
hudakz 0:72a9d7812a33 91 return(float(dword) / 8388607.0f);
hudakz 0:72a9d7812a33 92 }
hudakz 0:72a9d7812a33 93
hudakz 0:72a9d7812a33 94 /**
hudakz 0:72a9d7812a33 95 * @brief Converts normalised value to voltage.
hudakz 0:72a9d7812a33 96 * @note
hudakz 0:72a9d7812a33 97 * @param val Normalised measured value (ranging from -1.0f to + 1.0f)
hudakz 0:72a9d7812a33 98 * @retval Input voltage in Volts [V]
hudakz 0:72a9d7812a33 99 */
hudakz 0:72a9d7812a33 100 float HX711::toVolt(float val)
hudakz 0:72a9d7812a33 101 {
hudakz 0:72a9d7812a33 102 return((val * _avdd) / _gain / 2);
hudakz 0:72a9d7812a33 103 }
hudakz 0:72a9d7812a33 104
hudakz 0:72a9d7812a33 105 /**
hudakz 0:72a9d7812a33 106 * @brief Obtains HX711's input voltage in mV.
hudakz 0:72a9d7812a33 107 * @note
hudakz 0:72a9d7812a33 108 * @retval Measured voltage in mV.
hudakz 0:72a9d7812a33 109 */
hudakz 0:72a9d7812a33 110 float HX711::read()
hudakz 0:72a9d7812a33 111 {
hudakz 0:72a9d7812a33 112 return(toVolt(_toFloat(readInt())) * 1000 - _drift);
hudakz 0:72a9d7812a33 113 }