diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ADS1115_WE.cpp | 209 | ||||
-rw-r--r-- | src/ADS1115_WE.h | 137 |
2 files changed, 346 insertions, 0 deletions
diff --git a/src/ADS1115_WE.cpp b/src/ADS1115_WE.cpp new file mode 100644 index 0000000..f1af282 --- /dev/null +++ b/src/ADS1115_WE.cpp @@ -0,0 +1,209 @@ +/*****************************************
+* This is a library for the ADS1115 A/D Converter
+*
+* You'll find an example which should enable you to use the library.
+*
+* You are free to use it, change it or build on it. In case you like
+* it, it would be cool if you give it a star.
+*
+* If you find bugs, please inform me!
+*
+* Written by Wolfgang (Wolle) Ewald
+* https://wolles-elektronikkiste.de
+*
+*******************************************/
+
+#include "ADS1115_WE.h"
+
+ADS1115_WE::ADS1115_WE(int addr){
+ i2cAddress = addr;
+}
+
+ADS1115_WE::ADS1115_WE(){
+ i2cAddress = 0x48;
+}
+
+void ADS1115_WE::reset(){
+ Wire.beginTransmission(0);
+ Wire.write(0x06);
+ Wire.endTransmission();
+}
+
+bool ADS1115_WE::init(){
+ Wire.beginTransmission(i2cAddress);
+ bool success = Wire.endTransmission();
+ if(success){
+ return 0;
+ }
+ writeRegister(ADS1115_CONFIG_REG, ADS1115_REG_RESET_VAL);
+ setVoltageRange_mV(ADS1115_RANGE_2048);
+ return 1;
+}
+
+void ADS1115_WE::setAlertPinMode(ADS1115_COMP_QUE mode){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0003);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setAlertLatch(ADS1115_LATCH latch){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0004);
+ currentConfReg |= latch;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setAlertPol(ADS1115_ALERT_POL polarity){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0008);
+ currentConfReg |= polarity;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hiThres, float loThres){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0010);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+ int16_t alertLimit = calcLimit(hiThres);
+ writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
+ alertLimit = calcLimit(loThres);
+ writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
+
+}
+
+void ADS1115_WE::setConvRate(ADS1115_CONV_RATE rate){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x00E0);
+ currentConfReg |= rate;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setMeasureMode(ADS1115_MEASURE_MODE mode){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x0100);
+ currentConfReg |= mode;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setVoltageRange_mV(ADS1115_RANGE range){
+
+ switch(range){
+ case ADS1115_RANGE_6144:
+ voltageRange = 6144;
+ break;
+ case ADS1115_RANGE_4096:
+ voltageRange = 4096;
+ break;
+ case ADS1115_RANGE_2048:
+ voltageRange = 2048;
+ break;
+ case ADS1115_RANGE_1024:
+ voltageRange = 1024;
+ break;
+ case ADS1115_RANGE_0512:
+ voltageRange = 512;
+ break;
+ case ADS1115_RANGE_0256:
+ voltageRange = 256;
+ break;
+ }
+
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ uint16_t currentRange = (currentConfReg >> 9) & 7;
+ uint16_t currentAlertPinMode = currentConfReg & 3;
+ uint16_t currentCompMode = (currentConfReg>>4) & 1;
+
+ if ((currentRange != range) && (currentAlertPinMode != ADS1115_DISABLE_ALERT)){
+ int16_t alertLimit = readRegister(ADS1115_HI_THRESH_REG);
+ alertLimit = (alertLimit/currentRange) * range;
+ writeRegister(ADS1115_HI_THRESH_REG, alertLimit);
+
+ if(currentCompMode != ADS1115_MAX_LIMIT){
+ int16_t alertLimit = readRegister(ADS1115_LO_THRESH_REG);
+ alertLimit = (alertLimit/currentRange) * range;
+ writeRegister(ADS1115_LO_THRESH_REG, alertLimit);
+ }
+ }
+
+ currentConfReg &= ~(0x0E00);
+ currentConfReg |= range;
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+void ADS1115_WE::setCompareChannels(ADS1115_MUX mux){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg &= ~(0x7000);
+ currentConfReg |= (mux);
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+bool ADS1115_WE::isBusy(){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ return (!(currentConfReg>>15) & 1);
+}
+
+void ADS1115_WE::startSingleMeasurement(){
+ uint16_t currentConfReg = readRegister(ADS1115_CONFIG_REG);
+ currentConfReg |= (1 << 15);
+ writeRegister(ADS1115_CONFIG_REG, currentConfReg);
+}
+
+float ADS1115_WE::getResult_V(){
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange/1000;
+ return result;
+}
+
+float ADS1115_WE::getResult_mV(){
+ int16_t rawResult = readRegister(ADS1115_CONV_REG);
+ float result = (rawResult * 1.0 / ADS1115_REG_FACTOR) * voltageRange;
+ return result;
+}
+
+void ADS1115_WE::setAlertPinToConversionReady(){
+ writeRegister(ADS1115_LO_THRESH_REG, (0<<15));
+ writeRegister(ADS1115_HI_THRESH_REG, (1<<15));
+}
+
+void ADS1115_WE::unlatchAlertPin(){
+ readRegister(ADS1115_CONV_REG);
+}
+
+/************************************************
+ private functions
+*************************************************/
+
+int16_t ADS1115_WE::calcLimit(float rawLimit){
+ int16_t limit = (int16_t)((rawLimit * ADS1115_REG_FACTOR / voltageRange)*1000);
+ return limit;
+}
+
+uint8_t ADS1115_WE::writeRegister(uint8_t reg, uint16_t val){
+ Wire.beginTransmission(i2cAddress);
+ uint8_t lVal = val & 255;
+ uint8_t hVal = val >> 8;
+ Wire.write(reg);
+ Wire.write(hVal);
+ Wire.write(lVal);
+ return Wire.endTransmission();
+}
+
+uint16_t ADS1115_WE::readRegister(uint8_t reg){
+ uint8_t MSByte, LSByte = 0;
+ uint16_t regValue = 0;
+ Wire.beginTransmission(i2cAddress);
+ Wire.write(reg);
+ Wire.endTransmission();
+ Wire.requestFrom(i2cAddress,2);
+ if(Wire.available()){
+ MSByte = Wire.read();
+ LSByte = Wire.read();
+ }
+ regValue = (MSByte<<8) + LSByte;
+ return regValue;
+}
+
+
+
diff --git a/src/ADS1115_WE.h b/src/ADS1115_WE.h index 8b13789..4a671d4 100644 --- a/src/ADS1115_WE.h +++ b/src/ADS1115_WE.h @@ -1 +1,138 @@ +/****************************************************************************** + * + * This is a library for the ADS1115 A/D Converter + * + * You'll find several example sketches which should enable you to use the library. + * + * You are free to use it, change it or build on it. In case you like it, it would + * be cool if you give it a star. + * + * If you find bugs, please inform me! + * + * Written by Wolfgang (Wolle) Ewald + * https://wolles-elektronikkiste.de + * + * + ******************************************************************************/ + +#ifndef ADS1115_WE_H_ +#define ADS1115_WE_H_ + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include <Wire.h> + +/* registers */ +#define ADS1115_CONV_REG 0x00 //Conversion Register +#define ADS1115_CONFIG_REG 0x01 //Configuration Register +#define ADS1115_LO_THRESH_REG 0x02 //Low Threshold Register +#define ADS1115_HI_THRESH_REG 0x03 //High Threshold Register + +/* other */ +#define ADS1115_REG_FACTOR 32768 +#define ADS1115_REG_RESET_VAL 0x8583 + +typedef enum ADS1115_COMP_QUE { + ADS1115_ASSERT_AFTER_1 = 0x0000, + ADS1115_ASSERT_AFTER_2 = 0x0001, + ADS1115_ASSERT_AFTER_3 = 0x0002, + ADS1115_DISABLE_ALERT = 0x0003 +} compQue; + +typedef enum ADS1115_LATCH { + ADS1115_LATCH_DISABLED = 0x0000, + ADS1115_LATCH_ENABLED = 0x0004, +} latch; + +typedef enum ADS1115_ALERT_POL { + ADS1115_ACT_LOW = 0x0000, + ADS1115_ACT_HIGH = 0x0008 +} alertPol; + +typedef enum ADS1115_COMP_MODE{ + ADS1115_MAX_LIMIT = 0x0000, + ADS1115_WINDOW = 0x0010 +} compMode; + +typedef enum ADS1115_CONV_RATE{ + ADS1115_8_SPS = 0x0000, + ADS1115_16_SPS = 0x0020, + ADS1115_32_SPS = 0x0040, + ADS1115_64_SPS = 0x0050, + ADS1115_128_SPS = 0x0080, + ADS1115_250_SPS = 0x00A0, + ADS1115_475_SPS = 0x00C0, + ADS1115_860_SPS = 0x00E0 +} convRate; + +typedef enum ADS1115_MEASURE_MODE{ + ADS1115_CONTINOUS = 0x0000, + ADS1115_SINGLE = 0x0100 +} measureMode; + +typedef enum ADS1115_RANGE{ + ADS1115_RANGE_6144 = 0x0000, + ADS1115_RANGE_4096 = 0x0200, + ADS1115_RANGE_2048 = 0x0400, + ADS1115_RANGE_1024 = 0x0600, + ADS1115_RANGE_0512 = 0x0800, + ADS1115_RANGE_0256 = 0x0A00, +} range; + +typedef enum ADS1115_MUX{ + ADS1115_COMP_0_1 = 0x0000, + ADS1115_COMP_0_3 = 0x1000, + ADS1115_COMP_1_3 = 0x2000, + ADS1115_COMP_2_3 = 0x3000, + ADS1115_COMP_0_GND = 0x4000, + ADS1115_COMP_1_GND = 0x5000, + ADS1115_COMP_2_GND = 0x6000, + ADS1115_COMP_3_GND = 0x7000 +} mux; + +typedef enum ADS1115_STATUS_OR_START{ + ADS1115_BUSY = 0x0000, + ADS1115_START_ISREADY = 0x8000 +} statusOrStart; + + +class ADS1115_WE +{ +public: + ADS1115_WE(int addr); + ADS1115_WE(); //sets default I2C Address 0x48 + + void reset(); + bool init(); + void setAlertPinMode(ADS1115_COMP_QUE mode); + void setAlertLatch(ADS1115_LATCH latch); + void setAlertPol(ADS1115_ALERT_POL polarity); + void setAlertModeAndLimit_V(ADS1115_COMP_MODE mode, float hithres, float lothres); + void setConvRate(ADS1115_CONV_RATE rate); + void setMeasureMode(ADS1115_MEASURE_MODE mode); + void setVoltageRange_mV(ADS1115_RANGE range); + void setCompareChannels(ADS1115_MUX mux); + bool isBusy(); + void startSingleMeasurement(); + float getResult_V(); + float getResult_mV(); + void setAlertPinToConversionReady(); + void unlatchAlertPin(); + + +private: + uint16_t voltageRange; + ADS1115_MEASURE_MODE deviceMeasureMode; + int i2cAddress; + int16_t calcLimit(float rawLimit); + uint8_t writeRegister(uint8_t reg, uint16_t val); + uint16_t readRegister(uint8_t reg); + +}; + +#endif |