Library for I2C communication with LiPo battery fuel gauge to measure battery level.
Dependents: MAX1704X_example_app
Revision 0:b3b7aff20a1d, committed 2017-10-09
- Comitter:
- maclobdell
- Date:
- Mon Oct 09 12:52:15 2017 +0000
- Child:
- 1:b2d54467330d
- Commit message:
- initial version, haven't tested yet
Changed in this revision
| MAX1704X.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MAX1704X.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX1704X.cpp Mon Oct 09 12:52:15 2017 +0000
@@ -0,0 +1,168 @@
+/* MAX1704X Simple Driver Library
+ * Copyright (c) 2017 Mac Lobdell
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//todo: configure pin interrupt on battery level alert
+
+#include "MAX1704X.h"
+
+MAX1704X::MAX1704X(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
+{
+ //Set the I2C bus frequency
+ m_I2C.frequency(hz);
+
+ write16(REG_CONFIG_H,0x9700); //set alert to 32%
+
+}
+
+bool MAX1704X::open()
+{
+ //Probe for the I2C Device using a Zero Length Transfer
+ if (!m_I2C.write(m_ADDR, NULL, 0)) {
+ //Return success
+ return true;
+ } else {
+ //Return failure
+ return false;
+ }
+}
+
+uint32_t MAX1704X::read_ad()
+{
+ char xm, xl;
+ uint32_t temp, xo;
+
+ xm = read8(REG_VCELL_H); //high byte
+ xl = read8(REG_VCELL_L); //low byte
+
+ temp = (xl|(xm << 8));
+ //alternatively can read16
+ //xo = read16(REG_VCELL_H); //does this work?
+
+ xo = 1.25* (temp >>4);
+
+ //returns A/D value in mV
+ return xo;
+
+}
+
+uint16_t MAX1704X::read_config()
+{
+ char xm, xl;
+ uint16_t xo;
+
+ xm = read8(REG_CONFIG_H); //high byte
+ xl = read8(REG_CONFIG_L); //low byte
+
+ xo = xl|(xm << 8);
+
+ //alternatively can read16
+// xo = read16(REG_CONFIG_H); //does this work?
+
+ return xo;
+}
+
+uint32_t MAX1704X::read_percent()
+{
+ char xm, xl;
+ uint32_t xo;
+
+ xm = read8(REG_SOC_H); //high byte
+ xl = read8(REG_SOC_L); //low byte
+
+ xo = (xl|(xm << 8));
+
+ //alternatively can read16
+// xo = read16(REG_SOC_H); //does this work?
+
+ //percent_decimal = (0.003906)*xl + xm;
+
+ //returns percent
+ return xo;
+
+}
+
+ void MAX1704X::power_on_reset(void)
+ {
+
+ write8(REG_COMMAND_H, 0x54);
+ write8(REG_COMMAND_L, 0x00);
+
+ //alternatively can write16
+ //write16(REG_COMMAND_H, 0x5400); //does this work?
+
+ }
+/*
+//#ifdef MBED_OPERATORS //this no longer be needed?
+MAX1704X::operator uint32_t()
+{
+ //Return the current battery percentage
+ return read_percent();
+}
+//#endif
+*/
+char MAX1704X::read8(char reg)
+{
+ //Select the register
+ m_I2C.write(m_ADDR, ®, 1, true);
+
+ //Read the 8-bit register
+ m_I2C.read(m_ADDR, ®, 1);
+
+ //Return the byte
+ return reg;
+}
+
+void MAX1704X::write8(char reg, char data)
+{
+ //Create a temporary buffer
+ char buff[2];
+
+ //Load the register address and 8-bit data
+ buff[0] = reg;
+ buff[1] = data;
+
+ //Write the data
+ m_I2C.write(m_ADDR, buff, 2);
+}
+
+uint16_t MAX1704X::read16(char reg)
+{
+ //Create a temporary buffer
+ char buff[2];
+
+ //Select the register
+ m_I2C.write(m_ADDR, ®, 1, true);
+
+ //Read the 16-bit register
+ m_I2C.read(m_ADDR, buff, 2);
+
+ //Return the combined 16-bit value
+ return (buff[0] << 8) | buff[1];
+}
+
+void MAX1704X::write16(char reg, uint16_t data)
+{
+ //Create a temporary buffer
+ char buff[3];
+
+ //Load the register address and 16-bit data
+ buff[0] = reg;
+ buff[1] = data >> 8;
+ buff[2] = data;
+
+ //Write the data
+ m_I2C.write(m_ADDR, buff, 3);
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MAX1704X.h Mon Oct 09 12:52:15 2017 +0000
@@ -0,0 +1,139 @@
+/* MAX1704X Simple Driver Library
+ * Copyright (c) 2017 Mac Lobdell
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef MAX1704X_H
+#define MAX1704X_H
+
+#include "mbed.h"
+
+/** MAX1704X class.
+ * Used for reading MAX1704X lipo fuel guage connected via I2C.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "MAX1704X.h"
+ *
+ * //Create an MAX1704X object at the default address (ADDRESS_0)
+ * MAX1704X battery_level(p28, p27);
+ *
+ * int main()
+ * {
+ * //Try to open the MAX1704X
+ * if (battery_level.open()) {
+ * printf("Device detected!\n");
+ *
+ * while (1) {
+ * //Print the current battery level
+ * printf("battery = %d\n", battery_level);
+ *
+ * //Sleep for 1 seconds
+ * wait(1);
+ * }
+ * } else {
+ * error("Device not detected!\n");
+ * }
+ * }
+ * @endcode
+ */
+class MAX1704X
+{
+public:
+ /** Represents the different I2C address possibilities for the MAX1704X
+ */
+
+ enum Address {
+ ADDRESS_0 = (0x36 << 1) /* Slave Address 011 0110 + r/w bit */
+ };
+
+ /** Create an MAX1704X object connected to the specified I2C pins with the specified I2C slave address
+ *
+ * @param sda The I2C data pin.
+ * @param scl The I2C clock pin.
+ * @param addr The I2C slave address (defaults to ADDRESS_0).
+ * @param hz The I2C bus frequency (defaults to 400kHz).
+ */
+ MAX1704X(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000);
+
+ /** Probe for the MAX1704X and indicate if it's present on the bus
+ *
+ * @returns
+ * 'true' if the device exists on the bus,
+ * 'false' if the device doesn't exist on the bus.
+ */
+ bool open();
+
+ /** assert power on reset on MAX1704X
+ *
+ * @returns void
+ */
+ void power_on_reset(void);
+
+ /** Get the current ad of the MAX1704X
+ *
+ * @returns The current ad.
+ */
+ uint32_t read_ad();
+
+ /** Get the current config of the MAX1704X
+ *
+ * @returns The current config of the MAX170X.
+ */
+ uint16_t read_config();
+
+ /** Get the current battery charge percent from MAX1704X
+ *
+ * @returns The current battery charge percent from MAX1704X.
+ */
+ uint32_t read_percent();
+
+//#ifdef MBED_OPERATORS
+ /** A shorthand for percentage()
+ *
+ * @returns the current battery percentage measurement.
+ */
+// operator uint32_t();
+//#endif
+
+private:
+ //I2C register addresses
+ enum Register {
+ REG_VCELL_H = 0x02,
+ REG_VCELL_L = 0x03,
+ REG_SOC_H = 0x04,
+ REG_SOC_L = 0x05,
+ REG_MODE_H = 0x06,
+ REG_MODE_L = 0x07,
+ REG_VER_H = 0x08,
+ REG_VER_L = 0x09,
+ REG_CONFIG_H = 0x0C,
+ REG_CONFIG_L = 0x0D,
+ REG_COMMAND_H = 0xFE,
+ REG_COMMAND_L = 0xFF
+ };
+
+ //Member variables
+ I2C m_I2C;
+ const int m_ADDR;
+
+ //Internal functions
+ char read8(char reg);
+ void write8(char reg, char data);
+ uint16_t read16(char reg);
+ void write16(char reg, uint16_t data);
+};
+
+#endif