battery_monitor_code

Dependencies:   MAX30003 USBDevice

Revision:
1:4510fb7c9621
Parent:
0:76206161b30f
--- a/main.cpp	Wed May 30 23:34:01 2018 +0000
+++ b/main.cpp	Tue Oct 19 16:10:24 2021 +0000
@@ -41,9 +41,19 @@
 DigitalOut gLED(LED2);
 DigitalOut bLED(LED3);
 
-I2C i2cBus(I2C2_SDA, I2C2_SCL);  // P5_7, P6_0
+//AnalogIn   ain_0(AIN_0);
+//AnalogIn   ain_1(AIN_1);
+AnalogIn   ain_4(AIN_4);
+AnalogIn   ain_5(AIN_5);
+
+const float battery_max = 4.20; //maximum voltage of battery
+const float battery_min = 3.0;  //minimum voltage of battery before shutdown
+uint8_t output = 0;
+    
+//DigitalIn vref(P1_4);
+//I2C i2cBus(I2C2_SDA, I2C2_SCL);  // P5_7, P6_0
  
-MAX14690 max14690(&i2cBus);
+//MAX14690 max14690(&i2cBus);
   
 // main() runs in its own thread in the OS
 // (note the calls to Thread::wait below for delays)
@@ -69,23 +79,51 @@
     DigitalOut gLED(LED2, LED_OFF);
     DigitalOut bLED(LED3, LED_OFF);
     
+    Serial pc(P3_1, P3_0);            // Use USB debug probe for serial link
+    pc.baud(115200);                    // Baud rate = 115200
+    
     bLED = LED_ON;
     gLED = LED_ON;
 
-    i2cBus.frequency(400000);
+    /*i2cBus.frequency(400000);
 
     max14690.ldo2Millivolts = 3300;
     max14690.ldo3Millivolts = 3300;
     max14690.ldo2Mode = MAX14690::LDO_ENABLED;
     max14690.ldo3Mode = MAX14690::LDO_ENABLED;
-    max14690.monCfg = MAX14690::MON_HI_Z;
+    max14690.monCfg = MAX14690::MON_BAT;
     if (max14690.init() == MAX14690_ERROR) {
-        printf("Error initializing MAX14690.\r\n");
+        pc.printf("Error initializing MAX14690.\r\n");
         bLED = LED_OFF;
         gLED = LED_OFF;
         rLED = LED_ON;
     } else {
-        printf("MAX14690 PMIC initialization is succesful.\r\n");
-    }
+        pc.printf("MAX14690 PMIC initialization is succesful.\r\n");
+        gLED = LED_OFF;
+    }*/
+    while(1)
+    {
+        pc.printf("MAX14690 PMIC initialization is Done.\r\n");
+      //  pc.printf("analog read 0 %3.4f\n", ain_4.read());
+      //  pc.printf("percentage: %3.3f%%\n", ain_4.read() * 100.0f);
+       // pc.printf("normalized: 0x%04X \n", ain_4.read_u16());
+        
+        unsigned short raw;
+        raw = ain_4.read_u16();
+        float vin;
+
+       vin = ((raw*5*1.2)/ 65535.0);
+       pc.printf("battery voltage is: %3.1f\n", vin);
+       
+       output = ((vin - battery_min) / (battery_max - battery_min)) * 100;
+    if (output < 100)
+        pc.printf("percentage: %i\n",output); 
+    else
+        pc.printf("percentage: 99\n"); 
+        
+        wait(5);
+        
+        
+        }
 }