82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
#include "INA233.h"
|
|
|
|
INA233::INA233(const uint8_t address, TwoWire *wire)
|
|
{
|
|
_address = address;
|
|
_wire = wire;
|
|
// not calibrated values by default.
|
|
_current_LSB = 0;
|
|
_maxCurrent = 0;
|
|
_shunt = 0;
|
|
}
|
|
|
|
bool INA233::begin(const uint8_t sda, const uint8_t scl)
|
|
{
|
|
_wire->begin(sda, scl);
|
|
if (! isConnected()) return false;
|
|
return true;
|
|
}
|
|
|
|
bool INA233::isConnected()
|
|
{
|
|
_wire->beginTransmission(_address);
|
|
return ( _wire->endTransmission() == 0);
|
|
}
|
|
|
|
float INA233::getBusVoltage() {
|
|
uint8_t data[2];
|
|
int16_t rawVoltage;
|
|
|
|
// Request data from the PMBus device
|
|
Wire.beginTransmission(INA233::_address);
|
|
Wire.write(REGISTER_READ_VIN);
|
|
Wire.endTransmission();
|
|
Wire.requestFrom(INA233::_address, 2);
|
|
|
|
if (Wire.available() == 2) {
|
|
data[0] = Wire.read();
|
|
data[1] = Wire.read();
|
|
} else {
|
|
// Handle error (e.g., return 0.0 or a special error value)
|
|
return 0.0;
|
|
}
|
|
|
|
// Combine the two bytes into a single 16-bit value
|
|
rawVoltage = (data[1] << 8) | data[0];
|
|
|
|
// Convert raw value to actual voltage using the appropriate scaling
|
|
// For example, assuming a linear scaling with a coefficient of 1.25 mV per LSB:
|
|
float voltage = rawVoltage * 1.25 / 1000.0;
|
|
|
|
return voltage;
|
|
}
|
|
|
|
float INA233::getShuntVoltage() {
|
|
uint8_t data[2];
|
|
int16_t rawVoltage;
|
|
|
|
// Request data from the PMBus device
|
|
Wire.beginTransmission(INA233::_address);
|
|
Wire.write(REGISTER_READ_VSHUNT);
|
|
Wire.endTransmission();
|
|
Wire.requestFrom(INA233::_address, 2);
|
|
|
|
if (Wire.available() == 2) {
|
|
data[0] = Wire.read();
|
|
data[1] = Wire.read();
|
|
} else {
|
|
// return 0 on error
|
|
return 0.0;
|
|
}
|
|
|
|
// Combine the two bytes into a single 16-bit value
|
|
rawVoltage = (data[1] << 8) | data[0];
|
|
|
|
// convert it to volts
|
|
float voltage = rawVoltage * pow(10,-4);
|
|
return voltage;
|
|
}
|
|
|
|
float INA233::getShuntVoltage_mV() {
|
|
return getShuntVoltage() * 1000;
|
|
} |