Skip to content

Commit

Permalink
fix #82, minimal timeout 10 ms for RTOS
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Oct 17, 2024
1 parent 775ac76 commit 11755d1
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
11 changes: 7 additions & 4 deletions ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void ADS1X15::setMode(uint8_t mode)
switch (mode)
{
case 0: _mode = ADS1X15_MODE_CONTINUE; break;
default:
default: // catch invalid modi
case 1: _mode = ADS1X15_MODE_SINGLE; break;
}
}
Expand Down Expand Up @@ -449,13 +449,16 @@ uint32_t ADS1X15::getWireClock()
//
int16_t ADS1X15::_readADC(uint16_t readmode)
{
// note readmode includes the channel
_requestADC(readmode);

if (_mode == ADS1X15_MODE_SINGLE)
{
uint32_t start = millis();
// timeout == { 129, 65, 33, 17, 9, 5, 3, 2 }
// add 10 ms more than max conversion time.
// to prevent premature timeout in RTOS context. See #82
// timeout == { 138, 74, 42, 26, 18, 14, 12, 11 }
// added 10 ms more than maximum conversion time from datasheet.
// to prevent premature timeout in RTOS context.
// See #82
uint8_t timeOut = (128 >> (_datarate >> 5)) + 10;
while (isBusy())
{
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.5.1] - 2024-10-17
- fix #82, minimal timeout 10 ms for RTOS
- set error flag for TIMEOUT
- add error codes to keywords.txt

## [0.5.0] - 2024-08-20
- Fix #80, setComparatorPolarity() and setComparatorLatch() inverting.
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,16 @@ Default pin = 0 as this is convenient for the single channel devices.

See [examples](https://github.com/RobTillaart/ADS1X15/blob/master/examples/ADS_minimum/ADS_minimum.ino).


The **readADC()** can return **ADS1X15_ERROR_TIMEOUT (-101)** which is an errorcode.
This may conflict with a possible actual value of -101.
Therefore the user should check with **getError()** if an error has occurred after reading the ADC.

This need to be fixed in the future, see issue #84.


### Read the ADC in asynchronous way

To read the ADC in an asynchronous way (e.g. to minimize blocking) you need call three functions:

- **void requestADC(uint8_t pin = 0)** Start the conversion. pin = 0..3.
Expand Down
80 changes: 80 additions & 0 deletions examples/ADS_read_getError/ADS_read_getError.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// FILE: ADS_read_getError.ino
// AUTHOR: Rob.Tillaart
// PURPOSE: read analog inputs and check for error.
// URL: https://github.com/RobTillaart/ADS1X15

// test
// connect 1 potmeter per port.
//
// GND ---[ x ]------ 5V
// |
//
// measure at x (connect to AIN0).


#include "ADS1X15.h"

ADS1115 ADS(0x48);


void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(__FILE__);
Serial.print("ADS1X15_LIB_VERSION: ");
Serial.println(ADS1X15_LIB_VERSION);
Serial.println();

Wire.begin();
ADS.begin();
}


void loop()
{
ADS.setGain(0);

int16_t val_0 = ADS.readADC(0);
int err = ADS.getError();
if (err != ADS1X15_OK)
{
Serial.print("input 0 => ");
Serial.println(err);
}
int16_t val_1 = ADS.readADC(1);
err = ADS.getError();
if (err != ADS1X15_OK)
{
Serial.print("input 1 => ");
Serial.println(err);
}
int16_t val_2 = ADS.readADC(2);
err = ADS.getError();
if (err != ADS1X15_OK)
{
Serial.print("input 2 => ");
Serial.println(err);
}
int16_t val_3 = ADS.readADC(3);
err = ADS.getError();
if (err != ADS1X15_OK)
{
Serial.print("input 3 => ");
Serial.println(err);
}

float f = ADS.toVoltage(1); // voltage factor

Serial.print("\tAnalog0: "); Serial.print(val_0); Serial.print('\t'); Serial.println(val_0 * f, 3);
Serial.print("\tAnalog1: "); Serial.print(val_1); Serial.print('\t'); Serial.println(val_1 * f, 3);
Serial.print("\tAnalog2: "); Serial.print(val_2); Serial.print('\t'); Serial.println(val_2 * f, 3);
Serial.print("\tAnalog3: "); Serial.print(val_3); Serial.print('\t'); Serial.println(val_3 * f, 3);
Serial.println();

delay(1000);
}


// -- END OF FILE --
10 changes: 10 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ ADS1x15_COMP_MODE_WINDOW LITERAL1
ADS1x15_COMP_POL_FALLING_EDGE LITERAL1
ADS1x15_COMP_POL_RISING_EDGE LITERAL1


# Error Codes

ADS1X15_OK KEYWORD2
ADS1X15_INVALID_VOLTAGE KEYWORD2
ADS1X15_ERROR_TIMEOUT KEYWORD2
ADS1X15_ERROR_I2C KEYWORD2
ADS1X15_INVALID_GAIN KEYWORD2
ADS1X15_INVALID_MODE KEYWORD2

0 comments on commit 11755d1

Please sign in to comment.