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

151 lines
4.3 KiB
C
Raw Normal View History

2021-05-30 08:22:31 -04:00
#pragma once
2022-02-08 11:18:33 -05:00
// FILE: INA226.h
2021-05-30 08:22:31 -04:00
// AUTHOR: Rob Tillaart
2022-08-28 01:32:00 -04:00
// VERSION: 0.4.0
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
//
// Read the datasheet for the details
//
#include "Arduino.h"
#include "Wire.h"
2022-08-28 01:32:00 -04:00
#define INA226_LIB_VERSION (F("0.4.0"))
2021-05-30 08:22:31 -04:00
2022-08-28 01:32:00 -04:00
// set by setAlertRegister
2021-05-30 08:22:31 -04:00
#define INA226_SHUNT_OVER_VOLTAGE 0x8000
#define INA226_SHUNT_UNDER_VOLTAGE 0x4000
#define INA226_BUS_OVER_VOLTAGE 0x2000
#define INA226_BUS_UNDER_VOLTAGE 0x1000
#define INA226_POWER_OVER_LIMIT 0x0800
#define INA226_CONVERSION_READY 0x0400
2022-08-28 01:32:00 -04:00
// returned by getAlertFlag
2021-05-30 08:22:31 -04:00
#define INA226_ALERT_FUNCTION_FLAG 0x0010
#define INA226_CONVERSION_READY_FLAG 0x0008
#define INA226_MATH_OVERFLOW_FLAG 0x0004
#define INA226_ALERT_POLARITY_FLAG 0x0002
#define INA226_ALERT_LATCH_ENABLE_FLAG 0x0001
2022-08-28 01:32:00 -04:00
// returned by setMaxCurrentShunt
#define INA226_ERR_NONE 0x0000
#define INA226_ERR_SHUNTVOLTAGE_HIGH 0x8000
#define INA226_ERR_MAXCURRENT_LOW 0x8001
#define INA226_ERR_SHUNT_LOW 0x8002
2021-05-30 08:22:31 -04:00
class INA226
{
public:
2022-08-28 01:32:00 -04:00
// address between 0x40 and 0x4F
2021-11-05 14:39:16 -04:00
explicit INA226(const uint8_t address, TwoWire *wire = &Wire);
2021-05-30 08:22:31 -04:00
#if defined (ESP8266) || defined(ESP32)
bool begin(const uint8_t sda, const uint8_t scl);
#endif
bool begin();
bool isConnected();
2022-07-02 04:42:38 -04:00
uint8_t getAddress();
2021-05-30 08:22:31 -04:00
2021-06-22 09:22:35 -04:00
2022-08-28 01:32:00 -04:00
// Core functions
2021-05-30 08:22:31 -04:00
float getBusVoltage();
2021-06-22 14:38:00 -04:00
float getShuntVoltage();
2021-05-30 08:22:31 -04:00
float getCurrent();
2021-06-22 14:38:00 -04:00
float getPower();
2021-11-05 14:39:16 -04:00
2022-08-28 01:32:00 -04:00
// Scale helpers
2021-06-22 14:38:00 -04:00
float getBusVoltage_mV() { return getBusVoltage() * 1e3; };
float getShuntVoltage_mV() { return getShuntVoltage() * 1e3; };
float getCurrent_mA() { return getCurrent() * 1e3; };
float getPower_mW() { return getPower() * 1e3; };
float getShuntVoltage_uV() { return getShuntVoltage() * 1e6; };
float getCurrent_uA() { return getCurrent() * 1e6; };
float getPower_uW() { return getPower() * 1e6; };
2021-06-22 09:22:35 -04:00
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
void reset();
2021-06-22 09:22:35 -04:00
bool setAverage(uint8_t avg = 0);
2021-05-30 08:22:31 -04:00
uint8_t getAverage();
2021-06-22 09:22:35 -04:00
bool setBusVoltageConversionTime(uint8_t bvct = 4);
2021-05-30 08:22:31 -04:00
uint8_t getBusVoltageConversionTime();
2021-06-22 09:22:35 -04:00
bool setShuntVoltageConversionTime(uint8_t svct = 4);
2021-05-30 08:22:31 -04:00
uint8_t getShuntVoltageConversionTime();
2021-06-22 09:22:35 -04:00
2022-08-28 01:32:00 -04:00
// Calibration
// mandatory to set these!
// shunt * maxCurrent < 81 mV
// maxCurrent >= 0.001
// shunt >= 0.001
int setMaxCurrentShunt(float macCurrent = 20.0,
2022-02-08 11:18:33 -05:00
float shunt = 0.002,
2021-06-22 09:22:35 -04:00
bool normalize = true);
2022-02-08 11:18:33 -05:00
bool isCalibrated() { return _current_LSB != 0.0; };
2021-06-22 09:22:35 -04:00
2022-08-28 01:32:00 -04:00
// These functions return zero if not calibrated!
2021-06-22 14:38:00 -04:00
float getCurrentLSB() { return _current_LSB; };
float getCurrentLSB_mA() { return _current_LSB * 1e3; };
float getCurrentLSB_uA() { return _current_LSB * 1e6; };
float getShunt() { return _shunt; };
float getMaxCurrent() { return _maxCurrent; };
2021-05-30 08:22:31 -04:00
2022-08-28 01:32:00 -04:00
// Operating mode
2021-06-22 09:22:35 -04:00
bool setMode(uint8_t mode = 7);
2021-05-30 08:22:31 -04:00
uint8_t getMode();
2021-06-22 09:22:35 -04:00
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.
2021-05-30 08:22:31 -04:00
2022-08-28 01:32:00 -04:00
// Alert
// - separate functions per flag?
// - what is a reasonable limit?
// - which units to define a limit per mask ?
// same as voltage registers ?
// - how to test
2021-05-30 08:22:31 -04:00
void setAlertRegister(uint16_t mask);
uint16_t getAlertFlag();
void setAlertLimit(uint16_t limit);
uint16_t getAlertLimit();
2022-08-28 01:32:00 -04:00
// Meta information
2021-05-30 08:22:31 -04:00
uint16_t getManufacturerID(); // should return 0x5449
uint16_t getDieID(); // should return 0x2260
2022-08-28 01:32:00 -04:00
// DEBUG
2021-06-22 09:22:35 -04:00
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
2021-05-30 08:22:31 -04:00
private:
uint16_t _readRegister(uint8_t reg);
uint16_t _writeRegister(uint8_t reg, uint16_t value);
float _current_LSB;
2021-06-22 09:22:35 -04:00
float _shunt;
float _maxCurrent;
2021-05-30 08:22:31 -04:00
uint8_t _address;
TwoWire * _wire;
};
2021-11-05 14:39:16 -04:00
2021-05-30 08:22:31 -04:00
// -- END OF FILE --
2021-12-20 02:48:45 -05:00