-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAD5593R.cpp
More file actions
468 lines (406 loc) · 10.7 KB
/
Copy pathAD5593R.cpp
File metadata and controls
468 lines (406 loc) · 10.7 KB
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
//
// FILE: AD5593R.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// DATE: 2024-01-30
// PURPOSE: Arduino library for AD5593R, I2C, 8 channel ADC / DAC / GPIO device.
// URL: https://github.com/RobTillaart/AD5593R
#include "AD5593R.h"
// CONFIG REGISTERS (aka pointer bytes)
#define AD5593_NOP 0x00
#define AD5593_ADC_SEQ 0x02
#define AD5593_GEN_CTRL_REG 0x03
#define AD5593_ADC_CONFIG 0x04
#define AD5593_DAC_CONFIG 0x05
#define AD5593_PULLDOWN_CONFIG 0x06
#define AD5593_LDAC_MODE 0x07
#define AD5593_GPIO_CONFIG 0x08
#define AD5593_GPIO_OUTPUT 0x09
#define AD5593_GPIO_INPUT 0x0A
#define AD5593_POWERDOWN_REF_CTRL 0x0B
#define AD5593_GPIO_OPENDRAIN_CONFIG 0x0C
#define AD5593_IO_TS_CONFIG 0x0D
#define AD5593_SW_RESET 0x0F
// IO REGISTERS
#define AD5593_DAC_WRITE(x) (0x10 + (x))
#define AD5593_ADC_READ 0x40
#define AD5593_DAC_READ(x) (0x50 + (x))
#define AD5593_GPIO_READ 0x60
#define AD5593_GPIO_READ_CONFIG 0x70
AD5593R::AD5593R(const uint8_t deviceAddress, TwoWire *wire)
{
_address = deviceAddress;
_wire = wire;
_error = AD5593R_OK;
_Vref = 2.5;
_gain = 1;
}
bool AD5593R::begin()
{
if (! isConnected()) return false;
return true;
}
bool AD5593R::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t AD5593R::getAddress()
{
return _address;
}
////////////////////////////////////////////////////////////
//
// CONFIGURE ADC / DAC RANGE
//
int AD5593R::setADCRange2x(bool flag)
{
// remember for readTemperature().
if (flag) _gain = 2;
else _gain = 1;
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0020;
else bitMask &= ~0x0020;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
int AD5593R::setDACRange2x(bool flag)
{
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0010;
else bitMask &= ~0x0010;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
////////////////////////////////////////////////////////////
//
// CONFIGURE GENERAL CONTROL
//
int AD5593R::enableADCBufferPreCharge(bool flag)
{
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0200;
else bitMask &= ~0x0200;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
int AD5593R::enableADCBuffer(bool flag)
{
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0100;
else bitMask &= ~0x0100;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
int AD5593R::enableIOLock(bool flag)
{
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0080;
else bitMask &= ~0x0080;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
int AD5593R::writeAllDacs(bool flag)
{
uint16_t bitMask = readConfigRegister(AD5593_GEN_CTRL_REG);
if (flag) bitMask |= 0x0040;
else bitMask &= ~0x0040;
return writeRegister(AD5593_GEN_CTRL_REG, bitMask);
}
////////////////////////////////////////////////////////////
//
// MODE
//
int AD5593R::setMode(const char config[9])
{
// all channels need to be addressed
if (strlen(config) != 8) return -1;
uint8_t bitMaskDAC = 0x00;
uint8_t bitMaskADC = 0x00;
uint8_t bitMaskIn = 0x00;
uint8_t bitMaskOut = 0x00;
uint8_t bitMaskTS = 0x00;
// parse configuration string.
uint8_t bm = 0x01;
for (int i = 0; i < 8; i++)
{
switch(config[i])
{
// ADC
case 'a':
case 'A': bitMaskADC |= bm; break;
// DAC
case 'd':
case 'D': bitMaskDAC |= bm; break;
// INPUT
case 'i':
case 'I': bitMaskIn |= bm; break;
// THREE STATE ==> output mode
case 't':
case 'T': bitMaskTS |= bm; bitMaskOut |= bm; break;
// OUTPUT
case 'o':
case 'O': bitMaskOut |= bm; break;
default: break;
}
bm <<= 1;
}
// TODO handle return values.
setADCmode(bitMaskADC);
setDACmode(bitMaskDAC);
setINPUTmode(bitMaskIn);
setOUTPUTmode(bitMaskOut);
setTHREESTATEmode(bitMaskTS);
return 0;
}
int AD5593R::setADCmode(uint8_t bitMask)
{
// Page 25 / 32
// 1's => ADC
return writeRegister(AD5593_ADC_CONFIG, bitMask);
}
int AD5593R::setDACmode(uint8_t bitMask)
{
// Page 35
// 1's => DAC
return writeRegister(AD5593_DAC_CONFIG, bitMask);
}
int AD5593R::setINPUTmode(uint8_t bitMask)
{
// Page 26
// 1's => INPUT
return writeRegister(AD5593_GPIO_INPUT, bitMask);
}
int AD5593R::setOUTPUTmode(uint8_t bitMask)
{
// Page 26
// 1's => OUTPUT
return writeRegister(AD5593_GPIO_CONFIG, bitMask);
}
int AD5593R::setTHREESTATEmode(uint8_t bitMask)
{
// Page 26/43
// 1's => Three state OUTPUT
return writeRegister(AD5593_IO_TS_CONFIG, bitMask);
}
int AD5593R::setPULLDOWNmode(uint8_t bitMask)
{
// page 26/36
return writeRegister(AD5593_PULLDOWN_CONFIG, bitMask);
}
int AD5593R::setLDACmode(uint8_t mode)
{
// page 24
if (mode > AD5593R_LDAC_RELEASE)
{
return AD5593R_LDAC_ERROR;
}
return writeRegister(AD5593_LDAC_MODE, mode);
}
int AD5593R::setOpenDrainMode(uint8_t bitMask)
{
// Page 26 - output mode
return writeRegister(AD5593_GPIO_OPENDRAIN_CONFIG, bitMask);
}
////////////////////////////////////////////////////////////
//
// DIGITAL
//
// Page 26
uint16_t AD5593R::write1(uint8_t pin, uint8_t value)
{
if (pin > 7) return AD5593R_PIN_ERROR;
uint16_t bitMask = readConfigRegister(AD5593_GPIO_OUTPUT);
if (value == LOW) bitMask &= ~(1 << pin);
else bitMask |= (1 << pin);
return writeRegister(AD5593_GPIO_OUTPUT, bitMask);
}
uint16_t AD5593R::read1(uint8_t pin)
{
if (pin > 7) return AD5593R_PIN_ERROR;
uint16_t bitMask = readIORegister(AD5593_GPIO_READ);
return (bitMask >> pin) & 0x01;
}
uint16_t AD5593R::write8(uint8_t bitMask)
{
return writeRegister(AD5593_GPIO_OUTPUT, bitMask);
}
uint16_t AD5593R::read8()
{
return readIORegister(AD5593_GPIO_READ) & 0xFF;
}
////////////////////////////////////////////////////////////
//
// ANALOG
//
uint16_t AD5593R::writeDAC(uint8_t pin, uint16_t value)
{
if (pin > 7) return AD5593R_PIN_ERROR;
// max 12 bit == 4095 => clipping
if (value > 0x0FFF)
{
value = 0x0FFF;
}
return writeRegister(AD5593_DAC_WRITE(pin), value);
}
uint16_t AD5593R::readDAC(uint8_t pin)
{
if (pin > 7) return AD5593R_PIN_ERROR;
uint16_t raw = readIORegister(AD5593_DAC_READ(pin));
// if ((raw >> 12) != pin) error.
// remove four upper bits, contain the pin
raw &= 0x0FFF;
return raw;
}
uint16_t AD5593R::readADC(uint8_t pin)
{
// allow pin 8 for raw temperature
if (pin > 8) return AD5593R_PIN_ERROR;
// add all to the sequence including temperature.
// 0x0200 = REPeat bit
// 0x0100 = TEMPerature include bit
uint16_t pinmask = 1 << pin;
writeRegister(AD5593_ADC_SEQ, 0x0200 | pinmask);
// read one ADC conversion.
uint16_t raw = readIORegister(AD5593_ADC_READ);
// if ((raw >> 12) != pin) error.
// remove four upper bits, contain the pin
raw &= 0x0FFF;
return raw;
}
// note: identical to readADC(8)
float AD5593R::readTemperature()
{
// page 19.
// 0x0200 = REPeat bit
// 0x0100 = TEMPerature include bit
writeRegister(AD5593_ADC_SEQ, 0x0200 | 0x0100);
// read one ADC conversion.
uint16_t raw = readIORegister(AD5593_ADC_READ);
// if not temperature ADC
if ((raw & 0xF000) != 0x8000)
{
return -273.15; // 0 Kelvin.
}
// remove four upper bits, contain the pin
raw &= 0x0FFF;
// formulas page 19 refactored to minimize float math.
// verified with datasheet indicative numbers.
// RAW CELSIUS FAHRENHEIT
// 645 => -40 -40
// 1035 => +105 +221
// 1084 => +125 +257
return -283.59 + raw / 6.635 * _gain * _Vref;
// formulas page 19 refactored into one
// float gainVref = _gain * _Vref;
// float temp = raw - (0.5 * 4095) / gainVref;
// temp = temp / (2.654 * (2.5 / gainVref));
// return temp + 25.0;
//
// return -283.59 + raw / 6.635 * _gain * _Vref;
//
// float division can be optimized away.
// float tmp = -283.59 + raw * 0.1507159 * _Vref;
// if (_gain == 2) tmp *= 2;
// return tmp;
}
////////////////////////////////////////////////////////////
//
// V-REFERENCE
//
int AD5593R::setExternalReference(bool flag, float Vref)
{
// Page 40
uint16_t bitMask = readConfigRegister(AD5593_POWERDOWN_REF_CTRL);
if (flag) // external
{
bitMask &= ~0x0200;
_Vref = Vref;
}
else // internal
{
bitMask |= 0x0200;
_Vref = 2.5;
}
return writeRegister(AD5593_POWERDOWN_REF_CTRL, bitMask);
}
int AD5593R::powerDown()
{
// Page 40
uint16_t bitMask = readConfigRegister(AD5593_POWERDOWN_REF_CTRL);
bitMask |= 0x0400;
return writeRegister(AD5593_POWERDOWN_REF_CTRL, bitMask);
}
int AD5593R::wakeUp()
{
_Vref = 2.5;
// Page 40
uint16_t bitMask = readConfigRegister(AD5593_POWERDOWN_REF_CTRL);
bitMask &= ~0x0400;
return writeRegister(AD5593_POWERDOWN_REF_CTRL, bitMask);
}
int AD5593R::powerDownDac(uint8_t pin)
{
// Page 40
if (pin > 7) return 0;
uint16_t bitMask = readConfigRegister(AD5593_POWERDOWN_REF_CTRL);
bitMask |= (1 << pin);
return writeRegister(AD5593_POWERDOWN_REF_CTRL, bitMask);
}
int AD5593R::wakeUpDac(uint8_t pin)
{
// Page 40
if (pin > 7) return 0;
uint16_t bitMask = readConfigRegister(AD5593_POWERDOWN_REF_CTRL);
bitMask &= ~(1 << pin);
return writeRegister(AD5593_POWERDOWN_REF_CTRL, bitMask);
}
////////////////////////////////////////////////////////////
//
// RESET
//
int AD5593R::reset()
{
// page 19
return writeRegister(AD5593_SW_RESET, 0x0DAC);
_Vref = 2.5;
_gain = 1;
}
////////////////////////////////////////////////////////////
//
// PROTECTED
//
// Figure 36, page 20
int AD5593R::writeRegister(uint8_t reg, uint16_t data)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(data >> 8);
_wire->write(data & 0xFF);
_error = _wire->endTransmission();
return _error;
}
uint16_t AD5593R::readIORegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_error = _wire->endTransmission();
if (_wire->requestFrom(_address, (uint8_t)2, (uint8_t)1) != 2)
{
_error = AD5593R_I2C_ERROR;
return 0;
}
uint16_t data = _wire->read() << 8;
data += _wire->read();
return data;
}
uint16_t AD5593R::readConfigRegister(uint8_t reg)
{
_wire->beginTransmission(_address);
_wire->write(AD5593_GPIO_READ_CONFIG | reg);
_error = _wire->endTransmission();
if (_wire->requestFrom(_address, (uint8_t)2, (uint8_t)1) != 2)
{
_error = AD5593R_I2C_ERROR;
return 0;
}
uint16_t data = _wire->read() << 8;
data += _wire->read();
return data;
}
// -- END OF FILE --