0.1.2 INA226

This commit is contained in:
rob tillaart 2021-06-22 15:22:35 +02:00
parent a750333d50
commit 779d02adca
11 changed files with 473 additions and 83 deletions

View File

@ -1,13 +1,15 @@
// FILE: INA266.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.2
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA266 power sensor
// URL: https://github.com/RobTillaart/INA226
//
// HISTORY:
// 0.1.0 2021-05-18 initial version
// 0.1.1 2021-06-21 improved calibration + added functions
// 0.1.2 2021-06-22 add paramchecking of several functions + unit tests
// add getShunt() , getMaxCurrent()
#include "INA226.h"
@ -32,7 +34,10 @@ INA226::INA226(const int8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
// as these
_current_LSB = 0;
_maxCurrent = 0;
_shunt = 0;
}
@ -96,7 +101,7 @@ float INA226::getPower()
float INA226::getCurrent()
{
uint16_t val = _readRegister(INA226_CURRENT);
float val = _readRegister(INA226_CURRENT);
return val * _current_LSB;
}
@ -110,16 +115,18 @@ void INA226::reset()
uint16_t mask = _readRegister(INA226_CONFIGURATION);
mask |= 0x800;
_writeRegister(INA226_CONFIGURATION, mask);
// reset calibration?
}
void INA226::setAverage(uint8_t avg)
bool INA226::setAverage(uint8_t avg)
{
if (avg > 7) return;
if (avg > 7) return false;
uint16_t mask = _readRegister(INA226_CONFIGURATION);
mask &= 0xF1FF;
mask |= (avg << 9);
_writeRegister(INA226_CONFIGURATION, mask);
return true;
}
@ -132,13 +139,14 @@ uint8_t INA226::getAverage()
}
void INA226::setBusVoltageConversionTime(uint8_t bvct)
bool INA226::setBusVoltageConversionTime(uint8_t bvct)
{
if (bvct > 7) return;
if (bvct > 7) return false;
uint16_t mask = _readRegister(INA226_CONFIGURATION);
mask &= 0xFE3F;
mask |= (bvct << 6);
_writeRegister(INA226_CONFIGURATION, mask);
return true;
}
@ -151,13 +159,14 @@ uint8_t INA226::getBusVoltageConversionTime()
}
void INA226::setShuntVoltageConversionTime(uint8_t svct)
bool INA226::setShuntVoltageConversionTime(uint8_t svct)
{
if (svct > 7) return;
if (svct > 7) return false;
uint16_t mask = _readRegister(INA226_CONFIGURATION);
mask &= 0xFFC7;
mask |= (svct << 3);
_writeRegister(INA226_CONFIGURATION, mask);
return true;
}
@ -174,22 +183,38 @@ uint8_t INA226::getShuntVoltageConversionTime()
//
// Calibration
//
void INA226::setMaxCurrentShunt(float ampere, float ohm)
bool INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
{
_current_LSB = ampere * (1.0 / 32768.0);
// make the LSB a round number
float factor = 1;
// Serial.println(_current_LSB, 6);
while (_current_LSB < 1)
if (maxCurrent > 20 || maxCurrent < 0.001) return false;
if (shunt < 0.001) return false;
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent * 3.0517578125e-005; // maxCurrent / 32768;
// normalize the LSB to a round number
// not sure is this is more accurate / precise
if (normalize)
{
_current_LSB *= 10;
factor *= 10;
// Serial.print("current_LSB:\t");
// Serial.println(_current_LSB, 10);
uint32_t factor = 1;
while (_current_LSB < 1)
{
_current_LSB *= 10;
factor *= 10;
}
_current_LSB = 10.0 / factor;
// Serial.print("current_LSB:\t");
// Serial.println(_current_LSB, 10);
}
_current_LSB = 10.0 / factor;
// Serial.println(_current_LSB, 6);
uint16_t calib = round(0.00512 / (_current_LSB * ohm));
// Serial.println(calib);
uint16_t calib = round(0.00512 / (_current_LSB * _shunt));
_writeRegister(INA226_CALIBRATION, calib);
// Serial.print("Calibration:\t");
// Serial.println(calib);
return true;
}
@ -197,13 +222,14 @@ void INA226::setMaxCurrentShunt(float ampere, float ohm)
//
// operating mode
//
void INA226::setMode(uint8_t mode)
bool INA226::setMode(uint8_t mode)
{
if (mode > 7) return;
uint16_t m = _readRegister(INA226_CONFIGURATION);
m &= 0xFFF8;
m |= mode;
_writeRegister(INA226_CONFIGURATION, m);
if (mode > 7) return false;
uint16_t config = _readRegister(INA226_CONFIGURATION);
config &= 0xFFF8;
config |= mode;
_writeRegister(INA226_CONFIGURATION, config);
return true;
}

