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@5:ffce4fe12ed1, 2013-11-07 (annotated)
- Committer:
- neilt6
- Date:
- Thu Nov 07 17:53:44 2013 +0000
- Revision:
- 5:ffce4fe12ed1
- Parent:
- 4:e61b2723d2cf
- Child:
- 6:6927c72b2923
Minor improvements
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 | //Load the default RCOMP value |
neilt6 | 3:32087cca331f | 30 | writeRCOMP(m_RCOMP0); |
neilt6 | 3:32087cca331f | 31 | |
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 | 2:0a98e081b48c | 161 | void MAX17048::tempCompensation(float temp) |
neilt6 | 0:abc480f8eeab | 162 | { |
neilt6 | 0:abc480f8eeab | 163 | //Calculate the new RCOMP value |
neilt6 | 0:abc480f8eeab | 164 | char rcomp; |
neilt6 | 0:abc480f8eeab | 165 | if (temp > 20.0) { |
neilt6 | 2:0a98e081b48c | 166 | rcomp = m_RCOMP0 + (temp - 20.0) * -0.5; |
neilt6 | 0:abc480f8eeab | 167 | } else { |
neilt6 | 2:0a98e081b48c | 168 | rcomp = m_RCOMP0 + (temp - 20.0) * -5.0; |
neilt6 | 0:abc480f8eeab | 169 | } |
neilt6 | 0:abc480f8eeab | 170 | |
neilt6 | 0:abc480f8eeab | 171 | //Update the RCOMP value |
neilt6 | 2:0a98e081b48c | 172 | writeRCOMP(rcomp); |
neilt6 | 0:abc480f8eeab | 173 | } |
neilt6 | 0:abc480f8eeab | 174 | |
neilt6 | 5:ffce4fe12ed1 | 175 | bool MAX17048::sleeping() |
neilt6 | 0:abc480f8eeab | 176 | { |
neilt6 | 0:abc480f8eeab | 177 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 178 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 179 | |
neilt6 | 0:abc480f8eeab | 180 | //Return the status of the SLEEP bit |
neilt6 | 0:abc480f8eeab | 181 | if (value & (1 << 7)) |
neilt6 | 0:abc480f8eeab | 182 | return true; |
neilt6 | 0:abc480f8eeab | 183 | else |
neilt6 | 0:abc480f8eeab | 184 | return false; |
neilt6 | 0:abc480f8eeab | 185 | } |
neilt6 | 0:abc480f8eeab | 186 | |
neilt6 | 2:0a98e081b48c | 187 | void MAX17048::sleep(bool sleep) |
neilt6 | 0:abc480f8eeab | 188 | { |
neilt6 | 0:abc480f8eeab | 189 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 190 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 191 | |
neilt6 | 0:abc480f8eeab | 192 | //Set or clear the SLEEP bit |
neilt6 | 0:abc480f8eeab | 193 | if (sleep) |
neilt6 | 0:abc480f8eeab | 194 | value |= (1 << 7); |
neilt6 | 0:abc480f8eeab | 195 | else |
neilt6 | 0:abc480f8eeab | 196 | value &= ~(1 << 7); |
neilt6 | 0:abc480f8eeab | 197 | |
neilt6 | 0:abc480f8eeab | 198 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 199 | write(REG_CONFIG, value); |
neilt6 | 0:abc480f8eeab | 200 | } |
neilt6 | 0:abc480f8eeab | 201 | |
neilt6 | 5:ffce4fe12ed1 | 202 | bool MAX17048::socChangeAlertEnabled() |
neilt6 | 0:abc480f8eeab | 203 | { |
neilt6 | 0:abc480f8eeab | 204 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 205 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 206 | |
neilt6 | 0:abc480f8eeab | 207 | //Return the status of the ALSC bit |
neilt6 | 0:abc480f8eeab | 208 | if (value & (1 << 6)) |
neilt6 | 0:abc480f8eeab | 209 | return true; |
neilt6 | 0:abc480f8eeab | 210 | else |
neilt6 | 0:abc480f8eeab | 211 | return false; |
neilt6 | 0:abc480f8eeab | 212 | } |
neilt6 | 0:abc480f8eeab | 213 | |
neilt6 | 2:0a98e081b48c | 214 | void MAX17048::socChangeAlertEnabled(bool enabled) |
neilt6 | 0:abc480f8eeab | 215 | { |
neilt6 | 0:abc480f8eeab | 216 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 217 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 218 | |
neilt6 | 0:abc480f8eeab | 219 | //Set or clear the ALSC bit |
neilt6 | 0:abc480f8eeab | 220 | if (enabled) |
neilt6 | 0:abc480f8eeab | 221 | value |= (1 << 6); |
neilt6 | 0:abc480f8eeab | 222 | else |
neilt6 | 0:abc480f8eeab | 223 | value &= ~(1 << 6); |
neilt6 | 0:abc480f8eeab | 224 | |
neilt6 | 0:abc480f8eeab | 225 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 226 | write(REG_CONFIG, value); |
neilt6 | 0:abc480f8eeab | 227 | } |
neilt6 | 0:abc480f8eeab | 228 | |
neilt6 | 5:ffce4fe12ed1 | 229 | bool MAX17048::alerting() |
neilt6 | 0:abc480f8eeab | 230 | { |
neilt6 | 0:abc480f8eeab | 231 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 232 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 233 | |
neilt6 | 0:abc480f8eeab | 234 | //Return the status of the ALRT bit |
neilt6 | 0:abc480f8eeab | 235 | if (value & (1 << 5)) |
neilt6 | 0:abc480f8eeab | 236 | return true; |
neilt6 | 0:abc480f8eeab | 237 | else |
neilt6 | 0:abc480f8eeab | 238 | return false; |
neilt6 | 0:abc480f8eeab | 239 | } |
neilt6 | 0:abc480f8eeab | 240 | |
neilt6 | 5:ffce4fe12ed1 | 241 | void MAX17048::clearAlert() |
neilt6 | 0:abc480f8eeab | 242 | { |
neilt6 | 0:abc480f8eeab | 243 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 244 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 245 | |
neilt6 | 0:abc480f8eeab | 246 | //Clear the ALRT bit |
neilt6 | 0:abc480f8eeab | 247 | value &= ~(1 << 5); |
neilt6 | 0:abc480f8eeab | 248 | |
neilt6 | 0:abc480f8eeab | 249 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 250 | write(REG_CONFIG, value); |
neilt6 | 0:abc480f8eeab | 251 | } |
neilt6 | 0:abc480f8eeab | 252 | |
neilt6 | 5:ffce4fe12ed1 | 253 | char MAX17048::emptyAlertThreshold() |
neilt6 | 0:abc480f8eeab | 254 | { |
neilt6 | 0:abc480f8eeab | 255 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 256 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 257 | |
neilt6 | 0:abc480f8eeab | 258 | //Extract the threshold |
neilt6 | 0:abc480f8eeab | 259 | return 32 - (value & 0x001F); |
neilt6 | 0:abc480f8eeab | 260 | } |
neilt6 | 0:abc480f8eeab | 261 | |
neilt6 | 2:0a98e081b48c | 262 | void MAX17048::emptyAlertThreshold(char threshold) |
neilt6 | 0:abc480f8eeab | 263 | { |
neilt6 | 0:abc480f8eeab | 264 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 265 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 266 | |
neilt6 | 0:abc480f8eeab | 267 | //Range check threshold |
neilt6 | 0:abc480f8eeab | 268 | if (threshold < 1) |
neilt6 | 0:abc480f8eeab | 269 | threshold = 1; |
neilt6 | 0:abc480f8eeab | 270 | else if (threshold > 32) |
neilt6 | 0:abc480f8eeab | 271 | threshold = 32; |
neilt6 | 0:abc480f8eeab | 272 | |
neilt6 | 0:abc480f8eeab | 273 | //Update the register value |
neilt6 | 0:abc480f8eeab | 274 | value &= 0xFFE0; |
neilt6 | 0:abc480f8eeab | 275 | value |= 32 - threshold; |
neilt6 | 0:abc480f8eeab | 276 | |
neilt6 | 0:abc480f8eeab | 277 | //Write the 16-bit register |
neilt6 | 2:0a98e081b48c | 278 | write(REG_CONFIG, value); |
neilt6 | 0:abc480f8eeab | 279 | } |
neilt6 | 0:abc480f8eeab | 280 | |
neilt6 | 5:ffce4fe12ed1 | 281 | float MAX17048::vAlertMinThreshold() |
neilt6 | 0:abc480f8eeab | 282 | { |
neilt6 | 0:abc480f8eeab | 283 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 284 | unsigned short value = read(REG_VALRT); |
neilt6 | 0:abc480f8eeab | 285 | |
neilt6 | 0:abc480f8eeab | 286 | //Extract the alert threshold |
neilt6 | 0:abc480f8eeab | 287 | return (value >> 8) * 0.02; |
neilt6 | 0:abc480f8eeab | 288 | } |
neilt6 | 0:abc480f8eeab | 289 | |
neilt6 | 2:0a98e081b48c | 290 | void MAX17048::vAlertMinThreshold(float threshold) |
neilt6 | 0:abc480f8eeab | 291 | { |
neilt6 | 0:abc480f8eeab | 292 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 293 | unsigned short value = read(REG_VALRT); |
neilt6 | 0:abc480f8eeab | 294 | |
neilt6 | 0:abc480f8eeab | 295 | //Mask off the old value |
neilt6 | 0:abc480f8eeab | 296 | value &= 0x00FF; |
neilt6 | 0:abc480f8eeab | 297 | |
neilt6 | 0:abc480f8eeab | 298 | //Do a smart update |
neilt6 | 0:abc480f8eeab | 299 | if (threshold > 0.0) { |
neilt6 | 0:abc480f8eeab | 300 | if (threshold < 5.1) |
neilt6 | 0:abc480f8eeab | 301 | value |= (unsigned short)(threshold / 0.02) << 8; |
neilt6 | 0:abc480f8eeab | 302 | else |
neilt6 | 0:abc480f8eeab | 303 | value |= 0xFF00; |
neilt6 | 0:abc480f8eeab | 304 | } |
neilt6 | 0:abc480f8eeab | 305 | |
neilt6 | 0:abc480f8eeab | 306 | //Write the 16-bit register |
neilt6 | 2:0a98e081b48c | 307 | write(REG_VALRT, value); |
neilt6 | 0:abc480f8eeab | 308 | } |
neilt6 | 0:abc480f8eeab | 309 | |
neilt6 | 5:ffce4fe12ed1 | 310 | float MAX17048::vAlertMaxThreshold() |
neilt6 | 0:abc480f8eeab | 311 | { |
neilt6 | 0:abc480f8eeab | 312 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 313 | unsigned short value = read(REG_VALRT); |
neilt6 | 0:abc480f8eeab | 314 | |
neilt6 | 0:abc480f8eeab | 315 | //Extract the active threshold |
neilt6 | 0:abc480f8eeab | 316 | return (value & 0x00FF) * 0.02; |
neilt6 | 0:abc480f8eeab | 317 | } |
neilt6 | 0:abc480f8eeab | 318 | |
neilt6 | 2:0a98e081b48c | 319 | void MAX17048::vAlertMaxThreshold(float threshold) |
neilt6 | 0:abc480f8eeab | 320 | { |
neilt6 | 0:abc480f8eeab | 321 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 322 | unsigned short value = read(REG_VALRT); |
neilt6 | 0:abc480f8eeab | 323 | |
neilt6 | 0:abc480f8eeab | 324 | //Mask off the old value |
neilt6 | 0:abc480f8eeab | 325 | value &= 0xFF00; |
neilt6 | 0:abc480f8eeab | 326 | |
neilt6 | 0:abc480f8eeab | 327 | //Do a smart update |
neilt6 | 0:abc480f8eeab | 328 | if (threshold > 0.0) { |
neilt6 | 0:abc480f8eeab | 329 | if (threshold < 5.1) |
neilt6 | 0:abc480f8eeab | 330 | value |= (char)(threshold / 0.02); |
neilt6 | 0:abc480f8eeab | 331 | else |
neilt6 | 0:abc480f8eeab | 332 | value |= 0x00FF; |
neilt6 | 0:abc480f8eeab | 333 | } |
neilt6 | 0:abc480f8eeab | 334 | |
neilt6 | 0:abc480f8eeab | 335 | //Write the 16-bit register |
neilt6 | 2:0a98e081b48c | 336 | write(REG_VALRT, value); |
neilt6 | 0:abc480f8eeab | 337 | } |
neilt6 | 0:abc480f8eeab | 338 | |
neilt6 | 5:ffce4fe12ed1 | 339 | float MAX17048::vResetThreshold() |
neilt6 | 0:abc480f8eeab | 340 | { |
neilt6 | 0:abc480f8eeab | 341 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 342 | unsigned short value = read(REG_VRESET_ID); |
neilt6 | 0:abc480f8eeab | 343 | |
neilt6 | 0:abc480f8eeab | 344 | //Extract the threshold |
neilt6 | 0:abc480f8eeab | 345 | return (value >> 9) * 0.04; |
neilt6 | 0:abc480f8eeab | 346 | } |
neilt6 | 0:abc480f8eeab | 347 | |
neilt6 | 2:0a98e081b48c | 348 | void MAX17048::vResetThreshold(float threshold) |
neilt6 | 0:abc480f8eeab | 349 | { |
neilt6 | 0:abc480f8eeab | 350 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 351 | unsigned short value = read(REG_VRESET_ID); |
neilt6 | 0:abc480f8eeab | 352 | |
neilt6 | 0:abc480f8eeab | 353 | //Mask off the old value |
neilt6 | 0:abc480f8eeab | 354 | value &= 0x01FF; |
neilt6 | 0:abc480f8eeab | 355 | |
neilt6 | 0:abc480f8eeab | 356 | //Do a smart update |
neilt6 | 0:abc480f8eeab | 357 | if (threshold > 0.0) { |
neilt6 | 0:abc480f8eeab | 358 | if (threshold < 5.08) |
neilt6 | 0:abc480f8eeab | 359 | value |= (unsigned short)(threshold / 0.04) << 9; |
neilt6 | 0:abc480f8eeab | 360 | else |
neilt6 | 0:abc480f8eeab | 361 | value |= 0xFE00; |
neilt6 | 0:abc480f8eeab | 362 | } |
neilt6 | 0:abc480f8eeab | 363 | |
neilt6 | 0:abc480f8eeab | 364 | //Write the 16-bit register |
neilt6 | 2:0a98e081b48c | 365 | write(REG_VRESET_ID, value); |
neilt6 | 0:abc480f8eeab | 366 | } |
neilt6 | 0:abc480f8eeab | 367 | |
neilt6 | 5:ffce4fe12ed1 | 368 | bool MAX17048::comparatorEnabled() |
neilt6 | 0:abc480f8eeab | 369 | { |
neilt6 | 0:abc480f8eeab | 370 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 371 | unsigned short value = read(REG_VRESET_ID); |
neilt6 | 0:abc480f8eeab | 372 | |
neilt6 | 0:abc480f8eeab | 373 | //Return the status of the Dis bit |
neilt6 | 0:abc480f8eeab | 374 | if (value & (1 << 8)) |
neilt6 | 0:abc480f8eeab | 375 | return false; |
neilt6 | 0:abc480f8eeab | 376 | else |
neilt6 | 0:abc480f8eeab | 377 | return true; |
neilt6 | 0:abc480f8eeab | 378 | } |
neilt6 | 0:abc480f8eeab | 379 | |
neilt6 | 2:0a98e081b48c | 380 | void MAX17048::comparatorEnabled(bool enabled) |
neilt6 | 0:abc480f8eeab | 381 | { |
neilt6 | 0:abc480f8eeab | 382 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 383 | unsigned short value = read(REG_VRESET_ID); |
neilt6 | 0:abc480f8eeab | 384 | |
neilt6 | 0:abc480f8eeab | 385 | //Set or clear the Dis bit |
neilt6 | 0:abc480f8eeab | 386 | if (enabled) |
neilt6 | 0:abc480f8eeab | 387 | value &= ~(1 << 8); |
neilt6 | 0:abc480f8eeab | 388 | else |
neilt6 | 0:abc480f8eeab | 389 | value |= (1 << 8); |
neilt6 | 0:abc480f8eeab | 390 | |
neilt6 | 0:abc480f8eeab | 391 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 392 | write(REG_VRESET_ID, value); |
neilt6 | 0:abc480f8eeab | 393 | } |
neilt6 | 0:abc480f8eeab | 394 | |
neilt6 | 5:ffce4fe12ed1 | 395 | char MAX17048::id() |
neilt6 | 0:abc480f8eeab | 396 | { |
neilt6 | 0:abc480f8eeab | 397 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 398 | unsigned short value = read(REG_VRESET_ID); |
neilt6 | 0:abc480f8eeab | 399 | |
neilt6 | 0:abc480f8eeab | 400 | //Return only the ID bits |
neilt6 | 0:abc480f8eeab | 401 | return value; |
neilt6 | 0:abc480f8eeab | 402 | } |
neilt6 | 0:abc480f8eeab | 403 | |
neilt6 | 5:ffce4fe12ed1 | 404 | bool MAX17048::vResetAlertEnabled() |
neilt6 | 0:abc480f8eeab | 405 | { |
neilt6 | 0:abc480f8eeab | 406 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 407 | unsigned short value = read(REG_STATUS); |
neilt6 | 0:abc480f8eeab | 408 | |
neilt6 | 0:abc480f8eeab | 409 | //Return the status of the EnVR bit |
neilt6 | 0:abc480f8eeab | 410 | if (value & (1 << 14)) |
neilt6 | 0:abc480f8eeab | 411 | return true; |
neilt6 | 0:abc480f8eeab | 412 | else |
neilt6 | 0:abc480f8eeab | 413 | return false; |
neilt6 | 0:abc480f8eeab | 414 | } |
neilt6 | 0:abc480f8eeab | 415 | |
neilt6 | 2:0a98e081b48c | 416 | void MAX17048::vResetAlertEnabled(bool enabled) |
neilt6 | 0:abc480f8eeab | 417 | { |
neilt6 | 0:abc480f8eeab | 418 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 419 | unsigned short value = read(REG_STATUS); |
neilt6 | 0:abc480f8eeab | 420 | |
neilt6 | 0:abc480f8eeab | 421 | //Set or clear the EnVR bit |
neilt6 | 0:abc480f8eeab | 422 | if (enabled) |
neilt6 | 0:abc480f8eeab | 423 | value |= (1 << 14); |
neilt6 | 0:abc480f8eeab | 424 | else |
neilt6 | 0:abc480f8eeab | 425 | value &= ~(1 << 14); |
neilt6 | 0:abc480f8eeab | 426 | |
neilt6 | 0:abc480f8eeab | 427 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 428 | write(REG_STATUS, value); |
neilt6 | 0:abc480f8eeab | 429 | } |
neilt6 | 0:abc480f8eeab | 430 | |
neilt6 | 5:ffce4fe12ed1 | 431 | char MAX17048::alertFlags() |
neilt6 | 0:abc480f8eeab | 432 | { |
neilt6 | 0:abc480f8eeab | 433 | //Read the 16-bit register value |
neilt6 | 2:0a98e081b48c | 434 | unsigned short value = read(REG_STATUS); |
neilt6 | 0:abc480f8eeab | 435 | |
neilt6 | 0:abc480f8eeab | 436 | //Return only the flag bits |
neilt6 | 0:abc480f8eeab | 437 | return (value >> 8) & 0x3F; |
neilt6 | 0:abc480f8eeab | 438 | } |
neilt6 | 0:abc480f8eeab | 439 | |
neilt6 | 0:abc480f8eeab | 440 | void MAX17048::clearAlertFlags(char flags) |
neilt6 | 0:abc480f8eeab | 441 | { |
neilt6 | 0:abc480f8eeab | 442 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 443 | unsigned short value = read(REG_STATUS); |
neilt6 | 0:abc480f8eeab | 444 | |
neilt6 | 0:abc480f8eeab | 445 | //Clear the specified flag bits |
neilt6 | 0:abc480f8eeab | 446 | value &= ~((flags & 0x3F) << 8); |
neilt6 | 0:abc480f8eeab | 447 | |
neilt6 | 0:abc480f8eeab | 448 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 449 | write(REG_STATUS, value); |
neilt6 | 0:abc480f8eeab | 450 | } |
neilt6 | 0:abc480f8eeab | 451 | |
neilt6 | 5:ffce4fe12ed1 | 452 | float MAX17048::vcell() |
neilt6 | 0:abc480f8eeab | 453 | { |
neilt6 | 0:abc480f8eeab | 454 | //Read the 16-bit raw Vcell value |
neilt6 | 2:0a98e081b48c | 455 | unsigned short value = read(REG_VCELL); |
neilt6 | 0:abc480f8eeab | 456 | |
neilt6 | 0:abc480f8eeab | 457 | //Return Vcell in volts |
neilt6 | 0:abc480f8eeab | 458 | return value * 0.000078125; |
neilt6 | 0:abc480f8eeab | 459 | } |
neilt6 | 0:abc480f8eeab | 460 | |
neilt6 | 5:ffce4fe12ed1 | 461 | float MAX17048::soc() |
neilt6 | 0:abc480f8eeab | 462 | { |
neilt6 | 0:abc480f8eeab | 463 | //Read the 16-bit raw SOC value |
neilt6 | 2:0a98e081b48c | 464 | unsigned short value = read(REG_SOC); |
neilt6 | 0:abc480f8eeab | 465 | |
neilt6 | 0:abc480f8eeab | 466 | //Return SOC in percent |
neilt6 | 0:abc480f8eeab | 467 | return value * 0.00390625; |
neilt6 | 0:abc480f8eeab | 468 | } |
neilt6 | 0:abc480f8eeab | 469 | |
neilt6 | 5:ffce4fe12ed1 | 470 | int MAX17048::soc_int() |
neilt6 | 4:e61b2723d2cf | 471 | { |
neilt6 | 4:e61b2723d2cf | 472 | //Read the 16-bit raw SOC value |
neilt6 | 4:e61b2723d2cf | 473 | unsigned short value = read(REG_SOC); |
neilt6 | 4:e61b2723d2cf | 474 | |
neilt6 | 4:e61b2723d2cf | 475 | //Return only the top byte |
neilt6 | 4:e61b2723d2cf | 476 | return value >> 8; |
neilt6 | 4:e61b2723d2cf | 477 | } |
neilt6 | 4:e61b2723d2cf | 478 | |
neilt6 | 5:ffce4fe12ed1 | 479 | float MAX17048::crate() |
neilt6 | 0:abc480f8eeab | 480 | { |
neilt6 | 0:abc480f8eeab | 481 | //Read the 16-bit raw C/Rate value |
neilt6 | 2:0a98e081b48c | 482 | short value = read(REG_CRATE); |
neilt6 | 0:abc480f8eeab | 483 | |
neilt6 | 0:abc480f8eeab | 484 | //Return C/Rate in %/hr |
neilt6 | 0:abc480f8eeab | 485 | return value * 0.208; |
neilt6 | 0:abc480f8eeab | 486 | } |
neilt6 | 0:abc480f8eeab | 487 | |
neilt6 | 5:ffce4fe12ed1 | 488 | MAX17048::operator float() |
neilt6 | 5:ffce4fe12ed1 | 489 | { |
neilt6 | 5:ffce4fe12ed1 | 490 | //Return the current floating point SOC reading |
neilt6 | 5:ffce4fe12ed1 | 491 | return soc(); |
neilt6 | 5:ffce4fe12ed1 | 492 | } |
neilt6 | 5:ffce4fe12ed1 | 493 | |
neilt6 | 5:ffce4fe12ed1 | 494 | MAX17048::operator int() |
neilt6 | 5:ffce4fe12ed1 | 495 | { |
neilt6 | 5:ffce4fe12ed1 | 496 | //Return the current integer SOC reading |
neilt6 | 5:ffce4fe12ed1 | 497 | return soc_int(); |
neilt6 | 5:ffce4fe12ed1 | 498 | } |
neilt6 | 5:ffce4fe12ed1 | 499 | |
neilt6 | 2:0a98e081b48c | 500 | unsigned short MAX17048::read(char reg) |
neilt6 | 0:abc480f8eeab | 501 | { |
neilt6 | 0:abc480f8eeab | 502 | //Create a temporary buffer |
neilt6 | 0:abc480f8eeab | 503 | char buff[2]; |
neilt6 | 0:abc480f8eeab | 504 | |
neilt6 | 0:abc480f8eeab | 505 | //Select the register |
neilt6 | 2:0a98e081b48c | 506 | m_I2C.write(m_ADDR, ®, 1); |
neilt6 | 0:abc480f8eeab | 507 | |
neilt6 | 0:abc480f8eeab | 508 | //Read the 16-bit register |
neilt6 | 2:0a98e081b48c | 509 | m_I2C.read(m_ADDR, buff, 2); |
neilt6 | 0:abc480f8eeab | 510 | |
neilt6 | 0:abc480f8eeab | 511 | //Return the combined 16-bit value |
neilt6 | 0:abc480f8eeab | 512 | return (buff[0] << 8) | buff[1]; |
neilt6 | 0:abc480f8eeab | 513 | } |
neilt6 | 0:abc480f8eeab | 514 | |
neilt6 | 2:0a98e081b48c | 515 | void MAX17048::write(char reg, unsigned short data) |
neilt6 | 0:abc480f8eeab | 516 | { |
neilt6 | 0:abc480f8eeab | 517 | //Create a temporary buffer |
neilt6 | 0:abc480f8eeab | 518 | char buff[3]; |
neilt6 | 0:abc480f8eeab | 519 | |
neilt6 | 0:abc480f8eeab | 520 | //Load the register address and 16-bit data |
neilt6 | 0:abc480f8eeab | 521 | buff[0] = reg; |
neilt6 | 0:abc480f8eeab | 522 | buff[1] = data >> 8; |
neilt6 | 0:abc480f8eeab | 523 | buff[2] = data; |
neilt6 | 0:abc480f8eeab | 524 | |
neilt6 | 0:abc480f8eeab | 525 | //Write the data |
neilt6 | 2:0a98e081b48c | 526 | m_I2C.write(m_ADDR, buff, 3); |
neilt6 | 0:abc480f8eeab | 527 | } |
neilt6 | 0:abc480f8eeab | 528 | |
neilt6 | 2:0a98e081b48c | 529 | void MAX17048::writeRCOMP(char rcomp) |
neilt6 | 0:abc480f8eeab | 530 | { |
neilt6 | 0:abc480f8eeab | 531 | //Read the current 16-bit register value |
neilt6 | 2:0a98e081b48c | 532 | unsigned short value = read(REG_CONFIG); |
neilt6 | 0:abc480f8eeab | 533 | |
neilt6 | 0:abc480f8eeab | 534 | //Update the register value |
neilt6 | 0:abc480f8eeab | 535 | value &= 0x00FF; |
neilt6 | 0:abc480f8eeab | 536 | value |= rcomp << 8; |
neilt6 | 0:abc480f8eeab | 537 | |
neilt6 | 0:abc480f8eeab | 538 | //Write the value back out |
neilt6 | 2:0a98e081b48c | 539 | write(REG_CONFIG, value); |
neilt6 | 0:abc480f8eeab | 540 | } |