-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
ADS1X15.cpp
766 lines (601 loc) · 16.9 KB
/
ADS1X15.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//
// FILE: ADS1X15.cpp
// 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 "ADS1X15.h"
#define ADS1015_CONVERSION_DELAY 1
#define ADS1115_CONVERSION_DELAY 8
// Kept #defines a bit in line with Adafruit library.
// REGISTERS
#define ADS1X15_REG_CONVERT 0x00
#define ADS1X15_REG_CONFIG 0x01
#define ADS1X15_REG_LOW_THRESHOLD 0x02
#define ADS1X15_REG_HIGH_THRESHOLD 0x03
// CONFIG REGISTER
// BIT 15 Operational Status // 1 << 15
#define ADS1X15_OS_BUSY 0x0000
#define ADS1X15_OS_NOT_BUSY 0x8000
#define ADS1X15_OS_START_SINGLE 0x8000
// BIT 12-14 read differential
#define ADS1X15_MUX_DIFF_0_1 0x0000
#define ADS1X15_MUX_DIFF_0_3 0x1000
#define ADS1X15_MUX_DIFF_1_3 0x2000
#define ADS1X15_MUX_DIFF_2_3 0x3000
// read single
#define ADS1X15_READ_0 0x4000 // pin << 12
#define ADS1X15_READ_1 0x5000 // pin = 0..3
#define ADS1X15_READ_2 0x6000
#define ADS1X15_READ_3 0x7000
// BIT 9-11 gain // (0..5) << 9
#define ADS1X15_PGA_6_144V 0x0000 // voltage
#define ADS1X15_PGA_4_096V 0x0200 //
#define ADS1X15_PGA_2_048V 0x0400 // default
#define ADS1X15_PGA_1_024V 0x0600
#define ADS1X15_PGA_0_512V 0x0800
#define ADS1X15_PGA_0_256V 0x0A00
// BIT 8 mode // 1 << 8
#define ADS1X15_MODE_CONTINUE 0x0000
#define ADS1X15_MODE_SINGLE 0x0100
// BIT 5-7 data rate sample per second // (0..7) << 5
/*
differs for different devices, check datasheet or readme.md
| data rate | ADS101x | ADS111x | Notes |
|:-----------:|----------:|----------:|:---------:|
| 0 | 128 | 8 | slowest |
| 1 | 250 | 16 | |
| 2 | 490 | 32 | |
| 3 | 920 | 64 | |
| 4 | 1600 | 128 | default |
| 5 | 2400 | 250 | |
| 6 | 3300 | 475 | |
| 7 | 3300 | 860 | fastest |
*/
// BIT 4 comparator modi // 1 << 4
#define ADS1X15_COMP_MODE_TRADITIONAL 0x0000
#define ADS1X15_COMP_MODE_WINDOW 0x0010
// BIT 3 ALERT active value // 1 << 3
#define ADS1X15_COMP_POL_ACTIV_LOW 0x0000
#define ADS1X15_COMP_POL_ACTIV_HIGH 0x0008
// BIT 2 ALERT latching // 1 << 2
#define ADS1X15_COMP_NON_LATCH 0x0000
#define ADS1X15_COMP_LATCH 0x0004
// BIT 0-1 ALERT mode // (0..3)
#define ADS1X15_COMP_QUE_1_CONV 0x0000 // trigger alert after 1 convert
#define ADS1X15_COMP_QUE_2_CONV 0x0001 // trigger alert after 2 converts
#define ADS1X15_COMP_QUE_4_CONV 0x0002 // trigger alert after 4 converts
#define ADS1X15_COMP_QUE_NONE 0x0003 // disable comparator
// _CONFIG masks
//
// | bit | description |
// |:-----:|:-----------------------|
// | 0 | # channels |
// | 1 | - |
// | 2 | resolution |
// | 3 | - |
// | 4 | GAIN supported |
// | 5 | COMPARATOR supported |
// | 6 | - |
// | 7 | - |
//
#define ADS_CONF_CHAN_1 0x00
#define ADS_CONF_CHAN_4 0x01
#define ADS_CONF_RES_12 0x00
#define ADS_CONF_RES_16 0x04
#define ADS_CONF_NOGAIN 0x00
#define ADS_CONF_GAIN 0x10
#define ADS_CONF_NOCOMP 0x00
#define ADS_CONF_COMP 0x20
//////////////////////////////////////////////////////
//
// BASE CONSTRUCTOR
//
ADS1X15::ADS1X15()
{
reset();
}
//////////////////////////////////////////////////////
//
// PUBLIC
//
void ADS1X15::reset()
{
setGain(0); // _gain = ADS1X15_PGA_6_144V;
setMode(1); // _mode = ADS1X15_MODE_SINGLE;
setDataRate(4); // middle speed, depends on device.
// COMPARATOR variables # see notes .h
_compMode = 0;
_compPol = 1;
_compLatch = 0;
_compQueConvert = 3;
_lastRequest = 0xFFFF; // no request yet
}
bool ADS1X15::begin()
{
if ((_address < 0x48) || (_address > 0x4B)) return false;
if (! isConnected()) return false;
return true;
}
bool ADS1X15::isConnected()
{
_wire->beginTransmission(_address);
return (_wire->endTransmission() == 0);
}
void ADS1X15::setGain(uint8_t gain)
{
if (!(_config & ADS_CONF_GAIN)) gain = 0;
switch (gain)
{
default: // catch invalid values and go for the safest gain.
case 0: _gain = ADS1X15_PGA_6_144V; break;
case 1: _gain = ADS1X15_PGA_4_096V; break;
case 2: _gain = ADS1X15_PGA_2_048V; break;
case 4: _gain = ADS1X15_PGA_1_024V; break;
case 8: _gain = ADS1X15_PGA_0_512V; break;
case 16: _gain = ADS1X15_PGA_0_256V; break;
}
}
uint8_t ADS1X15::getGain()
{
if (!(_config & ADS_CONF_GAIN)) return 0;
switch (_gain)
{
case ADS1X15_PGA_6_144V: return 0;
case ADS1X15_PGA_4_096V: return 1;
case ADS1X15_PGA_2_048V: return 2;
case ADS1X15_PGA_1_024V: return 4;
case ADS1X15_PGA_0_512V: return 8;
case ADS1X15_PGA_0_256V: return 16;
}
_error = ADS1X15_INVALID_GAIN;
return _error;
}
float ADS1X15::toVoltage(int16_t value)
{
if (value == 0) return 0;
float volts = getMaxVoltage();
if (volts < 0) return volts; // propagate error
volts *= value;
if (_config & ADS_CONF_RES_16)
{
volts /= 32767; // value = 16 bits - sign bit = 15 bits mantissa
}
else
{
volts /= 2047; // value = 12 bits - sign bit = 11 bit mantissa
}
return volts;
}
float ADS1X15::getMaxVoltage()
{
switch (_gain)
{
case ADS1X15_PGA_6_144V: return 6.144;
case ADS1X15_PGA_4_096V: return 4.096;
case ADS1X15_PGA_2_048V: return 2.048;
case ADS1X15_PGA_1_024V: return 1.024;
case ADS1X15_PGA_0_512V: return 0.512;
case ADS1X15_PGA_0_256V: return 0.256;
}
_error = ADS1X15_INVALID_VOLTAGE;
return _error;
}
void ADS1X15::setMode(uint8_t mode)
{
switch (mode)
{
case 0: _mode = ADS1X15_MODE_CONTINUE; break;
default: // catch invalid modi
case 1: _mode = ADS1X15_MODE_SINGLE; break;
}
}
uint8_t ADS1X15::getMode(void)
{
switch (_mode)
{
case ADS1X15_MODE_CONTINUE: return 0;
case ADS1X15_MODE_SINGLE: return 1;
}
_error = ADS1X15_INVALID_MODE;
return _error;
}
void ADS1X15::setDataRate(uint8_t dataRate)
{
_datarate = dataRate;
if (_datarate > 7) _datarate = 4; // default
_datarate <<= 5; // convert 0..7 to mask needed.
}
uint8_t ADS1X15::getDataRate(void)
{
return (_datarate >> 5) & 0x07; // convert mask back to 0..7
}
int16_t ADS1X15::readADC(uint8_t pin)
{
if (pin >= _maxPorts) return 0;
uint16_t mode = ((4 + pin) << 12); // pin to mask
return _readADC(mode);
}
int16_t ADS1X15::readADC_Differential_0_1()
{
return _readADC(ADS1X15_MUX_DIFF_0_1);
}
int16_t ADS1X15::getValue()
{
int16_t raw = _readRegister(_address, ADS1X15_REG_CONVERT);
if (_bitShift) raw >>= _bitShift; // Shift 12-bit results
return raw;
}
void ADS1X15::requestADC(uint8_t pin)
{
if (pin >= _maxPorts) return;
uint16_t mode = ((4 + pin) << 12); // pin to mask
_requestADC(mode);
}
void ADS1X15::requestADC_Differential_0_1()
{
_requestADC(ADS1X15_MUX_DIFF_0_1);
}
bool ADS1X15::isBusy()
{
return isReady() == false;
}
bool ADS1X15::isReady()
{
uint16_t val = _readRegister(_address, ADS1X15_REG_CONFIG);
return ((val & ADS1X15_OS_NOT_BUSY) > 0);
}
uint8_t ADS1X15::lastRequest()
{
switch (_lastRequest)
{
case ADS1X15_READ_0: return 0x00;
case ADS1X15_READ_1: return 0x01;
case ADS1X15_READ_2: return 0x02;
case ADS1X15_READ_3: return 0x03;
// technically 0x01 -- but would collide with READ_1
case ADS1X15_MUX_DIFF_0_1: return 0x10;
case ADS1X15_MUX_DIFF_0_3: return 0x30;
case ADS1X15_MUX_DIFF_1_3: return 0x31;
case ADS1X15_MUX_DIFF_2_3: return 0x32;
}
return 0xFF;
}
void ADS1X15::setComparatorMode(uint8_t mode)
{
_compMode = mode == 0 ? 0 : 1;
}
uint8_t ADS1X15::getComparatorMode()
{
return _compMode;
}
void ADS1X15::setComparatorPolarity(uint8_t pol)
{
_compPol = pol == 0 ? 0 : 1;
}
uint8_t ADS1X15::getComparatorPolarity()
{
return _compPol;
}
void ADS1X15::setComparatorLatch(uint8_t latch)
{
_compLatch = latch == 0 ? 0 : 1;
}
uint8_t ADS1X15::getComparatorLatch()
{
return _compLatch;
}
void ADS1X15::setComparatorQueConvert(uint8_t mode)
{
_compQueConvert = (mode < 3) ? mode : 3;
}
uint8_t ADS1X15::getComparatorQueConvert()
{
return _compQueConvert;
}
void ADS1X15::setComparatorThresholdLow(int16_t lo)
{
_writeRegister(_address, ADS1X15_REG_LOW_THRESHOLD, lo);
}
int16_t ADS1X15::getComparatorThresholdLow()
{
return _readRegister(_address, ADS1X15_REG_LOW_THRESHOLD);
};
void ADS1X15::setComparatorThresholdHigh(int16_t hi)
{
_writeRegister(_address, ADS1X15_REG_HIGH_THRESHOLD, hi);
};
int16_t ADS1X15::getComparatorThresholdHigh()
{
return _readRegister(_address, ADS1X15_REG_HIGH_THRESHOLD);
};
int8_t ADS1X15::getError()
{
int8_t rv = _error;
_error = ADS1X15_OK;
return rv;
}
//////////////////////////////////////////////////////
//
// EXPERIMENTAL
//
void ADS1X15::setWireClock(uint32_t clockSpeed)
{
_clockSpeed = clockSpeed;
_wire->setClock(_clockSpeed);
}
// 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.
uint32_t ADS1X15::getWireClock()
{
// UNO 328 and
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
uint32_t speed = F_CPU / ((TWBR * 2) + 16);
return speed;
#elif defined(ESP32)
return (uint32_t) _wire->getClock();
// #elif defined(ESP8266)
// core_esp8266_si2c.cpp holds the data see => void Twi::setClock(
// not supported.
// return -1;
#else // best effort is remembering it
return _clockSpeed;
#endif
}
//////////////////////////////////////////////////////
//
// PROTECTED
//
int16_t ADS1X15::_readADC(uint16_t readmode)
{
// note readmode includes the channel
_requestADC(readmode);
if (_mode == ADS1X15_MODE_SINGLE)
{
uint32_t start = millis();
// 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())
{
if ( (millis() - start) > timeOut)
{
_error = ADS1X15_ERROR_TIMEOUT;
return ADS1X15_ERROR_TIMEOUT;
}
yield(); // wait for conversion; yield for ESP.
}
}
else
{
// needed in continuous mode too, otherwise one get an old value.
delay(_conversionDelay);
}
return getValue();
}
void ADS1X15::_requestADC(uint16_t readmode)
{
// write to register is needed in continuous mode as other flags can be changed
uint16_t config = ADS1X15_OS_START_SINGLE; // bit 15 force wake up if needed
config |= readmode; // bit 12-14
config |= _gain; // bit 9-11
config |= _mode; // bit 8
config |= _datarate; // bit 5-7
if (_compMode) config |= ADS1X15_COMP_MODE_WINDOW; // bit 4 comparator modi
else config |= ADS1X15_COMP_MODE_TRADITIONAL;
if (_compPol) config |= ADS1X15_COMP_POL_ACTIV_HIGH; // bit 3 ALERT active value
else config |= ADS1X15_COMP_POL_ACTIV_LOW;
if (_compLatch) config |= ADS1X15_COMP_LATCH;
else config |= ADS1X15_COMP_NON_LATCH; // bit 2 ALERT latching
config |= _compQueConvert; // bit 0..1 ALERT mode
_writeRegister(_address, ADS1X15_REG_CONFIG, config);
// remember last request type.
_lastRequest = readmode;
}
bool ADS1X15::_writeRegister(uint8_t address, uint8_t reg, uint16_t value)
{
_wire->beginTransmission(address);
_wire->write((uint8_t)reg);
_wire->write((uint8_t)(value >> 8));
_wire->write((uint8_t)(value & 0xFF));
int rv = _wire->endTransmission();
if (rv != 0)
{
_error = ADS1X15_ERROR_I2C;
return false;
}
return true;
}
uint16_t ADS1X15::_readRegister(uint8_t address, uint8_t reg)
{
_wire->beginTransmission(address);
_wire->write(reg);
int rv = _wire->endTransmission();
if (rv == 0)
{
rv = _wire->requestFrom((int) address, (int) 2);
if (rv == 2)
{
uint16_t value = _wire->read() << 8;
value += _wire->read();
return value;
}
}
_error = ADS1X15_ERROR_I2C;
return 0x0000;
}
///////////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES
//
///////////////////////////////////////////////////////////////////////////
//
// ADS1013
//
ADS1013::ADS1013(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_NOCOMP | ADS_CONF_NOGAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_1;
_conversionDelay = ADS1015_CONVERSION_DELAY;
_bitShift = 4;
_maxPorts = 1;
_gain = ADS1X15_PGA_2_048V; // fixed value
}
// ADS1x13 has no gain so set default.
// Table 8. Config Register Field Descriptions
void ADS1013::setGain(uint8_t gain)
{
_gain = gain; // keep compiler happy.
_gain = ADS1X15_PGA_2_048V; // fixed value
}
uint8_t ADS1013::getGain()
{
return 2; // fixed value
}
///////////////////////////////////////////////////////////////////////////
//
// ADS1014
//
ADS1014::ADS1014(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_1;
_conversionDelay = ADS1015_CONVERSION_DELAY;
_bitShift = 4;
_maxPorts = 1;
}
///////////////////////////////////////////////////////////////////////////
//
// ADS1015
//
ADS1015::ADS1015(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_12 | ADS_CONF_CHAN_4;
_conversionDelay = ADS1015_CONVERSION_DELAY;
_bitShift = 4;
_maxPorts = 4;
}
int16_t ADS1015::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}
int16_t ADS1015::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}
int16_t ADS1015::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}
int16_t ADS1015::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}
int16_t ADS1015::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}
void ADS1015::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}
void ADS1015::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}
void ADS1015::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
}
///////////////////////////////////////////////////////////////////////////
//
// ADS1113
//
ADS1113::ADS1113(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_NOCOMP | ADS_CONF_NOGAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_1;
_conversionDelay = ADS1115_CONVERSION_DELAY;
_bitShift = 0;
_maxPorts = 1;
_gain = ADS1X15_PGA_2_048V; // fixed value
}
// ADS1x13 has no gain so set default.
// Table 8. Config Register Field Descriptions
void ADS1113::setGain(uint8_t gain)
{
_gain = gain; // keep compiler happy.
_gain = ADS1X15_PGA_2_048V; // fixed value
}
uint8_t ADS1113::getGain()
{
return 2; // fixed value
}
///////////////////////////////////////////////////////////////////////////
//
// ADS1114
//
ADS1114::ADS1114(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_1;
_conversionDelay = ADS1115_CONVERSION_DELAY;
_bitShift = 0;
_maxPorts = 1;
}
///////////////////////////////////////////////////////////////////////////
//
// ADS1115
//
ADS1115::ADS1115(uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
_config = ADS_CONF_COMP | ADS_CONF_GAIN | ADS_CONF_RES_16 | ADS_CONF_CHAN_4;
_conversionDelay = ADS1115_CONVERSION_DELAY;
_bitShift = 0;
_maxPorts = 4;
}
int16_t ADS1115::readADC_Differential_0_3()
{
return _readADC(ADS1X15_MUX_DIFF_0_3);
}
int16_t ADS1115::readADC_Differential_1_3()
{
return _readADC(ADS1X15_MUX_DIFF_1_3);
}
int16_t ADS1115::readADC_Differential_2_3()
{
return _readADC(ADS1X15_MUX_DIFF_2_3);
}
int16_t ADS1115::readADC_Differential_0_2()
{
return readADC(2) - readADC(0);
}
int16_t ADS1115::readADC_Differential_1_2()
{
return readADC(2) - readADC(1);;
}
void ADS1115::requestADC_Differential_0_3()
{
_requestADC(ADS1X15_MUX_DIFF_0_3);
}
void ADS1115::requestADC_Differential_1_3()
{
_requestADC(ADS1X15_MUX_DIFF_1_3);
}
void ADS1115::requestADC_Differential_2_3()
{
_requestADC(ADS1X15_MUX_DIFF_2_3);
}
// -- END OF FILE --