GY-63_MS5611/libraries/INA226/INA226.cpp

428 lines
9.9 KiB
C++
Raw Normal View History

2022-07-02 04:42:38 -04:00
// FILE: INA226.cpp
2021-05-30 08:22:31 -04:00
// AUTHOR: Rob Tillaart
2024-01-13 06:44:00 -05:00
// VERSION: 0.5.2
2021-05-30 08:22:31 -04:00
// DATE: 2021-05-18
2022-02-08 11:18:33 -05:00
// PURPOSE: Arduino library for INA226 power sensor
2021-05-30 08:22:31 -04:00
// URL: https://github.com/RobTillaart/INA226
2024-01-13 06:44:00 -05:00
//
// Read the datasheet for the details
2021-11-05 14:39:16 -04:00
2021-05-30 08:22:31 -04:00
#include "INA226.h"
2022-08-28 01:32:00 -04:00
// REGISTERS
2024-01-13 06:44:00 -05:00
#define INA226_CONFIGURATION 0x00
#define INA226_SHUNT_VOLTAGE 0x01
#define INA226_BUS_VOLTAGE 0x02
#define INA226_POWER 0x03
#define INA226_CURRENT 0x04
#define INA226_CALIBRATION 0x05
#define INA226_MASK_ENABLE 0x06
#define INA226_ALERT_LIMIT 0x07
#define INA226_MANUFACTURER 0xFE
#define INA226_DIE_ID 0xFF
2021-05-30 08:22:31 -04:00
2022-07-02 04:42:38 -04:00
// CONFIGURATION MASKS
2024-01-13 06:44:00 -05:00
#define INA226_CONF_RESET_MASK 0x8000
#define INA226_CONF_AVERAGE_MASK 0x0E00
#define INA226_CONF_BUSVC_MASK 0x01C0
#define INA226_CONF_SHUNTVC_MASK 0x0038
#define INA226_CONF_MODE_MASK 0x0007
2022-07-02 04:42:38 -04:00
2021-05-30 08:22:31 -04:00
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// Constructor
2021-05-30 08:22:31 -04:00
//
2021-11-05 14:39:16 -04:00
INA226::INA226(const uint8_t address, TwoWire *wire)
2021-05-30 08:22:31 -04:00
{
_address = address;
_wire = wire;
2022-08-28 01:32:00 -04:00
// not calibrated values by default.
2021-05-30 08:22:31 -04:00
_current_LSB = 0;
2021-06-22 09:22:35 -04:00
_maxCurrent = 0;
_shunt = 0;
2021-05-30 08:22:31 -04:00
}
bool INA226::begin()
{
if (! isConnected()) return false;
return true;
}
bool INA226::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
2022-07-02 04:42:38 -04:00
uint8_t INA226::getAddress()
{
return _address;
};
2021-05-30 08:22:31 -04:00
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// Core functions
2021-05-30 08:22:31 -04:00
//
2024-01-13 06:44:00 -05:00
float INA226::getBusVoltage()
{
uint16_t val = _readRegister(INA226_BUS_VOLTAGE);
return val * 1.25e-3; // fixed 1.25 mV
}
2021-05-30 08:22:31 -04:00
float INA226::getShuntVoltage()
{
2022-02-08 11:18:33 -05:00
int16_t val = _readRegister(INA226_SHUNT_VOLTAGE);
2022-08-28 01:32:00 -04:00
return val * 2.5e-6; // fixed 2.50 uV
2021-05-30 08:22:31 -04:00
}
2024-01-13 06:44:00 -05:00
float INA226::getCurrent()
2021-05-30 08:22:31 -04:00
{
2024-01-13 06:44:00 -05:00
int16_t val = _readRegister(INA226_CURRENT);
return val * _current_LSB;
2021-05-30 08:22:31 -04:00
}
float INA226::getPower()
{
uint16_t val = _readRegister(INA226_POWER);
2023-04-04 06:40:56 -04:00
return val * 25 * _current_LSB; // fixed 25 Watt
2021-05-30 08:22:31 -04:00
}
2024-01-13 06:44:00 -05:00
bool INA226::isConversionReady()
2021-05-30 08:22:31 -04:00
{
2024-01-13 06:44:00 -05:00
uint16_t mask = _readRegister(INA226_MASK_ENABLE);
return (mask & INA226_CONVERSION_READY_FLAG) == INA226_CONVERSION_READY_FLAG;
2021-05-30 08:22:31 -04:00
}
2024-01-13 06:44:00 -05:00
bool INA226::waitConversionReady(uint32_t timeout)
{
uint32_t start = millis();
while ( (millis() - start) <= timeout)
{
if (isConversionReady()) return true;
delay(1); // implicit yield();
}
return false;
}
2021-05-30 08:22:31 -04:00
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// Configuration
2021-05-30 08:22:31 -04:00
//
2024-01-13 06:44:00 -05:00
bool INA226::reset()
2021-05-30 08:22:31 -04:00
{
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask |= INA226_CONF_RESET_MASK;
2024-01-13 06:44:00 -05:00
uint16_t result = _writeRegister(INA226_CONFIGURATION, mask);
// Serial.println(result);
if (result != 0) return false;
2022-08-28 01:32:00 -04:00
// reset calibration
2022-02-08 11:18:33 -05:00
_current_LSB = 0;
_maxCurrent = 0;
_shunt = 0;
2024-01-13 06:44:00 -05:00
return true;
2021-05-30 08:22:31 -04:00
}
2021-06-22 09:22:35 -04:00
bool INA226::setAverage(uint8_t avg)
2021-05-30 08:22:31 -04:00
{
2021-06-22 09:22:35 -04:00
if (avg > 7) return false;
2021-05-30 08:22:31 -04:00
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= ~INA226_CONF_AVERAGE_MASK;
2021-05-30 08:22:31 -04:00
mask |= (avg << 9);
_writeRegister(INA226_CONFIGURATION, mask);
2021-06-22 09:22:35 -04:00
return true;
2021-05-30 08:22:31 -04:00
}
uint8_t INA226::getAverage()
{
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= INA226_CONF_AVERAGE_MASK;
2021-05-30 08:22:31 -04:00
mask >>= 9;
return mask;
}
2021-06-22 09:22:35 -04:00
bool INA226::setBusVoltageConversionTime(uint8_t bvct)
2021-05-30 08:22:31 -04:00
{
2021-06-22 09:22:35 -04:00
if (bvct > 7) return false;
2021-05-30 08:22:31 -04:00
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= ~INA226_CONF_BUSVC_MASK;
2021-05-30 08:22:31 -04:00
mask |= (bvct << 6);
_writeRegister(INA226_CONFIGURATION, mask);
2021-06-22 09:22:35 -04:00
return true;
2021-05-30 08:22:31 -04:00
}
uint8_t INA226::getBusVoltageConversionTime()
{
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= INA226_CONF_BUSVC_MASK;
2021-05-30 08:22:31 -04:00
mask >>= 6;
return mask;
}
2021-06-22 09:22:35 -04:00
bool INA226::setShuntVoltageConversionTime(uint8_t svct)
2021-05-30 08:22:31 -04:00
{
2021-06-22 09:22:35 -04:00
if (svct > 7) return false;
2021-05-30 08:22:31 -04:00
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= ~INA226_CONF_SHUNTVC_MASK;
2021-05-30 08:22:31 -04:00
mask |= (svct << 3);
_writeRegister(INA226_CONFIGURATION, mask);
2021-06-22 09:22:35 -04:00
return true;
2021-05-30 08:22:31 -04:00
}
uint8_t INA226::getShuntVoltageConversionTime()
{
uint16_t mask = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
mask &= INA226_CONF_SHUNTVC_MASK;
2021-05-30 08:22:31 -04:00
mask >>= 3;
return mask;
}
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// Calibration
2021-05-30 08:22:31 -04:00
//
2022-08-28 01:32:00 -04:00
int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
2021-05-30 08:22:31 -04:00
{
2023-12-10 11:26:56 -05:00
// https://github.com/RobTillaart/INA226/pull/29
2022-08-28 01:32:00 -04:00
// #define printdebug true
2022-02-08 11:18:33 -05:00
2022-08-28 01:32:00 -04:00
// fix #16 - datasheet 6.5 Electrical Characteristics
// rounded value to 80 mV
2023-12-10 11:26:56 -05:00
float shuntVoltage = maxCurrent * shunt;
2023-05-24 06:54:25 -04:00
if (shuntVoltage > 0.080) return INA226_ERR_SHUNTVOLTAGE_HIGH;
if (maxCurrent < 0.001) return INA226_ERR_MAXCURRENT_LOW;
if (shunt < INA226_MINIMAL_SHUNT) return INA226_ERR_SHUNT_LOW;
2021-06-22 09:22:35 -04:00
2023-09-24 05:45:01 -04:00
_current_LSB = maxCurrent * 3.0517578125e-5; // maxCurrent / 32768;
2021-06-22 09:22:35 -04:00
2022-02-08 11:18:33 -05:00
#ifdef printdebug
Serial.println();
Serial.print("normalize:\t");
Serial.println(normalize ? " true":" false");
Serial.print("initial current_LSB:\t");
2023-12-10 11:26:56 -05:00
Serial.print(_current_LSB * 1e+6, 1);
2022-02-08 11:18:33 -05:00
Serial.println(" uA / bit");
#endif
2022-08-28 01:32:00 -04:00
// normalize the LSB to a round number
// LSB will increase
2021-06-22 09:22:35 -04:00
if (normalize)
2021-05-30 08:22:31 -04:00
{
2023-12-10 11:26:56 -05:00
/*
check if maxCurrent (normal) or shunt resistor
(due to unusual low resistor values in relation to maxCurrent) determines currentLSB
we have to take the upper value for currentLSB
calculation of currentLSB based on shunt resistor and calibration register limits (2 bytes)
cal = 0.00512 / ( shunt * currentLSB )
cal(max) = 2^16-1
currentLSB(min) = 0.00512 / ( shunt * cal(max) )
currentLSB(min) ~= 0.00512 / ( shunt * 2^16 )
currentLSB(min) ~= 2^9 * 1e-5 / ( shunt * 2^16 )
currentLSB(min) ~= 1e-5 / 2^7 / shunt
currentLSB(min) ~= 7.8125e-8 / shunt
*/
if ( 7.8125e-8 / shunt > _current_LSB ) {
// shunt resistor determines currentLSB -> take this a starting point for currentLSB
_current_LSB = 7.8125e-8 / shunt;
}
2022-02-08 11:18:33 -05:00
#ifdef printdebug
Serial.print("Prescale current_LSB:\t");
2023-12-10 11:26:56 -05:00
Serial.print(_current_LSB * 1e+6, 1);
2022-02-08 11:18:33 -05:00
Serial.println(" uA / bit");
#endif
2023-12-10 11:26:56 -05:00
// normalize _current_LSB to a value of 1, 2 or 5 * 1e-6 to 1e-3
// convert float to int
uint16_t currentLSB_uA = float(_current_LSB * 1e+6);
currentLSB_uA++; // ceil() would be more precise, but uses 176 bytes of flash.
uint16_t factor = 1; // 1uA to 1000uA
uint8_t i = 0; // 1 byte loop reduces footprint
bool result = false;
do {
if ( 1 * factor >= currentLSB_uA) {
_current_LSB = 1 * factor * 1e-6;
result = true;
} else if ( 2 * factor >= currentLSB_uA) {
_current_LSB = 2 * factor * 1e-6;
result = true;
} else if ( 5 * factor >= currentLSB_uA) {
_current_LSB = 5 * factor * 1e-6;
result = true;
} else {
factor *= 10;
i++;
}
} while( (i < 4) && (!result) ); // factor < 10000
if (result == false) { // not succeeded to normalize.
_current_LSB = 0;
return INA226_ERR_NORMALIZE_FAILED;
2021-06-22 09:22:35 -04:00
}
2023-12-10 11:26:56 -05:00
#ifdef printdebug
Serial.print("After scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
// done
2021-05-30 08:22:31 -04:00
}
2021-06-22 09:22:35 -04:00
2023-12-10 11:26:56 -05:00
// auto scale calibration if needed.
uint32_t calib = round(0.00512 / (_current_LSB * shunt));
2021-06-22 14:38:00 -04:00
while (calib > 65535)
{
2023-12-10 11:26:56 -05:00
_current_LSB *= 2;
calib >>= 1;
2021-06-22 14:38:00 -04:00
}
2021-05-30 08:22:31 -04:00
_writeRegister(INA226_CALIBRATION, calib);
2021-06-22 09:22:35 -04:00
2022-08-28 01:32:00 -04:00
_maxCurrent = _current_LSB * 32768;
2021-06-22 14:38:00 -04:00
_shunt = shunt;
2022-02-08 11:18:33 -05:00
#ifdef printdebug
Serial.print("Final current_LSB:\t");
2023-12-10 11:26:56 -05:00
Serial.print(_current_LSB * 1e+6, 1);
2022-02-08 11:18:33 -05:00
Serial.println(" uA / bit");
Serial.print("Calibration:\t");
Serial.println(calib);
Serial.print("Max current:\t");
2023-12-10 11:26:56 -05:00
Serial.print(_maxCurrent, 3);
2022-02-08 11:18:33 -05:00
Serial.println(" A");
Serial.print("Shunt:\t");
2023-12-10 11:26:56 -05:00
Serial.print(_shunt, 4);
Serial.println(" Ohm");
2022-08-28 01:32:00 -04:00
Serial.print("ShuntV:\t");
Serial.print(shuntVoltage, 4);
Serial.println(" Volt");
2022-02-08 11:18:33 -05:00
#endif
2022-08-28 01:32:00 -04:00
return INA226_ERR_NONE;
2021-05-30 08:22:31 -04:00
}
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// operating mode
2021-05-30 08:22:31 -04:00
//
2021-06-22 09:22:35 -04:00
bool INA226::setMode(uint8_t mode)
2021-05-30 08:22:31 -04:00
{
2021-06-22 09:22:35 -04:00
if (mode > 7) return false;
uint16_t config = _readRegister(INA226_CONFIGURATION);
2022-07-02 04:42:38 -04:00
config &= ~INA226_CONF_MODE_MASK;
2021-06-22 09:22:35 -04:00
config |= mode;
_writeRegister(INA226_CONFIGURATION, config);
return true;
2021-05-30 08:22:31 -04:00
}
uint8_t INA226::getMode()
{
2022-07-02 04:42:38 -04:00
uint16_t mode = _readRegister(INA226_CONFIGURATION);
mode &= INA226_CONF_MODE_MASK;
return mode;
2021-05-30 08:22:31 -04:00
}
////////////////////////////////////////////////////////
//
2023-04-04 06:40:56 -04:00
// alert
2021-05-30 08:22:31 -04:00
//
2024-01-13 06:44:00 -05:00
bool INA226::setAlertRegister(uint16_t mask)
2021-05-30 08:22:31 -04:00
{
2024-01-13 06:44:00 -05:00
uint16_t result = _writeRegister(INA226_MASK_ENABLE, (mask & 0xFC00));
// Serial.println(result);
if (result != 0) return false;
return true;
2021-05-30 08:22:31 -04:00
}
uint16_t INA226::getAlertFlag()
{
return _readRegister(INA226_MASK_ENABLE) & 0x001F;
}
2024-01-13 06:44:00 -05:00
bool INA226::setAlertLimit(uint16_t limit)
2021-05-30 08:22:31 -04:00
{
2024-01-13 06:44:00 -05:00
uint16_t result = _writeRegister(INA226_ALERT_LIMIT, limit);
// Serial.println(result);
if (result != 0) return false;
return true;
2021-05-30 08:22:31 -04:00
}
uint16_t INA226::getAlertLimit()
{
return _readRegister(INA226_ALERT_LIMIT);
}
////////////////////////////////////////////////////////
//
2023-04-04 06:40:56 -04:00
// meta information
2021-05-30 08:22:31 -04:00
//
uint16_t INA226::getManufacturerID()
{
return _readRegister(INA226_MANUFACTURER);
}
2023-09-24 05:45:01 -04:00
2021-05-30 08:22:31 -04:00
uint16_t INA226::getDieID()
{
return _readRegister(INA226_DIE_ID);
}
////////////////////////////////////////////////////////
//
2022-08-28 01:32:00 -04:00
// PRIVATE
2021-05-30 08:22:31 -04:00
//
uint16_t INA226::_readRegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->endTransmission();
_wire->requestFrom(_address, (uint8_t)2);
uint16_t value = _wire->read();
value <<= 8;
value |= _wire->read();
return value;
}
uint16_t INA226::_writeRegister(uint8_t reg, uint16_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value >> 8);
_wire->write(value & 0xFF);
return _wire->endTransmission();
}
2023-04-04 06:40:56 -04:00
// -- END OF FILE --
2021-05-30 08:22:31 -04:00