Library DS2782 Stand-Alone Fuel Gauge IC

Files at this revision

API Documentation at this revision

Comitter:
igbt6
Date:
Wed Dec 17 21:14:59 2014 +0000
Parent:
0:e6d413e57654
Commit message:
Library for DS2782 Stand-Alone Fuel Gauge IC

Changed in this revision

ds2782.cpp Show annotated file Show diff for this revision Revisions of this file
ds2782.h Show annotated file Show diff for this revision Revisions of this file
diff -r e6d413e57654 -r 7cd80eb4df4c ds2782.cpp
--- a/ds2782.cpp	Sun Nov 30 23:23:42 2014 +0000
+++ b/ds2782.cpp	Wed Dec 17 21:14:59 2014 +0000
@@ -5,11 +5,46 @@
     
     mI2c.frequency(i2cFrequencyHz);
     mTemperature=0;
+    //initDS2782();
+    //setACRRegister(0xffff);  //to clear flags
 }   
     
     
 bool DS2782::initDS2782(void){
      
+     uint16_t full40Reg = 0x3200;
+     uint8_t buf[2];
+     fillBuf(full40Reg, buf);
+     setEepromBlockRegister(FULL_40_MSB,buf, 2);
+    
+     buf[0]= 0xD5;
+     setEepromBlockRegister(VCHG,buf, 1);
+     
+     buf[0]=0x14;
+     setEepromBlockRegister(IMIN,buf,1);
+     
+     buf[0]=0xB3;
+     setEepromBlockRegister(VAE,buf,1);
+     
+     buf[0]=0x0a;
+     setEepromBlockRegister(IAE,buf,1);
+     
+     buf[0]=0x06;
+     setEepromBlockRegister(ACTIVE_EMPTY_40,buf,1);
+     
+     //fillBuf(full40Reg, buf);
+     //setEepromBlockRegister(RSGAIN_MSB,buf,2);
+     
+     buf[0]=0;
+     setEepromBlockRegister(RSTC,buf,1);
+     
+     buf[0]=0x32;
+     setEepromBlockRegister(RSNSP,buf,1);
+     
+     buf[0]=0;
+     setEepromBlockRegister(AB,buf,1);
+     
+     
      return true;
 }
 
@@ -85,22 +120,49 @@
 }
 
 
+bool DS2782::setACRRegister(uint16_t reg)
+{    
+    uint8_t buf[2];
+    buf[0]= ((reg>>8)&0xFF);
+    buf[1]= ((reg)&0xFF);
+    if(!(write(ACR_MSB_REG, buf,2))) return false;
+    return true;        
+}
 
 
-
-float DS2782::readTest(void){
+float DS2782::readAcrReg(void){
 
    uint8_t rawData[2];
    uint16_t rawRes=0;
-   /*if(!read(ACR_MSB_REG, rawData,2)) return false;
+   if(!read(ACR_MSB_REG, rawData,2)) return false;
       rawRes= get16BitData(rawData[0],rawData[1]);
-      */
-    /*if(rawRes &0x8000){
-      return ((float)(rawRes-65536)*0.07813);
+      
+    if(rawRes &0x8000){
+      return ((float)(rawRes-65536)*1.5625);
     }
     else 
-    */
-    if(!read(RARC_REG, rawData,1)) return false;
-      rawRes= get16BitData(rawData[0],rawData[1]);
-    return ((float)(rawData[0]*0.3906));
+      return ((float)(rawRes)*1.5625);
+}
+
+
+bool DS2782::setEepromBlockRegister(ParamEepromReg reg, uint8_t * value, uint8_t length){
+    uint8_t buf[length];
+    memcpy(buf,value,length);
+    if(!(write(reg, buf,length))) return false;
+    return true; 
+    
 }
+
+uint8_t DS2782::readRarcReg(void){
+    uint8_t rarcRegVal;  //unit [%]
+    if(!read(RARC_REG, &rarcRegVal,1)) return 255;    
+    return rarcRegVal;
+}
+
+
+uint8_t DS2782::readStatusReg(void){
+    uint8_t statusRegVal;
+    if(!read(STATUS, &statusRegVal,1)) return 255;    
+    return statusRegVal;
+    
+}
diff -r e6d413e57654 -r 7cd80eb4df4c ds2782.h
--- a/ds2782.h	Sun Nov 30 23:23:42 2014 +0000
+++ b/ds2782.h	Wed Dec 17 21:14:59 2014 +0000
@@ -13,6 +13,12 @@
 
   Documentation regarding the MAX9611 might be found here: 
   http://www.maximintegrated.com/en/products/power/battery-management/DS2782.html
+  
+  and some very useful tutorials:
+  http://www.maximintegrated.com/en/products/power/battery-management/DS2782.html/tb_tab2
+  http://www.maximintegrated.com/en/app-notes/index.mvp/id/3584
+  http://www.maximintegrated.com/en/app-notes/index.mvp/id/3463
+  
 */
 
 
@@ -71,6 +77,46 @@
          
  }RegAddr;
  