View File

@ -1,7 +1,7 @@
#pragma once
// FILE: INA266.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.2
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA266 power sensor
// URL: https://github.com/RobTillaart/INA226
@ -14,7 +14,7 @@
#include "Wire.h"
#define INA226_LIB_VERSION (F("0.1.0"))
#define INA226_LIB_VERSION (F("0.1.2"))
// set by setAlertRegister
@ -42,40 +42,54 @@ public:
#if defined (ESP8266) || defined(ESP32)
bool begin(const uint8_t sda, const uint8_t scl);
#endif
bool begin();
bool isConnected();
// Core functions
float getShuntVoltage();
float getBusVoltage();
float getPower();
float getCurrent();
// scale
float getShuntVoltage_mV() { return getShuntVoltage() * 1000.0; };
float getPower_mW() { return getPower() * 1000.0; };
float getCurrent_mA() { return getCurrent() * 1000.0; };
// Configuration
void reset();
void setAverage(uint8_t avg = 0);
bool setAverage(uint8_t avg = 0);
uint8_t getAverage();
void setBusVoltageConversionTime(uint8_t bvct = 4);
bool setBusVoltageConversionTime(uint8_t bvct = 4);
uint8_t getBusVoltageConversionTime();
void setShuntVoltageConversionTime(uint8_t svct = 4);
bool setShuntVoltageConversionTime(uint8_t svct = 4);
uint8_t getShuntVoltageConversionTime();
// Calibration
void setMaxCurrentShunt(float ampere = 10.0, float ohm = 0.1);
// mandatory to set these!
// maxCurrent = 0.001 .. 20
// shunt >= 0.001
bool setMaxCurrentShunt(float macCurrent = 20.0, float shunt = 0.002,
bool normalize = true);
// these return zero if not calibrated!
float getCurrentLSB() { return _current_LSB; };
float getShunt() { return _shunt; };
float getMaxCurrent() { return _maxCurrent; };
// Operating mode
void setMode(uint8_t mode = 7);
bool setMode(uint8_t mode = 7);
uint8_t getMode();
void shutDown() { setMode(0); };
void setModeShuntTrigger() { setMode(1); };
void setModeBusTrigger() { setMode(2); };
void setModeShuntBusTrigger() { setMode(3); };
void setModeShuntContinuous() { setMode(5); };
void setModeBusContinuous() { setMode(6); };
void setModeShuntBusContinuous() { setMode(7); }; // default.
bool shutDown() { return setMode(0); };
bool setModeShuntTrigger() { return setMode(1); };
bool setModeBusTrigger() { return setMode(2); };
bool setModeShuntBusTrigger() { return setMode(3); };
bool setModeShuntContinuous() { return setMode(5); };
bool setModeBusContinuous() { return setMode(6); };
bool setModeShuntBusContinuous() { return setMode(7); }; // default.
// Alert
@ -95,11 +109,17 @@ public:
uint16_t getDieID(); // should return 0x2260
// DEBUG
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
private:
uint16_t _readRegister(uint8_t reg);
uint16_t _writeRegister(uint8_t reg, uint16_t value);
float _current_LSB;
float _shunt;
float _maxCurrent;
uint8_t _address;
TwoWire * _wire;

View File

