-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_ADS1X15.cpp
486 lines (374 loc) · 15 KB
/
Adafruit_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
/**************************************************************************/
/*!
@file Adafruit_ADS1X15.cpp
@author K.Townsend (Adafruit Industries)
@mainpage Adafruit ADS1X15 ADC Breakout Driver
@section intro_sec Introduction
This is a library for the Adafruit ADS1X15 ADC breakout boards.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section author Author
Written by Kevin "KTOWN" Townsend for Adafruit Industries.
@section HISTORY
v1.0 - First release
v1.1 - Added ADS1115 support - W. Earl
v2.0 - Refactor - C. Nelson
@section license License
BSD license, all text here must be included in any redistribution
*/
/**************************************************************************/
#include "Adafruit_ADS1X15.h"
/*
*
* #include <Adafruit_ADS1X15.h>
*
*/
/**************************************************************************/
/*!
@brief Instantiates a new ADS1015 class w/appropriate properties
*/
/**************************************************************************/
Adafruit_ADS1015::Adafruit_ADS1015() {
_m_bitShift = 4;
_m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
_m_dataRate = RATE_ADS1015_1600SPS;
}
/**************************************************************************/
/*!
@brief Instantiates a new ADS1115 class w/appropriate properties
*/
/**************************************************************************/
Adafruit_ADS1115::Adafruit_ADS1115() {
_m_bitShift = 0;
_m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
_m_dataRate = RATE_ADS1115_128SPS;
}
/**************************************************************************/
/*!
@brief Sets up the HW (reads coefficients values, etc.)
@param i2c_addr I2C address of device
@param wire I2C bus
@return true if successful, otherwise false
*/
// ************************************************************************
bool Adafruit_ADS1X15::begin(uint8_t i2c_addr, i2c_port_t i2c_channel) {
esp_err_t espRc;
_i2c_addr = i2c_addr ; // inicializo la variable protegida con el valor recibido de la dirección del dispositivo
_i2c_channel = i2c_channel;
// Check if there is a device assigned to the address reveived
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i2c_addr << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(_i2c_channel, cmd, 10/portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (espRc == 0) {
return true; // device found
} else {
return false;
}
}
/**************************************************************************/
/*!
@brief Sets the gain and input voltage range
@param gain gain setting to use
*/
/**************************************************************************/
void Adafruit_ADS1X15::setGain(adsGain_t gain) { _m_gain = gain; }
/**************************************************************************/
/*!
@brief Gets a gain and input voltage range
@return the gain setting
*/
/**************************************************************************/
adsGain_t Adafruit_ADS1X15::getGain() { return _m_gain ; }
/**************************************************************************/
/*!
@brief Sets the data rate
@param rate the data rate to use
*/
/**************************************************************************/
void Adafruit_ADS1X15::setDataRate(uint16_t rate) { _m_dataRate = rate; }
/**************************************************************************/
/*!
@brief Gets the current data rate
@return the data rate
*/
/**************************************************************************/
uint16_t Adafruit_ADS1X15::getDataRate() { return _m_dataRate; }
/**************************************************************************/
/*!
@brief Gets a single-ended ADC reading from the specified channel
@param channel ADC channel to read
@return the ADC reading
*/
/**************************************************************************/
int16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
if (channel > 3) {
return 0;
}
// Start with default values
uint16_t config =
ADS1X15_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
// Set PGA/voltage range
config |= _m_gain ;
// Set data rate
config |= _m_dataRate;
// Set single-ended input channel
switch (channel) {
case (0):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_0;
break;
case (1):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_1;
break;
case (2):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_2;
break;
case (3):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_3;
break;
}
// Set 'start single-conversion' bit
config |= ADS1X15_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);
// Wait for the conversion to complete
while (!conversionComplete())
;
// Read the conversion results
return getLastConversionResults();
}
/**************************************************************************/
/*!
@brief Reads the conversion results, measuring the voltage
difference between the P (AIN0) and N (AIN1) input. Generates
a signed value since the difference can be either
positive or negative.
@return the ADC reading
*/
/**************************************************************************/
int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
// Start with default values
uint16_t config =
ADS1X15_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
// Set PGA/voltage range
config |= _m_gain ;
// Set data rate
config |= _m_dataRate;
// Set channels
config |= ADS1X15_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
// Set 'start single-conversion' bit
config |= ADS1X15_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);
// Wait for the conversion to complete
while (!conversionComplete())
;
// Read the conversion results
return getLastConversionResults();
}
/**************************************************************************/
/*!
@brief Reads the conversion results, measuring the voltage
difference between the P (AIN2) and N (AIN3) input. Generates
a signed value since the difference can be either
positive or negative.
@return the ADC reading
*/
/**************************************************************************/
int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
// Start with default values
uint16_t config =
ADS1X15_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
// Set PGA/voltage range
config |= _m_gain ;
// Set data rate
config |= _m_dataRate;
// Set channels
config |= ADS1X15_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
// Set 'start single-conversion' bit
config |= ADS1X15_REG_CONFIG_OS_SINGLE;
// Write config register to the ADC
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);
// Wait for the conversion to complete
while (!conversionComplete())
;
// Read the conversion results
return getLastConversionResults();
}
/**************************************************************************/
/*!
@brief Sets up the comparator to operate in basic mode, causing the
ALERT/RDY pin to assert (go from high to low) when the ADC
value exceeds the specified threshold.
This will also set the ADC in continuous conversion mode.
@param channel ADC channel to use
@param threshold comparator threshold
*/
/**************************************************************************/
void Adafruit_ADS1X15::startComparator_SingleEnded(uint8_t channel,
int16_t threshold) {
// Start with default values
uint16_t config =
ADS1X15_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1
// match
ADS1X15_REG_CONFIG_CLAT_LATCH | // Latching mode
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1X15_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
// Set PGA/voltage range
config |= _m_gain ;
// Set data rate
config |= _m_dataRate;
// Set single-ended input channel
switch (channel) {
case (0):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_0;
break;
case (1):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_1;
break;
case (2):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_2;
break;
case (3):
config |= ADS1X15_REG_CONFIG_MUX_SINGLE_3;
break;
}
// Set the high threshold register
// Shift 12-bit results left 4 bits for the ADS1015
writeRegister(ADS1X15_REG_POINTER_HITHRESH, threshold << _m_bitShift);
// Write config register to the ADC
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);
}
/**************************************************************************/
/*!
@brief In order to clear the comparator, we need to read the
conversion results. This function reads the last conversion
results without changing the config value.
@return the last ADC reading
*/
/**************************************************************************/
int16_t Adafruit_ADS1X15::getLastConversionResults() {
// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> _m_bitShift;
if (_m_bitShift == 0) {
return (int16_t)res;
} else {
// Shift 12-bit results right 4 bits for the ADS1015,
// making sure we keep the sign bit intact
if (res > 0x07FF) {
// negative number - extend the sign to 16th bit
res |= 0xF000;
}
return (int16_t)res;
}
}
/**************************************************************************/
/*!
@brief Returns true if conversion is complete, false otherwise.
@param counts the ADC reading in raw counts
@return the ADC reading in volts
*/
/**************************************************************************/
float Adafruit_ADS1X15::computeVolts(int16_t counts) {
// see data sheet Table 3
float fsRange;
switch (_m_gain ) {
case GAIN_TWOTHIRDS:
fsRange = 6.144f;
break;
case GAIN_ONE:
fsRange = 4.096f;
break;
case GAIN_TWO:
fsRange = 2.048f;
break;
case GAIN_FOUR:
fsRange = 1.024f;
break;
case GAIN_EIGHT:
fsRange = 0.512f;
break;
case GAIN_SIXTEEN:
fsRange = 0.256f;
break;
default:
fsRange = 0.0f;
}
return counts * (fsRange / (32768 >> _m_bitShift));
}
/**************************************************************************/
/*!
@brief Returns true if conversion is complete, false otherwise.
*/
/**************************************************************************/
bool Adafruit_ADS1X15::conversionComplete() {
return (readRegister(ADS1X15_REG_POINTER_CONFIG) & 0x8000) != 0;
}
/**************************************************************************/
/*!
@brief Writes 16-bits to the specified destination register
@param reg register address to write to
@param value value to write to register
*/
/**************************************************************************/
void Adafruit_ADS1X15::writeRegister(uint8_t reg, uint16_t value) {
_buffer[0] = reg;
_buffer[1] = value >> 8;
_buffer[2] = value & 0xFF;
i2c_cmd_handle_t cmd;
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (_i2c_addr<<1) |I2C_MASTER_WRITE, 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, _buffer[0], 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, _buffer[1], 1)); // 0x83
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, _buffer[2], 1));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(_i2c_channel, cmd, 1000/portTICK_RATE_MS));
i2c_cmd_link_delete(cmd);
//m_i2c_dev->write(_buffer, 3);
}
/**************************************************************************/
/*!
@brief Read 16-bits from the specified destination register
@param reg register address to read from
@return 16 bit register value read
*/
/**************************************************************************/
uint16_t Adafruit_ADS1X15::readRegister(uint8_t reg) {
uint16_t parcial;
i2c_cmd_handle_t cmd;
//uint16_t val;
_buffer[0] = reg;
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (_i2c_addr<<1)|I2C_MASTER_WRITE, 1));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, reg, 1));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(_i2c_channel, cmd, 1000/portTICK_RATE_MS));
i2c_cmd_link_delete(cmd);
cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (_i2c_addr<<1)|I2C_MASTER_READ, 1));
ESP_ERROR_CHECK(i2c_master_read(cmd, (uint8_t *)&_buffer, 2 ,(i2c_ack_type_t) 0));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
ESP_ERROR_CHECK(i2c_master_cmd_begin(_i2c_channel, cmd, 1000/portTICK_RATE_MS));
i2c_cmd_link_delete(cmd);
return ((_buffer[0] << 8) | _buffer[1]);
}