diff --git a/libraries/ADC081S/ADC081S.cpp b/libraries/ADC081S/ADC081S.cpp index 3884d48b..ec93da0a 100644 --- a/libraries/ADC081S/ADC081S.cpp +++ b/libraries/ADC081S/ADC081S.cpp @@ -1,7 +1,7 @@ // // FILE: ADC081S.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2024-01-10 // PURPOSE: Arduino library for ADC081S 8 bit ADC (SPI) // URL: https://github.com/RobTillaart/ADC081S @@ -10,30 +10,29 @@ #include "ADC081S.h" - - - // HARDWARE SPI ADC081S::ADC081S(__SPI_CLASS__ * mySPI) { - _dataIn = 255; - _clock = 255; - _select = 255; - _hwSPI = true; - _mySPI = mySPI; - _maxValue = 255; + _dataIn = 255; + _clock = 255; + _select = 255; + _hwSPI = true; + _mySPI = mySPI; + _maxValue = 255; + _isLowPower = false; } // SOFTWARE SPI ADC081S::ADC081S(uint8_t dataIn, uint8_t clock) { - _dataIn = dataIn; - _clock = clock; - _select = 255; - _hwSPI = false; - _mySPI = NULL; - _maxValue = 255; + _dataIn = dataIn; + _clock = clock; + _select = 255; + _hwSPI = false; + _mySPI = NULL; + _maxValue = 255; + _isLowPower = false; } @@ -98,6 +97,26 @@ bool ADC081S::usesHWSPI() } +void ADC081S::lowPower() +{ + _isLowPower = true; + shutDown(); +} + + +void ADC081S::wakeUp() +{ + readADC(); + _isLowPower = false; +} + + +bool ADC081S::isLowPower() +{ + return _isLowPower; +} + + ///////////////////////////////////////////////////////////////////////////// // // PROTECTED @@ -109,7 +128,7 @@ uint16_t ADC081S::readADC() uint16_t data = 0; digitalWrite(_select, LOW); - if (_hwSPI) + if (_hwSPI) // hardware SPI { _mySPI->beginTransaction(_spi_settings); data = _mySPI->transfer16(0); @@ -125,15 +144,31 @@ uint16_t ADC081S::readADC() } +void ADC081S::shutDown() +{ + digitalWrite(_select, LOW); + if (_hwSPI) // hardware SPI + { + _mySPI->beginTransaction(_spi_settings); + _mySPI->transfer(0); // 8 pulses + _mySPI->endTransaction(); + } + else // Software SPI + { + swSPI_transfer16(0x0010); // 4 pulses is enough + } + digitalWrite(_select, HIGH); +} + // MSBFIRST -uint16_t ADC081S::swSPI_transfer16() +uint16_t ADC081S::swSPI_transfer16(uint16_t m) { uint8_t clk = _clock; uint8_t dai = _dataIn; uint16_t rv = 0; - for (uint16_t mask = 0x8000; mask; mask >>= 1) + for (uint16_t mask = m; mask; mask >>= 1) { digitalWrite(clk, LOW); digitalWrite(clk, HIGH); diff --git a/libraries/ADC081S/ADC081S.h b/libraries/ADC081S/ADC081S.h index 40b9e928..65f95533 100644 --- a/libraries/ADC081S/ADC081S.h +++ b/libraries/ADC081S/ADC081S.h @@ -2,7 +2,7 @@ // // FILE: ADC081S.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.0 +// VERSION: 0.1.1 // DATE: 2024-01-10 // PURPOSE: Arduino library for ADC081S 8 bit ADC (SPI) // URL: https://github.com/RobTillaart/ADC081S @@ -13,7 +13,7 @@ #include "SPI.h" -#define ADC081S_LIB_VERSION (F("0.1.0")) +#define ADC081S_LIB_VERSION (F("0.1.1")) #ifndef __SPI_CLASS__ @@ -38,7 +38,6 @@ public: uint16_t maxValue(); uint16_t read(); - // speed in Hz void setSPIspeed(uint32_t speed); uint32_t getSPIspeed(); @@ -47,17 +46,21 @@ public: bool usesHWSPI(); uint32_t count(); // number of channels read. + void lowPower(); + void wakeUp(); + bool isLowPower(); protected: uint8_t _dataIn; uint8_t _clock; uint8_t _select; - bool _hwSPI; + uint16_t _maxValue; uint16_t readADC(); + void shutDown(); - uint16_t swSPI_transfer16(); + uint16_t swSPI_transfer16(uint16_t m = 0x8000); // 1 MHz is a safe value (datasheet); in a test 4 MHz worked. uint32_t _SPIspeed = 1000000; @@ -65,6 +68,7 @@ protected: SPISettings _spi_settings; uint32_t _count; + bool _isLowPower; }; diff --git a/libraries/ADC081S/CHANGELOG.md b/libraries/ADC081S/CHANGELOG.md index 5eb501c8..a0105715 100644 --- a/libraries/ADC081S/CHANGELOG.md +++ b/libraries/ADC081S/CHANGELOG.md @@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.1] - 2024-01-10 +- add **void lowPower()** function +- add **void wakeUp()** function +- add **bool isLowPower()** +- add example + + ## [0.1.0] - 2024-01-10 - initial version. diff --git a/libraries/ADC081S/README.md b/libraries/ADC081S/README.md index 1a87d6cc..0f60e37d 100644 --- a/libraries/ADC081S/README.md +++ b/libraries/ADC081S/README.md @@ -16,9 +16,36 @@ Arduino library for ADC081S 8 bit ADC (SPI). ## Description -This library reads the ADC ports of the ADC081S ADC convertor. +Experimental. -Feedback is as always welcome. +This library reads the ADC port of the ADC081S 8 bit ADC convertor. +The library supports both hardware and software SPI. +The interface is straightforward as one only need a **read()** call. + +The library can put the device in **lowPower()** and needs a call to +**wakeUp()** to wake up. Alternative way to wake up the device is to +do a dummy **read()**. + +The library is not tested with hardware yet. + +Feedback is as always welcome. + + +#### Compatibles + +Texas instruments has 9 devices in this series including the ADC081S021. + +| device name | bits | KSPS | compatibility | +|:--------------|:------:|:------:|:---------------:| +| ADC081S021 | 8 | 200 | +| ADC081S051 | 8 | 500 | +| ADC081S101 | 8 | 1000 | +| ADC101S021 | 10 | 200 | not supported yet | +| ADC101S051 | 10 | 500 | not supported yet | +| ADC101S101 | 10 | 1000 | not supported yet | +| ADC121S021 | 12 | 200 | not supported yet | +| ADC121S051 | 12 | 500 | not supported yet | +| ADC121S101 | 12 | 1000 | not supported yet | #### Related @@ -44,16 +71,20 @@ Feedback is as always welcome. - **void begin(uint8_t select)** set select pin. - **int16_t maxValue()** returns maxReading of ADC, => 256 - #### Base -- **uint16_t read()** reads the value of a single channel. +- **uint16_t read()** reads the value of the device. - **void setSPIspeed(uint32_t speed)** sets SPI clock in **Hz**, please read datasheet of the ADC first to get optimal speed. -- **uint32_t getSPIspeed()** gets current speed in **Hz**. +- **uint32_t getSPIspeed()** returns current speed in **Hz**. +#### Low power -### Debug +- **void lowPower()** put device in low power mode. +- **void wakeUp()** put device in normal power mode. +- **isLowPower()** returns true if in low power mode, so wakeUp needed(). + +#### Debug - **bool usesHWSPI()** returns true if hardware SPI is used. - **uint32_t count()** returns number of reads since start. @@ -69,12 +100,15 @@ of the ADC first to get optimal speed. #### Should -- add shutDown() + wakeUp() // select between normal and shutdown mode. -- +- investigate 10 and 12 bit versions of the device. + - how to support + #### Could + + #### Wont diff --git a/libraries/ADC081S/examples/ADC081S_low_power/ADC081S_low_power.ino b/libraries/ADC081S/examples/ADC081S_low_power/ADC081S_low_power.ino new file mode 100644 index 00000000..328f5e73 --- /dev/null +++ b/libraries/ADC081S/examples/ADC081S_low_power/ADC081S_low_power.ino @@ -0,0 +1,42 @@ +// +// FILE: ADC081S_low_power.ino +// AUTHOR: Rob Tillaart +// PURPOSE: demo +// URL: https://github.com/RobTillaart/ADC081S +// +// to see it work measure current of the sensor. + +#include "ADC081S.h" + +ADC081S adc01; // use HWSPI + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + Serial.print("ADC081S_LIB_VERSION: "); + Serial.println(ADC081S_LIB_VERSION); + + adc01.begin(10); +} + + +void loop() +{ + Serial.println("low power for 15 seconds."); + adc01.lowPower(); + delay(15000); + + Serial.println("wake up for 15 seconds."); + if (adc01.isLowPower()) + { + adc01.wakeUp(); + } + Serial.print("READ: "); + Serial.println(adc01.read()); + delay(15000); +} + + +// -- END OF FILE -- diff --git a/libraries/ADC081S/examples/ADC081S_performance/ADC081S_performance.ino b/libraries/ADC081S/examples/ADC081S_performance/ADC081S_performance.ino index ff75bb71..2bd84147 100644 --- a/libraries/ADC081S/examples/ADC081S_performance/ADC081S_performance.ino +++ b/libraries/ADC081S/examples/ADC081S_performance/ADC081S_performance.ino @@ -9,7 +9,7 @@ #include "ADC081S.h" ADC081S adc; // HW SPI -// ADC081S adc(6, 7); // SW SPI UNO - adjust pins if needed +// ADC081S adc(6, 7); // SW SPI UNO - adjust pins if needed // ADC081S adc(20, 21); // SW SPI ESP32 - adjust pins if needed const uint8_t SELECT_PIN = 10; diff --git a/libraries/ADC081S/keywords.txt b/libraries/ADC081S/keywords.txt index 2b8ce1ba..feec8431 100644 --- a/libraries/ADC081S/keywords.txt +++ b/libraries/ADC081S/keywords.txt @@ -16,6 +16,10 @@ usesHWSPI KEYWORD2 count KEYWORD2 +lowPower KEYWORD2 +wakeUp KEYWORD2 +isLowPower KEYWORD2 + # Constants (LITERAL1) ADC081S_LIB_VERSION LITERAL1 diff --git a/libraries/ADC081S/library.json b/libraries/ADC081S/library.json index 4918a0df..bffad093 100644 --- a/libraries/ADC081S/library.json +++ b/libraries/ADC081S/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/ADC081S.git" }, - "version": "0.1.0", + "version": "0.1.1", "license": "MIT", "frameworks": "*", "platforms": "*", diff --git a/libraries/ADC081S/library.properties b/libraries/ADC081S/library.properties index 99f67845..d2d92397 100644 --- a/libraries/ADC081S/library.properties +++ b/libraries/ADC081S/library.properties @@ -1,5 +1,5 @@ name=ADC081S -version=0.1.0 +version=0.1.1 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for ADC081S 8 bit ADC (SPI)