@ -16,27 +16,49 @@ Not all functionality is tested / investigated.
==> **USE WITH CARE**
The INA226 is a voltage, current and power measurement device. a few important maxima. (See datasheet, ch. 6)
The INA226 is a voltage, current and power measurement device. a few important maxima. (See datasheet, chapter 6)
| description | max | unit |
|:--------------|------:|-------:|
| bus voltage | 36 | Volt |
| bus voltage | 36 | Volt |
| shunt voltage | 80 | mVolt |
| current | ?? | Ampere |
| current | 20 | Ampere |
The sensor can have 16 different I2C addresses, which depends on how the A0 and A1 address lines are connected to the SCL, SDA, GND and VCC pins.
See datasheet - table 2 - datasheet.
TODO: elaborate.
## About Measurements
Calibration is mandatory to get **getCurrent()** and **getPower()** to work.
Some initial tests shows that the readings do not 100% add up.
I expect this is caused by fluctuations in my power supply used and
more important that the ADC is multiplexed so there is time between the bus voltage measurement
and the shunt voltage measurement. If the current has changed a bit these values are not necessary
in line.
Did some measurements with a load of 194 ohm and a shunt of 0.002 ohm that is a factor 10e5
Being on the edge of the sensitivity of the ADC measurements of current were up to ~9% too low.
Possible cause is that some math is done in 16 bit so numbers are truncated, not rounded.
(see issue #2) Sensors may have a different shunt resistor than the 0.002 I have. You should
always check and verify what is on the shunt and even verify with a DMM that this value is correct.
With the calibration function **setMaxCurrentShunt()** one can just set the actual value and even
compensate slightly if readings are structural too low or too high.
I noted that the **getPower()** function does not always equal **getBusVoltage()** times **getCurrent()**
Cause is rounding/trunking maths and time of measurement. You might prefer to multiply those values yourself
to get extra digits. Please be aware that more digits is not always more exact (think significant digits)
## Interface
read datasheet for details
### Constructor
- **INA226(const int8_t address, TwoWire \*wire = Wire)** Constructor to set address and optional Wire interface.
@ -54,8 +76,11 @@ the sensor. Also the value is not meaningful if there is no shunt connected.
- **float getShuntVoltage()** idem.
- **float getBusVoltage()** idem. Max 36 Volt.
- **float getCurrent()** is the current through the shunt.
- **float getPower()** is the current x BusVoltage
- **float getCurrent()** is the current through the shunt in Ampere
- **float getPower()** is the current x BusVoltage in Watt
- **float getShuntVoltage_mV()** idem, in millivolts
- **float getCurrent_mA()** idem in milliAmpere
- **float getPower_mW()** idem in milliWatt
### Configuration
@ -63,14 +88,14 @@ the sensor. Also the value is not meaningful if there is no shunt connected.
to be tested.
- **void reset()** software power on reset
- **void setAverage(uint8_t avg = 0)** see table below
(0 = default ==> 1 read)
- **bool setAverage(uint8_t avg = 0)** see table below
(0 = default ==> 1 read), returns false if parameter > 7
- **uint8_t getAverage()** returns the value set. Note this is not the count of samples.
- **void setBusVoltageConversionTime(uint8_t bvct = 4)** see table below
(4 = default ==> 1.1 ms)
- **bool setBusVoltageConversionTime(uint8_t bvct = 4)** see table below
(4 = default ==> 1.1 ms), returns false if parameter > 7
- **uint8_t getBusVoltageConversionTime()** return the value set. Note this is not a unit of time.
- **void setShuntVoltageConversionTime(uint8_t svct = 4)** see table below
(4 = default ==> 1.1 ms)
- **bool setShuntVoltageConversionTime(uint8_t svct = 4)** see table below
(4 = default ==> 1.1 ms), returns false if parameter > 7
- **uint8_t getShuntVoltageConversionTime()** return the value set. Note this is not a unit of time.
@ -104,10 +129,12 @@ Note that total conversion time can take up to 1024 \* 8.3 ms ~ 10 seconds.
### Calibration
See daatsheet, not tested yet.
See datasheet
- **void setMaxCurrentShunt(float ampere = 10.0, float ohm = 0.1)** set the calibration register based upon the shunt and the max ampere. From this the LSB is derived. Note the function will round up the LSB to nearest round value.
- **float getCurrentLSB()** returns the LSB == precission of the calibration
Calibration is mandatory to get **getCurrent()** and **getPower()** to work.
- **bool setMaxCurrentShunt(float ampere = 20.0, float ohm = 0.002, bool normalize = true)** set the calibration register based upon the shunt and the max ampere. From this the LSB is derived. Note the function will round up the LSB to nearest round value by default. This may cause loss of precision.
- **float getCurrentLSB()** returns the LSB == precision of the calibration
### Operating mode
@ -116,14 +143,14 @@ See datasheet, partially tested.
Mode = 4 is not used, is also a **shutdown()** unknown if there is a difference.
- **void setMode(uint8_t mode = 7)** mode = 0 .. 7
- **void shutDown()** mode 0 - not tested yet
- **void setModeShuntTrigger()** mode 1 - not tested yet - how to trigger to be investigated
- **void setModeBusTrigger()** mode 2 - not tested yet -
- **void setModeShuntBusTrigger()** mode 3 - not tested yet -
- **void setModeShuntContinuous()** mode 5
- **void setModeBusContinuous()** mode 6
- **void setModeShuntBusContinuous()** mode 7 - default
- **bool setMode(uint8_t mode = 7)** mode = 0 .. 7
- **bool shutDown()** mode 0 - not tested yet
- **bool setModeShuntTrigger()** mode 1 - not tested yet - how to trigger to be investigated
- **bool setModeBusTrigger()** mode 2 - not tested yet -
- **bool setModeShuntBusTrigger()** mode 3 - not tested yet -
- **bool setModeShuntContinuous()** mode 5
- **bool setModeBusContinuous()** mode 6
- **bool setModeShuntBusContinuous()** mode 7 - default
- **uint8_t getMode()** returns the mode (0..7) set by one of the functions above.
@ -131,7 +158,7 @@ Mode = 4 is not used, is also a **shutdown()** unknown if there is a difference.
See datasheet, not tested yet.
- **void setAlertRegister(uint16_t mask)** by setting the mask one of five an over- or underflow can be detected. Another feature that can be set si the conversion ready flag.
- **void setAlertRegister(uint16_t mask)** by setting the mask one of five an over- or underflow can be detected. Another feature that can be set is the conversion ready flag.
- **uint16_t getAlertFlag()** returns the mask set by **setAlertRegister()**
- **void setAlertLimit(uint16_t limit)** sets the limit that belongs to the chosen Alert Flag
- **uint16_t getAlertLimit()** returns the limit set by **setAlertLimit()**
@ -159,7 +186,6 @@ See datasheet, not tested yet.
The alert line falls when alert is reached.
### Meta information
- **uint16_t getManufacturerID()** should return 0x5449
@ -168,12 +194,20 @@ The alert line falls when alert is reached.
## Operational
See examples..
Not all examples are tested.
See examples..
## TODO
- testtestestest
- test different loads (low edge)
- test unit tests
- test examples
- investigate alert functions / interface
- improve readme.md
- disconnected load, can it be recognized?
- **lastError()** do we need this...
- cache configuration ? ==> 2 bytes
- getCurrentLSB_mA()
- bool isCalibrated()

View File

@ -1,7 +1,7 @@
//
// FILE: INA226_demo.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo
// DATE: 2021-05-18
// URL: https://github.com/RobTillaart/INA226
@ -30,9 +30,9 @@ void setup()
Serial.print("DIE:\t");
Serial.println(INA.getDieID(), HEX);
delay(100);
INA.setMaxCurrentShunt(15, 0.002);
INA.setMaxCurrentShunt(1, 0.002);
Serial.print("LSB:\t");
Serial.println(INA.getCurrentLSB(), 6);
Serial.println(INA.getCurrentLSB(), 10);
Serial.println("\n\n");
Serial.println("BUS\tSHUNT\tCURRENT\tPOWER");
@ -41,13 +41,20 @@ void setup()
void loop()
{
Serial.print(INA.getBusVoltage(), 4);
// for (int r = 0; r < 6; r++)
// {
// Serial.print(INA.getRegister(r), HEX);
// Serial.print('\t');
// }
// Serial.println();
Serial.print(INA.getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA.getShuntVoltage(), 4);
Serial.print(INA.getShuntVoltage_mV(), 3);
Serial.print("\t");
Serial.print(INA.getCurrent(), 4);
Serial.print(INA.getCurrent_mA(), 3);
Serial.print("\t");
Serial.print(INA.getPower(), 4);
Serial.print(INA.getPower_mW(), 3);
Serial.println();
delay(1000);
}

View File

@ -0,0 +1,64 @@
//
// FILE: INA226_demo_2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// PURPOSE: demo
// DATE: 2021-06-21
// URL: https://github.com/RobTillaart/INA226
#include "INA226.h"
#include "Wire.h"
INA226 INA(0x40);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Wire.begin();
if (!INA.begin() )
{
Serial.println("could not connect. Fix and Reboot");
}
Serial.println();
Serial.print("MAN:\t");
Serial.println(INA.getManufacturerID(), HEX);
Serial.print("DIE:\t");
Serial.println(INA.getDieID(), HEX);
delay(100);
INA.setMaxCurrentShunt(1, 0.002);
Serial.print("LSB:\t");
Serial.println(INA.getCurrentLSB(), 10);
Serial.println("\n\n");
Serial.println("POWER2 = busVoltage x current\n");
Serial.println("BUS\tCURRENT\tPOWER\tPOWER2");
}
void loop()
{
float bv = INA.getBusVoltage();
float sv = INA.getShuntVoltage_mV();
float cu = INA.getCurrent_mA();
float po = INA.getPower_mW();
Serial.print(bv, 3);
Serial.print("\t");
Serial.print(cu, 3);
Serial.print("\t");
Serial.print(po, 3);
Serial.print("\t");
Serial.print(bv * cu, 3);
Serial.print("\t");
Serial.print((po - bv * cu), 3);
Serial.println();
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,56 @@
//
// FILE: INA226_demo_plotter.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-06-21
// URL: https://github.com/RobTillaart/INA226
// Can be used for IDE-Tools->Plotter
#include "INA226.h"
#include "Wire.h"
INA226 INA(0x40);
void setup()
{
Serial.begin(115200);
Wire.begin();
if (!INA.begin() )
{
Serial.println("could not connect. Fix and Reboot");
}
// 1 ampere - 0.002 shunt.
INA.setMaxCurrentShunt(1, 0.002);
Serial.println("POWER2 = busVoltage x current\n");
Serial.println("BUS\tCURRENT\tPOWER\tPOWER2\tDIFF");
}
void loop()
{
float bv = INA.getBusVoltage();
float sv = INA.getShuntVoltage_mV();
float cu = INA.getCurrent_mA();
float po = INA.getPower_mW();
Serial.print(bv, 3);
Serial.print("\t");
Serial.print(cu, 3);
Serial.print("\t");
Serial.print(po, 3);
Serial.print("\t");
Serial.print(bv * cu, 3);
Serial.print("\t");
Serial.print((po - bv * cu), 3);
Serial.println();
delay(100);
}
// -- END OF FILE --

View File

@ -0,0 +1,71 @@
//
// FILE: INA226_dump_registers.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo
// DATE: 2021-06-21
// URL: https://github.com/RobTillaart/INA226
/*
expected output something like
REGISTER VALUE VALUE_X
-----------------------------------
CONFIGURATION: 16679 4127
SHUNT: 65533 FFFD
BUS VOLTAGE: 2 2
POWER: 0 0
CURRENT: 0 0
CALIBRATION: 0 0
*/
#include "INA226.h"
#include "Wire.h"
INA226 INA(0x40);
char names[6][20] =
{
"CONFIGURATION: ",
" SHUNT: ",
" BUS VOLTAGE: ",
" POWER: ",
" CURRENT: ",
" CALIBRATION: "
};
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Wire.begin();
if (!INA.begin() )
{
Serial.println("could not connect. Fix and Reboot");
}
}
void loop()
{
Serial.println("\n\tREGISTER\tVALUE\tVALUE_X");
for (int r = 0; r < 6; r++)
{
Serial.print('\t');
Serial.print(names[r]);
Serial.print('\t');
Serial.print(INA.getRegister(r), DEC);
Serial.print('\t');
Serial.println(INA.getRegister(r), HEX);
}
Serial.println();
delay(1000);
}
// -- END OF FILE --

