Guillaume Catry / HX711
Revision:
3:d246aa415f3a
Parent:
2:1af13a8a8275
Child:
4:d7a5cd03ed09
diff -r 1af13a8a8275 -r d246aa415f3a HX711.cpp
--- a/HX711.cpp	Tue Sep 12 14:38:13 2017 +0000
+++ b/HX711.cpp	Tue Sep 12 15:33:33 2017 +0000
@@ -241,7 +241,7 @@
     myNewRawData->myRawValue    =   0;                                          // Reset variable at the beginning
 
     // Check the gain if it is different, update it ( previous data will be ignored! )
-    if ( myChannel_Gain != CHANNEL_A_GAIN_128 )
+    if ( myChannel_Gain != _HX711_CHANNEL_GAIN )
         HX711_SetChannelAndGain ( myChannel_Gain );
 
 
@@ -491,14 +491,17 @@
 
 
 /**
- * @brief       HX711_SetAutoTare ( HX711_channel_gain_t , Vector_count_t* , float )
+ * @brief       HX711_SetAutoTare ( HX711_channel_gain_t ,float ,HX711_scale_t ,Vector_count_t* ,float )
  *
  * @details     It reads data without any mass on the load cell after the system is calibrated to calculate the tare weight.
  *
- * @param[in]    myChannel_Gain:        Gain/Channel to perform the new measurement.
- * @param[in]    myTime:                How long the auto-set lasts.
+ * @param[in]    myChannel_Gain:            Gain/Channel to perform the new measurement.
+ * @param[in]    myCalibratedMass:          A known value for the calibrated mass when myRawValue_WithCalibratedMass was
+ *                                          calculated.
+ * @param[in]    myScaleCalibratedMass:     The range of the calibrated mass ( kg, g, mg or ug ).
+ * @param[in]    myTime:                    How long the auto-set lasts.
  *
- * @param[out]   myNewRawData:          myRawValue_TareWeight ( ADC code taken without any mass ).
+ * @param[out]   myNewRawData:              myRawValue_TareWeight ( ADC code taken without any mass ).
  *
  *
  * @return       Status of HX711_SetAutoTare.
@@ -510,7 +513,7 @@
  * @pre         NaN.
  * @warning     NaN.
  */
-HX711::HX711_status_t  HX711::HX711_SetAutoTare   ( HX711_channel_gain_t myChannel_Gain, Vector_count_t* myNewRawData, float myTime )
+HX711::HX711_status_t  HX711::HX711_SetAutoTare   ( HX711_channel_gain_t myChannel_Gain, float myCalibratedMass, HX711_scale_t myScaleCalibratedMass, Vector_count_t* myNewRawData, float myTime )
 {
     HX711_status_t        aux;
     Vector_mass_t         myCalculatedMass;
@@ -524,14 +527,19 @@
         myAuxData   +=   myNewRawData->myRawValue;
         wait(1);
     }
-    
+
     myNewRawData->myRawValue    =    ( float )( myAuxData / myTime );
-    
+
     // Turn it into mass
-    myCalculatedMass     =   HX711_CalculateMass ( myNewRawData, _HX711_USER_CALIBATED_MASS, _HX711_SCALE );
+    myCalculatedMass     =   HX711_CalculateMass ( myNewRawData, myCalibratedMass, myScaleCalibratedMass );
 
     // Update the value without any mass
     myNewRawData->myRawValue_TareWeight  =   myCalculatedMass.myMass;
+    
+    
+    // Update Internal Parameters
+    _HX711_USER_CALIBATED_MASS   =   myCalibratedMass;
+    _HX711_SCALE                 =   myScaleCalibratedMass;
 
 
 
@@ -543,4 +551,93 @@
 
 
 
+/**
+ * @brief       HX711_SetManualTare ( float myTareWeight )
+ *
+ * @details     It sets a tare weight manually.
+ *
+ * @param[in]    myTareWeight:          Tare weight.
+ *
+ * @param[out]   NaN.
+ *
+ *
+ * @return       myRawValue_TareWeight.
+ *
+ *
+ * @author      Manuel Caballero
+ * @date        12/September/2017
+ * @version     12/September/2017   The ORIGIN
+ * @pre         NaN.
+ * @warning     NaN.
+ */
+HX711::Vector_count_t  HX711::HX711_SetManualTare   ( float myTareWeight )
+{
+    Vector_count_t myNewTareWeight;
 
+    // Update the value defined by the user
+    myNewTareWeight.myRawValue_TareWeight  =   myTareWeight;
+
+
+
+    return   myNewTareWeight;
+}
+
+
+
+/**
+ * @brief       HX711_CalculateVoltage ( Vector_count_t* ,float )
+ *
+ * @details     It calculates the mass.
+ *
+ * @param[in]    myChannel_Gain:            Gain/Channel of the measurement.
+ * @param[in]    myNewRawData:              myRawValue ( the current data taken by the system ).
+ * @param[in]    myVoltageReference:        The voltage at the converter reference input.
+ *
+ * @param[out]   NaN.
+ *
+ *
+ * @return       The calculated voltage.
+ *
+ *
+ * @author      Manuel Caballero
+ * @date        12/September/2017
+ * @version     12/September/2017   The ORIGIN
+ * @pre         NaN.
+ * @warning     NaN.
+ */
+HX711::Vector_voltage_t  HX711::HX711_CalculateVoltage ( Vector_count_t* myNewRawData, float myVoltageReference )
+{
+    // Terminology by Texas Instruments: sbau175a.pdf, p12 3.2 Measurement Modes Raw
+    float x, B, A;
+
+    Vector_voltage_t v;
+
+    
+    x   =    myNewRawData->myRawValue;
+    B   =    ( 16777216.0 - 1.0 );
+    
+    // Adatp the gain
+    switch ( _HX711_CHANNEL_GAIN ) {
+        default:
+        case CHANNEL_A_GAIN_128:
+            A             =   128.0;
+            break;
+
+        case CHANNEL_B_GAIN_32:
+            A             =   32.0;
+            break;
+
+        case CHANNEL_A_GAIN_64:
+            A             =   64.0;
+            break;
+    }
+
+
+    // Calculate the voltage ( v )
+    v.myVoltage = ( float )( ( x / B ) * ( myVoltageReference / A ) );          // The voltage
+
+
+
+
+    return   v;
+}