Fixed wrong shunt voltage reading
Some checks failed
Test compiling project / test (push) Failing after 2m24s

This commit is contained in:
2025-03-23 17:59:43 +01:00
parent 2fa4b0761b
commit 67e9ae1bca
5 changed files with 32 additions and 7 deletions

View File

@@ -23,7 +23,7 @@ uint16_t get_word(uint8_t address, uint8_t reg) {
return (data[1] << 8) | data[0];
}
String INA233::print_device_number() {
String INA233::get_device_model() {
char data[7]; // Array size includes space for the null terminator
// Request data from the PMBus device
@@ -57,6 +57,13 @@ void sendWord(uint8_t deviceAddress, uint8_t registerAddress, uint16_t value) {
Wire.endTransmission();
}
void sendByte(uint8_t deviceAddress, uint8_t registerAddress, uint8_t value) {
Wire.beginTransmission(deviceAddress);
Wire.write(registerAddress); // Send the register address
Wire.write((uint8_t)value); // Send the low byte first
Wire.endTransmission();
}
INA233::INA233(const uint8_t address, TwoWire *wire)
{
_address = address;
@@ -109,8 +116,18 @@ float INA233::getBusVoltage() {
}
float INA233::getShuntVoltage() {
uint16_t rawVoltage = get_word(INA233::_address, REGISTER_READ_VSHUNT);
float voltage = rawVoltage * pow(10,-4);
int16_t rawVoltage = get_word(INA233::_address, REGISTER_READ_VSHUNT); // Read raw voltage (16-bit register)
// Convert from two's complement to an integer value
// If the MSB (most significant bit) is set, the number is negative
if (rawVoltage & 0x8000) { // Check if MSB (bit 15) is set
rawVoltage = rawVoltage - 0x10000; // Convert two's complement negative number
}
// Convert value according to the datasheet
// LSB: 2.5 μV per bit -> Multiply by 2.5e-6 to convert to volts
float voltage = rawVoltage * 2.5e-6;
return voltage;
}
@@ -174,4 +191,8 @@ void INA233::setShuntVoltageConversionTime(ConversionTime conversion_time) {
uint16_t INA233::getConfigRegister() {
return get_word(INA233::_address, REGISTER_CONFIGURATION);
}
void INA233::reset() {
sendByte(INA233::_address, 0x12, 0x00);
}