A feature complete driver for the MAX17048 lithium fuel gauge from Maxim.
Dependents: MAX17048_HelloWorld ECGAFE_copy MAX17048_HelloWorld Orion_newPCB_test_LV ... more
Now fully tested!
Diff: MAX17048.h
- Revision:
- 2:0a98e081b48c
- Parent:
- 1:734b1a089a9c
- Child:
- 3:32087cca331f
--- a/MAX17048.h Fri Aug 09 22:22:48 2013 +0000 +++ b/MAX17048.h Wed Aug 14 04:52:16 2013 +0000 @@ -19,26 +19,6 @@ #include "mbed.h" -//i2c address definition -#define __MAX17048_ADDR (0x36 << 1) - -//i2c register definitions -#define __MAX17048_REG_VCELL 0x02 -#define __MAX17048_REG_SOC 0x04 -#define __MAX17048_REG_MODE 0x06 -#define __MAX17048_REG_VERSION 0x08 -#define __MAX17048_REG_HIBRT 0x0A -#define __MAX17048_REG_CONFIG 0x0C -#define __MAX17048_REG_VALRT 0x14 -#define __MAX17048_REG_CRATE 0x16 -#define __MAX17048_REG_VRESET_ID 0x18 -#define __MAX17048_REG_STATUS 0x1A -#define __MAX17048_REG_TABLE 0x40 -#define __MAX17048_REG_CMD 0xFE - -//RCOMP0 value definition -#define __MAX17048_RCOMP0 0x97 - /** MAX17048 class. * Used for controlling a MAX17048 fuel gauge connected via I2C. * @@ -47,16 +27,15 @@ * #include "mbed.h" * #include "MAX17048.h" * - * Serial pc(USBTX, USBRX); * MAX17048 gauge(p28, p27); * * int main() { * while (1) { * //Read the cell voltage - * float vcell = gauge.getVcell(); + * float vcell = gauge.vcell(); * * //Print the cell voltage - * pc.printf("Vcell = %f\n", vcell); + * printf("Vcell = %f\n", vcell); * * //Sleep for 0.5 seconds * wait(0.5); @@ -99,186 +78,186 @@ * 'true' if sleep mode is enabled, * 'false' if sleep mode is disabled. */ - bool isSleepEnabled(void); + bool sleepEnabled(void); /** Enable or disable sleep mode on the MAX17048 * * @param enabled Whether or not sleep mode is enabled. */ - void setSleepEnabled(bool enabled); - + void sleepEnabled(bool enabled); + /** Determine whether or not the MAX17048 is hibernating * * @returns * 'true' if hibernating, * 'false' if not hibernating. */ - bool isHibernating(void); - + bool hibernating(void); + /** Get the current hibernate threshold of the MAX17048 * * @returns The current hibernate threshold in \%/hr. */ - float getHibernateThreshold(void); - + float hibernateThreshold(void); + /** Set the hibernate threshold of the MAX17048 * * @param threshold The new hibernate threshold in \%/hr. */ - void setHibernateThreshold(float threshold); - + void hibernateThreshold(float threshold); + /** Get the current active threshold of the MAX17048 * * @returns The current active threshold in volts. */ - float getActiveThreshold(void); - + float activeThreshold(void); + /** Set the active threshold of the MAX17048 * * @param threshold The new active threshold in volts. */ - void setActiveThreshold(float threshold); - + void activeThreshold(float threshold); + /** Get the production version of the MAX17048 * * @returns The 16-bit production version. */ - unsigned short getVersion(void); - + unsigned short version(void); + /** Set the cell temperature compensation of the MAX17048 * * @param temp The current cell temperature in °C. */ - void setTempCompensation(float temp); - + void tempCompensation(float temp); + /** Determine whether or not the MAX17048 is in sleep mode * * @returns * 'true' if in sleep mode, * 'false' if not in sleep mode. */ - bool isSleeping(void); - + bool sleeping(void); + /** Enter or exit sleep mode on the MAX17048 (sleep mode must be enabled first) * * @param sleep Whether or not to sleep. */ - void setSleep(bool sleep); - + void sleep(bool sleep); + /** Determine whether or not the SOC 1% change alert is enabled on the MAX17048 * * @returns * 'true' if enabled, * 'false' if not enabled. */ - bool isSOCChangeAlertEnabled(void); - + bool socChangeAlertEnabled(void); + /** Enable or disable the SOC 1% change alert on the MAX17048 * * @param enabled Whether or the SOC 1% change alert is enabled. */ - void setSOCChangeAlertEnabled(bool enabled); - + void socChangeAlertEnabled(bool enabled); + /** Determine whether or not the MAX17048 is asserting the ALRT pin * * @returns * 'true' if alerting, * 'false' if not alerting. */ - bool isAlerting(void); - + bool alerting(void); + /** Command the MAX17048 to de-assert the ALRT pin */ void clearAlert(void); - + /** Get the current SOC empty alert threshold of the MAX17048 * * @returns The current SOC empty alert threshold in %. */ - char getEmptyAlertThreshold(void); - + char emptyAlertThreshold(void); + /** Set the SOC empty alert threshold of the MAX17048 * * @param threshold The new SOC empty alert threshold in %. */ - void setEmptyAlertThreshold(char threshold); - + void emptyAlertThreshold(char threshold); + /** Get the current low voltage alert threshold of the MAX17048 * * @returns The current low voltage alert threshold in volts. */ - float getVAlertMinThreshold(void); - + float vAlertMinThreshold(void); + /** Set the low voltage alert threshold of the MAX17048 * * @param threshold The new low voltage alert threshold in volts. */ - void setVAlertMinThreshold(float threshold); - + void vAlertMinThreshold(float threshold); + /** Get the current high voltage alert threshold of the MAX17048 * * @returns The current high voltage alert threshold in volts. */ - float getVAlertMaxThreshold(void); - + float vAlertMaxThreshold(void); + /** Set the high voltage alert threshold of the MAX17048 * * @param threshold The new high voltage alert threshold in volts. */ - void setVAlertMaxThreshold(float threshold); - + void vAlertMaxThreshold(float threshold); + /** Get the current reset voltage threshold of the MAX17048 * * @returns The current reset voltage threshold in volts. */ - float getVResetThreshold(void); - + float vResetThreshold(void); + /** Set the reset voltage threshold of the MAX17048 * * @param threshold The new reset voltage threshold in volts. */ - void setVResetThreshold(float threshold); - + void vResetThreshold(float threshold); + /** Determine whether or not the reset voltage comparator is enabled on the MAX17048 * * @returns * 'true' if enabled, * 'false' if not enabled. */ - bool isComparatorEnabled(void); - + bool comparatorEnabled(void); + /** Enable or disable the reset voltage comparator on the MAX17048 * * @param enabled Whether or not the reset voltage comparator is enabled. */ - void setComparatorEnabled(bool enabled); - + void comparatorEnabled(bool enabled); + /** Get the factory programmed 8-bit ID of the MAX17048 * * @returns The 8-bit ID. */ - char getID(void); - + char id(void); + /** Determine whether or not the voltage reset alert is enabled on the MAX17048 * * @returns * 'true' if enabled, * 'false' if not enabled. */ - bool isVResetAlertEnabled(void); - + bool vResetAlertEnabled(void); + /** Enable or disable the voltage reset alert on the MAX17048 * * @param enabled Whether or the voltage reset alert is enabled. */ - void setVResetAlertEnabled(bool enabled); - + void vResetAlertEnabled(bool enabled); + /** Get the current alert flags on the MAX17048 * * @returns The current alert flags as AlertFlags enum values OR'd together. */ - char getAlertFlags(void); - + char alertFlags(void); + /** Clear the specified alert flags on the MAX17048 * * @param flags The alert flags to clear as AlertFlags enum values OR'd together. @@ -289,25 +268,48 @@ * * @returns The cell voltage measurement as a float. */ - float getVcell(void); + float vcell(void); /** Get the current state of charge measurement of the MAX17048 * * @returns The state of charge measurement as a float. */ - float getSOC(void); + float soc(void); /** Get the current C rate measurement of the MAX17048 * * @returns The C rate measurement as a float. */ - float getCRate(void); + float crate(void); private: - I2C _i2c; - unsigned short _read(char reg); - void _write(char reg, unsigned short data); - void _writeRCOMP(char rcomp); + //I2C register addresses + enum Register { + REG_VCELL = 0x02, + REG_SOC = 0x04, + REG_MODE = 0x06, + REG_VERSION = 0x08, + REG_HIBRT = 0x0A, + REG_CONFIG = 0x0C, + REG_VALRT = 0x14, + REG_CRATE = 0x16, + REG_VRESET_ID = 0x18, + REG_STATUS = 0x1A, + REG_TABLE = 0x40, + REG_CMD = 0xFE + }; + + //Member constants + static const int m_ADDR = (0x36 << 1); + static const int m_RCOMP0 = 0x97; + + //Member variables + I2C m_I2C; + + //Internal functions + unsigned short read(char reg); + void write(char reg, unsigned short data); + void writeRCOMP(char rcomp); }; #endif