A feature complete driver for the MAX17048 lithium fuel gauge from Maxim.

Dependents:   MAX17048_HelloWorld ECGAFE_copy MAX17048_HelloWorld Orion_newPCB_test_LV ... more

Now fully tested!

Revision:
7:bf6972a21c61
Parent:
6:6927c72b2923
Child:
8:65c889800b3a
--- a/MAX17048.cpp	Thu Nov 07 18:23:02 2013 +0000
+++ b/MAX17048.cpp	Tue Nov 12 17:48:11 2013 +0000
@@ -26,9 +26,6 @@
 {
     //Probe for the MAX17048 using a Zero Length Transfer
     if (!m_I2C.write(m_ADDR, NULL, 0)) {
-        //Load the default RCOMP value
-        writeRCOMP(m_RCOMP0);
-
         //Return success
         return true;
     } else {
@@ -158,18 +155,40 @@
     return read(REG_VERSION);
 }
 
+char MAX17048::compensation()
+{
+    //Read the 16-bit register value
+    unsigned short value = read(REG_CONFIG);
+
+    //Return only the upper byte
+    return (char)(value >> 8);
+}
+
+void MAX17048::compensation(char rcomp)
+{
+    //Read the current 16-bit register value
+    unsigned short value = read(REG_CONFIG);
+
+    //Update the register value
+    value &= 0x00FF;
+    value |= rcomp << 8;
+
+    //Write the value back out
+    write(REG_CONFIG, value);
+}
+
 void MAX17048::tempCompensation(float temp)
 {
     //Calculate the new RCOMP value
     char rcomp;
     if (temp > 20.0) {
-        rcomp = m_RCOMP0 + (temp - 20.0) * -0.5;
+        rcomp = RCOMP0 + (temp - 20.0) * -0.5;
     } else {
-        rcomp = m_RCOMP0 + (temp - 20.0) * -5.0;
+        rcomp = RCOMP0 + (temp - 20.0) * -5.0;
     }
 
     //Update the RCOMP value
-    writeRCOMP(rcomp);
+    compensation(rcomp);
 }
 
 bool MAX17048::sleeping()
@@ -525,16 +544,3 @@
     //Write the data
     m_I2C.write(m_ADDR, buff, 3);
 }
-
-void MAX17048::writeRCOMP(char rcomp)
-{
-    //Read the current 16-bit register value
-    unsigned short value = read(REG_CONFIG);
-
-    //Update the register value
-    value &= 0x00FF;
-    value |= rcomp << 8;
-
-    //Write the value back out
-    write(REG_CONFIG, value);
-}