Sim Youngwoo / HX711

Dependents:   torque_calibration_ywsim

Revision:
3:d246aa415f3a
Parent:
2:1af13a8a8275
Child:
4:d7a5cd03ed09
--- a/HX711.h	Tue Sep 12 14:38:13 2017 +0000
+++ b/HX711.h	Tue Sep 12 15:33:33 2017 +0000
@@ -22,7 +22,83 @@
 /**
     Example:
 
-[todo]
+#include "mbed.h"
+#include "HX711.h"
+
+HX711  myWeightSensor        ( p5, p6 );
+Serial pc                    ( USBTX, USBRX );
+
+Ticker newReading;
+DigitalOut myled1            ( LED1 );
+DigitalOut myled2            ( LED2 );
+DigitalOut myled3            ( LED3 );
+DigitalOut myled4            ( LED4 );
+
+HX711::HX711_status_t        aux;
+HX711::Vector_count_t        myData;
+HX711::Vector_mass_t         myCalculatedMass;
+HX711::Vector_voltage_t      myCalculatedVoltage;
+
+
+void readDATA ( void )
+{
+    myled4    =  1;
+
+    aux                 =    myWeightSensor.HX711_ReadRawData       ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
+    myCalculatedMass    =    myWeightSensor.HX711_CalculateMass     ( &myData, 1.0, HX711::HX711_SCALE_kg );
+    myCalculatedVoltage =    myWeightSensor.HX711_CalculateVoltage  ( &myData, 5.0 );
+
+    pc.printf( "Raw Data: %ld  Mass: %0.5f kg Voltage: %0.5f mV\r\n", (uint32_t)myData.myRawValue, myCalculatedMass.myMass, 1000*myCalculatedVoltage.myVoltage );
+
+    myled4    =  0;
+}
+
+
+int main()
+{
+    pc.baud ( 115200 );
+
+
+    // Reset and wake the device up
+    aux = myWeightSensor.HX711_PowerDown    ();
+    aux = myWeightSensor.HX711_Reset        ();
+    wait(1);
+
+
+    // CALIBRATION time start!
+    // 1. REMOVE THE MASS ON THE LOAD CELL ( ALL LEDs OFF ). Read data without any mass on the load cell
+    aux = myWeightSensor.HX711_ReadData_WithoutMass ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
+
+    myled1   =   1;
+    wait(3);
+
+
+    // 2. PUT A KNOWN MASS ON THE LOAD CELL ( JUST LED1 ON ). Read data with an user-specified calibration mass
+    aux = myWeightSensor.HX711_ReadData_WithCalibratedMass ( HX711::CHANNEL_A_GAIN_128, &myData, 4 );
+    // CALIBRATION time end!
+
+
+    // REMOVE THE MASS ON THE LOAD CELL ( JUST LED2 ON ). Read the device without any mass to calculate the tare weight for 5 seconds
+    myled1   =   0;
+    myled2   =   1;
+    wait(3);
+    myled2   =   0;
+
+    // Calculating the tare weight ( JUST LED3 ON )
+    myled3   =   1;
+    aux = myWeightSensor.HX711_SetAutoTare ( HX711::CHANNEL_A_GAIN_128, 1.0, HX711::HX711_SCALE_kg, &myData, 5 );
+    myled3   =   0;
+
+
+    newReading.attach( &readDATA, 0.5 );                                        // the address of the function to be attached ( readDATA ) and the interval ( 0.5s ) ( JUST LED4 BLINKING )
+
+    // Let the callbacks take care of everything
+    while(1)
+    {
+        sleep();
+    }
+}
+
 */
 
 
@@ -74,10 +150,14 @@
         float myRawValue_TareWeight;
         uint32_t myRawValue;
     } Vector_count_t;
-    
+
     typedef struct {
         float myMass;
     } Vector_mass_t;
+
+    typedef struct {
+        float myVoltage;
+    } Vector_voltage_t;
 #endif
 
 
@@ -87,7 +167,7 @@
       */
 #define HX711_PIN_HIGH           0x01               /*!<   Pin 'HIGH'                                                       */
 #define HX711_PIN_LOW            0x00               /*!<   Pin 'LOW'                                                        */
-      
+
     typedef enum {
         HX711_SUCCESS     =       0,
         HX711_FAILURE     =       1,
@@ -110,43 +190,51 @@
     /** It performs an internal reset.
      */
     HX711_status_t  HX711_Reset                         ( void );
-    
+
     /** It puts the device into power-down mode.
      */
     HX711_status_t  HX711_PowerDown                     ( void );
-    
+
     /** It sets both the channel and the gain for the next measurement.
      */
     HX711_status_t  HX711_SetChannelAndGain             ( HX711_channel_gain_t myChannel_Gain );
-    
+
     /** It gets both the channel and the gain for the current measurement.
      */
     HX711_channel_gain_t  HX711_GetChannelAndGain       ( void );
-    
+
     /** It reads raw data from the device.
      */
     HX711_status_t  HX711_ReadRawData                   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
-    
+
     /** It reads raw data with an user-specified calibrated mass.
      */
     HX711_status_t  HX711_ReadData_WithCalibratedMass   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
-    
+
     /** It reads raw data without any mass.
      */
     HX711_status_t  HX711_ReadData_WithoutMass          ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, uint32_t myAverage );
-    
+
     /** It reads raw data without any mass after the system is calibrated.
      */
-    HX711_status_t  HX711_SetAutoTare                   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, float myTime );
-    
+    HX711_status_t  HX711_SetAutoTare                   ( HX711_channel_gain_t myChannel_Gain, float myCalibratedMass, HX711_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, float myTime );
+
+    /** It sets a tare weight manually.
+     */
+    Vector_count_t  HX711_SetManualTare                 ( float myTareWeight );
+
     /** It calculates scaled data.
      */
     Vector_mass_t  HX711_CalculateMass                  ( Vector_count_t* myNewRawData, float myCalibratedMass, HX711_scale_t myScaleCalibratedMass );
 
+    /** It calculates voltage data.
+     */
+    Vector_voltage_t  HX711_CalculateVoltage            ( Vector_count_t* myNewRawData, float myVoltageReference );
 
 
 
-private:    
+
+private:
     DigitalOut              _PD_SCK;
     DigitalIn               _DOUT;
     HX711_channel_gain_t    _HX711_CHANNEL_GAIN;