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.cpp
- Revision:
- 5:ffce4fe12ed1
- Parent:
- 4:e61b2723d2cf
- Child:
- 6:6927c72b2923
--- a/MAX17048.cpp Mon Sep 16 22:02:40 2013 +0000 +++ b/MAX17048.cpp Thu Nov 07 17:53:44 2013 +0000 @@ -18,10 +18,11 @@ MAX17048::MAX17048(PinName sda, PinName scl) : m_I2C(sda, scl) { - //Nothing else to initialize + //Set the I2C bus frequency to 400kHz + m_I2C.frequency(400000); } -bool MAX17048::open(void) +bool MAX17048::open() { //Probe for the MAX17048 using a Zero Length Transfer if (!m_I2C.write(m_ADDR, NULL, 0)) { @@ -36,13 +37,13 @@ } } -void MAX17048::reset(void) +void MAX17048::reset() { //Write the POR command write(REG_CMD, 0x5400); } -void MAX17048::quickStart(void) +void MAX17048::quickStart() { //Read the current 16-bit register value unsigned short value = read(REG_MODE); @@ -54,7 +55,7 @@ write(REG_MODE, value); } -bool MAX17048::sleepEnabled(void) +bool MAX17048::sleepEnabled() { //Read the 16-bit register value unsigned short value = read(REG_MODE); @@ -81,7 +82,7 @@ write(REG_MODE, value); } -bool MAX17048::hibernating(void) +bool MAX17048::hibernating() { //Read the 16-bit register value unsigned short value = read(REG_MODE); @@ -93,7 +94,7 @@ return false; } -float MAX17048::hibernateThreshold(void) +float MAX17048::hibernateThreshold() { //Read the 16-bit register value unsigned short value = read(REG_HIBRT); @@ -122,7 +123,7 @@ write(REG_HIBRT, value); } -float MAX17048::activeThreshold(void) +float MAX17048::activeThreshold() { //Read the 16-bit register value unsigned short value = read(REG_HIBRT); @@ -151,7 +152,7 @@ write(REG_HIBRT, value); } -unsigned short MAX17048::version(void) +unsigned short MAX17048::version() { //Return the 16-bit production version return read(REG_VERSION); @@ -171,7 +172,7 @@ writeRCOMP(rcomp); } -bool MAX17048::sleeping(void) +bool MAX17048::sleeping() { //Read the 16-bit register value unsigned short value = read(REG_CONFIG); @@ -198,7 +199,7 @@ write(REG_CONFIG, value); } -bool MAX17048::socChangeAlertEnabled(void) +bool MAX17048::socChangeAlertEnabled() { //Read the 16-bit register value unsigned short value = read(REG_CONFIG); @@ -225,7 +226,7 @@ write(REG_CONFIG, value); } -bool MAX17048::alerting(void) +bool MAX17048::alerting() { //Read the 16-bit register value unsigned short value = read(REG_CONFIG); @@ -237,7 +238,7 @@ return false; } -void MAX17048::clearAlert(void) +void MAX17048::clearAlert() { //Read the current 16-bit register value unsigned short value = read(REG_CONFIG); @@ -249,7 +250,7 @@ write(REG_CONFIG, value); } -char MAX17048::emptyAlertThreshold(void) +char MAX17048::emptyAlertThreshold() { //Read the 16-bit register value unsigned short value = read(REG_CONFIG); @@ -277,7 +278,7 @@ write(REG_CONFIG, value); } -float MAX17048::vAlertMinThreshold(void) +float MAX17048::vAlertMinThreshold() { //Read the 16-bit register value unsigned short value = read(REG_VALRT); @@ -306,7 +307,7 @@ write(REG_VALRT, value); } -float MAX17048::vAlertMaxThreshold(void) +float MAX17048::vAlertMaxThreshold() { //Read the 16-bit register value unsigned short value = read(REG_VALRT); @@ -335,7 +336,7 @@ write(REG_VALRT, value); } -float MAX17048::vResetThreshold(void) +float MAX17048::vResetThreshold() { //Read the 16-bit register value unsigned short value = read(REG_VRESET_ID); @@ -364,7 +365,7 @@ write(REG_VRESET_ID, value); } -bool MAX17048::comparatorEnabled(void) +bool MAX17048::comparatorEnabled() { //Read the 16-bit register value unsigned short value = read(REG_VRESET_ID); @@ -391,7 +392,7 @@ write(REG_VRESET_ID, value); } -char MAX17048::id(void) +char MAX17048::id() { //Read the 16-bit register value unsigned short value = read(REG_VRESET_ID); @@ -400,7 +401,7 @@ return value; } -bool MAX17048::vResetAlertEnabled(void) +bool MAX17048::vResetAlertEnabled() { //Read the 16-bit register value unsigned short value = read(REG_STATUS); @@ -427,7 +428,7 @@ write(REG_STATUS, value); } -char MAX17048::alertFlags(void) +char MAX17048::alertFlags() { //Read the 16-bit register value unsigned short value = read(REG_STATUS); @@ -448,7 +449,7 @@ write(REG_STATUS, value); } -float MAX17048::vcell(void) +float MAX17048::vcell() { //Read the 16-bit raw Vcell value unsigned short value = read(REG_VCELL); @@ -457,7 +458,7 @@ return value * 0.000078125; } -float MAX17048::soc(void) +float MAX17048::soc() { //Read the 16-bit raw SOC value unsigned short value = read(REG_SOC); @@ -466,7 +467,7 @@ return value * 0.00390625; } -int MAX17048::socInt(void) +int MAX17048::soc_int() { //Read the 16-bit raw SOC value unsigned short value = read(REG_SOC); @@ -475,7 +476,7 @@ return value >> 8; } -float MAX17048::crate(void) +float MAX17048::crate() { //Read the 16-bit raw C/Rate value short value = read(REG_CRATE); @@ -484,6 +485,18 @@ return value * 0.208; } +MAX17048::operator float() +{ + //Return the current floating point SOC reading + return soc(); +} + +MAX17048::operator int() +{ + //Return the current integer SOC reading + return soc_int(); +} + unsigned short MAX17048::read(char reg) { //Create a temporary buffer