Texas Instruments INA226 High-or Low-Side Measurement, Bi-Directional CURRENT/POWER MONITOR with I 2C™ Interface http://www.ti.com/product/ina226&DCMP=analog_signalchain_mr&HQS=ina226-pr

Files at this revision

API Documentation at this revision

Comitter:
gizmo69the2nd
Date:
Wed Aug 07 22:51:12 2013 +0000
Commit message:
Working version, not tested

Changed in this revision

INA226.cpp Show annotated file Show diff for this revision Revisions of this file
INA226.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3984961a3725 INA226.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA226.cpp	Wed Aug 07 22:51:12 2013 +0000
@@ -0,0 +1,227 @@
+/*
+ * INA226 voltage/temprature monitor library
+ *
+ *
+ * Copyright (c) 2013 Davy Van Belle, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/** @file
+ * @brief LTC29990 I2C
+ */
+ 
+#include "mbed.h"
+#include "INA226.h"
+
+INA226::INA226 (I2C* i2c, char addr)
+{
+    _i2c = i2c;
+    _addr = addr;
+
+    ShuntR = 0;
+    CURR_LSB = 0;
+    
+}
+
+void INA226::setConfig (unsigned short reg)
+{
+    char cmd[3];
+    cmd[0] = CONF;
+    cmd[1] = (char) ((reg & 0xFF00) >> 8);
+    cmd[2] = (char) (reg & 0x00FF);
+    _i2c->write(_addr,cmd,3);
+}    
+
+unsigned short INA226::getConfig()
+{
+    char cmd;
+    char buff[2];
+    
+    cmd = CONF;
+    
+    _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    return (buff[0] << 8) | buff[1];
+}
+
+float INA226::getShuntVolt()
+{
+    char cmd;
+    char buff[2];
+    bool sign;
+    short tmp;
+    
+    cmd = VSHUNT;
+    
+    _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    
+    sign = buff[0] & 0x80;
+    tmp = ((buff[0] & 0x7F) << 8) | buff[1];
+    if(!sign) return (float)tmp*SHUNT_LSB;                   //positive calulation.
+    else return ((float)tmp-32767)*SHUNT_LSB;                 //negative calculation.
+}
+
+float INA226::getBusVolt()
+{
+    char cmd;
+    char buff[2];
+    bool sign;
+    short tmp;
+    
+    cmd = VBUS;
+    
+    _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    
+    sign = buff[0] & 0x80;
+    tmp = ((buff[0] & 0x7F) << 8) | buff[1];
+    if(!sign) return (float)tmp*BUS_LSB;                   //positive calulation.
+    else return ((float)tmp-32767)*BUS_LSB;                 //negative calculation.
+}
+
+float INA226::getCurrent()
+{
+    char cmd;
+    char buff[2];
+    bool sign;
+    short tmp;
+    
+    cmd = CURRENT;
+    
+    _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    
+    sign = buff[0] & 0x80;
+    tmp = ((buff[0] & 0x7F) << 8) | buff[1];
+    if(!sign) return (float)tmp*CURR_LSB;                   //positive calulation.
+    else return ((float)tmp-32767)*CURR_LSB;                 //negative calculation.
+}
+
+float INA226::getPower()
+{
+    char cmd;
+    char buff[2];
+    bool sign;
+    short tmp;
+    
+    cmd = POWER;
+    
+     _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    
+    sign = buff[0] & 0x80;
+    tmp = ((buff[0] & 0x7F) << 8) | buff[1];
+    if(!sign) return (float)tmp*25*CURR_LSB;                   //positive calulation.
+    else return ((float)tmp-32767)*25*CURR_LSB;                 //negative calculation.
+}
+
+void INA226::setCalibration(unsigned short reg)
+{
+    char cmd[3];
+    cmd[0] = CAL;
+    cmd[1] = (char) ((reg & 0xFF00) >> 8);
+    cmd[2] = (char) (reg & 0x00FF);
+    _i2c->write(_addr,cmd,3);
+} 
+
+unsigned short INA226::getCalibration()
+{
+    char cmd;
+    char buff[2];
+    
+    cmd = CAL;
+    
+     _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    return (buff[0] << 8) | buff[1];
+}
+
+void INA226::setMaskEnable(unsigned short reg)
+{
+    char cmd[3];
+    cmd[0] = MASK;
+    cmd[1] = (char) ((reg & 0xFF00) >> 8);
+    cmd[2] = (char) (reg & 0x00FF);
+    _i2c->write(_addr,cmd,3);
+} 
+
+unsigned short INA226::getMaskEnable()
+{
+    char cmd;
+    char buff[2];
+    
+    cmd = MASK;
+    
+     _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    return (buff[0] << 8) | buff[1];
+}
+
+void INA226::setAlertLimit(unsigned short reg)
+{
+    char cmd[3];
+    cmd[0] = ALERT;
+    cmd[1] = (char) ((reg & 0xFF00) >> 8);
+    cmd[2] = (char) (reg & 0x00FF);
+    _i2c->write(_addr,cmd,3);
+} 
+
+unsigned short INA226::getAlertLimit()
+{
+    char cmd;
+    char buff[2];
+    
+    cmd = ALERT;
+    
+     _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    return (buff[0] << 8) | buff[1];
+}
+
+unsigned short INA226::getID()
+{
+    char cmd;
+    char buff[2];
+    
+    cmd = DIE_ID;
+    
+     _i2c->write(_addr,&cmd,1,true);
+    _i2c->read(_addr+1,buff,2);
+    return (buff[0] << 8) | buff[1];
+}
+
+void INA226::setShuntRValue(float val)
+{
+    ShuntR = val;
+}
+
+float INA226::getShuntRValue(void)
+{
+    return ShuntR;
+}
+
+
+void INA226::setCurrentLSB(float val)
+{
+    CURR_LSB = val;
+}
+
+float INA226::getCurrentLSB(float val)
+{
+    return CURR_LSB;
+}
diff -r 000000000000 -r 3984961a3725 INA226.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA226.h	Wed Aug 07 22:51:12 2013 +0000
@@ -0,0 +1,133 @@
+/*
+ * Texas Instruments INA226 voltage/ current/ power monitor library
+ *
+ *
+ * Copyright (c) 2013 Davy Van Belle, MIT License
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
+ * and associated documentation files (the "Software"), to deal in the Software without restriction, 
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or 
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/** @file
+ * @brief INA226 I2C
+ */
+ 
+#ifndef INA226_H
+#define INA226_H
+
+#include "mbed.h"
+
+#define CONF 0x00
+
+#define VSHUNT 0x01
+#define VBUS 0x02
+#define POWER 0x03
+#define CURRENT 0x04
+#define CAL 0x05
+#define MASK 0x06
+#define ALERT 0x07
+
+#define DIE_ID 0xFF
+
+#define SHUNT_LSB 0.0000025
+#define BUS_LSB 0.00125
+
+
+
+/** INA226 class 
+ */
+class INA226 {
+public:
+    /** init INA226 class
+     * @param *i2c pointer to I2C serial interface
+     * @param addr sensor I2C address
+     */
+    INA226 (I2C* i2c, char addr); 
+
+    /** Set the configuration of the device
+     * @param reg desired Configure Register bits
+     */
+    void setConfig (unsigned short reg);
+    
+    /** Get device Configuration register
+     */  
+    unsigned short getConfig();
+    
+    /** Get the voltage over the shunt
+    */
+    float getShuntVolt();
+
+    /** Get the voltage of the bus rail
+     */
+    float getBusVolt();
+    
+    /** Get the current measured through the shunt.
+     * in order to get the current you need to set the shut resistor value first
+     */  
+    float getCurrent();
+    
+    float getPower();
+    
+    /** Set the Calibartion register 
+    */
+    void setCalibration(unsigned short reg);
+    
+    /** Get the Calibration register
+    */
+    unsigned short getCalibration();
+    
+    /** Set the mask/enable Register
+    */
+    void setMaskEnable(unsigned short reg);
+    
+    /** Get the mask/enable Register
+    */
+    unsigned short getMaskEnable();
+    
+    void setAlertLimit(unsigned short reg);
+    
+    unsigned short getAlertLimit();
+    
+    /** Get the unique ID of the device
+    */
+    unsigned short getID();
+    
+    /** Set the shunt resistor
+    *@param val value in ohm
+    */
+    void setShuntRValue(float val);
+    
+    float getShuntRValue(void);
+    
+    /** Set the current LSB for maximum current in decimal
+    */
+    void setCurrentLSB(float val);
+
+    float getCurrentLSB(float val);
+    
+protected:
+
+
+private:
+    char _addr;
+    I2C *_i2c;
+    float ShuntR;
+    float CURR_LSB;
+    
+    
+    
+};
+    
+#endif    
\ No newline at end of file