From d26f8757632257d8606fa684b89255331e3c61d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sun, 17 Dec 2017 17:27:49 +0100 Subject: [PATCH] Remove dependency on I2Cdevlib by default --- code/espurna/config/prototypes.h | 2 +- code/espurna/config/sensors.h | 2 +- code/espurna/sensors/EmonADS1X15Sensor.h | 50 ++++++++++++++---------- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 2d62b486..4ace7b9c 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -78,7 +78,7 @@ template bool idbSend(const char * topic, unsigned char id, T payloa #if DS18B20_SUPPORT #include #endif -#if EMON_ADS1X15_SUPPORT & EMON_ADSX115_USE_I2CDEVLIB +#if EMON_ADS1X15_SUPPORT & EMON_ADS1X15_USE_I2CDEVLIB #include #endif diff --git a/code/espurna/config/sensors.h b/code/espurna/config/sensors.h index 7a593a84..be574c8b 100644 --- a/code/espurna/config/sensors.h +++ b/code/espurna/config/sensors.h @@ -189,7 +189,7 @@ #define EMON_ADS1X15_SUPPORT 1 // Do not build support by default #endif -#define EMON_ADSX115_USE_I2CDEVLIB 1 // Use I2CDevLib +#define EMON_ADS1X15_USE_I2CDEVLIB 0 // Use I2CDevLib #define EMON_ADS1X15_ADS1115 1 // 0 for ADS10115, 1 for ADS1115 #define EMON_ADS1X15_PORT_MASK 0x08 // A0=1 A1=2 A2=4 A4=8 diff --git a/code/espurna/sensors/EmonADS1X15Sensor.h b/code/espurna/sensors/EmonADS1X15Sensor.h index b5e3309f..e68b9c31 100644 --- a/code/espurna/sensors/EmonADS1X15Sensor.h +++ b/code/espurna/sensors/EmonADS1X15Sensor.h @@ -8,7 +8,7 @@ #include "BaseSensor.h" #include "EmonSensor.h" -#if EMON_ADSX115_USE_I2CDEVLIB +#if EMON_ADS1X15_USE_I2CDEVLIB #include #else #if I2C_USE_BRZO @@ -91,6 +91,7 @@ #define ADS1X15_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions #define ADS1X15_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions #define ADS1X15_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default) + #define ADS1X15_CHANNELS 4 #define EMON_ADS1X15_MAGNITUDES_PER_PORT 2 @@ -206,7 +207,7 @@ class EmonADS1X15Sensor : public EmonSensor { //---------------------------------------------------------------------- void init() { - #if EMON_ADSX115_USE_I2CDEVLIB + #if EMON_ADS1X15_USE_I2CDEVLIB _ads = new ADS1115(_address); _ads->initialize(); _ads->setMode(ADS1115_MODE_CONTINUOUS); @@ -215,25 +216,31 @@ class EmonADS1X15Sensor : public EmonSensor { #endif } - #if EMON_ADSX115_USE_I2CDEVLIB == 0 + #if EMON_ADS1X15_USE_I2CDEVLIB == 0 - void setChannel(unsigned char channel, bool continuous) { + void setConfigRegistry(unsigned char channel, bool continuous, bool start) { // Start with default values uint16_t config = 0; - config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range + config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range (0x0200) + config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0) + //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000) + //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000) + //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000) + config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003) + if (start) { + config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000) + } if (continuous) { - config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) + //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000) } else { - config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode - config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Set 'start single-conversion' bit + config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100) } - config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed - config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) - config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) - config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) - config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) - config |= ((channel + 4) << 12); // Set single-ended input channel + config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000) + + #if EMON_DEBUG + Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config); + #endif // Write config register to the ADC #if I2C_USE_BRZO @@ -258,17 +265,20 @@ class EmonADS1X15Sensor : public EmonSensor { double getCurrent(unsigned char channel) { - #if EMON_ADSX115_USE_I2CDEVLIB + #if EMON_ADS1X15_USE_I2CDEVLIB _ads->setMultiplexer(channel + 4); #else // Force stop by setting single mode and back to continuous static unsigned char previous = 9; if (previous != channel) { - setChannel(previous, false); - delay(50); + setConfigRegistry(channel, true, false); + setConfigRegistry(channel, false, false); + setConfigRegistry(channel, false, true); + delay(10); + readADC(channel); previous = channel; } - setChannel(channel, true); + setConfigRegistry(channel, true, true); #endif return read(channel, _pivot[channel]); @@ -279,7 +289,7 @@ class EmonADS1X15Sensor : public EmonSensor { unsigned int value = 0; - #if EMON_ADSX115_USE_I2CDEVLIB + #if EMON_ADS1X15_USE_I2CDEVLIB value = _ads->getConversion(); #elif I2C_USE_BRZO @@ -309,7 +319,7 @@ class EmonADS1X15Sensor : public EmonSensor { } - #if EMON_ADSX115_USE_I2CDEVLIB + #if EMON_ADS1X15_USE_I2CDEVLIB ADS1115 * _ads; #endif