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:
- 2:0a98e081b48c
- Parent:
- 0:abc480f8eeab
- Child:
- 3:32087cca331f
--- a/MAX17048.cpp Fri Aug 09 22:22:48 2013 +0000 +++ b/MAX17048.cpp Wed Aug 14 04:52:16 2013 +0000 @@ -17,7 +17,7 @@ #include "MAX17048.h" #include "mbed.h" -MAX17048::MAX17048(PinName sda, PinName scl) : _i2c(sda, scl) +MAX17048::MAX17048(PinName sda, PinName scl) : m_I2C(sda, scl) { //Nothing else to initialize } @@ -25,25 +25,25 @@ void MAX17048::reset(void) { //Write the POR command - _write(__MAX17048_REG_CMD, 0x5400); + write(REG_CMD, 0x5400); } void MAX17048::quickStart(void) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_MODE); + unsigned short value = read(REG_MODE); //Set the QuickStart bit value |= (1 << 14); //Write the value back out - _write(__MAX17048_REG_MODE, value); + write(REG_MODE, value); } -bool MAX17048::isSleepEnabled(void) +bool MAX17048::sleepEnabled(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_MODE); + unsigned short value = read(REG_MODE); //Return the status of the EnSleep bit if (value & (1 << 13)) @@ -52,10 +52,10 @@ return false; } -void MAX17048::setSleepEnabled(bool enabled) +void MAX17048::sleepEnabled(bool enabled) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_MODE); + unsigned short value = read(REG_MODE); //Set or clear the EnSleep bit if (enabled) @@ -64,13 +64,13 @@ value &= ~(1 << 13); //Write the value back out - _write(__MAX17048_REG_MODE, value); + write(REG_MODE, value); } -bool MAX17048::isHibernating(void) +bool MAX17048::hibernating(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_MODE); + unsigned short value = read(REG_MODE); //Return the status of the HibStat bit if (value & (1 << 12)) @@ -79,19 +79,19 @@ return false; } -float MAX17048::getHibernateThreshold(void) +float MAX17048::hibernateThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_HIBRT); + unsigned short value = read(REG_HIBRT); //Extract the hibernate threshold return (value >> 8) * 0.208; } -void MAX17048::setHibernateThreshold(float threshold) +void MAX17048::hibernateThreshold(float threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_HIBRT); + unsigned short value = read(REG_HIBRT); //Mask off the old value value &= 0x00FF; @@ -105,22 +105,22 @@ } //Write the 16-bit register - _write(__MAX17048_REG_HIBRT, value); + write(REG_HIBRT, value); } -float MAX17048::getActiveThreshold(void) +float MAX17048::activeThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_HIBRT); + unsigned short value = read(REG_HIBRT); //Extract the active threshold return (value & 0x00FF) * 0.00125; } -void MAX17048::setActiveThreshold(float threshold) +void MAX17048::activeThreshold(float threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_HIBRT); + unsigned short value = read(REG_HIBRT); //Mask off the old value value &= 0xFF00; @@ -134,33 +134,33 @@ } //Write the 16-bit register - _write(__MAX17048_REG_HIBRT, value); + write(REG_HIBRT, value); } -unsigned short MAX17048::getVersion(void) +unsigned short MAX17048::version(void) { //Return the 16-bit production version - return _read(__MAX17048_REG_VERSION); + return read(REG_VERSION); } -void MAX17048::setTempCompensation(float temp) +void MAX17048::tempCompensation(float temp) { //Calculate the new RCOMP value char rcomp; if (temp > 20.0) { - rcomp = __MAX17048_RCOMP0 + (temp - 20.0) * -0.5; + rcomp = m_RCOMP0 + (temp - 20.0) * -0.5; } else { - rcomp = __MAX17048_RCOMP0 + (temp - 20.0) * -5.0; + rcomp = m_RCOMP0 + (temp - 20.0) * -5.0; } //Update the RCOMP value - _writeRCOMP(rcomp); + writeRCOMP(rcomp); } -bool MAX17048::isSleeping(void) +bool MAX17048::sleeping(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Return the status of the SLEEP bit if (value & (1 << 7)) @@ -169,10 +169,10 @@ return false; } -void MAX17048::setSleep(bool sleep) +void MAX17048::sleep(bool sleep) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Set or clear the SLEEP bit if (sleep) @@ -181,13 +181,13 @@ value &= ~(1 << 7); //Write the value back out - _write(__MAX17048_REG_CONFIG, value); + write(REG_CONFIG, value); } -bool MAX17048::isSOCChangeAlertEnabled(void) +bool MAX17048::socChangeAlertEnabled(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Return the status of the ALSC bit if (value & (1 << 6)) @@ -196,10 +196,10 @@ return false; } -void MAX17048::setSOCChangeAlertEnabled(bool enabled) +void MAX17048::socChangeAlertEnabled(bool enabled) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Set or clear the ALSC bit if (enabled) @@ -208,13 +208,13 @@ value &= ~(1 << 6); //Write the value back out - _write(__MAX17048_REG_CONFIG, value); + write(REG_CONFIG, value); } -bool MAX17048::isAlerting(void) +bool MAX17048::alerting(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Return the status of the ALRT bit if (value & (1 << 5)) @@ -226,28 +226,28 @@ void MAX17048::clearAlert(void) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Clear the ALRT bit value &= ~(1 << 5); //Write the value back out - _write(__MAX17048_REG_CONFIG, value); + write(REG_CONFIG, value); } -char MAX17048::getEmptyAlertThreshold(void) +char MAX17048::emptyAlertThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Extract the threshold return 32 - (value & 0x001F); } -void MAX17048::setEmptyAlertThreshold(char threshold) +void MAX17048::emptyAlertThreshold(char threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Range check threshold if (threshold < 1) @@ -260,22 +260,22 @@ value |= 32 - threshold; //Write the 16-bit register - _write(__MAX17048_REG_CONFIG, value); + write(REG_CONFIG, value); } -float MAX17048::getVAlertMinThreshold(void) +float MAX17048::vAlertMinThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_VALRT); + unsigned short value = read(REG_VALRT); //Extract the alert threshold return (value >> 8) * 0.02; } -void MAX17048::setVAlertMinThreshold(float threshold) +void MAX17048::vAlertMinThreshold(float threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_VALRT); + unsigned short value = read(REG_VALRT); //Mask off the old value value &= 0x00FF; @@ -289,22 +289,22 @@ } //Write the 16-bit register - _write(__MAX17048_REG_VALRT, value); + write(REG_VALRT, value); } -float MAX17048::getVAlertMaxThreshold(void) +float MAX17048::vAlertMaxThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_VALRT); + unsigned short value = read(REG_VALRT); //Extract the active threshold return (value & 0x00FF) * 0.02; } -void MAX17048::setVAlertMaxThreshold(float threshold) +void MAX17048::vAlertMaxThreshold(float threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_VALRT); + unsigned short value = read(REG_VALRT); //Mask off the old value value &= 0xFF00; @@ -318,22 +318,22 @@ } //Write the 16-bit register - _write(__MAX17048_REG_VALRT, value); + write(REG_VALRT, value); } -float MAX17048::getVResetThreshold(void) +float MAX17048::vResetThreshold(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_VRESET_ID); + unsigned short value = read(REG_VRESET_ID); //Extract the threshold return (value >> 9) * 0.04; } -void MAX17048::setVResetThreshold(float threshold) +void MAX17048::vResetThreshold(float threshold) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_VRESET_ID); + unsigned short value = read(REG_VRESET_ID); //Mask off the old value value &= 0x01FF; @@ -347,13 +347,13 @@ } //Write the 16-bit register - _write(__MAX17048_REG_VRESET_ID, value); + write(REG_VRESET_ID, value); } -bool MAX17048::isComparatorEnabled(void) +bool MAX17048::comparatorEnabled(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_VRESET_ID); + unsigned short value = read(REG_VRESET_ID); //Return the status of the Dis bit if (value & (1 << 8)) @@ -362,10 +362,10 @@ return true; } -void MAX17048::setComparatorEnabled(bool enabled) +void MAX17048::comparatorEnabled(bool enabled) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_VRESET_ID); + unsigned short value = read(REG_VRESET_ID); //Set or clear the Dis bit if (enabled) @@ -374,22 +374,22 @@ value |= (1 << 8); //Write the value back out - _write(__MAX17048_REG_VRESET_ID, value); + write(REG_VRESET_ID, value); } -char MAX17048::getID(void) +char MAX17048::id(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_VRESET_ID); + unsigned short value = read(REG_VRESET_ID); //Return only the ID bits return value; } -bool MAX17048::isVResetAlertEnabled(void) +bool MAX17048::vResetAlertEnabled(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_STATUS); + unsigned short value = read(REG_STATUS); //Return the status of the EnVR bit if (value & (1 << 14)) @@ -398,10 +398,10 @@ return false; } -void MAX17048::setVResetAlertEnabled(bool enabled) +void MAX17048::vResetAlertEnabled(bool enabled) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_STATUS); + unsigned short value = read(REG_STATUS); //Set or clear the EnVR bit if (enabled) @@ -410,13 +410,13 @@ value &= ~(1 << 14); //Write the value back out - _write(__MAX17048_REG_STATUS, value); + write(REG_STATUS, value); } -char MAX17048::getAlertFlags(void) +char MAX17048::alertFlags(void) { //Read the 16-bit register value - unsigned short value = _read(__MAX17048_REG_STATUS); + unsigned short value = read(REG_STATUS); //Return only the flag bits return (value >> 8) & 0x3F; @@ -425,58 +425,58 @@ void MAX17048::clearAlertFlags(char flags) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_STATUS); + unsigned short value = read(REG_STATUS); //Clear the specified flag bits value &= ~((flags & 0x3F) << 8); //Write the value back out - _write(__MAX17048_REG_STATUS, value); + write(REG_STATUS, value); } -float MAX17048::getVcell(void) +float MAX17048::vcell(void) { //Read the 16-bit raw Vcell value - unsigned short value = _read(__MAX17048_REG_VCELL); + unsigned short value = read(REG_VCELL); //Return Vcell in volts return value * 0.000078125; } -float MAX17048::getSOC(void) +float MAX17048::soc(void) { //Read the 16-bit raw SOC value - unsigned short value = _read(__MAX17048_REG_SOC); + unsigned short value = read(REG_SOC); //Return SOC in percent return value * 0.00390625; } -float MAX17048::getCRate(void) +float MAX17048::crate(void) { //Read the 16-bit raw C/Rate value - short value = _read(__MAX17048_REG_CRATE); + short value = read(REG_CRATE); //Return C/Rate in %/hr return value * 0.208; } -unsigned short MAX17048::_read(char reg) +unsigned short MAX17048::read(char reg) { //Create a temporary buffer char buff[2]; //Select the register - _i2c.write(__MAX17048_ADDR, ®, 1); + m_I2C.write(m_ADDR, ®, 1); //Read the 16-bit register - _i2c.read(__MAX17048_ADDR, buff, 2); + m_I2C.read(m_ADDR, buff, 2); //Return the combined 16-bit value return (buff[0] << 8) | buff[1]; } -void MAX17048::_write(char reg, unsigned short data) +void MAX17048::write(char reg, unsigned short data) { //Create a temporary buffer char buff[3]; @@ -487,18 +487,18 @@ buff[2] = data; //Write the data - _i2c.write(__MAX17048_ADDR, buff, 3); + m_I2C.write(m_ADDR, buff, 3); } -void MAX17048::_writeRCOMP(char rcomp) +void MAX17048::writeRCOMP(char rcomp) { //Read the current 16-bit register value - unsigned short value = _read(__MAX17048_REG_CONFIG); + unsigned short value = read(REG_CONFIG); //Update the register value value &= 0x00FF; value |= rcomp << 8; //Write the value back out - _write(__MAX17048_REG_CONFIG, value); + write(REG_CONFIG, value); }