View File

@ -1,6 +1,6 @@
# Syntax Coloring Map For INA226
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
INA226 KEYWORD1
# Methods and Functions (KEYWORD2)
@ -12,6 +12,10 @@ getBusVoltage KEYWORD2
getPower KEYWORD2
getCurrent KEYWORD2
getShuntVoltage_mV KEYWORD2
getPower_mW KEYWORD2
getCurrent_mA KEYWORD2
reset KEYWORD2
setAverage KEYWORD2
getAverage KEYWORD2
@ -22,10 +26,13 @@ getShuntVoltageConversionTime KEYWORD2
setMaxCurrentShunt KEYWORD2
getCurrentLSB KEYWORD2
getShunt KEYWORD2
getMaxCurrent KEYWORD2
setMode KEYWORD2
getMode KEYWORD2
shutDown KEYWORD2
setModeShuntTrigger KEYWORD2
setModeBusTrigger KEYWORD2
setModeShuntBusTrigger KEYWORD2
@ -44,3 +51,17 @@ getDieID KEYWORD2
# Constants (LITERAL1)
INA226_LIB_VERSION LITERAL1
INA226_SHUNT_OVER_VOLTAGE LITERAL1
INA226_SHUNT_UNDER_VOLTAGE LITERAL1
INA226_BUS_OVER_VOLTAGE LITERAL1
INA226_BUS_UNDER_VOLTAGE LITERAL1
INA226_POWER_OVER_LIMIT LITERAL1
INA226_CONVERSION_READY LITERAL1
INA226_ALERT_FUNCTION_FLAG LITERAL1
INA226_CONVERSION_READY_FLAG LITERAL1
INA226_MATH_OVERFLOW_FLAG LITERAL1
INA226_ALERT_POLARITY_FLAG LITERAL1
INA226_ALERT_LATCH_ENABLE_FLAG LITERAL1

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA226.git"
},
"version": "0.1.0",
"version": "0.1.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,5 +1,5 @@
name=INA226
version=0.1.0
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for INA226 power sensor

