Library for I2C communication with LiPo battery fuel gauge to measure battery level.
Dependents: MAX1704X_example_app
MAX1704X.cpp@0:b3b7aff20a1d, 2017-10-09 (annotated)
- Committer:
- maclobdell
- Date:
- Mon Oct 09 12:52:15 2017 +0000
- Revision:
- 0:b3b7aff20a1d
- Child:
- 1:b2d54467330d
initial version, haven't tested yet
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:b3b7aff20a1d | 1 | /* MAX1704X Simple Driver Library |
maclobdell | 0:b3b7aff20a1d | 2 | * Copyright (c) 2017 Mac Lobdell |
maclobdell | 0:b3b7aff20a1d | 3 | * |
maclobdell | 0:b3b7aff20a1d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
maclobdell | 0:b3b7aff20a1d | 5 | * you may not use this file except in compliance with the License. |
maclobdell | 0:b3b7aff20a1d | 6 | * You may obtain a copy of the License at |
maclobdell | 0:b3b7aff20a1d | 7 | * |
maclobdell | 0:b3b7aff20a1d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
maclobdell | 0:b3b7aff20a1d | 9 | * |
maclobdell | 0:b3b7aff20a1d | 10 | * Unless required by applicable law or agreed to in writing, software |
maclobdell | 0:b3b7aff20a1d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
maclobdell | 0:b3b7aff20a1d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
maclobdell | 0:b3b7aff20a1d | 13 | * See the License for the specific language governing permissions and |
maclobdell | 0:b3b7aff20a1d | 14 | * limitations under the License. |
maclobdell | 0:b3b7aff20a1d | 15 | */ |
maclobdell | 0:b3b7aff20a1d | 16 | |
maclobdell | 0:b3b7aff20a1d | 17 | //todo: configure pin interrupt on battery level alert |
maclobdell | 0:b3b7aff20a1d | 18 | |
maclobdell | 0:b3b7aff20a1d | 19 | #include "MAX1704X.h" |
maclobdell | 0:b3b7aff20a1d | 20 | |
maclobdell | 0:b3b7aff20a1d | 21 | MAX1704X::MAX1704X(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr) |
maclobdell | 0:b3b7aff20a1d | 22 | { |
maclobdell | 0:b3b7aff20a1d | 23 | //Set the I2C bus frequency |
maclobdell | 0:b3b7aff20a1d | 24 | m_I2C.frequency(hz); |
maclobdell | 0:b3b7aff20a1d | 25 | |
maclobdell | 0:b3b7aff20a1d | 26 | write16(REG_CONFIG_H,0x9700); //set alert to 32% |
maclobdell | 0:b3b7aff20a1d | 27 | |
maclobdell | 0:b3b7aff20a1d | 28 | } |
maclobdell | 0:b3b7aff20a1d | 29 | |
maclobdell | 0:b3b7aff20a1d | 30 | bool MAX1704X::open() |
maclobdell | 0:b3b7aff20a1d | 31 | { |
maclobdell | 0:b3b7aff20a1d | 32 | //Probe for the I2C Device using a Zero Length Transfer |
maclobdell | 0:b3b7aff20a1d | 33 | if (!m_I2C.write(m_ADDR, NULL, 0)) { |
maclobdell | 0:b3b7aff20a1d | 34 | //Return success |
maclobdell | 0:b3b7aff20a1d | 35 | return true; |
maclobdell | 0:b3b7aff20a1d | 36 | } else { |
maclobdell | 0:b3b7aff20a1d | 37 | //Return failure |
maclobdell | 0:b3b7aff20a1d | 38 | return false; |
maclobdell | 0:b3b7aff20a1d | 39 | } |
maclobdell | 0:b3b7aff20a1d | 40 | } |
maclobdell | 0:b3b7aff20a1d | 41 | |
maclobdell | 0:b3b7aff20a1d | 42 | uint32_t MAX1704X::read_ad() |
maclobdell | 0:b3b7aff20a1d | 43 | { |
maclobdell | 0:b3b7aff20a1d | 44 | char xm, xl; |
maclobdell | 0:b3b7aff20a1d | 45 | uint32_t temp, xo; |
maclobdell | 0:b3b7aff20a1d | 46 | |
maclobdell | 0:b3b7aff20a1d | 47 | xm = read8(REG_VCELL_H); //high byte |
maclobdell | 0:b3b7aff20a1d | 48 | xl = read8(REG_VCELL_L); //low byte |
maclobdell | 0:b3b7aff20a1d | 49 | |
maclobdell | 0:b3b7aff20a1d | 50 | temp = (xl|(xm << 8)); |
maclobdell | 0:b3b7aff20a1d | 51 | //alternatively can read16 |
maclobdell | 0:b3b7aff20a1d | 52 | //xo = read16(REG_VCELL_H); //does this work? |
maclobdell | 0:b3b7aff20a1d | 53 | |
maclobdell | 0:b3b7aff20a1d | 54 | xo = 1.25* (temp >>4); |
maclobdell | 0:b3b7aff20a1d | 55 | |
maclobdell | 0:b3b7aff20a1d | 56 | //returns A/D value in mV |
maclobdell | 0:b3b7aff20a1d | 57 | return xo; |
maclobdell | 0:b3b7aff20a1d | 58 | |
maclobdell | 0:b3b7aff20a1d | 59 | } |
maclobdell | 0:b3b7aff20a1d | 60 | |
maclobdell | 0:b3b7aff20a1d | 61 | uint16_t MAX1704X::read_config() |
maclobdell | 0:b3b7aff20a1d | 62 | { |
maclobdell | 0:b3b7aff20a1d | 63 | char xm, xl; |
maclobdell | 0:b3b7aff20a1d | 64 | uint16_t xo; |
maclobdell | 0:b3b7aff20a1d | 65 | |
maclobdell | 0:b3b7aff20a1d | 66 | xm = read8(REG_CONFIG_H); //high byte |
maclobdell | 0:b3b7aff20a1d | 67 | xl = read8(REG_CONFIG_L); //low byte |
maclobdell | 0:b3b7aff20a1d | 68 | |
maclobdell | 0:b3b7aff20a1d | 69 | xo = xl|(xm << 8); |
maclobdell | 0:b3b7aff20a1d | 70 | |
maclobdell | 0:b3b7aff20a1d | 71 | //alternatively can read16 |
maclobdell | 0:b3b7aff20a1d | 72 | // xo = read16(REG_CONFIG_H); //does this work? |
maclobdell | 0:b3b7aff20a1d | 73 | |
maclobdell | 0:b3b7aff20a1d | 74 | return xo; |
maclobdell | 0:b3b7aff20a1d | 75 | } |
maclobdell | 0:b3b7aff20a1d | 76 | |
maclobdell | 0:b3b7aff20a1d | 77 | uint32_t MAX1704X::read_percent() |
maclobdell | 0:b3b7aff20a1d | 78 | { |
maclobdell | 0:b3b7aff20a1d | 79 | char xm, xl; |
maclobdell | 0:b3b7aff20a1d | 80 | uint32_t xo; |
maclobdell | 0:b3b7aff20a1d | 81 | |
maclobdell | 0:b3b7aff20a1d | 82 | xm = read8(REG_SOC_H); //high byte |
maclobdell | 0:b3b7aff20a1d | 83 | xl = read8(REG_SOC_L); //low byte |
maclobdell | 0:b3b7aff20a1d | 84 | |
maclobdell | 0:b3b7aff20a1d | 85 | xo = (xl|(xm << 8)); |
maclobdell | 0:b3b7aff20a1d | 86 | |
maclobdell | 0:b3b7aff20a1d | 87 | //alternatively can read16 |
maclobdell | 0:b3b7aff20a1d | 88 | // xo = read16(REG_SOC_H); //does this work? |
maclobdell | 0:b3b7aff20a1d | 89 | |
maclobdell | 0:b3b7aff20a1d | 90 | //percent_decimal = (0.003906)*xl + xm; |
maclobdell | 0:b3b7aff20a1d | 91 | |
maclobdell | 0:b3b7aff20a1d | 92 | //returns percent |
maclobdell | 0:b3b7aff20a1d | 93 | return xo; |
maclobdell | 0:b3b7aff20a1d | 94 | |
maclobdell | 0:b3b7aff20a1d | 95 | } |
maclobdell | 0:b3b7aff20a1d | 96 | |
maclobdell | 0:b3b7aff20a1d | 97 | void MAX1704X::power_on_reset(void) |
maclobdell | 0:b3b7aff20a1d | 98 | { |
maclobdell | 0:b3b7aff20a1d | 99 | |
maclobdell | 0:b3b7aff20a1d | 100 | write8(REG_COMMAND_H, 0x54); |
maclobdell | 0:b3b7aff20a1d | 101 | write8(REG_COMMAND_L, 0x00); |
maclobdell | 0:b3b7aff20a1d | 102 | |
maclobdell | 0:b3b7aff20a1d | 103 | //alternatively can write16 |
maclobdell | 0:b3b7aff20a1d | 104 | //write16(REG_COMMAND_H, 0x5400); //does this work? |
maclobdell | 0:b3b7aff20a1d | 105 | |
maclobdell | 0:b3b7aff20a1d | 106 | } |
maclobdell | 0:b3b7aff20a1d | 107 | /* |
maclobdell | 0:b3b7aff20a1d | 108 | //#ifdef MBED_OPERATORS //this no longer be needed? |
maclobdell | 0:b3b7aff20a1d | 109 | MAX1704X::operator uint32_t() |
maclobdell | 0:b3b7aff20a1d | 110 | { |
maclobdell | 0:b3b7aff20a1d | 111 | //Return the current battery percentage |
maclobdell | 0:b3b7aff20a1d | 112 | return read_percent(); |
maclobdell | 0:b3b7aff20a1d | 113 | } |
maclobdell | 0:b3b7aff20a1d | 114 | //#endif |
maclobdell | 0:b3b7aff20a1d | 115 | */ |
maclobdell | 0:b3b7aff20a1d | 116 | char MAX1704X::read8(char reg) |
maclobdell | 0:b3b7aff20a1d | 117 | { |
maclobdell | 0:b3b7aff20a1d | 118 | //Select the register |
maclobdell | 0:b3b7aff20a1d | 119 | m_I2C.write(m_ADDR, ®, 1, true); |
maclobdell | 0:b3b7aff20a1d | 120 | |
maclobdell | 0:b3b7aff20a1d | 121 | //Read the 8-bit register |
maclobdell | 0:b3b7aff20a1d | 122 | m_I2C.read(m_ADDR, ®, 1); |
maclobdell | 0:b3b7aff20a1d | 123 | |
maclobdell | 0:b3b7aff20a1d | 124 | //Return the byte |
maclobdell | 0:b3b7aff20a1d | 125 | return reg; |
maclobdell | 0:b3b7aff20a1d | 126 | } |
maclobdell | 0:b3b7aff20a1d | 127 | |
maclobdell | 0:b3b7aff20a1d | 128 | void MAX1704X::write8(char reg, char data) |
maclobdell | 0:b3b7aff20a1d | 129 | { |
maclobdell | 0:b3b7aff20a1d | 130 | //Create a temporary buffer |
maclobdell | 0:b3b7aff20a1d | 131 | char buff[2]; |
maclobdell | 0:b3b7aff20a1d | 132 | |
maclobdell | 0:b3b7aff20a1d | 133 | //Load the register address and 8-bit data |
maclobdell | 0:b3b7aff20a1d | 134 | buff[0] = reg; |
maclobdell | 0:b3b7aff20a1d | 135 | buff[1] = data; |
maclobdell | 0:b3b7aff20a1d | 136 | |
maclobdell | 0:b3b7aff20a1d | 137 | //Write the data |
maclobdell | 0:b3b7aff20a1d | 138 | m_I2C.write(m_ADDR, buff, 2); |
maclobdell | 0:b3b7aff20a1d | 139 | } |
maclobdell | 0:b3b7aff20a1d | 140 | |
maclobdell | 0:b3b7aff20a1d | 141 | uint16_t MAX1704X::read16(char reg) |
maclobdell | 0:b3b7aff20a1d | 142 | { |
maclobdell | 0:b3b7aff20a1d | 143 | //Create a temporary buffer |
maclobdell | 0:b3b7aff20a1d | 144 | char buff[2]; |
maclobdell | 0:b3b7aff20a1d | 145 | |
maclobdell | 0:b3b7aff20a1d | 146 | //Select the register |
maclobdell | 0:b3b7aff20a1d | 147 | m_I2C.write(m_ADDR, ®, 1, true); |
maclobdell | 0:b3b7aff20a1d | 148 | |
maclobdell | 0:b3b7aff20a1d | 149 | //Read the 16-bit register |
maclobdell | 0:b3b7aff20a1d | 150 | m_I2C.read(m_ADDR, buff, 2); |
maclobdell | 0:b3b7aff20a1d | 151 | |
maclobdell | 0:b3b7aff20a1d | 152 | //Return the combined 16-bit value |
maclobdell | 0:b3b7aff20a1d | 153 | return (buff[0] << 8) | buff[1]; |
maclobdell | 0:b3b7aff20a1d | 154 | } |
maclobdell | 0:b3b7aff20a1d | 155 | |
maclobdell | 0:b3b7aff20a1d | 156 | void MAX1704X::write16(char reg, uint16_t data) |
maclobdell | 0:b3b7aff20a1d | 157 | { |
maclobdell | 0:b3b7aff20a1d | 158 | //Create a temporary buffer |
maclobdell | 0:b3b7aff20a1d | 159 | char buff[3]; |
maclobdell | 0:b3b7aff20a1d | 160 | |
maclobdell | 0:b3b7aff20a1d | 161 | //Load the register address and 16-bit data |
maclobdell | 0:b3b7aff20a1d | 162 | buff[0] = reg; |
maclobdell | 0:b3b7aff20a1d | 163 | buff[1] = data >> 8; |
maclobdell | 0:b3b7aff20a1d | 164 | buff[2] = data; |
maclobdell | 0:b3b7aff20a1d | 165 | |
maclobdell | 0:b3b7aff20a1d | 166 | //Write the data |
maclobdell | 0:b3b7aff20a1d | 167 | m_I2C.write(m_ADDR, buff, 3); |
maclobdell | 0:b3b7aff20a1d | 168 | } |