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!
Revision 7:bf6972a21c61, committed 2013-11-12
- Comitter:
- neilt6
- Date:
- Tue Nov 12 17:48:11 2013 +0000
- Parent:
- 6:6927c72b2923
- Child:
- 8:65c889800b3a
- Commit message:
- open() no longer loads the default compensation value
Changed in this revision
MAX17048.cpp | Show annotated file Show diff for this revision Revisions of this file |
MAX17048.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MAX17048.cpp Thu Nov 07 18:23:02 2013 +0000 +++ b/MAX17048.cpp Tue Nov 12 17:48:11 2013 +0000 @@ -26,9 +26,6 @@ { //Probe for the MAX17048 using a Zero Length Transfer if (!m_I2C.write(m_ADDR, NULL, 0)) { - //Load the default RCOMP value - writeRCOMP(m_RCOMP0); - //Return success return true; } else { @@ -158,18 +155,40 @@ return read(REG_VERSION); } +char MAX17048::compensation() +{ + //Read the 16-bit register value + unsigned short value = read(REG_CONFIG); + + //Return only the upper byte + return (char)(value >> 8); +} + +void MAX17048::compensation(char rcomp) +{ + //Read the current 16-bit register value + unsigned short value = read(REG_CONFIG); + + //Update the register value + value &= 0x00FF; + value |= rcomp << 8; + + //Write the value back out + write(REG_CONFIG, value); +} + void MAX17048::tempCompensation(float temp) { //Calculate the new RCOMP value char rcomp; if (temp > 20.0) { - rcomp = m_RCOMP0 + (temp - 20.0) * -0.5; + rcomp = RCOMP0 + (temp - 20.0) * -0.5; } else { - rcomp = m_RCOMP0 + (temp - 20.0) * -5.0; + rcomp = RCOMP0 + (temp - 20.0) * -5.0; } //Update the RCOMP value - writeRCOMP(rcomp); + compensation(rcomp); } bool MAX17048::sleeping() @@ -525,16 +544,3 @@ //Write the data m_I2C.write(m_ADDR, buff, 3); } - -void MAX17048::writeRCOMP(char rcomp) -{ - //Read the current 16-bit register value - unsigned short value = read(REG_CONFIG); - - //Update the register value - value &= 0x00FF; - value |= rcomp << 8; - - //Write the value back out - write(REG_CONFIG, value); -}
--- a/MAX17048.h Thu Nov 07 18:23:02 2013 +0000 +++ b/MAX17048.h Tue Nov 12 17:48:11 2013 +0000 @@ -35,6 +35,9 @@ * if (gauge.open()) { * printf("Device detected!\n"); * + * //Load the default compensation value + * gauge.compensation(MAX17048::RCOMP0); + * * while (1) { * //Print the current state of charge * printf("SOC = %f%%\n", (float)gauge); @@ -51,6 +54,10 @@ class MAX17048 { public: + /** The default compensation value for the MAX17048 + */ + static const int RCOMP0 = 0x97; + /** Represents the different alert flags for the MAX17048 */ enum AlertFlags { @@ -69,7 +76,7 @@ */ MAX17048(PinName sda, PinName scl); - /** Probe for the MAX17048 and load the default RCOMP value if present + /** Probe for the MAX17048 and indicate if it's present on the bus * * @returns * 'true' if the device exists on the bus, @@ -137,7 +144,19 @@ */ unsigned short version(); - /** Set the cell temperature compensation of the MAX17048 + /** Get the current compensation value of the MAX17048 + * + * @returns The current compensation value as an unsigned char (0 to 255). + */ + char compensation(); + + /** Set the compensation value of the MAX17048 + * + * @param rcomp The new compensation value as an unsigned char (0 to 255). + */ + void compensation(char rcomp); + + /** Set the compensation value of the MAX17048 from the current cell temperature * * @param temp The current cell temperature in °C. */ @@ -333,8 +352,7 @@ }; //Member constants - static const int m_ADDR = (0x36 << 1); - static const int m_RCOMP0 = 0x97; + static const int m_ADDR = (0x36 << 1); //Member variables I2C m_I2C; @@ -342,7 +360,6 @@ //Internal functions unsigned short read(char reg); void write(char reg, unsigned short data); - void writeRCOMP(char rcomp); }; #endif