2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: ADS1X15.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-17 05:10:46 -04:00
|
|
|
// VERSION: 0.3.8
|
2021-01-29 06:31:58 -05:00
|
|
|
// DATE: 2013-03-24
|
|
|
|
// PUPROSE: Arduino library for ADS1015 and ADS1115
|
|
|
|
// URL: https://github.com/RobTillaart/ADS1X15
|
|
|
|
|
|
|
|
|
|
|
|
#include "ADS1X15.h"
|
|
|
|
|
|
|
|
#define ADS1015_CONVERSION_DELAY 1
|
|
|
|
#define ADS1115_CONVERSION_DELAY 8
|
|
|
|
|
|
|
|
|
2021-10-08 03:28:06 -04:00
|
|
|
// Kept #defines a bit in line with Adafruit library.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
// REGISTERS
|
|
|
|
#define ADS1X15_REG_CONVERT 0x00
|
|
|
|
#define ADS1X15_REG_CONFIG 0x01
|
|
|
|
#define ADS1X15_REG_LOW_THRESHOLD 0x02
|
|
|
|
#define ADS1X15_REG_HIGH_THRESHOLD 0x03
|
|
|
|
|
|
|
|
|
|
|
|
// CONFIG REGISTER
|
|
|
|
|
|
|
|
// BIT 15 Operational Status // 1 << 15
|
|
|
|
#define ADS1X15_OS_BUSY 0x0000
|
|
|
|
#define ADS1X15_OS_NOT_BUSY 0x8000
|
|
|
|
#define ADS1X15_OS_START_SINGLE 0x8000
|
|
|
|
|
|
|
|
// BIT 12-14 read differential
|
|
|
|
#define ADS1X15_MUX_DIFF_0_1 0x0000
|
|
|
|
#define ADS1X15_MUX_DIFF_0_3 0x1000
|
|
|
|
#define ADS1X15_MUX_DIFF_1_3 0x2000
|
|
|
|
#define ADS1X15_MUX_DIFF_2_3 0x3000
|
|
|
|
// read single
|
|
|
|
#define ADS1X15_READ_0 0x4000 // pin << 12
|
|
|
|
#define ADS1X15_READ_1 0x5000 // pin = 0..3
|
|
|
|
#define ADS1X15_READ_2 0x6000
|
|
|
|
#define ADS1X15_READ_3 0x7000
|
|
|
|
|
|
|
|
|
|
|
|
// BIT 9-11 gain // (0..5) << 9
|
|
|
|
#define ADS1X15_PGA_6_144V 0x0000 // voltage
|
|
|
|
#define ADS1X15_PGA_4_096V 0x0200 //
|
|
|
|
#define ADS1X15_PGA_2_048V 0x0400 // default
|
|
|
|
#define ADS1X15_PGA_1_024V 0x0600
|
|
|
|
#define ADS1X15_PGA_0_512V 0x0800
|
|
|
|
#define ADS1X15_PGA_0_256V 0x0A00
|
|
|
|
|
|
|
|
// BIT 8 mode // 1 << 8
|
|
|
|
#define ADS1X15_MODE_CONTINUE 0x0000
|
|
|
|
#define ADS1X15_MODE_SINGLE 0x0100
|
|
|
|
|
2021-10-08 03:28:06 -04:00
|
|
|
// BIT 5-7 data rate sample per second // (0..7) << 5
|
2021-01-29 06:31:58 -05:00
|
|
|
/*
|
|
|
|
differs for different devices, check datasheet or readme.md
|
2021-07-14 04:59:33 -04:00
|
|
|
|
|
|
|
| data rate | ADS101x | ADS 111x | Notes |
|
|
|
|
|:---------:|--------:|---------:|:-------:|
|
|
|
|
| 0 | 128 | 8 | slowest |
|
|
|
|
| 1 | 250 | 16 | |
|
|
|
|
| 2 | 490 | 32 | |
|
|
|
|
| 3 | 920 | 64 | |
|
|
|
|
| 4 | 1600 | 128 | default |
|
|
|
|
| 5 | 2400 | 250 | |
|
|
|
|
| 6 | 3300 | 475 | |
|
|
|
|
| 7 | 3300 | 860 | fastest |
|
2021-01-29 06:31:58 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
// BIT 4 comparator modi // 1 << 4
|
|
|
|
#define ADS1X15_COMP_MODE_TRADITIONAL 0x0000
|
|
|
|
#define ADS1X15_COMP_MODE_WINDOW 0x0010
|
|
|
|
|
|
|
|
// BIT 3 ALERT active value // 1 << 3
|
|
|
|
#define ADS1X15_COMP_POL_ACTIV_LOW 0x0000
|
|
|
|
#define ADS1X15_COMP_POL_ACTIV_HIGH 0x0008
|
|
|
|
|
|
|
|
// BIT 2 ALERT latching // 1 << 2
|
|
|
|
#define ADS1X15_COMP_NON_LATCH 0x0000
|
|
|
|
#define ADS1X15_COMP_LATCH 0x0004
|
|
|
|
|
|
|
|
// BIT 0-1 ALERT mode // (0..3)
|
|
|
|
#define ADS1X15_COMP_QUE_1_CONV 0x0000 // trigger alert after 1 convert
|
|
|
|
#define ADS1X15_COMP_QUE_2_CONV 0x0001 // trigger alert after 2 converts
|
|
|
|
#define ADS1X15_COMP_QUE_4_CONV 0x0002 // trigger alert after 4 converts
|
2021-12-11 10:46:06 -05:00
|
|
|
#define ADS1X15_COMP_QUE_NONE 0x0003 // disable comparator
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// _CONFIG masks
|
|
|
|
//
|
2021-10-08 03:28:06 -04:00
|
|
|
// | bit | description |
|
|
|
|
// |:----:|:---------------------|
|
|
|
|
// | 0 | # channels |
|
|
|
|
// | 1 | - |
|
|
|
|
// | 2 | resolution |
|
|
|
|
// | 3 | - |
|
|
|
|
// | 4 | GAIN supported |
|
2021-01-29 06:31:58 -05:00
|
|
|
// | 5 | COMPARATOR supported |
|
2021-10-08 03:28:06 -04:00
|
|
|
// | 6 | - |
|
|
|
|
// | 7 | - |
|
2021-07-14 04:59:33 -04:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
#define ADS_CONF_CHAN_1 0x00
|
|
|
|
#define ADS_CONF_CHAN_4 0x01
|
|
|
|
#define ADS_CONF_RES_12 0x00
|
|
|
|
#define ADS_CONF_RES_16 0x04
|
|
|
|
#define ADS_CONF_NOGAIN 0x00
|
|
|
|
#define ADS_CONF_GAIN 0x10
|
|
|
|
#define ADS_CONF_NOCOMP 0x00
|
|
|
|
#define ADS_CONF_COMP 0x20
|
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// BASE CONSTRUCTOR
|
|
|
|
//
|
|
|
|
ADS1X15::ADS1X15()
|
|
|
|
{
|
2021-04-25 13:56:44 -04:00
|
|
|
reset();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PUBLIC
|
|
|
|
//
|
2021-04-25 13:56:44 -04:00
|
|
|
void ADS1X15::reset()
|
|
|
|
{
|
|
|
|
setGain(0); // _gain = ADS1X15_PGA_6_144V;
|
|
|
|
setMode(1); // _mode = ADS1X15_MODE_SINGLE;
|
|
|
|
setDataRate(4); // middle speed, depends on device.
|
|
|
|
|
2021-10-08 03:28:06 -04:00
|
|
|
// COMPARATOR variables # see notes .h
|
2021-04-25 13:56:44 -04:00
|
|
|
_compMode = 0;
|
|
|
|
_compPol = 1;
|
|
|
|
_compLatch = 0;
|
|
|
|
_compQueConvert = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#if defined (ESP8266) || defined(ESP32)
|
2022-06-21 04:53:30 -04:00
|
|
|
bool ADS1X15::begin(int sda, int scl)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = &Wire;
|
|
|
|
_wire->begin(sda, scl);
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((_address < 0x48) || (_address > 0x4B)) return false;
|
2021-04-07 07:31:22 -04:00
|
|
|
if (! isConnected()) return false;
|
2021-01-29 06:31:58 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-10-17 05:10:46 -04:00
|
|
|
#if defined (ARDUINO_ARCH_RP2040)
|
|
|
|
|
|
|
|
bool ADS1X15::begin(int sda, int scl)
|
|
|
|
{
|
|
|
|
_wire->setSDA(sda);
|
|
|
|
_wire->setSCL(scl);
|
|
|
|
_wire->begin();
|
|
|
|
if ((_address < 0x48) || (_address > 0x4B)) return false;
|
|
|
|
if (! isConnected()) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool ADS1X15::begin()
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire->begin();
|
2021-01-29 06:31:58 -05:00
|
|
|
if ((_address < 0x48) || (_address > 0x4B)) return false;
|
2021-04-07 07:31:22 -04:00
|
|
|
if (! isConnected()) return false;
|
2021-01-29 06:31:58 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool ADS1X15::isBusy()
|
2021-10-08 03:28:06 -04:00
|
|
|
{
|
|
|
|
return isReady() == false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ADS1X15::isReady()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
uint16_t val = _readRegister(_address, ADS1X15_REG_CONFIG);
|
2021-10-08 03:28:06 -04:00
|
|
|
return ((val & ADS1X15_OS_NOT_BUSY) > 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
bool ADS1X15::isConnected()
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire->beginTransmission(_address);
|
|
|
|
return (_wire->endTransmission() == 0);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::setGain(uint8_t gain)
|
|
|
|
{
|
|
|
|
if (!(_config & ADS_CONF_GAIN)) gain = 0;
|
|
|
|
switch (gain)
|
|
|
|
{
|
|
|
|
default: // catch invalid values and go for the safest gain.
|
|
|
|
case 0: _gain = ADS1X15_PGA_6_144V; break;
|
|
|
|
case 1: _gain = ADS1X15_PGA_4_096V; break;
|
|
|
|
case 2: _gain = ADS1X15_PGA_2_048V; break;
|
|
|
|
case 4: _gain = ADS1X15_PGA_1_024V; break;
|
|
|
|
case 8: _gain = ADS1X15_PGA_0_512V; break;
|
|
|
|
case 16: _gain = ADS1X15_PGA_0_256V; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t ADS1X15::getGain()
|
|
|
|
{
|
|
|
|
if (!(_config & ADS_CONF_GAIN)) return 0;
|
|
|
|
switch (_gain)
|
|
|
|
{
|
|
|
|
case ADS1X15_PGA_6_144V: return 0;
|
|
|
|
case ADS1X15_PGA_4_096V: return 1;
|
|
|
|
case ADS1X15_PGA_2_048V: return 2;
|
|
|
|
case ADS1X15_PGA_1_024V: return 4;
|
|
|
|
case ADS1X15_PGA_0_512V: return 8;
|
|
|
|
case ADS1X15_PGA_0_256V: return 16;
|
|
|
|
}
|
|
|
|
_err = ADS1X15_INVALID_GAIN;
|
|
|
|
return _err;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
float ADS1X15::toVoltage(int16_t value)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-12-11 10:46:06 -05:00
|
|
|
if (value == 0) return 0;
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
float volts = getMaxVoltage();
|
|
|
|
if (volts < 0) return volts;
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
volts *= value;
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_config & ADS_CONF_RES_16)
|
|
|
|
{
|
2021-12-11 10:46:06 -05:00
|
|
|
volts /= 32767; // value = 16 bits - sign bit = 15 bits mantissa
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-11 10:46:06 -05:00
|
|
|
volts /= 2047; // value = 12 bits - sign bit = 11 bit mantissa
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
return volts;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
float ADS1X15::getMaxVoltage()
|
|
|
|
{
|
|
|
|
switch (_gain)
|
|
|
|
{
|
|
|
|
case ADS1X15_PGA_6_144V: return 6.144;
|
|
|
|
case ADS1X15_PGA_4_096V: return 4.096;
|
|
|
|
case ADS1X15_PGA_2_048V: return 2.048;
|
|
|
|
case ADS1X15_PGA_1_024V: return 1.024;
|
|
|
|
case ADS1X15_PGA_0_512V: return 0.512;
|
|
|
|
case ADS1X15_PGA_0_256V: return 0.256;
|
|
|
|
}
|
|
|
|
_err = ADS1X15_INVALID_VOLTAGE;
|
|
|
|
return _err;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::setMode(uint8_t mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case 0: _mode = ADS1X15_MODE_CONTINUE; break;
|
|
|
|
default:
|
|
|
|
case 1: _mode = ADS1X15_MODE_SINGLE; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t ADS1X15::getMode(void)
|
|
|
|
{
|
|
|
|
switch (_mode)
|
|
|
|
{
|
|
|
|
case ADS1X15_MODE_CONTINUE: return 0;
|
|
|
|
case ADS1X15_MODE_SINGLE: return 1;
|
|
|
|
}
|
|
|
|
_err = ADS1X15_INVALID_MODE;
|
|
|
|
return _err;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::setDataRate(uint8_t dataRate)
|
|
|
|
{
|
|
|
|
_datarate = dataRate;
|
|
|
|
if (_datarate > 7) _datarate = 4; // default
|
|
|
|
_datarate <<= 5; // convert 0..7 to mask needed.
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t ADS1X15::getDataRate(void)
|
|
|
|
{
|
2021-04-25 13:56:44 -04:00
|
|
|
return (_datarate >> 5) & 0x07; // convert mask back to 0..7
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1X15::readADC(uint8_t pin)
|
|
|
|
{
|
|
|
|
if (pin >= _maxPorts) return 0;
|
|
|
|
uint16_t mode = ((4 + pin) << 12); // pin to mask
|
|
|
|
return _readADC(mode);
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::requestADC_Differential_0_1()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_0_1);
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1X15::readADC_Differential_0_1()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_0_1);
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::requestADC(uint8_t pin)
|
|
|
|
{
|
|
|
|
if (pin >= _maxPorts) return;
|
|
|
|
uint16_t mode = ((4 + pin) << 12); // pin to mask
|
|
|
|
_requestADC(mode);
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1X15::getValue()
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
int16_t raw = _readRegister(_address, ADS1X15_REG_CONVERT);
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_bitShift) raw >>= _bitShift; // Shift 12-bit results
|
|
|
|
return raw;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::setComparatorThresholdLow(int16_t lo)
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
_writeRegister(_address, ADS1X15_REG_LOW_THRESHOLD, lo);
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1X15::getComparatorThresholdLow()
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
return _readRegister(_address, ADS1X15_REG_LOW_THRESHOLD);
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::setComparatorThresholdHigh(int16_t hi)
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
_writeRegister(_address, ADS1X15_REG_HIGH_THRESHOLD, hi);
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1X15::getComparatorThresholdHigh()
|
|
|
|
{
|
2021-04-07 07:31:22 -04:00
|
|
|
return _readRegister(_address, ADS1X15_REG_HIGH_THRESHOLD);
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
|
|
|
int8_t ADS1X15::getError()
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
int8_t rv = _err;
|
|
|
|
_err = ADS1X15_OK;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:56:44 -04:00
|
|
|
|
|
|
|
void ADS1X15::setWireClock(uint32_t clockSpeed)
|
|
|
|
{
|
|
|
|
_clockSpeed = clockSpeed;
|
|
|
|
_wire->setClock(_clockSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-01-21 13:23:09 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// EXPERIMENTAL
|
|
|
|
//
|
|
|
|
// see https://github.com/RobTillaart/ADS1X15/issues/22
|
|
|
|
// https://github.com/arduino/Arduino/issues/11457
|
2021-04-25 13:56:44 -04:00
|
|
|
// TODO: get the real clock speed from the I2C interface if possible.
|
|
|
|
uint32_t ADS1X15::getWireClock()
|
|
|
|
{
|
2022-01-21 13:23:09 -05:00
|
|
|
// UNO 328 and
|
|
|
|
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
|
2021-04-25 13:56:44 -04:00
|
|
|
uint32_t speed = F_CPU / ((TWBR * 2) + 16);
|
|
|
|
return speed;
|
|
|
|
|
|
|
|
#elif defined(ESP32)
|
|
|
|
return (uint32_t) _wire->getClock();
|
|
|
|
|
|
|
|
// #elif defined(ESP8266)
|
|
|
|
// core_esp8266_si2c.cpp holds the data see => void Twi::setClock(
|
|
|
|
// not supported.
|
|
|
|
// return -1;
|
|
|
|
|
2022-01-21 13:23:09 -05:00
|
|
|
#else // best effort is remembering it
|
2021-04-25 13:56:44 -04:00
|
|
|
return _clockSpeed;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
// PROTECTED
|
2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
int16_t ADS1X15::_readADC(uint16_t readmode)
|
|
|
|
{
|
|
|
|
_requestADC(readmode);
|
|
|
|
if (_mode == ADS1X15_MODE_SINGLE)
|
|
|
|
{
|
|
|
|
while ( isBusy() ) yield(); // wait for conversion; yield for ESP.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delay(_conversionDelay); // TODO needed in continuous mode?
|
|
|
|
}
|
|
|
|
return getValue();
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1X15::_requestADC(uint16_t readmode)
|
|
|
|
{
|
|
|
|
// write to register is needed in continuous mode as other flags can be changed
|
|
|
|
uint16_t config = ADS1X15_OS_START_SINGLE; // bit 15 force wake up if needed
|
|
|
|
config |= readmode; // bit 12-14
|
|
|
|
config |= _gain; // bit 9-11
|
|
|
|
config |= _mode; // bit 8
|
|
|
|
config |= _datarate; // bit 5-7
|
|
|
|
if (_compMode) config |= ADS1X15_COMP_MODE_WINDOW; // bit 4 comparator modi
|
|
|
|
else config |= ADS1X15_COMP_MODE_TRADITIONAL;
|
|
|
|
if (_compPol) config |= ADS1X15_COMP_POL_ACTIV_HIGH; // bit 3 ALERT active value
|
|
|
|
else config |= ADS1X15_COMP_POL_ACTIV_LOW;
|
|
|
|
if (_compLatch) config |= ADS1X15_COMP_LATCH;
|
|
|
|
else config |= ADS1X15_COMP_NON_LATCH; // bit 2 ALERT latching
|
|
|
|
config |= _compQueConvert; // bit 0..1 ALERT mode
|
2021-04-07 07:31:22 -04:00
|
|
|
_writeRegister(_address, ADS1X15_REG_CONFIG, config);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-04-07 07:31:22 -04:00
|
|
|
bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
|
|
|
|
{
|
|
|
|
_wire->beginTransmission(address);
|
|
|
|
_wire->write((uint8_t)reg);
|
|
|
|
_wire->write((uint8_t)(value >> 8));
|
|
|
|
_wire->write((uint8_t)(value & 0xFF));
|
|
|
|
return (_wire->endTransmission() == 0);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-04-07 07:31:22 -04:00
|
|
|
uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
|
|
|
|
{
|
|
|
|
_wire->beginTransmission(address);
|
|
|
|
_wire->write(reg);
|
|
|
|
_wire->endTransmission();
|
|
|
|
|
2022-01-21 13:23:09 -05:00
|
|
|
int rv = _wire->requestFrom((int) address, (int) 2);
|
2021-10-08 03:28:06 -04:00
|
|
|
if (rv == 2)
|
2021-04-07 07:31:22 -04:00
|
|
|
{
|
|
|
|
uint16_t value = _wire->read() << 8;
|
|
|
|
value += _wire->read();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return 0x0000;
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1013
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1013::ADS1013(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_NOCOMP | ADS_CONF_NOGAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_1;
|
|
|
|
_conversionDelay = ADS1015_CONVERSION_DELAY;
|
|
|
|
_bitShift = 4;
|
|
|
|
_maxPorts = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1014
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1014::ADS1014(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_1;
|
|
|
|
_conversionDelay = ADS1015_CONVERSION_DELAY;
|
|
|
|
_bitShift = 4;
|
|
|
|
_maxPorts = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1015
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1015::ADS1015(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_4;
|
|
|
|
_conversionDelay = ADS1015_CONVERSION_DELAY;
|
|
|
|
_bitShift = 4;
|
|
|
|
_maxPorts = 4;
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1015::readADC_Differential_0_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_0_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1015::readADC_Differential_1_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_1_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1015::readADC_Differential_2_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_2_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1015::readADC_Differential_0_2()
|
|
|
|
{
|
|
|
|
return readADC(2) - readADC(0);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1015::readADC_Differential_1_2()
|
|
|
|
{
|
|
|
|
return readADC(2) - readADC(1);;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ADS1015::requestADC_Differential_0_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_0_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1015::requestADC_Differential_1_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_1_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1015::requestADC_Differential_2_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_2_3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1113
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1113::ADS1113(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_NOCOMP | ADS_CONF_NOGAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_1;
|
|
|
|
_conversionDelay = ADS1115_CONVERSION_DELAY;
|
|
|
|
_bitShift = 0;
|
|
|
|
_maxPorts = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1114
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1114::ADS1114(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_1;
|
|
|
|
_conversionDelay = ADS1115_CONVERSION_DELAY;
|
|
|
|
_bitShift = 0;
|
|
|
|
_maxPorts = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ADS1115
|
|
|
|
//
|
2021-04-07 07:31:22 -04:00
|
|
|
ADS1115::ADS1115(uint8_t address, TwoWire *wire)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
_address = address;
|
2021-04-07 07:31:22 -04:00
|
|
|
_wire = wire;
|
2021-01-29 06:31:58 -05:00
|
|
|
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_4;
|
|
|
|
_conversionDelay = ADS1115_CONVERSION_DELAY;
|
|
|
|
_bitShift = 0;
|
|
|
|
_maxPorts = 4;
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1115::readADC_Differential_0_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_0_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1115::readADC_Differential_1_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_1_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1115::readADC_Differential_2_3()
|
|
|
|
{
|
|
|
|
return _readADC(ADS1X15_MUX_DIFF_2_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1115::readADC_Differential_0_2()
|
|
|
|
{
|
|
|
|
return readADC(2) - readADC(0);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t ADS1115::readADC_Differential_1_2()
|
|
|
|
{
|
|
|
|
return readADC(2) - readADC(1);;
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1115::requestADC_Differential_0_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_0_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1115::requestADC_Differential_1_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_1_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void ADS1115::requestADC_Differential_2_3()
|
|
|
|
{
|
|
|
|
_requestADC(ADS1X15_MUX_DIFF_2_3);
|
|
|
|
}
|
|
|
|
|
2021-12-11 10:46:06 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// --- END OF FILE
|
2021-12-11 10:46:06 -05:00
|
|
|
|