Lectura de una celda de Carga HX711.

Dependencies:   mbed

Revision:
1:392a3fb6d177
Parent:
0:5d67331a6e7e
--- a/main.cpp	Fri May 05 20:16:31 2017 +0000
+++ b/main.cpp	Fri Sep 03 04:51:59 2021 +0000
@@ -1,30 +1,65 @@
 #include "mbed.h"
-#include "HX711.h"
-#include "TextLCD.h" 
+#include "Hx711.h"
+
+#define n_muestras 10
 
-DigitalOut led(LED_BLUE);
-HX711 scale(PTC9, PTC8);
+DigitalOut led(LED1);
+
+Hx711 LoadCeld(A1, A0, 1, 1.0);
 
 Serial rs232(USBTX, USBRX);    // USB Serial Terminal
-TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2); // Rs, E, d4, d5, d6, d7 
+
+
+
+
 
+
+float escala = 0.0;
+float offset; 
 float calibration_factor = 1000; //Ajustar este valor para calibrar el peso exacto
-int averageSamples = 100;
+int averageSamples = 300;
 
 int main(void)
 {
      
-    scale.setScale(0);
-    scale.tare(); //Reset the scale to 0
+     
+    for(int i = 0; i < n_muestras; i++ )
+    {
+        while(!LoadCeld.is_ready());
+        
+        offset = ( offset + LoadCeld.readRaw());
+
+    }
     
-    long zero_factor = scale.averageValue(averageSamples); // Saca promedio de varias lecturas para estabilizar la medida
+    offset = (offset / n_muestras);
+    LoadCeld.set_offset(offset);
+    LoadCeld.set_scale(1.0);
+    rs232.printf("Offset: %.2f \n", offset);
+    rs232.printf("Offset: %.2f \n", LoadCeld.get_offset());
+    offset = 0.0;
+    
+    //scale.tare(); //Reset the scale to 0
+    
+    //long zero_factor = scale.averageValue(averageSamples); // Saca promedio de varias lecturas para estabilizar la medida
      
     while (true) {
-        scale.setScale(calibration_factor); 
-        float weight = scale.getGram();
-        rs232.printf("Peso: %.2f\n", weight);
-        lcd.locate(0,0);
-        lcd.printf("Peso: %.2f Grms", weight);
+        
+        
+        for(int i = 0; i < n_muestras; i++ )
+        {
+            while(!LoadCeld.is_ready());
+            
+            escala = ( escala + LoadCeld.read());
+
+        }
+        escala = (escala / n_muestras);
+        rs232.printf("Peso: %.2f \n", escala); 
+        escala = 0.0;
+        //scale.setScale(calibration_factor); 
+        //float weight = scale.getGram();
+        //rs232.printf("Peso: %.2f\n", weight);
+        //lcd.locate(0,0);
+        //lcd.printf("Peso: %.2f Grms", weight);
         led = !led; // toggle led
         wait(0.2f);
     }