+ typedef enum {
+     
+   CONTROL = 0x60,   //Control Register
+   AB =0x61,         //Accumulation Bias
+   AC_MSB = 0x62,    //Aging Capacity MSB
+   AC_LSB = 0x63,    //Aging Capacity LSB
+   VCHG = 0x64,      //Charge Voltage
+   IMIN =0x65,       //Minimum Charge Current
+   VAE = 0x66,       //Active Empty Voltage
+   IAE = 0x67,       //Active Empty Current
+   ACTIVE_EMPTY_40, 
+   RSNSP,     //Sense Resistor Prime
+   FULL_40_MSB,
+   FULL_40_LSB,
+   FULL_3040_SLOPE,
+   FULL_2030_SLOPE,
+   FULL_1020_SLOPE,
+   FULL_0010_SLOPE,
+   AE_3040_SLOPE,
+   AE_2030_SLOPE,
+   AE_1020_SLOPE,
+   AE_0010_SLOPE,
+   SE_3040_SLOPE,
+   SE_2030_SLOPE,
+   SE_1020_SLOPE,
+   SE_0010_SLOPE,
+   RSGAIN_MSB,          //Sense Resistor Gain MSB
+   RSGAIN_LSB,          //Sense Resistor Gain LSB
+   RSTC,                //Sense Resistor Temp. Coeff.
+   FRSGAIN_MSB,         //Factory Gain MSB
+   FRSGAIN_LSB,         //Factory Gain LSB
+   I2C_SLAVE_ADDR= 0x7E //2-Wire Slave Address     
+ }ParamEepromReg;
+ 
+ 
+ 
+
+ 
+ 
+ 
     /** Write data to the given register
      *  
      * @returns
@@ -98,6 +144,15 @@
         uint16_t data16Bit= (msbByte<<8)|(lsbByte);
         return data16Bit; 
     }
+    
+    /** divide 16 bit word to 2 8bit bytes
+    * @param 1st byte
+    * @param buf
+    */
+    inline void fillBuf(uint16_t varVal, uint8_t* buf){
+        buf[0]= ((varVal>>8)&0xFF);
+        buf[1]= ((varVal)&0xFF);
+    }
         
     I2C mI2c;   
     uint8_t mI2cAddr;
@@ -111,6 +166,15 @@
    
    /**********public methods********************************/
     public:
+    
+    typedef enum {
+        PORF = 0x02,     //Power-On Reset Flag – Useful for reset detection, see text below.
+        UVF =0x04,       //Under-Voltage Flag
+        LEARNF = 0x10,   //Learn Flag – When set to 1, a charge cycle can be used to learn battery capacity.
+        SEF = 0x20,      //Standby Empty Flag
+        AEF = 0x40,      //Active Empty Flag
+        CHGTF =0x80,     //Charge Termination Flag
+    }StatusReg ;
 
     /** Create an SI7020 instance
      * @param sda pin 
@@ -136,13 +200,18 @@
     bool readTemperature(void);   
     bool readCurrent(void);
     bool readVoltage(void);
+    bool setACRRegister(uint16_t reg);  // set to 0 clears LEARNF and other flags
+    bool setEepromBlockRegister(ParamEepromReg reg, uint8_t * value, uint8_t length);
+    uint8_t readStatusReg(void);
+    float readAcrReg(void);
+    uint8_t readRarcReg(void);
+    
     
     // setters-getters
     float getTemperature(void);
     float getCurrent(void);
     float getVoltage(void);
-    
-    float readTest(void);
+
 };
 
 #endif
\ No newline at end of file