This commit is contained in:
82
lib/INA233/INA233.cpp
Normal file
82
lib/INA233/INA233.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#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;
|
||||
}
|
||||
26
lib/INA233/INA233.h
Normal file
26
lib/INA233/INA233.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define REGISTER_READ_VIN 0x88
|
||||
#define REGISTER_READ_VSHUNT 0xd1
|
||||
|
||||
class INA233{
|
||||
public:
|
||||
INA233(uint8_t addr, TwoWire *wire = &Wire);
|
||||
bool begin(const uint8_t sda, const uint8_t scl);
|
||||
float getBusVoltage(void);
|
||||
float getShuntVoltage_mV(void);
|
||||
float getShuntVoltage(void);
|
||||
bool isConnected(void);
|
||||
|
||||
private:
|
||||
float _current_LSB;
|
||||
float _shunt;
|
||||
float _maxCurrent;
|
||||
|
||||
uint8_t _address;
|
||||
TwoWire * _wire;
|
||||
};
|
||||
1
lib/infinityPV_INA233_Arduino-library
Submodule
1
lib/infinityPV_INA233_Arduino-library
Submodule
Submodule lib/infinityPV_INA233_Arduino-library added at bc68e03c01
Reference in New Issue
Block a user