Skip to content

Commit

Permalink
Merge pull request #11 from Dennis-van-Gils/main
Browse files Browse the repository at this point in the history
Extended code, bug-fixes and adjusted default values in `setShunt()`
  • Loading branch information
caternuson authored Jun 20, 2024
2 parents 492d438 + 5168419 commit 741d1f1
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 51 deletions.
99 changes: 81 additions & 18 deletions Adafruit_INA228.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ Adafruit_INA228::Adafruit_INA228(void) {}
* The I2C address to be used.
* @param theWire
* The Wire object to be used for I2C connections.
* @param skipReset
* When set to true, will omit resetting all INA228 registers to
* their default values. Default: false.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_INA228::begin(uint8_t i2c_address, TwoWire *theWire, bool skipReset) {
bool Adafruit_INA228::begin(uint8_t i2c_address, TwoWire *theWire,
bool skipReset) {
i2c_dev = new Adafruit_I2CDevice(i2c_address, theWire);

if (!i2c_dev->begin()) {
Expand Down Expand Up @@ -94,33 +98,70 @@ void Adafruit_INA228::reset(void) {
alert_conv.write(1);
setMode(INA228_MODE_CONTINUOUS);
}

/**************************************************************************/
/*!
@brief Sets the shunt calibration by resistor
@param shunt_res Resistance of the shunt in ohms (floating point)
@brief Resets the energy and charge accumulators of the INA228 chip
to 0.
*/
/**************************************************************************/
void Adafruit_INA228::setShunt(float shunt_res, float max_current) {
_current_lsb = max_current / (float)(1UL << 19);
// Serial.print("current lsb is (uA) ");
// Serial.println(_current_lsb * 1000000, 8);
void Adafruit_INA228::resetAccumulators(void) {
Adafruit_I2CRegisterBits reset_accumulators =
Adafruit_I2CRegisterBits(Config, 1, 14);
reset_accumulators.write(1);
}

bool adcrange = (Config->read() >> 4) & 1;
/**************************************************************************/
/*!
@brief Updates the shunt calibration value to the INA228 register.
*/
/**************************************************************************/
void Adafruit_INA228::_updateShuntCalRegister() {
float scale = 1;
if (adcrange) {
if (getADCRange()) {
scale = 4;
}

float shunt_cal = 13107.2 * 1000000.0 * shunt_res * _current_lsb * scale;
// Serial.print("shunt cal is ");
// Serial.println(shunt_cal);
float shunt_cal = 13107.2 * 1000000.0 * _shunt_res * _current_lsb * scale;

Adafruit_I2CRegister shunt =
Adafruit_I2CRegister(i2c_dev, INA228_REG_SHUNTCAL, 2, MSBFIRST);
shunt.write(shunt_cal);
}

/**************************************************************************/
/*!
@brief Sets the shunt calibration by resistor.
@param shunt_res Resistance of the shunt in ohms (floating point)
@param max_current Maximum expected current in A (floating point)
*/
/**************************************************************************/
void Adafruit_INA228::setShunt(float shunt_res, float max_current) {
_shunt_res = shunt_res;
_current_lsb = max_current / (float)(1UL << 19);
_updateShuntCalRegister();
}

/**************************************************************************/
/*!
@brief Sets the shunt full scale ADC range across IN+ and IN-.
@param adc_range
Shunt full scale ADC range (0: +/-163.84 mV or 1: +/-40.96 mV)
*/
/**************************************************************************/
void Adafruit_INA228::setADCRange(uint8_t adc_range) {
Adafruit_I2CRegisterBits adc_range_bit =
Adafruit_I2CRegisterBits(Config, 1, 4);
adc_range_bit.write(adc_range);
_updateShuntCalRegister();
}

/**************************************************************************/
/*!
@brief Reads the shunt full scale ADC range across IN+ and IN-.
@return Shunt full scale ADC range (0: +/-163.84 mV or 1: +/-40.96 mV)
*/
/**************************************************************************/
uint8_t Adafruit_INA228::getADCRange() { return (Config->read() >> 4) & 1; }

/**************************************************************************/
/*!
@brief Reads the die temperature
Expand Down Expand Up @@ -167,9 +208,8 @@ float Adafruit_INA228::readBusVoltage(void) {
*/
/**************************************************************************/
float Adafruit_INA228::readShuntVoltage(void) {
bool adcrange = (Config->read() >> 4) & 1;
float scale = 312.5;
if (adcrange) {
if (getADCRange()) {
scale = 78.125;
}

Expand All @@ -178,7 +218,7 @@ float Adafruit_INA228::readShuntVoltage(void) {
int32_t v = shunt_voltage.read();
if (v & 0x800000)
v |= 0xFF000000;
return v / 16.0 * scale / 1000000.0;
return (float)v / 16.0 * scale / 1000000.0;
}

/**************************************************************************/
Expand Down Expand Up @@ -219,7 +259,7 @@ float Adafruit_INA228::readEnergy(void) {
*/
/**************************************************************************/
INA228_MeasurementMode Adafruit_INA228::getMode(void) {
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(Config, 3, 0);
Adafruit_I2CRegisterBits mode = Adafruit_I2CRegisterBits(ADC_Config, 4, 12);
return (INA228_MeasurementMode)mode.read();
}
/**************************************************************************/
Expand Down Expand Up @@ -302,6 +342,29 @@ void Adafruit_INA228::setVoltageConversionTime(INA228_ConversionTime time) {
Adafruit_I2CRegisterBits(ADC_Config, 3, 9);
voltage_conversion_time.write(time);
}
/**************************************************************************/
/*!
@brief Reads the temperature conversion time
@return The temperature conversion time
*/
/**************************************************************************/
INA228_ConversionTime Adafruit_INA228::getTemperatureConversionTime(void) {
Adafruit_I2CRegisterBits temperature_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 3);
return (INA228_ConversionTime)temperature_conversion_time.read();
}
/**************************************************************************/
/*!
@brief Sets the temperature conversion time
@param time
The new temperature conversion time
*/
/**************************************************************************/
void Adafruit_INA228::setTemperatureConversionTime(INA228_ConversionTime time) {
Adafruit_I2CRegisterBits temperature_conversion_time =
Adafruit_I2CRegisterBits(ADC_Config, 3, 3);
temperature_conversion_time.write(time);
}

/**************************************************************************/
/*!
Expand Down
105 changes: 72 additions & 33 deletions Adafruit_INA228.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,74 @@

#define INA228_I2CADDR_DEFAULT 0x40 ///< INA228 default i2c address
#define INA228_REG_CONFIG 0x00 ///< Configuration register
#define INA228_REG_ADCCFG 0x01
#define INA228_REG_SHUNTCAL 0x02
#define INA228_REG_SHUNTTEMPCO 0x03
#define INA228_REG_VSHUNT 0x04
#define INA228_REG_VBUS 0x05
#define INA228_REG_DIETEMP 0x06
#define INA228_REG_CURRENT 0x07
#define INA228_REG_POWER 0x08
#define INA228_REG_ENERGY 0x09
#define INA228_REG_CHARGE 0x0A
#define INA228_REG_DIAGALRT 0x0B
#define INA228_REG_SOVL 0x0C
#define INA228_REG_SUVL 0x0D
#define INA228_REG_BOVL 0x0E
#define INA228_REG_BUVL 0x0F
#define INA228_REG_TEMPLIMIT 0x10
#define INA228_REG_PWRLIMIT 0x10
#define INA228_REG_MFG_UID 0x3E ///< Manufacturer ID Register
#define INA228_REG_DVC_UID 0x3F ///< Device ID and Revision Register
#define INA228_REG_ADCCFG 0x01 ///< ADC configuration register
#define INA228_REG_SHUNTCAL 0x02 ///< Shunt calibration register
#define INA228_REG_SHUNTTEMPCO 0x03 ///< Shunt temperature coefficient register
#define INA228_REG_VSHUNT 0x04 ///< Shunt voltage measurement register
#define INA228_REG_VBUS 0x05 ///< Bus voltage measurement register
#define INA228_REG_DIETEMP 0x06 ///< Temperature measurement register
#define INA228_REG_CURRENT 0x07 ///< Current result register
#define INA228_REG_POWER 0x08 ///< Power result register
#define INA228_REG_ENERGY 0x09 ///< Energy result register
#define INA228_REG_CHARGE 0x0A ///< Charge result register
#define INA228_REG_DIAGALRT 0x0B ///< Diagnostic flags and alert register
#define INA228_REG_SOVL 0x0C ///< Shunt overvoltage threshold register
#define INA228_REG_SUVL 0x0D ///< Shunt undervoltage threshold register
#define INA228_REG_BOVL 0x0E ///< Bus overvoltage threshold register
#define INA228_REG_BUVL 0x0F ///< Bus undervoltage threshold register
#define INA228_REG_TEMPLIMIT 0x10 ///< Temperature over-limit threshold register
#define INA228_REG_PWRLIMIT 0x10 ///< Power over-limit threshold register
#define INA228_REG_MFG_UID 0x3E ///< Manufacturer ID register
#define INA228_REG_DVC_UID 0x3F ///< Device ID and revision register

/**
* @brief Mode options.
*
* Allowed values for setMode.
*/
typedef enum _mode {
INA228_MODE_SHUTDOWN = 0x00, /**< SHUTDOWN: Minimize quiescient current and
turn off current into the device inputs. Set
another mode to exit shutown mode **/
INA228_MODE_TRIGGERED =
0x07, /**< TRIGGERED: Trigger a one-shot measurement
of temp, current and bus voltage. Set the TRIGGERED
mode again to take a new measurement **/
INA228_MODE_CONTINUOUS = 0x0F, /**< CONTINUOUS: (Default) Continuously update
the temp, current, bus voltage and power
registers with new measurements **/
/**< SHUTDOWN: Minimize quiescient current and turn off current into the
device inputs. Set another mode to exit shutown mode **/
INA228_MODE_SHUTDOWN = 0x00,

/**< Triggered bus voltage, single shot **/
INA228_MODE_TRIG_BUS = 0x01,
/**< Triggered shunt voltage, single shot **/
INA228_MODE_TRIG_SHUNT = 0x02,
/**< Triggered shunt voltage and bus voltage, single shot **/
INA228_MODE_TRIG_BUS_SHUNT = 0x03,
/**< Triggered temperature, single shot **/
INA228_MODE_TRIG_TEMP = 0x04,
/**< Triggered temperature and bus voltage, single shot **/
INA228_MODE_TRIG_TEMP_BUS = 0x05,
/**< Triggered temperature and shunt voltage, single shot **/
INA228_MODE_TRIG_TEMP_SHUNT = 0x06,
/**< Triggered bus voltage, shunt voltage and temperature, single shot **/
INA228_MODE_TRIG_TEMP_BUS_SHUNT = 0x07,

/**< Shutdown **/
INA228_MODE_SHUTDOWN2 = 0x08,
/**< Continuous bus voltage only **/
INA228_MODE_CONT_BUS = 0x09,
/**< Continuous shunt voltage only **/
INA228_MODE_CONT_SHUNT = 0x0A,
/**< Continuous shunt and bus voltage **/
INA228_MODE_CONT_BUS_SHUNT = 0x0B,
/**< Continuous temperature only **/
INA228_MODE_CONT_TEMP = 0x0C,
/**< Continuous bus voltage and temperature **/
INA228_MODE_CONT_TEMP_BUS = 0x0D,
/**< Continuous temperature and shunt voltage **/
INA228_MODE_CONT_TEMP_SHUNT = 0x0E,
/**< Continuous bus voltage, shunt voltage and temperature **/
INA228_MODE_CONT_TEMP_BUS_SHUNT = 0x0F,

/**< TRIGGERED: Trigger a one-shot measurement of temp, current and bus
voltage. Set the TRIGGERED mode again to take a new measurement **/
INA228_MODE_TRIGGERED = INA228_MODE_TRIG_TEMP_BUS_SHUNT,
/**< CONTINUOUS: (Default) Continuously update the temp, current, bus
voltage and power registers with new measurements **/
INA228_MODE_CONTINUOUS = INA228_MODE_CONT_TEMP_BUS_SHUNT
} INA228_MeasurementMode;

/**
Expand Down Expand Up @@ -141,8 +173,11 @@ class Adafruit_INA228 {
bool begin(uint8_t i2c_addr = INA228_I2CADDR_DEFAULT,
TwoWire *theWire = &Wire, bool skipReset = false);
void reset(void);
void resetAccumulators(void);

void setShunt(float shunt_res = 0.1, float max_current = 3.2);
void setADCRange(uint8_t);
uint8_t getADCRange(void);
float readDieTemp(void);

float readCurrent(void);
Expand All @@ -157,19 +192,21 @@ class Adafruit_INA228 {
bool conversionReady(void);
uint16_t alertFunctionFlags(void);

float getAlertLimit(void);
void setAlertLimit(float limit);
// float getAlertLimit(void);
// void setAlertLimit(float limit);
INA228_AlertLatch getAlertLatch(void);
void setAlertLatch(INA228_AlertLatch state);
INA228_AlertPolarity getAlertPolarity(void);
void setAlertPolarity(INA228_AlertPolarity polarity);
INA228_AlertType getAlertType(void);
void setAlertType(INA228_AlertType alert);
// INA228_AlertType getAlertType(void);
// void setAlertType(INA228_AlertType alert);

INA228_ConversionTime getCurrentConversionTime(void);
void setCurrentConversionTime(INA228_ConversionTime time);
INA228_ConversionTime getVoltageConversionTime(void);
void setVoltageConversionTime(INA228_ConversionTime time);
INA228_ConversionTime getTemperatureConversionTime(void);
void setTemperatureConversionTime(INA228_ConversionTime time);
INA228_AveragingCount getAveragingCount(void);
void setAveragingCount(INA228_AveragingCount count);

Expand All @@ -179,6 +216,8 @@ class Adafruit_INA228 {
*AlertLimit; ///< BusIO Register for AlertLimit

private:
void _updateShuntCalRegister(void);
float _shunt_res;
float _current_lsb;
Adafruit_I2CDevice *i2c_dev;
};
Expand Down

0 comments on commit 741d1f1

Please sign in to comment.