Library for I2C communication with LiPo battery fuel gauge to measure battery level.

Dependents:   MAX1704X_example_app

Committer:
maclobdell
Date:
Thu Nov 09 21:01:04 2017 +0000
Revision:
1:b2d54467330d
Parent:
0:b3b7aff20a1d
update read_percent to just read out high byte for percentage of battery charge

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef MAX1704X_H
maclobdell 0:b3b7aff20a1d 18 #define MAX1704X_H
maclobdell 0:b3b7aff20a1d 19
maclobdell 0:b3b7aff20a1d 20 #include "mbed.h"
maclobdell 0:b3b7aff20a1d 21
maclobdell 0:b3b7aff20a1d 22 /** MAX1704X class.
maclobdell 0:b3b7aff20a1d 23 * Used for reading MAX1704X lipo fuel guage connected via I2C.
maclobdell 0:b3b7aff20a1d 24 *
maclobdell 0:b3b7aff20a1d 25 * Example:
maclobdell 0:b3b7aff20a1d 26 * @code
maclobdell 0:b3b7aff20a1d 27 * #include "mbed.h"
maclobdell 0:b3b7aff20a1d 28 * #include "MAX1704X.h"
maclobdell 0:b3b7aff20a1d 29 *
maclobdell 0:b3b7aff20a1d 30 * //Create an MAX1704X object at the default address (ADDRESS_0)
maclobdell 0:b3b7aff20a1d 31 * MAX1704X battery_level(p28, p27);
maclobdell 0:b3b7aff20a1d 32 *
maclobdell 0:b3b7aff20a1d 33 * int main()
maclobdell 0:b3b7aff20a1d 34 * {
maclobdell 0:b3b7aff20a1d 35 * //Try to open the MAX1704X
maclobdell 0:b3b7aff20a1d 36 * if (battery_level.open()) {
maclobdell 0:b3b7aff20a1d 37 * printf("Device detected!\n");
maclobdell 0:b3b7aff20a1d 38 *
maclobdell 0:b3b7aff20a1d 39 * while (1) {
maclobdell 0:b3b7aff20a1d 40 * //Print the current battery level
maclobdell 0:b3b7aff20a1d 41 * printf("battery = %d\n", battery_level);
maclobdell 0:b3b7aff20a1d 42 *
maclobdell 0:b3b7aff20a1d 43 * //Sleep for 1 seconds
maclobdell 0:b3b7aff20a1d 44 * wait(1);
maclobdell 0:b3b7aff20a1d 45 * }
maclobdell 0:b3b7aff20a1d 46 * } else {
maclobdell 0:b3b7aff20a1d 47 * error("Device not detected!\n");
maclobdell 0:b3b7aff20a1d 48 * }
maclobdell 0:b3b7aff20a1d 49 * }
maclobdell 0:b3b7aff20a1d 50 * @endcode
maclobdell 0:b3b7aff20a1d 51 */
maclobdell 0:b3b7aff20a1d 52 class MAX1704X
maclobdell 0:b3b7aff20a1d 53 {
maclobdell 0:b3b7aff20a1d 54 public:
maclobdell 0:b3b7aff20a1d 55 /** Represents the different I2C address possibilities for the MAX1704X
maclobdell 0:b3b7aff20a1d 56 */
maclobdell 0:b3b7aff20a1d 57
maclobdell 0:b3b7aff20a1d 58 enum Address {
maclobdell 0:b3b7aff20a1d 59 ADDRESS_0 = (0x36 << 1) /* Slave Address 011 0110 + r/w bit */
maclobdell 0:b3b7aff20a1d 60 };
maclobdell 0:b3b7aff20a1d 61
maclobdell 0:b3b7aff20a1d 62 /** Create an MAX1704X object connected to the specified I2C pins with the specified I2C slave address
maclobdell 0:b3b7aff20a1d 63 *
maclobdell 0:b3b7aff20a1d 64 * @param sda The I2C data pin.
maclobdell 0:b3b7aff20a1d 65 * @param scl The I2C clock pin.
maclobdell 0:b3b7aff20a1d 66 * @param addr The I2C slave address (defaults to ADDRESS_0).
maclobdell 0:b3b7aff20a1d 67 * @param hz The I2C bus frequency (defaults to 400kHz).
maclobdell 0:b3b7aff20a1d 68 */
maclobdell 0:b3b7aff20a1d 69 MAX1704X(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000);
maclobdell 0:b3b7aff20a1d 70
maclobdell 0:b3b7aff20a1d 71 /** Probe for the MAX1704X and indicate if it's present on the bus
maclobdell 0:b3b7aff20a1d 72 *
maclobdell 0:b3b7aff20a1d 73 * @returns
maclobdell 0:b3b7aff20a1d 74 * 'true' if the device exists on the bus,
maclobdell 0:b3b7aff20a1d 75 * 'false' if the device doesn't exist on the bus.
maclobdell 0:b3b7aff20a1d 76 */
maclobdell 0:b3b7aff20a1d 77 bool open();
maclobdell 0:b3b7aff20a1d 78
maclobdell 0:b3b7aff20a1d 79 /** assert power on reset on MAX1704X
maclobdell 0:b3b7aff20a1d 80 *
maclobdell 0:b3b7aff20a1d 81 * @returns void
maclobdell 0:b3b7aff20a1d 82 */
maclobdell 0:b3b7aff20a1d 83 void power_on_reset(void);
maclobdell 0:b3b7aff20a1d 84
maclobdell 0:b3b7aff20a1d 85 /** Get the current ad of the MAX1704X
maclobdell 0:b3b7aff20a1d 86 *
maclobdell 0:b3b7aff20a1d 87 * @returns The current ad.
maclobdell 0:b3b7aff20a1d 88 */
maclobdell 0:b3b7aff20a1d 89 uint32_t read_ad();
maclobdell 0:b3b7aff20a1d 90
maclobdell 0:b3b7aff20a1d 91 /** Get the current config of the MAX1704X
maclobdell 0:b3b7aff20a1d 92 *
maclobdell 0:b3b7aff20a1d 93 * @returns The current config of the MAX170X.
maclobdell 0:b3b7aff20a1d 94 */
maclobdell 0:b3b7aff20a1d 95 uint16_t read_config();
maclobdell 0:b3b7aff20a1d 96
maclobdell 0:b3b7aff20a1d 97 /** Get the current battery charge percent from MAX1704X
maclobdell 0:b3b7aff20a1d 98 *
maclobdell 0:b3b7aff20a1d 99 * @returns The current battery charge percent from MAX1704X.
maclobdell 0:b3b7aff20a1d 100 */
maclobdell 0:b3b7aff20a1d 101 uint32_t read_percent();
maclobdell 0:b3b7aff20a1d 102
maclobdell 0:b3b7aff20a1d 103 //#ifdef MBED_OPERATORS
maclobdell 0:b3b7aff20a1d 104 /** A shorthand for percentage()
maclobdell 0:b3b7aff20a1d 105 *
maclobdell 0:b3b7aff20a1d 106 * @returns the current battery percentage measurement.
maclobdell 0:b3b7aff20a1d 107 */
maclobdell 0:b3b7aff20a1d 108 // operator uint32_t();
maclobdell 0:b3b7aff20a1d 109 //#endif
maclobdell 0:b3b7aff20a1d 110
maclobdell 0:b3b7aff20a1d 111 private:
maclobdell 0:b3b7aff20a1d 112 //I2C register addresses
maclobdell 0:b3b7aff20a1d 113 enum Register {
maclobdell 0:b3b7aff20a1d 114 REG_VCELL_H = 0x02,
maclobdell 0:b3b7aff20a1d 115 REG_VCELL_L = 0x03,
maclobdell 0:b3b7aff20a1d 116 REG_SOC_H = 0x04,
maclobdell 0:b3b7aff20a1d 117 REG_SOC_L = 0x05,
maclobdell 0:b3b7aff20a1d 118 REG_MODE_H = 0x06,
maclobdell 0:b3b7aff20a1d 119 REG_MODE_L = 0x07,
maclobdell 0:b3b7aff20a1d 120 REG_VER_H = 0x08,
maclobdell 0:b3b7aff20a1d 121 REG_VER_L = 0x09,
maclobdell 0:b3b7aff20a1d 122 REG_CONFIG_H = 0x0C,
maclobdell 0:b3b7aff20a1d 123 REG_CONFIG_L = 0x0D,
maclobdell 0:b3b7aff20a1d 124 REG_COMMAND_H = 0xFE,
maclobdell 0:b3b7aff20a1d 125 REG_COMMAND_L = 0xFF
maclobdell 0:b3b7aff20a1d 126 };
maclobdell 0:b3b7aff20a1d 127
maclobdell 0:b3b7aff20a1d 128 //Member variables
maclobdell 0:b3b7aff20a1d 129 I2C m_I2C;
maclobdell 0:b3b7aff20a1d 130 const int m_ADDR;
maclobdell 0:b3b7aff20a1d 131
maclobdell 0:b3b7aff20a1d 132 //Internal functions
maclobdell 0:b3b7aff20a1d 133 char read8(char reg);
maclobdell 0:b3b7aff20a1d 134 void write8(char reg, char data);
maclobdell 0:b3b7aff20a1d 135 uint16_t read16(char reg);
maclobdell 0:b3b7aff20a1d 136 void write16(char reg, uint16_t data);
maclobdell 0:b3b7aff20a1d 137 };
maclobdell 0:b3b7aff20a1d 138
maclobdell 0:b3b7aff20a1d 139 #endif