Manuel Caballero / HX711

Dependents:   EXPO_ANDA EXPO_ANDA_Copilotoco

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HX711.h Source File

HX711.h

00001 /**
00002  * @brief       HX711.h
00003  * @details     24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales.
00004  *              Header file.
00005  *
00006  *
00007  * @return      NA
00008  *
00009  * @author      Manuel Caballero
00010  * @date        11/September/2017
00011  * @version     11/September/2017    The ORIGIN
00012  * @pre         NaN.
00013  * @warning     NaN
00014  * @pre         This code belongs to Nimbus Centre ( http://www.nimbus.cit.ie ).
00015  */
00016 #ifndef HX711_H
00017 #define HX711_H
00018 
00019 #include "mbed.h"
00020 
00021 
00022 /**
00023     Example:
00024 
00025 #include "mbed.h"
00026 #include "HX711.h"
00027 
00028 HX711  myWeightSensor        ( p5, p6 );
00029 Serial pc                    ( USBTX, USBRX );
00030 
00031 Ticker newReading;
00032 DigitalOut myled1            ( LED1 );
00033 DigitalOut myled2            ( LED2 );
00034 DigitalOut myled3            ( LED3 );
00035 DigitalOut myled4            ( LED4 );
00036 
00037 HX711::HX711_status_t        aux;
00038 HX711::Vector_count_t        myData;
00039 HX711::Vector_mass_t         myCalculatedMass;
00040 HX711::Vector_voltage_t      myCalculatedVoltage;
00041 
00042 
00043 void readDATA ( void )
00044 {
00045     myled4    =  1;
00046 
00047     aux                 =    myWeightSensor.HX711_ReadRawData       ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
00048     myCalculatedMass    =    myWeightSensor.HX711_CalculateMass     ( &myData, 1.0, HX711::HX711_SCALE_kg );
00049     myCalculatedVoltage =    myWeightSensor.HX711_CalculateVoltage  ( &myData, 5.0 );
00050 
00051     pc.printf( "Raw Data: %ld  Mass: %0.5f kg Voltage: %0.5f mV\r\n", (uint32_t)myData.myRawValue, myCalculatedMass.myMass, 1000*myCalculatedVoltage.myVoltage );
00052 
00053     myled4    =  0;
00054 }
00055 
00056 
00057 int main()
00058 {
00059     pc.baud ( 115200 );
00060 
00061 
00062     // Reset and wake the device up
00063     aux = myWeightSensor.HX711_PowerDown    ();
00064     aux = myWeightSensor.HX711_Reset        ();
00065     wait(1);
00066 
00067 
00068     // CALIBRATION time start!
00069     // 1. REMOVE THE MASS ON THE LOAD CELL ( ALL LEDs OFF ). Read data without any mass on the load cell
00070     aux = myWeightSensor.HX711_ReadData_WithoutMass ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
00071 
00072     myled1   =   1;
00073     wait(3);
00074 
00075 
00076     // 2. PUT A KNOWN MASS ON THE LOAD CELL ( JUST LED1 ON ). Read data with an user-specified calibration mass
00077     aux = myWeightSensor.HX711_ReadData_WithCalibratedMass ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
00078     // CALIBRATION time end!
00079 
00080 
00081     // [ OPTIONAL ] REMOVE THE MASS ON THE LOAD CELL ( JUST LED2 ON ). Read the device without any mass to calculate the tare weight for 5 seconds
00082     myled1   =   0;
00083     myled2   =   1;
00084     wait(3);
00085     myled2   =   0;
00086 
00087     // Calculating the tare weight ( JUST LED3 ON )
00088     myled3   =   1;
00089     aux = myWeightSensor.HX711_SetAutoTare ( HX711::CHANNEL_A_GAIN_128, 1.0, HX711::HX711_SCALE_kg, &myData, 5 );
00090     myled3   =   0;
00091 
00092 
00093     newReading.attach( &readDATA, 0.5 );                                        // the address of the function to be attached ( readDATA ) and the interval ( 0.5s ) ( JUST LED4 BLINKING )
00094 
00095     // Let the callbacks take care of everything
00096     while(1)
00097     {
00098         sleep();
00099     }
00100 }
00101 
00102 */
00103 
00104 
00105 /*!
00106  Library for the HX711 24-Bit Analog-to-Digital Converter (ADC) for Weigh Scales.
00107 */
00108 class HX711
00109 {
00110 public:
00111     /**
00112       * @brief   CHANNELS & GAIN
00113       */
00114     typedef enum {
00115         CHANNEL_A_GAIN_128       =   0,              /*!<  Channel A 128 Gain.                                               */
00116         CHANNEL_B_GAIN_32        =   1,              /*!<  Channel B 32 Gain.                                                */
00117         CHANNEL_A_GAIN_64        =   2               /*!<  Channel A 64 Gain.                                                */
00118     } HX711_channel_gain_t;
00119 
00120 
00121 
00122     /**
00123       * @brief   READY/BUSY DATA
00124       */
00125     typedef enum {
00126         HX711_DATA_BUSY          =   0,              /*!<  HX711 data is NOT ready to be read.                                */
00127         HX711_DATA_READY         =   1               /*!<  HX711 data is ready to be read.                                    */
00128     } HX711_data_output_status_t;
00129 
00130 
00131 
00132     /**
00133       * @brief   SCALE
00134       */
00135     typedef enum {
00136         HX711_SCALE_kg           =   0,              /*!<  HX711 Scale in kg.                                                 */
00137         HX711_SCALE_g            =   1,              /*!<  HX711 Scale in  g.                                                 */
00138         HX711_SCALE_mg           =   2,              /*!<  HX711 Scale in mg.                                                 */
00139         HX711_SCALE_ug           =   3               /*!<  HX711 Scale in ug.                                                 */
00140     } HX711_scale_t;
00141 
00142 
00143 
00144 
00145 #ifndef VECTOR_STRUCT_H
00146 #define VECTOR_STRUCT_H
00147     typedef struct {
00148         float myRawValue_WithCalibratedMass;
00149         float myRawValue_WithoutCalibratedMass;
00150         float myRawValue_TareWeight;
00151         uint32_t myRawValue;
00152     } Vector_count_t;
00153 
00154     typedef struct {
00155         float myMass;
00156     } Vector_mass_t;
00157 
00158     typedef struct {
00159         float myVoltage;
00160     } Vector_voltage_t;
00161 #endif
00162 
00163 
00164 
00165     /**
00166       * @brief   INTERNAL CONSTANTS
00167       */
00168 #define HX711_PIN_HIGH           0x01               /*!<   Pin 'HIGH'                                                       */
00169 #define HX711_PIN_LOW            0x00               /*!<   Pin 'LOW'                                                        */
00170 
00171     typedef enum {
00172         HX711_SUCCESS     =       0,
00173         HX711_FAILURE     =       1,
00174     } HX711_status_t;
00175 
00176 
00177 
00178 
00179     /** Create an HX711 object connected to the specified pins.
00180       *
00181       * @param pd_sck           HX711 Power down control (high active) and serial clock input
00182       * @param dout             HX711 Serial data output
00183       */
00184     HX711 ( PinName PD_SCK, PinName DOUT );
00185 
00186     /** Delete HX711 object.
00187      */
00188     ~HX711();
00189 
00190     /** It performs an internal reset.
00191      */
00192     HX711_status_t  HX711_Reset                         ( void );
00193 
00194     /** It puts the device into power-down mode.
00195      */
00196     HX711_status_t  HX711_PowerDown                     ( void );
00197 
00198     /** It sets both the channel and the gain for the next measurement.
00199      */
00200     HX711_status_t  HX711_SetChannelAndGain             ( HX711_channel_gain_t myChannel_Gain );
00201 
00202     /** It gets both the channel and the gain for the current measurement.
00203      */
00204     HX711_channel_gain_t  HX711_GetChannelAndGain       ( void );
00205 
00206     /** It reads raw data from the device.
00207      */
00208     HX711_status_t  HX711_ReadRawData                   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
00209 
00210     /** It reads raw data with an user-specified calibrated mass.
00211      */
00212     HX711_status_t  HX711_ReadData_WithCalibratedMass   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
00213 
00214     /** It reads raw data without any mass.
00215      */
00216     HX711_status_t  HX711_ReadData_WithoutMass          ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
00217 
00218     /** It reads raw data without any mass after the system is calibrated.
00219      */
00220     HX711_status_t  HX711_SetAutoTare                   ( HX711_channel_gain_t myChannel_Gain, float myCalibratedMass, HX711_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, float myTime );
00221 
00222     /** It sets a tare weight manually.
00223      */
00224     Vector_count_t  HX711_SetManualTare                 ( float myTareWeight );
00225 
00226     /** It calculates scaled data.
00227      */
00228     Vector_mass_t  HX711_CalculateMass                  ( Vector_count_t* myNewRawData, float myCalibratedMass, HX711_scale_t myScaleCalibratedMass );
00229 
00230     /** It calculates voltage data.
00231      */
00232     Vector_voltage_t  HX711_CalculateVoltage            ( Vector_count_t* myNewRawData, float myVoltageReference );
00233 
00234 
00235 
00236 
00237 private:
00238     DigitalOut              _PD_SCK;
00239     DigitalIn               _DOUT;
00240     HX711_channel_gain_t    _HX711_CHANNEL_GAIN;
00241     HX711_scale_t           _HX711_SCALE;
00242     float                   _HX711_USER_CALIBATED_MASS;
00243 };
00244 
00245 #endif