Library to interface Lineair LTC2990 battery monitor.

Committer:
gizmo69the2nd
Date:
Wed Aug 07 19:55:21 2013 +0000
Revision:
0:8cb0dbc1b2ba
Library to interface Lineair LTC2990 battery monitor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gizmo69the2nd 0:8cb0dbc1b2ba 1 /*
gizmo69the2nd 0:8cb0dbc1b2ba 2 * LTC2990 voltage/temprature monitor library
gizmo69the2nd 0:8cb0dbc1b2ba 3 *
gizmo69the2nd 0:8cb0dbc1b2ba 4 *
gizmo69the2nd 0:8cb0dbc1b2ba 5 * Copyright (c) 2013 Davy Van Belle, MIT License
gizmo69the2nd 0:8cb0dbc1b2ba 6 *
gizmo69the2nd 0:8cb0dbc1b2ba 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
gizmo69the2nd 0:8cb0dbc1b2ba 8 * and associated documentation files (the "Software"), to deal in the Software without restriction,
gizmo69the2nd 0:8cb0dbc1b2ba 9 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
gizmo69the2nd 0:8cb0dbc1b2ba 10 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
gizmo69the2nd 0:8cb0dbc1b2ba 11 * furnished to do so, subject to the following conditions:
gizmo69the2nd 0:8cb0dbc1b2ba 12 *
gizmo69the2nd 0:8cb0dbc1b2ba 13 * The above copyright notice and this permission notice shall be included in all copies or
gizmo69the2nd 0:8cb0dbc1b2ba 14 * substantial portions of the Software.
gizmo69the2nd 0:8cb0dbc1b2ba 15 *
gizmo69the2nd 0:8cb0dbc1b2ba 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
gizmo69the2nd 0:8cb0dbc1b2ba 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
gizmo69the2nd 0:8cb0dbc1b2ba 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
gizmo69the2nd 0:8cb0dbc1b2ba 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
gizmo69the2nd 0:8cb0dbc1b2ba 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
gizmo69the2nd 0:8cb0dbc1b2ba 21 */
gizmo69the2nd 0:8cb0dbc1b2ba 22
gizmo69the2nd 0:8cb0dbc1b2ba 23 /** @file
gizmo69the2nd 0:8cb0dbc1b2ba 24 * @brief LTC29990 I2C
gizmo69the2nd 0:8cb0dbc1b2ba 25 */
gizmo69the2nd 0:8cb0dbc1b2ba 26
gizmo69the2nd 0:8cb0dbc1b2ba 27 #include "mbed.h"
gizmo69the2nd 0:8cb0dbc1b2ba 28 #include "LTC2990.h"
gizmo69the2nd 0:8cb0dbc1b2ba 29
gizmo69the2nd 0:8cb0dbc1b2ba 30 LTC2990::LTC2990 (I2C* i2c, char addr)
gizmo69the2nd 0:8cb0dbc1b2ba 31 {
gizmo69the2nd 0:8cb0dbc1b2ba 32 _i2c = i2c;
gizmo69the2nd 0:8cb0dbc1b2ba 33 _addr = addr;
gizmo69the2nd 0:8cb0dbc1b2ba 34 }
gizmo69the2nd 0:8cb0dbc1b2ba 35
gizmo69the2nd 0:8cb0dbc1b2ba 36 void LTC2990::init (char control)
gizmo69the2nd 0:8cb0dbc1b2ba 37 {
gizmo69the2nd 0:8cb0dbc1b2ba 38 char cmd[2];
gizmo69the2nd 0:8cb0dbc1b2ba 39 cmd[0] = CONTROL;
gizmo69the2nd 0:8cb0dbc1b2ba 40 cmd[1] = control;
gizmo69the2nd 0:8cb0dbc1b2ba 41 _i2c->write(_addr,cmd,2);
gizmo69the2nd 0:8cb0dbc1b2ba 42 }
gizmo69the2nd 0:8cb0dbc1b2ba 43
gizmo69the2nd 0:8cb0dbc1b2ba 44 char LTC2990::status()
gizmo69the2nd 0:8cb0dbc1b2ba 45 {
gizmo69the2nd 0:8cb0dbc1b2ba 46 char status;
gizmo69the2nd 0:8cb0dbc1b2ba 47 _i2c->write(_addr,STATUS,1,true);
gizmo69the2nd 0:8cb0dbc1b2ba 48 _i2c->read(_addr+1,&status,1);
gizmo69the2nd 0:8cb0dbc1b2ba 49 return status;
gizmo69the2nd 0:8cb0dbc1b2ba 50 }
gizmo69the2nd 0:8cb0dbc1b2ba 51
gizmo69the2nd 0:8cb0dbc1b2ba 52 void LTC2990::start()
gizmo69the2nd 0:8cb0dbc1b2ba 53 {
gizmo69the2nd 0:8cb0dbc1b2ba 54 char cmd[3];
gizmo69the2nd 0:8cb0dbc1b2ba 55 cmd[0] = TRIGGER;
gizmo69the2nd 0:8cb0dbc1b2ba 56 cmd[1] = 0x55;
gizmo69the2nd 0:8cb0dbc1b2ba 57 cmd[2] = 0xAA;
gizmo69the2nd 0:8cb0dbc1b2ba 58 _i2c->write(_addr,cmd,2);
gizmo69the2nd 0:8cb0dbc1b2ba 59 }
gizmo69the2nd 0:8cb0dbc1b2ba 60
gizmo69the2nd 0:8cb0dbc1b2ba 61
gizmo69the2nd 0:8cb0dbc1b2ba 62 float LTC2990::getTemperature(T_SOURCE source)
gizmo69the2nd 0:8cb0dbc1b2ba 63 {
gizmo69the2nd 0:8cb0dbc1b2ba 64 char cmd[2];
gizmo69the2nd 0:8cb0dbc1b2ba 65 char buff[2];
gizmo69the2nd 0:8cb0dbc1b2ba 66
gizmo69the2nd 0:8cb0dbc1b2ba 67
gizmo69the2nd 0:8cb0dbc1b2ba 68 switch (source)
gizmo69the2nd 0:8cb0dbc1b2ba 69 {
gizmo69the2nd 0:8cb0dbc1b2ba 70 case INT: cmd[0] = TINT_MSB; break;
gizmo69the2nd 0:8cb0dbc1b2ba 71 case TR1: cmd[0] = V1_MSB; break;
gizmo69the2nd 0:8cb0dbc1b2ba 72 case TR2: cmd[0] = V3_MSB; break;
gizmo69the2nd 0:8cb0dbc1b2ba 73 default: return 999.8;
gizmo69the2nd 0:8cb0dbc1b2ba 74 }
gizmo69the2nd 0:8cb0dbc1b2ba 75
gizmo69the2nd 0:8cb0dbc1b2ba 76 cmd[1] = cmd[0]+1;
gizmo69the2nd 0:8cb0dbc1b2ba 77
gizmo69the2nd 0:8cb0dbc1b2ba 78 bool sign;
gizmo69the2nd 0:8cb0dbc1b2ba 79 short tmp;
gizmo69the2nd 0:8cb0dbc1b2ba 80 //MSB
gizmo69the2nd 0:8cb0dbc1b2ba 81 _i2c->write(_addr,&cmd[0],1,true);
gizmo69the2nd 0:8cb0dbc1b2ba 82 _i2c->read(_addr+1,&buff[0],1);
gizmo69the2nd 0:8cb0dbc1b2ba 83 //LSB
gizmo69the2nd 0:8cb0dbc1b2ba 84 _i2c->write(_addr,&cmd[1],1,true);
gizmo69the2nd 0:8cb0dbc1b2ba 85 _i2c->read(_addr+1,&buff[1],1);
gizmo69the2nd 0:8cb0dbc1b2ba 86 sign = buff[0] & 0x10;
gizmo69the2nd 0:8cb0dbc1b2ba 87 tmp = ((buff[0] & 0x0F) << 8) | buff[1];
gizmo69the2nd 0:8cb0dbc1b2ba 88 if(!sign) return (float)tmp/16; //positive temp calulation.
gizmo69the2nd 0:8cb0dbc1b2ba 89 else return ((float)tmp-4096)/16; //negative temp calculation.
gizmo69the2nd 0:8cb0dbc1b2ba 90
gizmo69the2nd 0:8cb0dbc1b2ba 91 }
gizmo69the2nd 0:8cb0dbc1b2ba 92
gizmo69the2nd 0:8cb0dbc1b2ba 93
gizmo69the2nd 0:8cb0dbc1b2ba 94 float LTC2990::getVoltage(V_SOURCE source)
gizmo69the2nd 0:8cb0dbc1b2ba 95 {
gizmo69the2nd 0:8cb0dbc1b2ba 96 char cmd[2];
gizmo69the2nd 0:8cb0dbc1b2ba 97 char buff[2];
gizmo69the2nd 0:8cb0dbc1b2ba 98 float factor;
gizmo69the2nd 0:8cb0dbc1b2ba 99
gizmo69the2nd 0:8cb0dbc1b2ba 100 switch (source)
gizmo69the2nd 0:8cb0dbc1b2ba 101 {
gizmo69the2nd 0:8cb0dbc1b2ba 102 case V1: cmd[0] = V1_MSB; factor = SINGLE; break;
gizmo69the2nd 0:8cb0dbc1b2ba 103 case V2: cmd[0] = V2_MSB; factor = SINGLE; break;
gizmo69the2nd 0:8cb0dbc1b2ba 104 case V3: cmd[0] = V3_MSB; factor = SINGLE; break;
gizmo69the2nd 0:8cb0dbc1b2ba 105 case V4: cmd[0] = V4_MSB; factor = SINGLE; break;
gizmo69the2nd 0:8cb0dbc1b2ba 106 case VCC: cmd[0] = VCC_MSB; factor = SINGLE; break;
gizmo69the2nd 0:8cb0dbc1b2ba 107 case V1_V2: cmd[0] = V1_MSB; factor = DIFF; break;
gizmo69the2nd 0:8cb0dbc1b2ba 108 case V3_V4: cmd[0] = V2_MSB; factor = DIFF; break;
gizmo69the2nd 0:8cb0dbc1b2ba 109 default: return 9999;
gizmo69the2nd 0:8cb0dbc1b2ba 110 }
gizmo69the2nd 0:8cb0dbc1b2ba 111
gizmo69the2nd 0:8cb0dbc1b2ba 112 cmd[1] = cmd[0]+1;
gizmo69the2nd 0:8cb0dbc1b2ba 113 bool sign;
gizmo69the2nd 0:8cb0dbc1b2ba 114 short tmp;
gizmo69the2nd 0:8cb0dbc1b2ba 115 //MSB
gizmo69the2nd 0:8cb0dbc1b2ba 116 _i2c->write(_addr,&cmd[0],1,true);
gizmo69the2nd 0:8cb0dbc1b2ba 117 _i2c->read(_addr+1,&buff[0],1);
gizmo69the2nd 0:8cb0dbc1b2ba 118 //LSB
gizmo69the2nd 0:8cb0dbc1b2ba 119 _i2c->write(_addr,&cmd[1],1,true);
gizmo69the2nd 0:8cb0dbc1b2ba 120 _i2c->read(_addr+1,&buff[1],1);
gizmo69the2nd 0:8cb0dbc1b2ba 121 sign = buff[0] & 0x40;
gizmo69the2nd 0:8cb0dbc1b2ba 122 tmp = ((buff[0] & 0x3F) << 8) | buff[1];
gizmo69the2nd 0:8cb0dbc1b2ba 123 if (source != VCC)
gizmo69the2nd 0:8cb0dbc1b2ba 124 {
gizmo69the2nd 0:8cb0dbc1b2ba 125 if (!sign) return (float)tmp * factor;
gizmo69the2nd 0:8cb0dbc1b2ba 126 else return ((float)tmp - 16384) * factor;
gizmo69the2nd 0:8cb0dbc1b2ba 127 }
gizmo69the2nd 0:8cb0dbc1b2ba 128 else
gizmo69the2nd 0:8cb0dbc1b2ba 129 {
gizmo69the2nd 0:8cb0dbc1b2ba 130 return ((float)tmp * factor) + 2.5;
gizmo69the2nd 0:8cb0dbc1b2ba 131 }
gizmo69the2nd 0:8cb0dbc1b2ba 132 }
gizmo69the2nd 0:8cb0dbc1b2ba 133
gizmo69the2nd 0:8cb0dbc1b2ba 134 bool LTC2990::isBusy()
gizmo69the2nd 0:8cb0dbc1b2ba 135 {
gizmo69the2nd 0:8cb0dbc1b2ba 136 return (status() & 0x01);
gizmo69the2nd 0:8cb0dbc1b2ba 137 }
gizmo69the2nd 0:8cb0dbc1b2ba 138
gizmo69the2nd 0:8cb0dbc1b2ba 139 bool LTC2990::isReady(V_SOURCE source)
gizmo69the2nd 0:8cb0dbc1b2ba 140 {
gizmo69the2nd 0:8cb0dbc1b2ba 141 switch (source)
gizmo69the2nd 0:8cb0dbc1b2ba 142 {
gizmo69the2nd 0:8cb0dbc1b2ba 143 case V1: return (status() & 0x04);
gizmo69the2nd 0:8cb0dbc1b2ba 144 case V2: return (status() & 0x08);
gizmo69the2nd 0:8cb0dbc1b2ba 145 case V3: return (status() & 0x10);
gizmo69the2nd 0:8cb0dbc1b2ba 146 case V4: return (status() & 0x20);
gizmo69the2nd 0:8cb0dbc1b2ba 147 case VCC: return (status() & 0x40);
gizmo69the2nd 0:8cb0dbc1b2ba 148 case V1_V2: return (status() & 0x04);
gizmo69the2nd 0:8cb0dbc1b2ba 149 case V3_V4: return (status() & 0x10);
gizmo69the2nd 0:8cb0dbc1b2ba 150 default: return 0;
gizmo69the2nd 0:8cb0dbc1b2ba 151 }
gizmo69the2nd 0:8cb0dbc1b2ba 152 }
gizmo69the2nd 0:8cb0dbc1b2ba 153
gizmo69the2nd 0:8cb0dbc1b2ba 154
gizmo69the2nd 0:8cb0dbc1b2ba 155 bool LTC2990::isReady(T_SOURCE source)
gizmo69the2nd 0:8cb0dbc1b2ba 156 {
gizmo69the2nd 0:8cb0dbc1b2ba 157 switch (source)
gizmo69the2nd 0:8cb0dbc1b2ba 158 {
gizmo69the2nd 0:8cb0dbc1b2ba 159 case INT: return (status() & 0x02);
gizmo69the2nd 0:8cb0dbc1b2ba 160 case TR1: return (status() & 0x04);
gizmo69the2nd 0:8cb0dbc1b2ba 161 case TR2: return (status() & 0x010);
gizmo69the2nd 0:8cb0dbc1b2ba 162 default: return 0;
gizmo69the2nd 0:8cb0dbc1b2ba 163 }
gizmo69the2nd 0:8cb0dbc1b2ba 164 }