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

Committer:
gizmo69the2nd
Date:
Wed Aug 07 22:51:12 2013 +0000
Revision:
0:3984961a3725
Working version, not tested

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gizmo69the2nd 0:3984961a3725 1 /*
gizmo69the2nd 0:3984961a3725 2 * INA226 voltage/temprature monitor library
gizmo69the2nd 0:3984961a3725 3 *
gizmo69the2nd 0:3984961a3725 4 *
gizmo69the2nd 0:3984961a3725 5 * Copyright (c) 2013 Davy Van Belle, MIT License
gizmo69the2nd 0:3984961a3725 6 *
gizmo69the2nd 0:3984961a3725 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gizmo69the2nd 0:3984961a3725 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gizmo69the2nd 0:3984961a3725 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gizmo69the2nd 0:3984961a3725 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gizmo69the2nd 0:3984961a3725 11 * furnished to do so, subject to the following conditions:
gizmo69the2nd 0:3984961a3725 12 *
gizmo69the2nd 0:3984961a3725 13 * The above copyright notice and this permission notice shall be included in all copies or
gizmo69the2nd 0:3984961a3725 14 * substantial portions of the Software.
gizmo69the2nd 0:3984961a3725 15 *
gizmo69the2nd 0:3984961a3725 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gizmo69the2nd 0:3984961a3725 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gizmo69the2nd 0:3984961a3725 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gizmo69the2nd 0:3984961a3725 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gizmo69the2nd 0:3984961a3725 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gizmo69the2nd 0:3984961a3725 21 */
gizmo69the2nd 0:3984961a3725 22
gizmo69the2nd 0:3984961a3725 23 /** @file
gizmo69the2nd 0:3984961a3725 24 * @brief LTC29990 I2C
gizmo69the2nd 0:3984961a3725 25 */
gizmo69the2nd 0:3984961a3725 26
gizmo69the2nd 0:3984961a3725 27 #include "mbed.h"
gizmo69the2nd 0:3984961a3725 28 #include "INA226.h"
gizmo69the2nd 0:3984961a3725 29
gizmo69the2nd 0:3984961a3725 30 INA226::INA226 (I2C* i2c, char addr)
gizmo69the2nd 0:3984961a3725 31 {
gizmo69the2nd 0:3984961a3725 32 _i2c = i2c;
gizmo69the2nd 0:3984961a3725 33 _addr = addr;
gizmo69the2nd 0:3984961a3725 34
gizmo69the2nd 0:3984961a3725 35 ShuntR = 0;
gizmo69the2nd 0:3984961a3725 36 CURR_LSB = 0;
gizmo69the2nd 0:3984961a3725 37
gizmo69the2nd 0:3984961a3725 38 }
gizmo69the2nd 0:3984961a3725 39
gizmo69the2nd 0:3984961a3725 40 void INA226::setConfig (unsigned short reg)
gizmo69the2nd 0:3984961a3725 41 {
gizmo69the2nd 0:3984961a3725 42 char cmd[3];
gizmo69the2nd 0:3984961a3725 43 cmd[0] = CONF;
gizmo69the2nd 0:3984961a3725 44 cmd[1] = (char) ((reg & 0xFF00) >> 8);
gizmo69the2nd 0:3984961a3725 45 cmd[2] = (char) (reg & 0x00FF);
gizmo69the2nd 0:3984961a3725 46 _i2c->write(_addr,cmd,3);
gizmo69the2nd 0:3984961a3725 47 }
gizmo69the2nd 0:3984961a3725 48
gizmo69the2nd 0:3984961a3725 49 unsigned short INA226::getConfig()
gizmo69the2nd 0:3984961a3725 50 {
gizmo69the2nd 0:3984961a3725 51 char cmd;
gizmo69the2nd 0:3984961a3725 52 char buff[2];
gizmo69the2nd 0:3984961a3725 53
gizmo69the2nd 0:3984961a3725 54 cmd = CONF;
gizmo69the2nd 0:3984961a3725 55
gizmo69the2nd 0:3984961a3725 56 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 57 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 58 return (buff[0] << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 59 }
gizmo69the2nd 0:3984961a3725 60
gizmo69the2nd 0:3984961a3725 61 float INA226::getShuntVolt()
gizmo69the2nd 0:3984961a3725 62 {
gizmo69the2nd 0:3984961a3725 63 char cmd;
gizmo69the2nd 0:3984961a3725 64 char buff[2];
gizmo69the2nd 0:3984961a3725 65 bool sign;
gizmo69the2nd 0:3984961a3725 66 short tmp;
gizmo69the2nd 0:3984961a3725 67
gizmo69the2nd 0:3984961a3725 68 cmd = VSHUNT;
gizmo69the2nd 0:3984961a3725 69
gizmo69the2nd 0:3984961a3725 70 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 71 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 72
gizmo69the2nd 0:3984961a3725 73 sign = buff[0] & 0x80;
gizmo69the2nd 0:3984961a3725 74 tmp = ((buff[0] & 0x7F) << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 75 if(!sign) return (float)tmp*SHUNT_LSB; //positive calulation.
gizmo69the2nd 0:3984961a3725 76 else return ((float)tmp-32767)*SHUNT_LSB; //negative calculation.
gizmo69the2nd 0:3984961a3725 77 }
gizmo69the2nd 0:3984961a3725 78
gizmo69the2nd 0:3984961a3725 79 float INA226::getBusVolt()
gizmo69the2nd 0:3984961a3725 80 {
gizmo69the2nd 0:3984961a3725 81 char cmd;
gizmo69the2nd 0:3984961a3725 82 char buff[2];
gizmo69the2nd 0:3984961a3725 83 bool sign;
gizmo69the2nd 0:3984961a3725 84 short tmp;
gizmo69the2nd 0:3984961a3725 85
gizmo69the2nd 0:3984961a3725 86 cmd = VBUS;
gizmo69the2nd 0:3984961a3725 87
gizmo69the2nd 0:3984961a3725 88 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 89 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 90
gizmo69the2nd 0:3984961a3725 91 sign = buff[0] & 0x80;
gizmo69the2nd 0:3984961a3725 92 tmp = ((buff[0] & 0x7F) << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 93 if(!sign) return (float)tmp*BUS_LSB; //positive calulation.
gizmo69the2nd 0:3984961a3725 94 else return ((float)tmp-32767)*BUS_LSB; //negative calculation.
gizmo69the2nd 0:3984961a3725 95 }
gizmo69the2nd 0:3984961a3725 96
gizmo69the2nd 0:3984961a3725 97 float INA226::getCurrent()
gizmo69the2nd 0:3984961a3725 98 {
gizmo69the2nd 0:3984961a3725 99 char cmd;
gizmo69the2nd 0:3984961a3725 100 char buff[2];
gizmo69the2nd 0:3984961a3725 101 bool sign;
gizmo69the2nd 0:3984961a3725 102 short tmp;
gizmo69the2nd 0:3984961a3725 103
gizmo69the2nd 0:3984961a3725 104 cmd = CURRENT;
gizmo69the2nd 0:3984961a3725 105
gizmo69the2nd 0:3984961a3725 106 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 107 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 108
gizmo69the2nd 0:3984961a3725 109 sign = buff[0] & 0x80;
gizmo69the2nd 0:3984961a3725 110 tmp = ((buff[0] & 0x7F) << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 111 if(!sign) return (float)tmp*CURR_LSB; //positive calulation.
gizmo69the2nd 0:3984961a3725 112 else return ((float)tmp-32767)*CURR_LSB; //negative calculation.
gizmo69the2nd 0:3984961a3725 113 }
gizmo69the2nd 0:3984961a3725 114
gizmo69the2nd 0:3984961a3725 115 float INA226::getPower()
gizmo69the2nd 0:3984961a3725 116 {
gizmo69the2nd 0:3984961a3725 117 char cmd;
gizmo69the2nd 0:3984961a3725 118 char buff[2];
gizmo69the2nd 0:3984961a3725 119 bool sign;
gizmo69the2nd 0:3984961a3725 120 short tmp;
gizmo69the2nd 0:3984961a3725 121
gizmo69the2nd 0:3984961a3725 122 cmd = POWER;
gizmo69the2nd 0:3984961a3725 123
gizmo69the2nd 0:3984961a3725 124 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 125 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 126
gizmo69the2nd 0:3984961a3725 127 sign = buff[0] & 0x80;
gizmo69the2nd 0:3984961a3725 128 tmp = ((buff[0] & 0x7F) << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 129 if(!sign) return (float)tmp*25*CURR_LSB; //positive calulation.
gizmo69the2nd 0:3984961a3725 130 else return ((float)tmp-32767)*25*CURR_LSB; //negative calculation.
gizmo69the2nd 0:3984961a3725 131 }
gizmo69the2nd 0:3984961a3725 132
gizmo69the2nd 0:3984961a3725 133 void INA226::setCalibration(unsigned short reg)
gizmo69the2nd 0:3984961a3725 134 {
gizmo69the2nd 0:3984961a3725 135 char cmd[3];
gizmo69the2nd 0:3984961a3725 136 cmd[0] = CAL;
gizmo69the2nd 0:3984961a3725 137 cmd[1] = (char) ((reg & 0xFF00) >> 8);
gizmo69the2nd 0:3984961a3725 138 cmd[2] = (char) (reg & 0x00FF);
gizmo69the2nd 0:3984961a3725 139 _i2c->write(_addr,cmd,3);
gizmo69the2nd 0:3984961a3725 140 }
gizmo69the2nd 0:3984961a3725 141
gizmo69the2nd 0:3984961a3725 142 unsigned short INA226::getCalibration()
gizmo69the2nd 0:3984961a3725 143 {
gizmo69the2nd 0:3984961a3725 144 char cmd;
gizmo69the2nd 0:3984961a3725 145 char buff[2];
gizmo69the2nd 0:3984961a3725 146
gizmo69the2nd 0:3984961a3725 147 cmd = CAL;
gizmo69the2nd 0:3984961a3725 148
gizmo69the2nd 0:3984961a3725 149 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 150 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 151 return (buff[0] << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 152 }
gizmo69the2nd 0:3984961a3725 153
gizmo69the2nd 0:3984961a3725 154 void INA226::setMaskEnable(unsigned short reg)
gizmo69the2nd 0:3984961a3725 155 {
gizmo69the2nd 0:3984961a3725 156 char cmd[3];
gizmo69the2nd 0:3984961a3725 157 cmd[0] = MASK;
gizmo69the2nd 0:3984961a3725 158 cmd[1] = (char) ((reg & 0xFF00) >> 8);
gizmo69the2nd 0:3984961a3725 159 cmd[2] = (char) (reg & 0x00FF);
gizmo69the2nd 0:3984961a3725 160 _i2c->write(_addr,cmd,3);
gizmo69the2nd 0:3984961a3725 161 }
gizmo69the2nd 0:3984961a3725 162
gizmo69the2nd 0:3984961a3725 163 unsigned short INA226::getMaskEnable()
gizmo69the2nd 0:3984961a3725 164 {
gizmo69the2nd 0:3984961a3725 165 char cmd;
gizmo69the2nd 0:3984961a3725 166 char buff[2];
gizmo69the2nd 0:3984961a3725 167
gizmo69the2nd 0:3984961a3725 168 cmd = MASK;
gizmo69the2nd 0:3984961a3725 169
gizmo69the2nd 0:3984961a3725 170 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 171 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 172 return (buff[0] << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 173 }
gizmo69the2nd 0:3984961a3725 174
gizmo69the2nd 0:3984961a3725 175 void INA226::setAlertLimit(unsigned short reg)
gizmo69the2nd 0:3984961a3725 176 {
gizmo69the2nd 0:3984961a3725 177 char cmd[3];
gizmo69the2nd 0:3984961a3725 178 cmd[0] = ALERT;
gizmo69the2nd 0:3984961a3725 179 cmd[1] = (char) ((reg & 0xFF00) >> 8);
gizmo69the2nd 0:3984961a3725 180 cmd[2] = (char) (reg & 0x00FF);
gizmo69the2nd 0:3984961a3725 181 _i2c->write(_addr,cmd,3);
gizmo69the2nd 0:3984961a3725 182 }
gizmo69the2nd 0:3984961a3725 183
gizmo69the2nd 0:3984961a3725 184 unsigned short INA226::getAlertLimit()
gizmo69the2nd 0:3984961a3725 185 {
gizmo69the2nd 0:3984961a3725 186 char cmd;
gizmo69the2nd 0:3984961a3725 187 char buff[2];
gizmo69the2nd 0:3984961a3725 188
gizmo69the2nd 0:3984961a3725 189 cmd = ALERT;
gizmo69the2nd 0:3984961a3725 190
gizmo69the2nd 0:3984961a3725 191 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 192 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 193 return (buff[0] << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 194 }
gizmo69the2nd 0:3984961a3725 195
gizmo69the2nd 0:3984961a3725 196 unsigned short INA226::getID()
gizmo69the2nd 0:3984961a3725 197 {
gizmo69the2nd 0:3984961a3725 198 char cmd;
gizmo69the2nd 0:3984961a3725 199 char buff[2];
gizmo69the2nd 0:3984961a3725 200
gizmo69the2nd 0:3984961a3725 201 cmd = DIE_ID;
gizmo69the2nd 0:3984961a3725 202
gizmo69the2nd 0:3984961a3725 203 _i2c->write(_addr,&cmd,1,true);
gizmo69the2nd 0:3984961a3725 204 _i2c->read(_addr+1,buff,2);
gizmo69the2nd 0:3984961a3725 205 return (buff[0] << 8) | buff[1];
gizmo69the2nd 0:3984961a3725 206 }
gizmo69the2nd 0:3984961a3725 207
gizmo69the2nd 0:3984961a3725 208 void INA226::setShuntRValue(float val)
gizmo69the2nd 0:3984961a3725 209 {
gizmo69the2nd 0:3984961a3725 210 ShuntR = val;
gizmo69the2nd 0:3984961a3725 211 }
gizmo69the2nd 0:3984961a3725 212
gizmo69the2nd 0:3984961a3725 213 float INA226::getShuntRValue(void)
gizmo69the2nd 0:3984961a3725 214 {
gizmo69the2nd 0:3984961a3725 215 return ShuntR;
gizmo69the2nd 0:3984961a3725 216 }
gizmo69the2nd 0:3984961a3725 217
gizmo69the2nd 0:3984961a3725 218
gizmo69the2nd 0:3984961a3725 219 void INA226::setCurrentLSB(float val)
gizmo69the2nd 0:3984961a3725 220 {
gizmo69the2nd 0:3984961a3725 221 CURR_LSB = val;
gizmo69the2nd 0:3984961a3725 222 }
gizmo69the2nd 0:3984961a3725 223
gizmo69the2nd 0:3984961a3725 224 float INA226::getCurrentLSB(float val)
gizmo69the2nd 0:3984961a3725 225 {
gizmo69the2nd 0:3984961a3725 226 return CURR_LSB;
gizmo69the2nd 0:3984961a3725 227 }