Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions Arduino-MAX17055_Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ MAX17055::MAX17055(uint16_t batteryCapacity)
writeReg16Bit(DesignCap, batteryCapacity*2);
}
// Public Methods
bool MAX17055::init()
{
Wire.beginTransmission(I2CAddress);
byte error = Wire.endTransmission();
if (error == 0) //Device Acknowledged
{
bool POR = readReg16Bit(Status)&0x0002;
if (POR)
{
setCapacity(2600); //default values
setResistSensor(0.01); //default values
//TODO: load configs as needed
writeReg16Bit(Status,readReg16Bit(Status)&0xFFFD); //reset POR Status
}
return true;
}
return false; //device not found
}

void MAX17055::setCapacity(uint16_t batteryCapacity)
{
//calcuation based on AN6358 page 13 figure 1.3 for Capacity, but reversed to get the register value
Expand All @@ -93,7 +112,13 @@ float MAX17055::getResistSensor()
return resistSensor;
}

float MAX17055::getInstantaneousCurrent()
float MAX17055::getAverageCurrent() //+ve current is charging, -ve is discharging
{
int16_t current_raw = readReg16Bit(AvgCurrent);
return current_raw * current_multiplier_mV;
}

float MAX17055::getInstantaneousCurrent() //+ve current is charging, -ve is discharging
{
int16_t current_raw = readReg16Bit(Current);
return current_raw * current_multiplier_mV;
Expand All @@ -117,6 +142,21 @@ float MAX17055::getTimeToEmpty()
return TTE_raw * time_multiplier_Hours;
}

float MAX17055::getTemperature() {
uint16_t temp_raw= readReg16Bit(Temperature);
return temp_raw * percentage_multiplier;
}

float MAX17055::getAge() {
uint16_t age_raw= readReg16Bit(Age);
return age_raw * percentage_multiplier ; //Return value is % age, with 100% being a fully healthy, new battery.
}

bool MAX17055::getPresent() { //TODO: Doesn't seem to detect battery removal/re-insertion
uint16_t pres_raw= readReg16Bit(Status) & 8; //returns just the 4th bit, with 0 = battery present, 1 = battery missing
return !pres_raw; //hence we invert to return true if battery is present
}

// Private Methods

void MAX17055::writeReg16Bit(uint8_t reg, uint16_t value)
Expand All @@ -140,4 +180,4 @@ uint16_t MAX17055::readReg16Bit(uint8_t reg)
value = Wire.read();
value |= (uint16_t)Wire.read() << 8; // value low byte
return value;
}
}
8 changes: 8 additions & 0 deletions Arduino-MAX17055_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@
// Currently, code only displays register values in hexadecimal format. User needs to state which register they're addressing by hand
// in the ino file.


class MAX17055
{
public:
// register addresses
enum regAddr
{
Status = 0x00, //Maintains all flags related to alert thresholds and battery insertion or removal.
Age = 0x07, //calculated percentage value of capacity compared to original design capacity.
Temperature = 0x08, //Temperature of MAX17055 chip
VCell = 0x09, //VCell reports the voltage measured between BATT and CSP.
AvgVCell = 0x19, //The AvgVCell register reports an average of the VCell register readings.
Current = 0x0A, //Voltage between the CSP and CSN pins, and would need to convert to current
Expand All @@ -84,6 +87,8 @@ class MAX17055
MAX17055(void); //Constructor prototype
MAX17055(uint16_t batteryCapacity); //Constructor allowing user to set capacity of battery

bool init();
float getAverageCurrent();
float getInstantaneousCurrent();
float getInstantaneousVoltage();
void setCapacity(uint16_t batteryCapacity);
Expand All @@ -92,6 +97,9 @@ class MAX17055
float getResistSensor();
float getSOC(); //SOC = State of Charge
float getTimeToEmpty();
float getTemperature();
float getAge();
bool getPresent();

private:
//variables
Expand Down
18 changes: 11 additions & 7 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
#######################################
# Class (KEYWORD1)
#######################################
MAX17055 KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

getAverageCurrent KEYWORD2
getInstantaneousCurrent KEYWORD2
getInstantaneousVoltage KEYWORD2
setCapacity KEYWORD2
getCapacity KEYWORD2
setResistSensor KEYWORD2
getResistSensor KEYWORD2
getSOC KEYWORD2
getTimeToEmpty KEYWORD2
setCapacity KEYWORD2
getCapacity KEYWORD2
setResistSensor KEYWORD2
getResistSensor KEYWORD2
getSOC KEYWORD2
getTimeToEmpty KEYWORD2
getTemperature KEYWORD2
getAge KEYWORD2
getPresent KEYWORD2

#######################################
# Constants (LITERAL1)
Expand Down