View File

@ -48,12 +48,103 @@ unittest_teardown()
unittest(test_constructor)
{
fprintf(stderr, "\nVERSION: %s\n", INA226_LIB_VERSION);
INA226 INA();
INA226 INA(0x40);
assertTrue(1);
assertTrue(INA.begin());
assertTrue(INA.isConnected());
}
unittest(test_constants)
{
fprintf(stderr, "\nVERSION: %s\n", INA226_LIB_VERSION);
assertEqual(0x8000, INA226_SHUNT_OVER_VOLTAGE);
assertEqual(0x4000, INA226_SHUNT_UNDER_VOLTAGE);
assertEqual(0x2000, INA226_BUS_OVER_VOLTAGE);
assertEqual(0x1000, INA226_BUS_UNDER_VOLTAGE);
assertEqual(0x0800, INA226_POWER_OVER_LIMIT);
assertEqual(0x0400, INA226_CONVERSION_READY);
assertEqual(0x0010, INA226_ALERT_FUNCTION_FLAG);
assertEqual(0x0008, INA226_CONVERSION_READY_FLAG);
assertEqual(0x0004, INA226_MATH_OVERFLOW_FLAG);
assertEqual(0x0002, INA226_ALERT_POLARITY_FLAG);
assertEqual(0x0001, INA226_ALERT_LATCH_ENABLE_FLAG);
}
unittest(test_core_functions)
{
INA226 INA(0x40);
// assertTrue(INA.begin());
fprintf(stderr, "need mock up\n");
/*
fprintf(stderr, "%f\n", INA.getShuntVoltage());
fprintf(stderr, "%f\n", INA.getBusVoltage());
fprintf(stderr, "%f\n", INA.getPower());
fprintf(stderr, "%f\n", INA.getCurrent());
*/
}
unittest(test_configuration)
{
INA226 INA(0x40);
// assertTrue(INA.begin());
// only errors can be tested
assertFalse(INA.setAverage(8));
assertFalse(INA.setAverage(255));
assertFalse(INA.setBusVoltageConversionTime(8));
assertFalse(INA.setBusVoltageConversionTime(255));
assertFalse(INA.setShuntVoltageConversionTime(8));
assertFalse(INA.setShuntVoltageConversionTime(255));
}
unittest(test_calibration)
{
INA226 INA(0x40);
// assertTrue(INA.begin());
// only errors can be tested
assertFalse(INA.setMaxCurrentShunt(30));
assertFalse(INA.setMaxCurrentShunt(0.0009));
assertFalse(INA.setMaxCurrentShunt(0));
assertFalse(INA.setMaxCurrentShunt(-1));
assertFalse(INA.setMaxCurrentShunt(10, 0));
assertFalse(INA.setMaxCurrentShunt(10, 0.0009));
}
unittest(test_setMode)
{
INA226 INA(0x40);
// assertTrue(INA.begin());
// only errors can be tested
assertFalse(INA.setMode(8));
assertFalse(INA.setMode(255));
assertFalse(INA.setMode(-1));
/*
assertTrue(INA.shutDown());
assertTrue(INA.setModeShuntTrigger());
assertTrue(INA.setModeBusTrigger());
assertTrue(INA.setModeShuntBusTrigger());
assertTrue(INA.setModeShuntContinuous());
assertTrue(INA.setModeBusContinuous());
assertTrue(INA.setModeShuntBusContinuous());
*/
}
unittest_main()
// --------