Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MAX1704X_example_app
MAX1704X.h
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 #ifndef MAX1704X_H 00018 #define MAX1704X_H 00019 00020 #include "mbed.h" 00021 00022 /** MAX1704X class. 00023 * Used for reading MAX1704X lipo fuel guage connected via I2C. 00024 * 00025 * Example: 00026 * @code 00027 * #include "mbed.h" 00028 * #include "MAX1704X.h" 00029 * 00030 * //Create an MAX1704X object at the default address (ADDRESS_0) 00031 * MAX1704X battery_level(p28, p27); 00032 * 00033 * int main() 00034 * { 00035 * //Try to open the MAX1704X 00036 * if (battery_level.open()) { 00037 * printf("Device detected!\n"); 00038 * 00039 * while (1) { 00040 * //Print the current battery level 00041 * printf("battery = %d\n", battery_level); 00042 * 00043 * //Sleep for 1 seconds 00044 * wait(1); 00045 * } 00046 * } else { 00047 * error("Device not detected!\n"); 00048 * } 00049 * } 00050 * @endcode 00051 */ 00052 class MAX1704X 00053 { 00054 public: 00055 /** Represents the different I2C address possibilities for the MAX1704X 00056 */ 00057 00058 enum Address { 00059 ADDRESS_0 = (0x36 << 1) /* Slave Address 011 0110 + r/w bit */ 00060 }; 00061 00062 /** Create an MAX1704X object connected to the specified I2C pins with the specified I2C slave address 00063 * 00064 * @param sda The I2C data pin. 00065 * @param scl The I2C clock pin. 00066 * @param addr The I2C slave address (defaults to ADDRESS_0). 00067 * @param hz The I2C bus frequency (defaults to 400kHz). 00068 */ 00069 MAX1704X(PinName sda, PinName scl, Address addr = ADDRESS_0, int hz = 400000); 00070 00071 /** Probe for the MAX1704X and indicate if it's present on the bus 00072 * 00073 * @returns 00074 * 'true' if the device exists on the bus, 00075 * 'false' if the device doesn't exist on the bus. 00076 */ 00077 bool open(); 00078 00079 /** assert power on reset on MAX1704X 00080 * 00081 * @returns void 00082 */ 00083 void power_on_reset(void); 00084 00085 /** Get the current ad of the MAX1704X 00086 * 00087 * @returns The current ad. 00088 */ 00089 uint32_t read_ad(); 00090 00091 /** Get the current config of the MAX1704X 00092 * 00093 * @returns The current config of the MAX170X. 00094 */ 00095 uint16_t read_config(); 00096 00097 /** Get the current battery charge percent from MAX1704X 00098 * 00099 * @returns The current battery charge percent from MAX1704X. 00100 */ 00101 uint32_t read_percent(); 00102 00103 //#ifdef MBED_OPERATORS 00104 /** A shorthand for percentage() 00105 * 00106 * @returns the current battery percentage measurement. 00107 */ 00108 // operator uint32_t(); 00109 //#endif 00110 00111 private: 00112 //I2C register addresses 00113 enum Register { 00114 REG_VCELL_H = 0x02, 00115 REG_VCELL_L = 0x03, 00116 REG_SOC_H = 0x04, 00117 REG_SOC_L = 0x05, 00118 REG_MODE_H = 0x06, 00119 REG_MODE_L = 0x07, 00120 REG_VER_H = 0x08, 00121 REG_VER_L = 0x09, 00122 REG_CONFIG_H = 0x0C, 00123 REG_CONFIG_L = 0x0D, 00124 REG_COMMAND_H = 0xFE, 00125 REG_COMMAND_L = 0xFF 00126 }; 00127 00128 //Member variables 00129 I2C m_I2C; 00130 const int m_ADDR; 00131 00132 //Internal functions 00133 char read8(char reg); 00134 void write8(char reg, char data); 00135 uint16_t read16(char reg); 00136 void write16(char reg, uint16_t data); 00137 }; 00138 00139 #endif
Generated on Sun Jul 17 2022 14:24:55 by
1.7.2