Mac Lobdell / MAX1704X

Dependents:   MAX1704X_example_app

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX1704X.cpp Source File

MAX1704X.cpp

00001 /* MAX1704X Simple Driver Library
00002  * Copyright (c) 2017 Mac Lobdell
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 //todo: configure pin interrupt on battery level alert
00018 
00019 #include "MAX1704X.h"
00020 
00021 MAX1704X::MAX1704X(PinName sda, PinName scl, Address addr, int hz) : m_I2C(sda, scl), m_ADDR((int)addr)
00022 {
00023     //Set the I2C bus frequency
00024     m_I2C.frequency(hz);
00025 
00026     write16(REG_CONFIG_H,0x9700);  //set alert to 32%
00027     
00028 }
00029 
00030 bool MAX1704X::open()
00031 {
00032     //Probe for the I2C Device using a Zero Length Transfer
00033     if (!m_I2C.write(m_ADDR, NULL, 0)) {
00034         //Return success
00035         return true;
00036     } else {
00037         //Return failure
00038         return false;
00039     }
00040 }
00041 
00042 uint32_t MAX1704X::read_ad()
00043 {
00044   char xm, xl;
00045   uint32_t temp, xo;
00046   
00047   xm = read8(REG_VCELL_H);  //high byte
00048   xl = read8(REG_VCELL_L);  //low byte
00049   
00050   temp = (xl|(xm << 8));
00051   //alternatively can read16
00052  //xo = read16(REG_VCELL_H);    //does this work?
00053   
00054   xo = 1.25* (temp >>4);
00055 
00056   //returns A/D value in mV
00057   return xo;
00058   
00059 }
00060   
00061 uint16_t MAX1704X::read_config()
00062 {
00063   char xm, xl;
00064   uint16_t xo;
00065   
00066   xm = read8(REG_CONFIG_H);   //high byte 
00067   xl = read8(REG_CONFIG_L); //low byte
00068 
00069   xo = xl|(xm << 8);
00070 
00071   //alternatively can read16
00072 //  xo = read16(REG_CONFIG_H);    //does this work?
00073   
00074   return xo;  
00075 }
00076 
00077 uint32_t MAX1704X::read_percent()
00078 {
00079   char xm, xl;
00080   uint32_t xo;
00081   
00082   xm = read8(REG_SOC_H);  //high byte
00083   //xl = read8(REG_SOC_L);  //low byte
00084   
00085   //xo = (xl|(xm << 8));
00086   xo = xm;  //just read out high byte which returns 0-100
00087             // low byte provides unecessary extra resolution
00088   //percent_decimal = (0.003906)*xl + xm;
00089 
00090   //returns percent
00091   return xo;
00092     
00093 }
00094 
00095  void MAX1704X::power_on_reset(void)
00096  {
00097 
00098    write8(REG_COMMAND_H, 0x54);
00099    write8(REG_COMMAND_L, 0x00);
00100 
00101   //alternatively can write16
00102   //write16(REG_COMMAND_H, 0x5400);  //does this work?
00103   
00104  }
00105 /*
00106 //#ifdef MBED_OPERATORS  //this no longer be needed? 
00107 MAX1704X::operator uint32_t()
00108 {
00109     //Return the current battery percentage
00110     return read_percent();
00111 }
00112 //#endif
00113 */
00114 char MAX1704X::read8(char reg)
00115 {
00116     //Select the register
00117     m_I2C.write(m_ADDR, &reg, 1, true);
00118 
00119     //Read the 8-bit register
00120     m_I2C.read(m_ADDR, &reg, 1);
00121 
00122     //Return the byte
00123     return reg;
00124 }
00125 
00126 void MAX1704X::write8(char reg, char data)
00127 {
00128     //Create a temporary buffer
00129     char buff[2];
00130 
00131     //Load the register address and 8-bit data
00132     buff[0] = reg;
00133     buff[1] = data;
00134 
00135     //Write the data
00136     m_I2C.write(m_ADDR, buff, 2);
00137 }
00138 
00139 uint16_t MAX1704X::read16(char reg)
00140 {
00141     //Create a temporary buffer
00142     char buff[2];
00143 
00144     //Select the register
00145     m_I2C.write(m_ADDR, &reg, 1, true);
00146 
00147     //Read the 16-bit register
00148     m_I2C.read(m_ADDR, buff, 2);
00149 
00150     //Return the combined 16-bit value
00151     return (buff[0] << 8) | buff[1];
00152 }
00153 
00154 void MAX1704X::write16(char reg, uint16_t data)
00155 {
00156     //Create a temporary buffer
00157     char buff[3];
00158 
00159     //Load the register address and 16-bit data
00160     buff[0] = reg;
00161     buff[1] = data >> 8;
00162     buff[2] = data;
00163 
00164     //Write the data
00165     m_I2C.write(m_ADDR, buff, 3);
00166 }