Sim Youngwoo / HX711

Dependents:   torque_calibration_ywsim

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