Adafruit_INA228/Adafruit_INA237.cpp
2025-05-13 14:19:50 -04:00

131 lines
No EOL
3.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*!
* @file Adafruit_INA237.cpp
*
* @section ina237_intro Introduction
*
* I2C Driver for the INA237 I2C Current and Power sensor
*
* This is a library for the Adafruit INA237 breakout:
* http://www.adafruit.com/products/xxxx
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing products from
* Adafruit!
*
* @section ina237_dependencies Dependencies
*
* This library depends on the Adafruit BusIO library
*
* @section ina237_author Author
*
* Bryan Siepert for Adafruit Industries
*
* @section ina237_license License
*
* BSD (see license.txt)
*
* @section ina237_history HISTORY
*
* v1.0 - First release
*/
#include "Adafruit_INA237.h"
#include <Wire.h>
#include "Arduino.h"
/*!
* @brief Instantiates a new INA237 class
*/
Adafruit_INA237::Adafruit_INA237(void) {}
/*!
* @brief Sets up the HW
* @param i2c_address
* 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 INA237 registers to
* their default values. Default: false.
* @return True if initialization was successful, otherwise false.
*/
bool Adafruit_INA237::begin(uint8_t i2c_address, TwoWire* theWire,
bool skipReset) {
if (!Adafruit_INA2xx::begin(i2c_address, theWire, skipReset)) {
return false;
}
// make sure we're talking to the right chip
if (_device_id != INA237_DEVICE_ID) {
return false;
}
return true;
}
/**************************************************************************/
/*!
@brief Updates the shunt calibration value to the INA237 register.
The INA237 uses a different calculation than the INA228.
*/
/**************************************************************************/
void Adafruit_INA237::_updateShuntCalRegister() {
// Formula from INA237 datasheet (SBOSA20A)
// SHUNT_CAL = 13107.2 × (RSHUNT × CURRENT_LSB)
float scale = 1;
if (getADCRange()) {
scale = 4; // For lower range (+/-40.96mV)
}
// Different calculation for INA237
float shunt_cal = 13107.2 * _shunt_res * _current_lsb * scale;
Adafruit_I2CRegister shunt =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_SHUNTCAL, 2, MSBFIRST);
shunt.write(shunt_cal);
}
/**************************************************************************/
/*!
@brief Returns the current alert type
@return The current alert type
*/
/**************************************************************************/
INA237_AlertType Adafruit_INA237::getAlertType(void) {
// The alert bits are in different positions in the INA237
Adafruit_I2CRegisterBits alert_type =
Adafruit_I2CRegisterBits(Diag_Alert, 7, 5);
return (INA237_AlertType)alert_type.read();
}
/**************************************************************************/
/*!
@brief Sets a new alert type
@param alert
The new alert type to be set
*/
/**************************************************************************/
void Adafruit_INA237::setAlertType(INA237_AlertType alert) {
// The alert bits are in different positions in the INA237
Adafruit_I2CRegisterBits alert_type =
Adafruit_I2CRegisterBits(Diag_Alert, 7, 5);
alert_type.write(alert);
}
/**************************************************************************/
/*!
@brief Reads the die temperature with the INA237-specific conversion factor
@return The current die temp in deg C
*/
/**************************************************************************/
float Adafruit_INA237::readDieTemp(void) {
Adafruit_I2CRegister temp =
Adafruit_I2CRegister(i2c_dev, INA2XX_REG_DIETEMP, 2, MSBFIRST);
int16_t t = temp.read();
// INA237 uses 12 bits for temperature (bits 15:4) with 125 m°C/LSB
// Shift by 4 to get the actual value from register bits 15:4
return (float)(t >> 4) * 125.0 / 1000.0;
}