Library to interface Lineair LTC2990 battery monitor.
Revision 0:8cb0dbc1b2ba, committed 2013-08-07
- Comitter:
- gizmo69the2nd
- Date:
- Wed Aug 07 19:55:21 2013 +0000
- Commit message:
- Library to interface Lineair LTC2990 battery monitor.
Changed in this revision
LTC2990.cpp | Show annotated file Show diff for this revision Revisions of this file |
LTC2990.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 8cb0dbc1b2ba LTC2990.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LTC2990.cpp Wed Aug 07 19:55:21 2013 +0000 @@ -0,0 +1,164 @@ +/* + * LTC2990 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 "LTC2990.h" + +LTC2990::LTC2990 (I2C* i2c, char addr) +{ + _i2c = i2c; + _addr = addr; +} + +void LTC2990::init (char control) +{ + char cmd[2]; + cmd[0] = CONTROL; + cmd[1] = control; + _i2c->write(_addr,cmd,2); +} + +char LTC2990::status() +{ + char status; + _i2c->write(_addr,STATUS,1,true); + _i2c->read(_addr+1,&status,1); + return status; +} + +void LTC2990::start() +{ + char cmd[3]; + cmd[0] = TRIGGER; + cmd[1] = 0x55; + cmd[2] = 0xAA; + _i2c->write(_addr,cmd,2); +} + + +float LTC2990::getTemperature(T_SOURCE source) +{ + char cmd[2]; + char buff[2]; + + + switch (source) + { + case INT: cmd[0] = TINT_MSB; break; + case TR1: cmd[0] = V1_MSB; break; + case TR2: cmd[0] = V3_MSB; break; + default: return 999.8; + } + + cmd[1] = cmd[0]+1; + + bool sign; + short tmp; + //MSB + _i2c->write(_addr,&cmd[0],1,true); + _i2c->read(_addr+1,&buff[0],1); + //LSB + _i2c->write(_addr,&cmd[1],1,true); + _i2c->read(_addr+1,&buff[1],1); + sign = buff[0] & 0x10; + tmp = ((buff[0] & 0x0F) << 8) | buff[1]; + if(!sign) return (float)tmp/16; //positive temp calulation. + else return ((float)tmp-4096)/16; //negative temp calculation. + +} + + +float LTC2990::getVoltage(V_SOURCE source) +{ + char cmd[2]; + char buff[2]; + float factor; + + switch (source) + { + case V1: cmd[0] = V1_MSB; factor = SINGLE; break; + case V2: cmd[0] = V2_MSB; factor = SINGLE; break; + case V3: cmd[0] = V3_MSB; factor = SINGLE; break; + case V4: cmd[0] = V4_MSB; factor = SINGLE; break; + case VCC: cmd[0] = VCC_MSB; factor = SINGLE; break; + case V1_V2: cmd[0] = V1_MSB; factor = DIFF; break; + case V3_V4: cmd[0] = V2_MSB; factor = DIFF; break; + default: return 9999; + } + + cmd[1] = cmd[0]+1; + bool sign; + short tmp; + //MSB + _i2c->write(_addr,&cmd[0],1,true); + _i2c->read(_addr+1,&buff[0],1); + //LSB + _i2c->write(_addr,&cmd[1],1,true); + _i2c->read(_addr+1,&buff[1],1); + sign = buff[0] & 0x40; + tmp = ((buff[0] & 0x3F) << 8) | buff[1]; + if (source != VCC) + { + if (!sign) return (float)tmp * factor; + else return ((float)tmp - 16384) * factor; + } + else + { + return ((float)tmp * factor) + 2.5; + } +} + +bool LTC2990::isBusy() +{ + return (status() & 0x01); +} + +bool LTC2990::isReady(V_SOURCE source) +{ + switch (source) + { + case V1: return (status() & 0x04); + case V2: return (status() & 0x08); + case V3: return (status() & 0x10); + case V4: return (status() & 0x20); + case VCC: return (status() & 0x40); + case V1_V2: return (status() & 0x04); + case V3_V4: return (status() & 0x10); + default: return 0; + } +} + + +bool LTC2990::isReady(T_SOURCE source) +{ + switch (source) + { + case INT: return (status() & 0x02); + case TR1: return (status() & 0x04); + case TR2: return (status() & 0x010); + default: return 0; + } +}
diff -r 000000000000 -r 8cb0dbc1b2ba LTC2990.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LTC2990.h Wed Aug 07 19:55:21 2013 +0000 @@ -0,0 +1,138 @@ +/* + * LTC2990 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 + */ + +#ifndef LTC2990_H +#define LTC2990_H + +#include "mbed.h" + +#define STATUS 0x00 +#define CONTROL 0x01 +#define TRIGGER 0x02 + +#define TINT_MSB 0x04 +#define TINT_LSB 0x05 + +#define V1_MSB 0x06 +#define V1_LSB 0x07 + +#define V2_MSB 0x08 +#define V2_LSB 0x09 + +#define V3_MSB 0x0A +#define V3_LSB 0x0B + +#define V4_MSB 0x0C +#define V4_LSB 0x0D + +#define VCC_MSB 0x0E +#define VCC_LSB 0x0F + +#define SINGLE 0.00030518 //LSB factor for single ended measurments (305.18µV) +#define DIFF 0.00001942 //LSB factor for differential measurments (19.42µV) + + +enum T_SOURCE { + INT, + TR1, + TR2, +}; + +enum V_SOURCE { + V1, + V2, + V3, + V4, + VCC, + V1_V2, + V3_V4, +}; + + +/** LTC2990 class + */ +class LTC2990 { +public: + /** init LTC2990 class + * @param *i2c pointer to I2C serial interface + * @param addr sensor I2C address + */ + LTC2990 (I2C* i2c, char addr); + + /** Initiate the device + * @param control desired control register bits + */ + void init (char control); + + /** Get device STATUS register + */ + char status(); + + /** Start a conversion + */ + void start(); + + /** Get temperature in °C or °K + * @param source define which temperature to measure (INT, TR1, TR2) + */ + float getTemperature(T_SOURCE source); + + /** Get voltage + * @param source define which voltage te measure (V1, V2, V3, V4, VCC, V1-V2, V3-V4) + */ + float getVoltage(V_SOURCE source); + + /** + *@param *ack pointer to return succesfull acknowledgement + *@return True: Converion in progress + *@return False: Acquisition Cycle Complete + */ + bool isBusy(); + + /** + *@return True: Register contains new data + *@return False: Register contains old data + *@param source define which register to check (V1, V2, V3, V4, VCC, V1-V2, V3-V4) + */ + bool isReady(V_SOURCE source); + + /** + $@return True: Register contains new data + *@return False: Register contains old data + *@param source define which register to check (INT, TR1, TR2) + */ + bool isReady(T_SOURCE source); + +protected: + + +private: + char _addr; + I2C *_i2c; + +}; + +#endif \ No newline at end of file