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 //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 1:b2d54467330d 83 //xl = read8(REG_SOC_L); //low byte
maclobdell 0:b3b7aff20a1d 84
maclobdell 1:b2d54467330d 85 //xo = (xl|(xm << 8));
maclobdell 1:b2d54467330d 86 xo = xm; //just read out high byte which returns 0-100
maclobdell 1:b2d54467330d 87 // low byte provides unecessary extra resolution
maclobdell 0:b3b7aff20a1d 88 //percent_decimal = (0.003906)*xl + xm;
maclobdell 0:b3b7aff20a1d 89
maclobdell 0:b3b7aff20a1d 90 //returns percent
maclobdell 0:b3b7aff20a1d 91 return xo;
maclobdell 0:b3b7aff20a1d 92
maclobdell 0:b3b7aff20a1d 93 }
maclobdell 0:b3b7aff20a1d 94
maclobdell 0:b3b7aff20a1d 95 void MAX1704X::power_on_reset(void)
maclobdell 0:b3b7aff20a1d 96 {
maclobdell 0:b3b7aff20a1d 97
maclobdell 0:b3b7aff20a1d 98 write8(REG_COMMAND_H, 0x54);
maclobdell 0:b3b7aff20a1d 99 write8(REG_COMMAND_L, 0x00);
maclobdell 0:b3b7aff20a1d 100
maclobdell 0:b3b7aff20a1d 101 //alternatively can write16
maclobdell 0:b3b7aff20a1d 102 //write16(REG_COMMAND_H, 0x5400); //does this work?
maclobdell 0:b3b7aff20a1d 103
maclobdell 0:b3b7aff20a1d 104 }
maclobdell 0:b3b7aff20a1d 105 /*
maclobdell 0:b3b7aff20a1d 106 //#ifdef MBED_OPERATORS //this no longer be needed?
maclobdell 0:b3b7aff20a1d 107 MAX1704X::operator uint32_t()
maclobdell 0:b3b7aff20a1d 108 {
maclobdell 0:b3b7aff20a1d 109 //Return the current battery percentage
maclobdell 0:b3b7aff20a1d 110 return read_percent();
maclobdell 0:b3b7aff20a1d 111 }
maclobdell 0:b3b7aff20a1d 112 //#endif
maclobdell 0:b3b7aff20a1d 113 */
maclobdell 0:b3b7aff20a1d 114 char MAX1704X::read8(char reg)
maclobdell 0:b3b7aff20a1d 115 {
maclobdell 0:b3b7aff20a1d 116 //Select the register
maclobdell 0:b3b7aff20a1d 117 m_I2C.write(m_ADDR, &reg, 1, true);
maclobdell 0:b3b7aff20a1d 118
maclobdell 0:b3b7aff20a1d 119 //Read the 8-bit register
maclobdell 0:b3b7aff20a1d 120 m_I2C.read(m_ADDR, &reg, 1);
maclobdell 0:b3b7aff20a1d 121
maclobdell 0:b3b7aff20a1d 122 //Return the byte
maclobdell 0:b3b7aff20a1d 123 return reg;
maclobdell 0:b3b7aff20a1d 124 }
maclobdell 0:b3b7aff20a1d 125
maclobdell 0:b3b7aff20a1d 126 void MAX1704X::write8(char reg, char data)
maclobdell 0:b3b7aff20a1d 127 {
maclobdell 0:b3b7aff20a1d 128 //Create a temporary buffer
maclobdell 0:b3b7aff20a1d 129 char buff[2];
maclobdell 0:b3b7aff20a1d 130
maclobdell 0:b3b7aff20a1d 131 //Load the register address and 8-bit data
maclobdell 0:b3b7aff20a1d 132 buff[0] = reg;
maclobdell 0:b3b7aff20a1d 133 buff[1] = data;
maclobdell 0:b3b7aff20a1d 134
maclobdell 0:b3b7aff20a1d 135 //Write the data
maclobdell 0:b3b7aff20a1d 136 m_I2C.write(m_ADDR, buff, 2);
maclobdell 0:b3b7aff20a1d 137 }
maclobdell 0:b3b7aff20a1d 138
maclobdell 0:b3b7aff20a1d 139 uint16_t MAX1704X::read16(char reg)
maclobdell 0:b3b7aff20a1d 140 {
maclobdell 0:b3b7aff20a1d 141 //Create a temporary buffer
maclobdell 0:b3b7aff20a1d 142 char buff[2];
maclobdell 0:b3b7aff20a1d 143
maclobdell 0:b3b7aff20a1d 144 //Select the register
maclobdell 0:b3b7aff20a1d 145 m_I2C.write(m_ADDR, &reg, 1, true);
maclobdell 0:b3b7aff20a1d 146
maclobdell 0:b3b7aff20a1d 147 //Read the 16-bit register
maclobdell 0:b3b7aff20a1d 148 m_I2C.read(m_ADDR, buff, 2);
maclobdell 0:b3b7aff20a1d 149
maclobdell 0:b3b7aff20a1d 150 //Return the combined 16-bit value
maclobdell 0:b3b7aff20a1d 151 return (buff[0] << 8) | buff[1];
maclobdell 0:b3b7aff20a1d 152 }
maclobdell 0:b3b7aff20a1d 153
maclobdell 0:b3b7aff20a1d 154 void MAX1704X::write16(char reg, uint16_t data)
maclobdell 0:b3b7aff20a1d 155 {
maclobdell 0:b3b7aff20a1d 156 //Create a temporary buffer
maclobdell 0:b3b7aff20a1d 157 char buff[3];
maclobdell 0:b3b7aff20a1d 158
maclobdell 0:b3b7aff20a1d 159 //Load the register address and 16-bit data
maclobdell 0:b3b7aff20a1d 160 buff[0] = reg;
maclobdell 0:b3b7aff20a1d 161 buff[1] = data >> 8;
maclobdell 0:b3b7aff20a1d 162 buff[2] = data;
maclobdell 0:b3b7aff20a1d 163
maclobdell 0:b3b7aff20a1d 164 //Write the data
maclobdell 0:b3b7aff20a1d 165 m_I2C.write(m_ADDR, buff, 3);
maclobdell 0:b3b7aff20a1d 166 }