-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ADS1X15.h
273 lines (211 loc) · 7.36 KB
/
ADS1X15.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#pragma once
//
// FILE: ADS1X15.h
// AUTHOR: Rob Tillaart
// VERSION: 0.5.1
// DATE: 2013-03-24
// PURPOSE: Arduino library for ADS1015 and ADS1115
// URL: https://github.com/RobTillaart/ADS1X15
//
#include "Arduino.h"
#include "Wire.h"
#define ADS1X15_LIB_VERSION (F("0.5.1"))
// allow compile time default address
// address in { 0x48, 0x49, 0x4A, 0x4B }, no test...
#ifndef ADS1015_ADDRESS
#define ADS1015_ADDRESS 0x48
#endif
#ifndef ADS1115_ADDRESS
#define ADS1115_ADDRESS 0x48
#endif
#define ADS1X15_OK 0
#define ADS1X15_INVALID_VOLTAGE -100
#define ADS1X15_ERROR_TIMEOUT -101
#define ADS1X15_ERROR_I2C -102
#define ADS1X15_INVALID_GAIN 0xFF
#define ADS1X15_INVALID_MODE 0xFE
// PARAMETER CONSTANTS NOT USED IN CODE YET
// enum ?
#define ADS1X15_GAIN_6144MV 0x00
#define ADS1X15_GAIN_4096MV 0x01
#define ADS1X15_GAIN_2048MV 0x02
#define ADS1X15_GAIN_1024MV 0x04
#define ADS1X15_GAIN_0512MV 0x08
#define ADS1X15_GAIN_0256MV 0x10
#define ADS1x15_COMP_MODE_TRADITIONAL 0x00
#define ADS1x15_COMP_MODE_WINDOW 0x01
#define ADS1x15_COMP_POL_FALLING_EDGE 0x00
#define ADS1x15_COMP_POL_RISING_EDGE 0x01
#define ADS1x15_COMP_POL_LATCH 0x00
#define ADS1x15_COMP_POL_NOLATCH 0x01
class ADS1X15
{
public:
void reset();
bool begin();
bool isConnected();
// GAIN
// 0 = +- 6.144V default
// 1 = +- 4.096V
// 2 = +- 2.048V
// 4 = +- 1.024V
// 8 = +- 0.512V
// 16 = +- 0.256V
void setGain(uint8_t gain = 0); // invalid values are mapped to 0 (default).
uint8_t getGain(); // 0xFF == invalid gain error.
// both may return ADS1X15_INVALID_VOLTAGE if the gain is invalid.
float toVoltage(int16_t value = 1); // converts raw to voltage
float getMaxVoltage(); // -100 == invalid voltage error
// 0 = CONTINUOUS
// 1 = SINGLE default
void setMode(uint8_t mode = 1); // invalid values are mapped to 1 (default)
uint8_t getMode(); // 0xFE == invalid mode error.
// 0 = slowest
// 7 = fastest
// 4 = default
void setDataRate(uint8_t dataRate = 4); // invalid values are mapped on 4 (default)
uint8_t getDataRate(); // actual speed depends on device
int16_t readADC(uint8_t pin = 0);
int16_t readADC_Differential_0_1();
// used by continuous mode and async mode.
// [[deprecated("Use getValue() instead")]]
// int16_t getLastValue() { return getValue(); }; // will be obsolete in the future 0.4.0
int16_t getValue();
// ASYNC INTERFACE
// requestADC(pin) -> isBusy() or isReady() -> getValue();
// see examples
void requestADC(uint8_t pin = 0);
void requestADC_Differential_0_1();
bool isBusy();
bool isReady();
// returns a pin 0x0[0..3] or
// a differential "mode" 0x[pin second][pin first] or
// 0xFF (no request / invalid request)
uint8_t lastRequest();
// COMPARATOR
// 0 = TRADITIONAL > high => on < low => off
// else = WINDOW > high or < low => on between => off
void setComparatorMode(uint8_t mode);
uint8_t getComparatorMode();
// 0 = LOW (default)
// else = HIGH
void setComparatorPolarity(uint8_t pol);
uint8_t getComparatorPolarity();
// 0 = NON LATCH
// else = LATCH
void setComparatorLatch(uint8_t latch);
uint8_t getComparatorLatch();
// 0 = trigger alert after 1 conversion
// 1 = trigger alert after 2 conversions
// 2 = trigger alert after 4 conversions
// 3 = Disable comparator = default, also for all other values.
void setComparatorQueConvert(uint8_t mode);
uint8_t getComparatorQueConvert();
void setComparatorThresholdLow(int16_t lo);
int16_t getComparatorThresholdLow();
void setComparatorThresholdHigh(int16_t hi);
int16_t getComparatorThresholdHigh();
int8_t getError();
// EXPERIMENTAL
// see https://github.com/RobTillaart/ADS1X15/issues/22
void setWireClock(uint32_t clockSpeed = 100000);
// prototype
// - getWireClock returns the value set by setWireClock
// not necessary the actual value
uint32_t getWireClock();
protected:
ADS1X15();
// CONFIGURATION
// BIT DESCRIPTION
// 0 # channels 0 == 1 1 == 4;
// 1 0
// 2 # resolution 0 == 12 1 == 16
// 3 0
// 4 has gain 0 = NO 1 = YES
// 5 has comparator 0 = NO 1 = YES
// 6 0
// 7 0
uint8_t _config;
uint8_t _maxPorts;
uint8_t _address;
uint8_t _conversionDelay;
uint8_t _bitShift;
uint16_t _gain;
uint16_t _mode;
uint16_t _datarate;
// 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
// can be or-ed. (low priority)
uint8_t _compMode;
uint8_t _compPol;
uint8_t _compLatch;
uint8_t _compQueConvert;
// variable to track the last pin requested,
// to allow for round robin query of
// pins based on this state == if no last request then == 0xFFFF.
uint16_t _lastRequest;
int16_t _readADC(uint16_t readmode);
void _requestADC(uint16_t readmode);
bool _writeRegister(uint8_t address, uint8_t reg, uint16_t value);
uint16_t _readRegister(uint8_t address, uint8_t reg);
int8_t _error = ADS1X15_OK;
TwoWire* _wire;
uint32_t _clockSpeed = 0;
};
///////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES from ADS1X15
//
class ADS1013 : public ADS1X15
{
public:
ADS1013(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
void setGain(uint8_t gain);
uint8_t getGain();
};
class ADS1014 : public ADS1X15
{
public:
ADS1014(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
};
class ADS1015 : public ADS1X15
{
public:
ADS1015(uint8_t Address = ADS1015_ADDRESS, TwoWire *wire = &Wire);
int16_t readADC_Differential_0_3();
int16_t readADC_Differential_1_3();
int16_t readADC_Differential_2_3();
int16_t readADC_Differential_0_2(); // not possible in async
int16_t readADC_Differential_1_2(); // not possible in async
void requestADC_Differential_0_3();
void requestADC_Differential_1_3();
void requestADC_Differential_2_3();
};
class ADS1113 : public ADS1X15
{
public:
ADS1113(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
void setGain(uint8_t gain);
uint8_t getGain();
};
class ADS1114 : public ADS1X15
{
public:
ADS1114(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
};
class ADS1115 : public ADS1X15
{
public:
ADS1115(uint8_t address = ADS1115_ADDRESS, TwoWire *wire = &Wire);
int16_t readADC_Differential_0_3();
int16_t readADC_Differential_1_3();
int16_t readADC_Differential_2_3();
int16_t readADC_Differential_0_2(); // not possible in async
int16_t readADC_Differential_1_2(); // not possible in async
void requestADC_Differential_0_3();
void requestADC_Differential_1_3();
void requestADC_Differential_2_3();
};
// -- END OF FILE --