A feature complete driver for the MAX17048 lithium fuel gauge from Maxim.

Dependents:   MAX17048_HelloWorld ECGAFE_copy MAX17048_HelloWorld Orion_newPCB_test_LV ... more

Now fully tested!

Committer:
neilt6
Date:
Fri May 30 19:02:51 2014 +0000
Revision:
10:4f695111eb54
Parent:
9:2c1d82ecd63c
Added MBED_OPERATORS check to implementation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilt6 0:abc480f8eeab 1 /* MAX17048 Driver Library
neilt6 0:abc480f8eeab 2 * Copyright (c) 2013 Neil Thiessen
neilt6 0:abc480f8eeab 3 *
neilt6 0:abc480f8eeab 4 * Licensed under the Apache License, Version 2.0 (the "License");
neilt6 0:abc480f8eeab 5 * you may not use this file except in compliance with the License.
neilt6 0:abc480f8eeab 6 * You may obtain a copy of the License at
neilt6 0:abc480f8eeab 7 *
neilt6 0:abc480f8eeab 8 * http://www.apache.org/licenses/LICENSE-2.0
neilt6 0:abc480f8eeab 9 *
neilt6 0:abc480f8eeab 10 * Unless required by applicable law or agreed to in writing, software
neilt6 0:abc480f8eeab 11 * distributed under the License is distributed on an "AS IS" BASIS,
neilt6 0:abc480f8eeab 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
neilt6 0:abc480f8eeab 13 * See the License for the specific language governing permissions and
neilt6 0:abc480f8eeab 14 * limitations under the License.
neilt6 0:abc480f8eeab 15 */
neilt6 0:abc480f8eeab 16
neilt6 0:abc480f8eeab 17 #include "MAX17048.h"
neilt6 0:abc480f8eeab 18
neilt6 8:65c889800b3a 19 const char MAX17048::RCOMP0 = 0x97;
neilt6 8:65c889800b3a 20 const int MAX17048::m_ADDR = (0x36 << 1);
neilt6 8:65c889800b3a 21
neilt6 9:2c1d82ecd63c 22 MAX17048::MAX17048(PinName sda, PinName scl, int hz) : m_I2C(sda, scl)
neilt6 0:abc480f8eeab 23 {
neilt6 9:2c1d82ecd63c 24 //Set the I2C bus frequency
neilt6 9:2c1d82ecd63c 25 m_I2C.frequency(hz);
neilt6 0:abc480f8eeab 26 }
neilt6 0:abc480f8eeab 27
neilt6 5:ffce4fe12ed1 28 bool MAX17048::open()
neilt6 3:32087cca331f 29 {
neilt6 3:32087cca331f 30 //Probe for the MAX17048 using a Zero Length Transfer
neilt6 3:32087cca331f 31 if (!m_I2C.write(m_ADDR, NULL, 0)) {
neilt6 3:32087cca331f 32 //Return success
neilt6 3:32087cca331f 33 return true;
neilt6 3:32087cca331f 34 } else {
neilt6 3:32087cca331f 35 //Return failure
neilt6 3:32087cca331f 36 return false;
neilt6 3:32087cca331f 37 }
neilt6 3:32087cca331f 38 }
neilt6 3:32087cca331f 39
neilt6 5:ffce4fe12ed1 40 void MAX17048::reset()
neilt6 0:abc480f8eeab 41 {
neilt6 0:abc480f8eeab 42 //Write the POR command
neilt6 2:0a98e081b48c 43 write(REG_CMD, 0x5400);
neilt6 0:abc480f8eeab 44 }
neilt6 0:abc480f8eeab 45
neilt6 5:ffce4fe12ed1 46 void MAX17048::quickStart()
neilt6 0:abc480f8eeab 47 {
neilt6 0:abc480f8eeab 48 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 49 unsigned short value = read(REG_MODE);
neilt6 0:abc480f8eeab 50
neilt6 0:abc480f8eeab 51 //Set the QuickStart bit
neilt6 0:abc480f8eeab 52 value |= (1 << 14);
neilt6 0:abc480f8eeab 53
neilt6 0:abc480f8eeab 54 //Write the value back out
neilt6 2:0a98e081b48c 55 write(REG_MODE, value);
neilt6 0:abc480f8eeab 56 }
neilt6 0:abc480f8eeab 57
neilt6 5:ffce4fe12ed1 58 bool MAX17048::sleepEnabled()
neilt6 0:abc480f8eeab 59 {
neilt6 0:abc480f8eeab 60 //Read the 16-bit register value
neilt6 2:0a98e081b48c 61 unsigned short value = read(REG_MODE);
neilt6 0:abc480f8eeab 62
neilt6 0:abc480f8eeab 63 //Return the status of the EnSleep bit
neilt6 0:abc480f8eeab 64 if (value & (1 << 13))
neilt6 0:abc480f8eeab 65 return true;
neilt6 0:abc480f8eeab 66 else
neilt6 0:abc480f8eeab 67 return false;
neilt6 0:abc480f8eeab 68 }
neilt6 0:abc480f8eeab 69
neilt6 2:0a98e081b48c 70 void MAX17048::sleepEnabled(bool enabled)
neilt6 0:abc480f8eeab 71 {
neilt6 0:abc480f8eeab 72 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 73 unsigned short value = read(REG_MODE);
neilt6 0:abc480f8eeab 74
neilt6 0:abc480f8eeab 75 //Set or clear the EnSleep bit
neilt6 0:abc480f8eeab 76 if (enabled)
neilt6 0:abc480f8eeab 77 value |= (1 << 13);
neilt6 0:abc480f8eeab 78 else
neilt6 0:abc480f8eeab 79 value &= ~(1 << 13);
neilt6 0:abc480f8eeab 80
neilt6 0:abc480f8eeab 81 //Write the value back out
neilt6 2:0a98e081b48c 82 write(REG_MODE, value);
neilt6 0:abc480f8eeab 83 }
neilt6 0:abc480f8eeab 84
neilt6 5:ffce4fe12ed1 85 bool MAX17048::hibernating()
neilt6 0:abc480f8eeab 86 {
neilt6 0:abc480f8eeab 87 //Read the 16-bit register value
neilt6 2:0a98e081b48c 88 unsigned short value = read(REG_MODE);
neilt6 0:abc480f8eeab 89
neilt6 0:abc480f8eeab 90 //Return the status of the HibStat bit
neilt6 0:abc480f8eeab 91 if (value & (1 << 12))
neilt6 0:abc480f8eeab 92 return true;
neilt6 0:abc480f8eeab 93 else
neilt6 0:abc480f8eeab 94 return false;
neilt6 0:abc480f8eeab 95 }
neilt6 0:abc480f8eeab 96
neilt6 5:ffce4fe12ed1 97 float MAX17048::hibernateThreshold()
neilt6 0:abc480f8eeab 98 {
neilt6 0:abc480f8eeab 99 //Read the 16-bit register value
neilt6 2:0a98e081b48c 100 unsigned short value = read(REG_HIBRT);
neilt6 0:abc480f8eeab 101
neilt6 0:abc480f8eeab 102 //Extract the hibernate threshold
neilt6 0:abc480f8eeab 103 return (value >> 8) * 0.208;
neilt6 0:abc480f8eeab 104 }
neilt6 0:abc480f8eeab 105
neilt6 2:0a98e081b48c 106 void MAX17048::hibernateThreshold(float threshold)
neilt6 0:abc480f8eeab 107 {
neilt6 0:abc480f8eeab 108 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 109 unsigned short value = read(REG_HIBRT);
neilt6 0:abc480f8eeab 110
neilt6 0:abc480f8eeab 111 //Mask off the old value
neilt6 0:abc480f8eeab 112 value &= 0x00FF;
neilt6 0:abc480f8eeab 113
neilt6 0:abc480f8eeab 114 //Do a smart update
neilt6 0:abc480f8eeab 115 if (threshold > 0.0) {
neilt6 0:abc480f8eeab 116 if (threshold < 53.04)
neilt6 0:abc480f8eeab 117 value |= (unsigned short)(threshold / 0.208) << 8;
neilt6 0:abc480f8eeab 118 else
neilt6 0:abc480f8eeab 119 value |= 0xFF00;
neilt6 0:abc480f8eeab 120 }
neilt6 0:abc480f8eeab 121
neilt6 0:abc480f8eeab 122 //Write the 16-bit register
neilt6 2:0a98e081b48c 123 write(REG_HIBRT, value);
neilt6 0:abc480f8eeab 124 }
neilt6 0:abc480f8eeab 125
neilt6 5:ffce4fe12ed1 126 float MAX17048::activeThreshold()
neilt6 0:abc480f8eeab 127 {
neilt6 0:abc480f8eeab 128 //Read the 16-bit register value
neilt6 2:0a98e081b48c 129 unsigned short value = read(REG_HIBRT);
neilt6 0:abc480f8eeab 130
neilt6 0:abc480f8eeab 131 //Extract the active threshold
neilt6 0:abc480f8eeab 132 return (value & 0x00FF) * 0.00125;
neilt6 0:abc480f8eeab 133 }
neilt6 0:abc480f8eeab 134
neilt6 2:0a98e081b48c 135 void MAX17048::activeThreshold(float threshold)
neilt6 0:abc480f8eeab 136 {
neilt6 0:abc480f8eeab 137 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 138 unsigned short value = read(REG_HIBRT);
neilt6 0:abc480f8eeab 139
neilt6 0:abc480f8eeab 140 //Mask off the old value
neilt6 0:abc480f8eeab 141 value &= 0xFF00;
neilt6 0:abc480f8eeab 142
neilt6 0:abc480f8eeab 143 //Do a smart update
neilt6 0:abc480f8eeab 144 if (threshold > 0.0) {
neilt6 0:abc480f8eeab 145 if (threshold < 0.31875)
neilt6 0:abc480f8eeab 146 value |= (char)(threshold / 0.00125);
neilt6 0:abc480f8eeab 147 else
neilt6 0:abc480f8eeab 148 value |= 0x00FF;
neilt6 0:abc480f8eeab 149 }
neilt6 0:abc480f8eeab 150
neilt6 0:abc480f8eeab 151 //Write the 16-bit register
neilt6 2:0a98e081b48c 152 write(REG_HIBRT, value);
neilt6 0:abc480f8eeab 153 }
neilt6 0:abc480f8eeab 154
neilt6 5:ffce4fe12ed1 155 unsigned short MAX17048::version()
neilt6 0:abc480f8eeab 156 {
neilt6 0:abc480f8eeab 157 //Return the 16-bit production version
neilt6 2:0a98e081b48c 158 return read(REG_VERSION);
neilt6 0:abc480f8eeab 159 }
neilt6 0:abc480f8eeab 160
neilt6 7:bf6972a21c61 161 char MAX17048::compensation()
neilt6 7:bf6972a21c61 162 {
neilt6 7:bf6972a21c61 163 //Read the 16-bit register value
neilt6 7:bf6972a21c61 164 unsigned short value = read(REG_CONFIG);
neilt6 7:bf6972a21c61 165
neilt6 7:bf6972a21c61 166 //Return only the upper byte
neilt6 7:bf6972a21c61 167 return (char)(value >> 8);
neilt6 7:bf6972a21c61 168 }
neilt6 7:bf6972a21c61 169
neilt6 7:bf6972a21c61 170 void MAX17048::compensation(char rcomp)
neilt6 7:bf6972a21c61 171 {
neilt6 7:bf6972a21c61 172 //Read the current 16-bit register value
neilt6 7:bf6972a21c61 173 unsigned short value = read(REG_CONFIG);
neilt6 7:bf6972a21c61 174
neilt6 7:bf6972a21c61 175 //Update the register value
neilt6 7:bf6972a21c61 176 value &= 0x00FF;
neilt6 7:bf6972a21c61 177 value |= rcomp << 8;
neilt6 7:bf6972a21c61 178
neilt6 7:bf6972a21c61 179 //Write the value back out
neilt6 7:bf6972a21c61 180 write(REG_CONFIG, value);
neilt6 7:bf6972a21c61 181 }
neilt6 7:bf6972a21c61 182
neilt6 2:0a98e081b48c 183 void MAX17048::tempCompensation(float temp)
neilt6 0:abc480f8eeab 184 {
neilt6 0:abc480f8eeab 185 //Calculate the new RCOMP value
neilt6 0:abc480f8eeab 186 char rcomp;
neilt6 0:abc480f8eeab 187 if (temp > 20.0) {
neilt6 7:bf6972a21c61 188 rcomp = RCOMP0 + (temp - 20.0) * -0.5;
neilt6 0:abc480f8eeab 189 } else {
neilt6 7:bf6972a21c61 190 rcomp = RCOMP0 + (temp - 20.0) * -5.0;
neilt6 0:abc480f8eeab 191 }
neilt6 0:abc480f8eeab 192
neilt6 0:abc480f8eeab 193 //Update the RCOMP value
neilt6 7:bf6972a21c61 194 compensation(rcomp);
neilt6 0:abc480f8eeab 195 }
neilt6 0:abc480f8eeab 196
neilt6 5:ffce4fe12ed1 197 bool MAX17048::sleeping()
neilt6 0:abc480f8eeab 198 {
neilt6 0:abc480f8eeab 199 //Read the 16-bit register value
neilt6 2:0a98e081b48c 200 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 201
neilt6 0:abc480f8eeab 202 //Return the status of the SLEEP bit
neilt6 0:abc480f8eeab 203 if (value & (1 << 7))
neilt6 0:abc480f8eeab 204 return true;
neilt6 0:abc480f8eeab 205 else
neilt6 0:abc480f8eeab 206 return false;
neilt6 0:abc480f8eeab 207 }
neilt6 0:abc480f8eeab 208
neilt6 2:0a98e081b48c 209 void MAX17048::sleep(bool sleep)
neilt6 0:abc480f8eeab 210 {
neilt6 0:abc480f8eeab 211 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 212 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 213
neilt6 0:abc480f8eeab 214 //Set or clear the SLEEP bit
neilt6 0:abc480f8eeab 215 if (sleep)
neilt6 0:abc480f8eeab 216 value |= (1 << 7);
neilt6 0:abc480f8eeab 217 else
neilt6 0:abc480f8eeab 218 value &= ~(1 << 7);
neilt6 0:abc480f8eeab 219
neilt6 0:abc480f8eeab 220 //Write the value back out
neilt6 2:0a98e081b48c 221 write(REG_CONFIG, value);
neilt6 0:abc480f8eeab 222 }
neilt6 0:abc480f8eeab 223
neilt6 5:ffce4fe12ed1 224 bool MAX17048::socChangeAlertEnabled()
neilt6 0:abc480f8eeab 225 {
neilt6 0:abc480f8eeab 226 //Read the 16-bit register value
neilt6 2:0a98e081b48c 227 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 228
neilt6 0:abc480f8eeab 229 //Return the status of the ALSC bit
neilt6 0:abc480f8eeab 230 if (value & (1 << 6))
neilt6 0:abc480f8eeab 231 return true;
neilt6 0:abc480f8eeab 232 else
neilt6 0:abc480f8eeab 233 return false;
neilt6 0:abc480f8eeab 234 }
neilt6 0:abc480f8eeab 235
neilt6 2:0a98e081b48c 236 void MAX17048::socChangeAlertEnabled(bool enabled)
neilt6 0:abc480f8eeab 237 {
neilt6 0:abc480f8eeab 238 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 239 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 240
neilt6 0:abc480f8eeab 241 //Set or clear the ALSC bit
neilt6 0:abc480f8eeab 242 if (enabled)
neilt6 0:abc480f8eeab 243 value |= (1 << 6);
neilt6 0:abc480f8eeab 244 else
neilt6 0:abc480f8eeab 245 value &= ~(1 << 6);
neilt6 0:abc480f8eeab 246
neilt6 0:abc480f8eeab 247 //Write the value back out
neilt6 2:0a98e081b48c 248 write(REG_CONFIG, value);
neilt6 0:abc480f8eeab 249 }
neilt6 0:abc480f8eeab 250
neilt6 5:ffce4fe12ed1 251 bool MAX17048::alerting()
neilt6 0:abc480f8eeab 252 {
neilt6 0:abc480f8eeab 253 //Read the 16-bit register value
neilt6 2:0a98e081b48c 254 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 255
neilt6 0:abc480f8eeab 256 //Return the status of the ALRT bit
neilt6 0:abc480f8eeab 257 if (value & (1 << 5))
neilt6 0:abc480f8eeab 258 return true;
neilt6 0:abc480f8eeab 259 else
neilt6 0:abc480f8eeab 260 return false;
neilt6 0:abc480f8eeab 261 }
neilt6 0:abc480f8eeab 262
neilt6 5:ffce4fe12ed1 263 void MAX17048::clearAlert()
neilt6 0:abc480f8eeab 264 {
neilt6 0:abc480f8eeab 265 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 266 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 267
neilt6 0:abc480f8eeab 268 //Clear the ALRT bit
neilt6 0:abc480f8eeab 269 value &= ~(1 << 5);
neilt6 0:abc480f8eeab 270
neilt6 0:abc480f8eeab 271 //Write the value back out
neilt6 2:0a98e081b48c 272 write(REG_CONFIG, value);
neilt6 0:abc480f8eeab 273 }
neilt6 0:abc480f8eeab 274
neilt6 5:ffce4fe12ed1 275 char MAX17048::emptyAlertThreshold()
neilt6 0:abc480f8eeab 276 {
neilt6 0:abc480f8eeab 277 //Read the 16-bit register value
neilt6 2:0a98e081b48c 278 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 279
neilt6 0:abc480f8eeab 280 //Extract the threshold
neilt6 0:abc480f8eeab 281 return 32 - (value & 0x001F);
neilt6 0:abc480f8eeab 282 }
neilt6 0:abc480f8eeab 283
neilt6 2:0a98e081b48c 284 void MAX17048::emptyAlertThreshold(char threshold)
neilt6 0:abc480f8eeab 285 {
neilt6 0:abc480f8eeab 286 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 287 unsigned short value = read(REG_CONFIG);
neilt6 0:abc480f8eeab 288
neilt6 0:abc480f8eeab 289 //Range check threshold
neilt6 0:abc480f8eeab 290 if (threshold < 1)
neilt6 0:abc480f8eeab 291 threshold = 1;
neilt6 0:abc480f8eeab 292 else if (threshold > 32)
neilt6 0:abc480f8eeab 293 threshold = 32;
neilt6 0:abc480f8eeab 294
neilt6 0:abc480f8eeab 295 //Update the register value
neilt6 0:abc480f8eeab 296 value &= 0xFFE0;
neilt6 0:abc480f8eeab 297 value |= 32 - threshold;
neilt6 0:abc480f8eeab 298
neilt6 0:abc480f8eeab 299 //Write the 16-bit register
neilt6 2:0a98e081b48c 300 write(REG_CONFIG, value);
neilt6 0:abc480f8eeab 301 }
neilt6 0:abc480f8eeab 302
neilt6 5:ffce4fe12ed1 303 float MAX17048::vAlertMinThreshold()
neilt6 0:abc480f8eeab 304 {
neilt6 0:abc480f8eeab 305 //Read the 16-bit register value
neilt6 2:0a98e081b48c 306 unsigned short value = read(REG_VALRT);
neilt6 0:abc480f8eeab 307
neilt6 0:abc480f8eeab 308 //Extract the alert threshold
neilt6 0:abc480f8eeab 309 return (value >> 8) * 0.02;
neilt6 0:abc480f8eeab 310 }
neilt6 0:abc480f8eeab 311
neilt6 2:0a98e081b48c 312 void MAX17048::vAlertMinThreshold(float threshold)
neilt6 0:abc480f8eeab 313 {
neilt6 0:abc480f8eeab 314 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 315 unsigned short value = read(REG_VALRT);
neilt6 0:abc480f8eeab 316
neilt6 0:abc480f8eeab 317 //Mask off the old value
neilt6 0:abc480f8eeab 318 value &= 0x00FF;
neilt6 0:abc480f8eeab 319
neilt6 0:abc480f8eeab 320 //Do a smart update
neilt6 0:abc480f8eeab 321 if (threshold > 0.0) {
neilt6 0:abc480f8eeab 322 if (threshold < 5.1)
neilt6 0:abc480f8eeab 323 value |= (unsigned short)(threshold / 0.02) << 8;
neilt6 0:abc480f8eeab 324 else
neilt6 0:abc480f8eeab 325 value |= 0xFF00;
neilt6 0:abc480f8eeab 326 }
neilt6 0:abc480f8eeab 327
neilt6 0:abc480f8eeab 328 //Write the 16-bit register
neilt6 2:0a98e081b48c 329 write(REG_VALRT, value);
neilt6 0:abc480f8eeab 330 }
neilt6 0:abc480f8eeab 331
neilt6 5:ffce4fe12ed1 332 float MAX17048::vAlertMaxThreshold()
neilt6 0:abc480f8eeab 333 {
neilt6 0:abc480f8eeab 334 //Read the 16-bit register value
neilt6 2:0a98e081b48c 335 unsigned short value = read(REG_VALRT);
neilt6 0:abc480f8eeab 336
neilt6 0:abc480f8eeab 337 //Extract the active threshold
neilt6 0:abc480f8eeab 338 return (value & 0x00FF) * 0.02;
neilt6 0:abc480f8eeab 339 }
neilt6 0:abc480f8eeab 340
neilt6 2:0a98e081b48c 341 void MAX17048::vAlertMaxThreshold(float threshold)
neilt6 0:abc480f8eeab 342 {
neilt6 0:abc480f8eeab 343 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 344 unsigned short value = read(REG_VALRT);
neilt6 0:abc480f8eeab 345
neilt6 0:abc480f8eeab 346 //Mask off the old value
neilt6 0:abc480f8eeab 347 value &= 0xFF00;
neilt6 0:abc480f8eeab 348
neilt6 0:abc480f8eeab 349 //Do a smart update
neilt6 0:abc480f8eeab 350 if (threshold > 0.0) {
neilt6 0:abc480f8eeab 351 if (threshold < 5.1)
neilt6 0:abc480f8eeab 352 value |= (char)(threshold / 0.02);
neilt6 0:abc480f8eeab 353 else
neilt6 0:abc480f8eeab 354 value |= 0x00FF;
neilt6 0:abc480f8eeab 355 }
neilt6 0:abc480f8eeab 356
neilt6 0:abc480f8eeab 357 //Write the 16-bit register
neilt6 2:0a98e081b48c 358 write(REG_VALRT, value);
neilt6 0:abc480f8eeab 359 }
neilt6 0:abc480f8eeab 360
neilt6 5:ffce4fe12ed1 361 float MAX17048::vResetThreshold()
neilt6 0:abc480f8eeab 362 {
neilt6 0:abc480f8eeab 363 //Read the 16-bit register value
neilt6 2:0a98e081b48c 364 unsigned short value = read(REG_VRESET_ID);
neilt6 0:abc480f8eeab 365
neilt6 0:abc480f8eeab 366 //Extract the threshold
neilt6 0:abc480f8eeab 367 return (value >> 9) * 0.04;
neilt6 0:abc480f8eeab 368 }
neilt6 0:abc480f8eeab 369
neilt6 2:0a98e081b48c 370 void MAX17048::vResetThreshold(float threshold)
neilt6 0:abc480f8eeab 371 {
neilt6 0:abc480f8eeab 372 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 373 unsigned short value = read(REG_VRESET_ID);
neilt6 0:abc480f8eeab 374
neilt6 0:abc480f8eeab 375 //Mask off the old value
neilt6 0:abc480f8eeab 376 value &= 0x01FF;
neilt6 0:abc480f8eeab 377
neilt6 0:abc480f8eeab 378 //Do a smart update
neilt6 0:abc480f8eeab 379 if (threshold > 0.0) {
neilt6 0:abc480f8eeab 380 if (threshold < 5.08)
neilt6 0:abc480f8eeab 381 value |= (unsigned short)(threshold / 0.04) << 9;
neilt6 0:abc480f8eeab 382 else
neilt6 0:abc480f8eeab 383 value |= 0xFE00;
neilt6 0:abc480f8eeab 384 }
neilt6 0:abc480f8eeab 385
neilt6 0:abc480f8eeab 386 //Write the 16-bit register
neilt6 2:0a98e081b48c 387 write(REG_VRESET_ID, value);
neilt6 0:abc480f8eeab 388 }
neilt6 0:abc480f8eeab 389
neilt6 5:ffce4fe12ed1 390 bool MAX17048::comparatorEnabled()
neilt6 0:abc480f8eeab 391 {
neilt6 0:abc480f8eeab 392 //Read the 16-bit register value
neilt6 2:0a98e081b48c 393 unsigned short value = read(REG_VRESET_ID);
neilt6 0:abc480f8eeab 394
neilt6 0:abc480f8eeab 395 //Return the status of the Dis bit
neilt6 0:abc480f8eeab 396 if (value & (1 << 8))
neilt6 0:abc480f8eeab 397 return false;
neilt6 0:abc480f8eeab 398 else
neilt6 0:abc480f8eeab 399 return true;
neilt6 0:abc480f8eeab 400 }
neilt6 0:abc480f8eeab 401
neilt6 2:0a98e081b48c 402 void MAX17048::comparatorEnabled(bool enabled)
neilt6 0:abc480f8eeab 403 {
neilt6 0:abc480f8eeab 404 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 405 unsigned short value = read(REG_VRESET_ID);
neilt6 0:abc480f8eeab 406
neilt6 0:abc480f8eeab 407 //Set or clear the Dis bit
neilt6 0:abc480f8eeab 408 if (enabled)
neilt6 0:abc480f8eeab 409 value &= ~(1 << 8);
neilt6 0:abc480f8eeab 410 else
neilt6 0:abc480f8eeab 411 value |= (1 << 8);
neilt6 0:abc480f8eeab 412
neilt6 0:abc480f8eeab 413 //Write the value back out
neilt6 2:0a98e081b48c 414 write(REG_VRESET_ID, value);
neilt6 0:abc480f8eeab 415 }
neilt6 0:abc480f8eeab 416
neilt6 5:ffce4fe12ed1 417 char MAX17048::id()
neilt6 0:abc480f8eeab 418 {
neilt6 0:abc480f8eeab 419 //Read the 16-bit register value
neilt6 2:0a98e081b48c 420 unsigned short value = read(REG_VRESET_ID);
neilt6 0:abc480f8eeab 421
neilt6 0:abc480f8eeab 422 //Return only the ID bits
neilt6 0:abc480f8eeab 423 return value;
neilt6 0:abc480f8eeab 424 }
neilt6 0:abc480f8eeab 425
neilt6 5:ffce4fe12ed1 426 bool MAX17048::vResetAlertEnabled()
neilt6 0:abc480f8eeab 427 {
neilt6 0:abc480f8eeab 428 //Read the 16-bit register value
neilt6 2:0a98e081b48c 429 unsigned short value = read(REG_STATUS);
neilt6 0:abc480f8eeab 430
neilt6 0:abc480f8eeab 431 //Return the status of the EnVR bit
neilt6 0:abc480f8eeab 432 if (value & (1 << 14))
neilt6 0:abc480f8eeab 433 return true;
neilt6 0:abc480f8eeab 434 else
neilt6 0:abc480f8eeab 435 return false;
neilt6 0:abc480f8eeab 436 }
neilt6 0:abc480f8eeab 437
neilt6 2:0a98e081b48c 438 void MAX17048::vResetAlertEnabled(bool enabled)
neilt6 0:abc480f8eeab 439 {
neilt6 0:abc480f8eeab 440 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 441 unsigned short value = read(REG_STATUS);
neilt6 0:abc480f8eeab 442
neilt6 0:abc480f8eeab 443 //Set or clear the EnVR bit
neilt6 0:abc480f8eeab 444 if (enabled)
neilt6 0:abc480f8eeab 445 value |= (1 << 14);
neilt6 0:abc480f8eeab 446 else
neilt6 0:abc480f8eeab 447 value &= ~(1 << 14);
neilt6 0:abc480f8eeab 448
neilt6 0:abc480f8eeab 449 //Write the value back out
neilt6 2:0a98e081b48c 450 write(REG_STATUS, value);
neilt6 0:abc480f8eeab 451 }
neilt6 0:abc480f8eeab 452
neilt6 5:ffce4fe12ed1 453 char MAX17048::alertFlags()
neilt6 0:abc480f8eeab 454 {
neilt6 0:abc480f8eeab 455 //Read the 16-bit register value
neilt6 2:0a98e081b48c 456 unsigned short value = read(REG_STATUS);
neilt6 0:abc480f8eeab 457
neilt6 0:abc480f8eeab 458 //Return only the flag bits
neilt6 0:abc480f8eeab 459 return (value >> 8) & 0x3F;
neilt6 0:abc480f8eeab 460 }
neilt6 0:abc480f8eeab 461
neilt6 0:abc480f8eeab 462 void MAX17048::clearAlertFlags(char flags)
neilt6 0:abc480f8eeab 463 {
neilt6 0:abc480f8eeab 464 //Read the current 16-bit register value
neilt6 2:0a98e081b48c 465 unsigned short value = read(REG_STATUS);
neilt6 0:abc480f8eeab 466
neilt6 0:abc480f8eeab 467 //Clear the specified flag bits
neilt6 0:abc480f8eeab 468 value &= ~((flags & 0x3F) << 8);
neilt6 0:abc480f8eeab 469
neilt6 0:abc480f8eeab 470 //Write the value back out
neilt6 2:0a98e081b48c 471 write(REG_STATUS, value);
neilt6 0:abc480f8eeab 472 }
neilt6 0:abc480f8eeab 473
neilt6 5:ffce4fe12ed1 474 float MAX17048::vcell()
neilt6 0:abc480f8eeab 475 {
neilt6 0:abc480f8eeab 476 //Read the 16-bit raw Vcell value
neilt6 2:0a98e081b48c 477 unsigned short value = read(REG_VCELL);
neilt6 0:abc480f8eeab 478
neilt6 0:abc480f8eeab 479 //Return Vcell in volts
neilt6 0:abc480f8eeab 480 return value * 0.000078125;
neilt6 0:abc480f8eeab 481 }
neilt6 0:abc480f8eeab 482
neilt6 5:ffce4fe12ed1 483 float MAX17048::soc()
neilt6 0:abc480f8eeab 484 {
neilt6 0:abc480f8eeab 485 //Read the 16-bit raw SOC value
neilt6 2:0a98e081b48c 486 unsigned short value = read(REG_SOC);
neilt6 0:abc480f8eeab 487
neilt6 0:abc480f8eeab 488 //Return SOC in percent
neilt6 0:abc480f8eeab 489 return value * 0.00390625;
neilt6 0:abc480f8eeab 490 }
neilt6 0:abc480f8eeab 491
neilt6 5:ffce4fe12ed1 492 int MAX17048::soc_int()
neilt6 4:e61b2723d2cf 493 {
neilt6 4:e61b2723d2cf 494 //Read the 16-bit raw SOC value
neilt6 4:e61b2723d2cf 495 unsigned short value = read(REG_SOC);
neilt6 4:e61b2723d2cf 496
neilt6 4:e61b2723d2cf 497 //Return only the top byte
neilt6 4:e61b2723d2cf 498 return value >> 8;
neilt6 4:e61b2723d2cf 499 }
neilt6 4:e61b2723d2cf 500
neilt6 5:ffce4fe12ed1 501 float MAX17048::crate()
neilt6 0:abc480f8eeab 502 {
neilt6 0:abc480f8eeab 503 //Read the 16-bit raw C/Rate value
neilt6 2:0a98e081b48c 504 short value = read(REG_CRATE);
neilt6 0:abc480f8eeab 505
neilt6 0:abc480f8eeab 506 //Return C/Rate in %/hr
neilt6 0:abc480f8eeab 507 return value * 0.208;
neilt6 0:abc480f8eeab 508 }
neilt6 0:abc480f8eeab 509
neilt6 10:4f695111eb54 510 #ifdef MBED_OPERATORS
neilt6 5:ffce4fe12ed1 511 MAX17048::operator float()
neilt6 5:ffce4fe12ed1 512 {
neilt6 5:ffce4fe12ed1 513 //Return the current floating point SOC reading
neilt6 5:ffce4fe12ed1 514 return soc();
neilt6 5:ffce4fe12ed1 515 }
neilt6 5:ffce4fe12ed1 516
neilt6 5:ffce4fe12ed1 517 MAX17048::operator int()
neilt6 5:ffce4fe12ed1 518 {
neilt6 5:ffce4fe12ed1 519 //Return the current integer SOC reading
neilt6 5:ffce4fe12ed1 520 return soc_int();
neilt6 5:ffce4fe12ed1 521 }
neilt6 10:4f695111eb54 522 #endif
neilt6 5:ffce4fe12ed1 523
neilt6 2:0a98e081b48c 524 unsigned short MAX17048::read(char reg)
neilt6 0:abc480f8eeab 525 {
neilt6 0:abc480f8eeab 526 //Create a temporary buffer
neilt6 0:abc480f8eeab 527 char buff[2];
neilt6 0:abc480f8eeab 528
neilt6 0:abc480f8eeab 529 //Select the register
neilt6 6:6927c72b2923 530 m_I2C.write(m_ADDR, &reg, 1, true);
neilt6 0:abc480f8eeab 531
neilt6 0:abc480f8eeab 532 //Read the 16-bit register
neilt6 2:0a98e081b48c 533 m_I2C.read(m_ADDR, buff, 2);
neilt6 0:abc480f8eeab 534
neilt6 0:abc480f8eeab 535 //Return the combined 16-bit value
neilt6 0:abc480f8eeab 536 return (buff[0] << 8) | buff[1];
neilt6 0:abc480f8eeab 537 }
neilt6 0:abc480f8eeab 538
neilt6 2:0a98e081b48c 539 void MAX17048::write(char reg, unsigned short data)
neilt6 0:abc480f8eeab 540 {
neilt6 0:abc480f8eeab 541 //Create a temporary buffer
neilt6 0:abc480f8eeab 542 char buff[3];
neilt6 0:abc480f8eeab 543
neilt6 0:abc480f8eeab 544 //Load the register address and 16-bit data
neilt6 0:abc480f8eeab 545 buff[0] = reg;
neilt6 0:abc480f8eeab 546 buff[1] = data >> 8;
neilt6 0:abc480f8eeab 547 buff[2] = data;
neilt6 0:abc480f8eeab 548
neilt6 0:abc480f8eeab 549 //Write the data
neilt6 2:0a98e081b48c 550 m_I2C.write(m_ADDR, buff, 3);
neilt6 0:abc480f8eeab 551 }