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

143 lines
4.0 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-07-02 04:42:38 -04:00
// VERSION: 0.3.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-07-02 04:42:38 -04:00
#define INA226_LIB_VERSION (F("0.3.0"))
2021-05-30 08:22:31 -04:00
// set by setAlertRegister
#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
// returned by getAlertFlag
#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
class INA226
{
public:
// 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
2021-05-30 08:22:31 -04:00
// Core functions
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
2021-06-22 14:38:00 -04:00
// scale helpers
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
// Configuration
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
2021-05-30 08:22:31 -04:00
// Calibration
2021-06-22 09:22:35 -04:00
// mandatory to set these!
// maxCurrent = 0.001 .. 20
// shunt >= 0.001
2022-02-08 11:18:33 -05:00
bool setMaxCurrentShunt(float macCurrent = 20.0,
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
// these 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
// 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
// Alert
// - separate functions per flag?
// - what is a reasonable limit?
// - which units to define a limit per mask ?
// same as voltage registers ?
2021-12-20 02:48:45 -05:00
// - 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();
// Meta information
uint16_t getManufacturerID(); // should return 0x5449
uint16_t getDieID(); // should return 0x2260
2021-06-22 09:22:35 -04:00
// DEBUG
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