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