From a05e819cbfbad6f7e623861ba7620c788fa1c7ed Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Fri, 21 Jan 2022 19:20:16 +0100 Subject: [PATCH] fix #36 Support NANO Every (#37) * fix #36 Support NANO Every --- .arduino-ci.yml | 2 +- ADS1X15.cpp | 18 +++++++++++++----- ADS1X15.h | 24 +++++++++++++----------- README.md | 23 ++++++++++++++++------- library.json | 2 +- library.properties | 2 +- 6 files changed, 45 insertions(+), 26 deletions(-) diff --git a/.arduino-ci.yml b/.arduino-ci.yml index e7cb463..cecf585 100644 --- a/.arduino-ci.yml +++ b/.arduino-ci.yml @@ -8,4 +8,4 @@ compile: - m4 - esp32 # - esp8266 - # - mega2560 \ No newline at end of file + # - mega2560 diff --git a/ADS1X15.cpp b/ADS1X15.cpp index f764958..e8e34e8 100644 --- a/ADS1X15.cpp +++ b/ADS1X15.cpp @@ -1,7 +1,7 @@ // // FILE: ADS1X15.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.4 +// VERSION: 0.3.5 // DATE: 2013-03-24 // PUPROSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 @@ -24,6 +24,8 @@ // 0.3.3 2021-10-17 update build-CI (esp32), readme.md, keywords.txt // 0.3.4 2021-12-11 update library.json, license, minor edits incl layout) // add unit test constants. +// 0.3.5 2022-01-21 fix #36 support for Nano Every + #include "ADS1X15.h" @@ -380,11 +382,17 @@ void ADS1X15::setWireClock(uint32_t clockSpeed) } +////////////////////////////////////////////////////// +// +// EXPERIMENTAL +// +// see https://github.com/RobTillaart/ADS1X15/issues/22 +// https://github.com/arduino/Arduino/issues/11457 // TODO: get the real clock speed from the I2C interface if possible. -// ESP ==> ?? uint32_t ADS1X15::getWireClock() { -#if defined(__AVR__) +// UNO 328 and +#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__) uint32_t speed = F_CPU / ((TWBR * 2) + 16); return speed; @@ -396,7 +404,7 @@ uint32_t ADS1X15::getWireClock() // not supported. // return -1; -#else // best effort ... +#else // best effort is remembering it return _clockSpeed; #endif } @@ -456,7 +464,7 @@ uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg) _wire->write(reg); _wire->endTransmission(); - int rv = _wire->requestFrom(address, (uint8_t) 2); + int rv = _wire->requestFrom((int) address, (int) 2); if (rv == 2) { uint16_t value = _wire->read() << 8; diff --git a/ADS1X15.h b/ADS1X15.h index 966e227..d545467 100644 --- a/ADS1X15.h +++ b/ADS1X15.h @@ -2,7 +2,7 @@ // // FILE: ADS1X15.H // AUTHOR: Rob Tillaart -// VERSION: 0.3.4 +// VERSION: 0.3.5 // DATE: 2013-03-24 // PUPROSE: Arduino library for ADS1015 and ADS1115 // URL: https://github.com/RobTillaart/ADS1X15 @@ -12,23 +12,23 @@ #include "Arduino.h" #include "Wire.h" -#define ADS1X15_LIB_VERSION (F("0.3.4")) +#define ADS1X15_LIB_VERSION (F("0.3.5")) // allow compile time default address // address in { 0x48, 0x49, 0x4A, 0x4B }, no test... #ifndef ADS1015_ADDRESS -#define ADS1015_ADDRESS 0x48 +#define ADS1015_ADDRESS 0x48 #endif #ifndef ADS1115_ADDRESS -#define ADS1115_ADDRESS 0x48 +#define ADS1115_ADDRESS 0x48 #endif -#define ADS1X15_OK 0 -#define ADS1X15_INVALID_VOLTAGE -100 -#define ADS1X15_INVALID_GAIN 0xFF -#define ADS1X15_INVALID_MODE 0xFE +#define ADS1X15_OK 0 +#define ADS1X15_INVALID_VOLTAGE -100 +#define ADS1X15_INVALID_GAIN 0xFF +#define ADS1X15_INVALID_MODE 0xFE class ADS1X15 @@ -76,7 +76,7 @@ class ADS1X15 // ASYNC INTERFACE - // requestADC(pin) -> isBusy() or isReady() -> getValue(); + // requestADC(pin) -> isBusy() or isReady() -> getValue(); // see examples void requestADC(uint8_t pin); void requestADC_Differential_0_1(); @@ -115,7 +115,9 @@ class ADS1X15 int8_t getError(); - void setWireClock(uint32_t clockSpeed); + // EXPERIMENTAL + // see https://github.com/RobTillaart/ADS1X15/issues/22 + void setWireClock(uint32_t clockSpeed = 100000); // proto - getWireClock returns the value set by setWireClock not necessary the actual value uint32_t getWireClock(); @@ -144,7 +146,7 @@ class ADS1X15 // COMPARATOR variables // TODO merge these into one COMPARATOR MASK? (low priority) // would speed up code in _requestADC() and save 3 bytes RAM. - // TODO boolean flags for first three, or make it mask value that + // TODO boolean flags for first three, or make it mask value that // can be or-ed. (low priority) uint8_t _compMode; uint8_t _compPol; diff --git a/README.md b/README.md index 0c364fa..1bd151f 100644 --- a/README.md +++ b/README.md @@ -56,8 +56,18 @@ and optional the Wire interface as parameter. - **ADS1115(address, TwoWire \*wire = &Wire)** Constructor with device address, and optional the Wire interface as parameter. -The function **void setWireClock(uint32_t speed)** is used to set the clock speed -of the used I2C interface. + +After construction the **ADS.begin()** need to be called. This will return false +if an invalid address is used. +The function **bool isConnected()** can be used to verify the reading of the ADS. +The function **void reset()** is sets the parameters to their initial value as +in the constructor. + + +#### I2C clock speed + +The function **void setWireClock(uint32_t speed = 100000)** is used to set the clock speed +in Hz of the used I2C interface. typical value is 100 KHz. The function **uint32_t getWireClock()** is a prototype. It returns the value set by setWireClock(). @@ -66,11 +76,9 @@ When no value is set **getWireClock()** returns 0. Need to implement a read / calculate from low level I2C code (e.g. TWBR on AVR), better the Arduino Wire lib should support this call (ESP32 does). -After construction the **ADS.begin()** need to be called. This will return false -if an invalid address is used. -The function **bool isConnected()** can be used to verify the reading of the ADS. -The function **void reset()** is sets the parameters to their initial value as -in the constructor. +See - https://github.com/arduino/Arduino/issues/11457 + +Question: should this functionality be in this library? #### Programmable Gain @@ -321,6 +329,7 @@ mean something different see - Comparator Mode above or datasheet. - SMB alert command (00011001) on I2C bus? + ## Operation See examples diff --git a/library.json b/library.json index 8e881d5..25ac6d9 100644 --- a/library.json +++ b/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ADS1X15" }, - "version": "0.3.4", + "version": "0.3.5", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/library.properties b/library.properties index 6c8bf29..a94dfaa 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ADS1X15 -version=0.3.4 +version=0.3.5 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for ADS1015 - I2C 12 bit ADC and ADS1115 I2C 16 bit ADC