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