-
Notifications
You must be signed in to change notification settings - Fork 2
/
Indio.cpp
executable file
·606 lines (509 loc) · 19.6 KB
/
Indio.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
/* Indio Library, by Rustem Iskuzhin for Industruino - wwww.industruino.com
* This library enables communication with the Industruino IND.I/O Baseboard.
* The IND.I/O Baseboard has 8CH of 24V digital I/O (6.5-32V supported),
* 4CH 0-10V/4-20mA ADC, 2CH 0-10V/4-20mA DAC, RS485 transceiver.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
Analog Calibration method:
Analog IN, 0-10V: In your sketch set the analogReadMode to V10_raw. Feed a known voltage between 0-10V into on of the 4 input channels. Read the corresponding raw ADC value from the serial terminal and write it down. Do this at two voltage levels, for example 2.5V and 7.5V. Enter the recorded raw ADC value and corresponding voltage (in mV) into the calibration array below. Repeat this for all 4 channels.
Analog IN, 4-20mA: In your sketch set the analogReadMode to mA_raw. Feed a known current between 0-20mA into on of the 4 input channels. Read the corresponding raw ADC value from the serial terminal and write it down. Do this at two current levels, for example 2.5V and 7.5V. Enter the recorded raw ADC value and corresponding voltage (in uA) into the calibration array below. Repeat this for all 4 channels. As a current source you can for example use a 500mA resistor in series with a 0-20V voltage source and multimeter in series.
Analog OUT, 0-10V: In your sketch set the analogWriteMode to V10_raw. Set the analogWrite value to anything between 0-4096. Read the corresponding output voltage on a multimeter and write it down. Do this at two DAC output values, for example 1000 and 3600. Enter the recorded raw DAC value and corresponding voltage (in mV) into the calibration array below. Repeat this for all 2 channels.
Analog OUT, 0-10V: In your sketch set the analogWriteMode to mA_raw. Set the analogWrite value to anything between 0-4096. Read the corresponding output current on a multimeter (connect multimeter between output and ground, best with a 100-500Ohm resistor in series) and write it down. Do this at two DAC output values, for example 1000 and 3600. Enter the recorded raw DAC value and corresponding current (in uA) into the calibration array below. Repeat this for all 2 channels.
*/
//Calibration data array for ADC, 0-10V mode
const int ADC_voltage_low_raw[5] = {0,384,384,382,386};//raw ADC value for low reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_voltage_low_mV[5] = {0,1006,1006,1006,1006}; //corresponding mV for low reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_voltage_high_raw[5] = {0,2939,2946,2952,2946}; //raw ADC value for high reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_voltage_high_mV[5] = {0,7985,7985,7985,7985}; //corresponding mV for high reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
//Calibration data array for ADC, 4-20mA mode
const int ADC_current_low_raw[5] = {0,1863,1863,1863,1863}; //raw ADC value for low reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_current_low_uA[5] = {0,10000,10000,10000,10000}; //corresponding uA for low reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_current_high_raw[5] = {0,3692,3692,3692,3692}; //raw ADC value for high reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
const int ADC_current_high_uA[5] = {0,20000,20000,20000,20000}; //corresponding uA for high reference calibration point. Ignore first 0, subsequent is CH1-CH4 from left to right.
//Calibration data array for DAC, 0-10V mode
const int DAC_voltage_low_raw[3] = {0,1000,1000};//raw DAC value for low reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_voltage_low_mV[3] = {0,2641,2630}; //corresponding mV for low reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_voltage_high_raw[3] = {0,2500,2500}; //raw DAC value for high reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_voltage_high_mV[3] = {0,6823,6805}; //corresponding mV for high reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
//Calibration data array for DAC, 4-20mA mode
const int DAC_current_low_raw[3] = {0,1000,1000}; //raw DAC value for low reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_current_low_uA[3] = {0,5162,5162}; //corresponding uA for low reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_current_high_raw[3] = {0,3600,3600}; //raw DAC value for high reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
const int DAC_current_high_uA[3] = {0,19530,19530}; //corresponding uA for high reference calibration point. Ignore first 0, subsequent is CH1-CH2 from left to right.
#include "Indio.h"
#include <Wire.h>
#include <Arduino.h>
#define BUFFER_SIZE 128
// I2C address for MCP3422 - base address for MCP3424
#define MCP3422_ADDRESS 0X68
// fields in configuration register
#define MCP342X_GAIN_FIELD 0X03 // PGA field
#define MCP342X_GAIN_X1 0X00 // PGA gain X1
#define MCP342X_GAIN_X2 0X01 // PGA gain X2
#define MCP342X_GAIN_X4 0X02 // PGA gain X4
#define MCP342X_GAIN_X8 0X03 // PGA gain X8
#define MCP342X_RES_FIELD 0X0C // resolution/rate field
#define MCP342X_RES_SHIFT 2 // shift to low bits
#define MCP342X_12_BIT 0X00 // 12-bit 240 SPS
#define MCP342X_14_BIT 0X04 // 14-bit 60 SPS
#define MCP342X_16_BIT 0X08 // 16-bit 15 SPS
#define MCP342X_18_BIT 0X0C // 18-bit 3.75 SPS
#define MCP342X_CONTINUOUS 0X10 // 1 = continuous, 0 = one-shot
#define MCP342X_CHAN_FIELD 0X60 // channel field
#define MCP342X_CHANNEL_1 0X00 // select MUX channel 1
#define MCP342X_CHANNEL_2 0X20 // select MUX channel 2
#define MCP342X_CHANNEL_3 0X40 // select MUX channel 3
#define MCP342X_CHANNEL_4 0X60 // select MUX channel 4
#define MCP342X_START 0X80 // write: start a conversion
#define MCP342X_BUSY 0X80 // read: output not ready
#define MCP4726_CMD_WRITEDAC (0x40) // Writes data to the DAC
#define MCP4726_CMD_WRITEDACEEPROM (0x60) // Writes data to the DAC and the EEPROM
// read mcp342x data
float IndioClass::analogRead(int pin)
{
float current, voltage;
long data;
if (pin != previouspin)
{
this->mcp342xWrite(adcConfig[pin]);
delay(sample_rate);
previouspin = pin;
}
// pointer used to form int32 data
uint8_t *p = (uint8_t *)&data;
// timeout - not really needed?
uint32_t start = millis();
do {
// assume 18-bit mode
Wire.requestFrom(MCP3422_ADDRESS, 4);
if (Wire.available() != 4) {
Serial.println("read failed");
}
for (int8_t i = 2; i >= 0; i--) {
p[i] = Wire.read();
}
// extend sign bits
p[3] = p[2] & 0X80 ? 0XFF : 0;
// read config/status byte
uint8_t s = Wire.read();
if ((s & MCP342X_RES_FIELD) != MCP342X_18_BIT) {
// not 18 bits - shift bytes for 12, 14, or 16 bits
p[0] = p[1];
p[1] = p[2];
p[2] = p[3];
}
if (mode_ADC[pin]==1)
{
current=map(((data/mvDivisor)+2048), ADC_current_low_raw[pin], ADC_current_high_raw[pin], ADC_current_low_uA[pin], ADC_current_high_uA[pin]);
return current /= 1000;
}
if (mode_ADC[pin]==2)
{
voltage=map(((data/mvDivisor)+2048), ADC_voltage_low_raw[pin], ADC_voltage_high_raw[pin], ADC_voltage_low_mV[pin], ADC_voltage_high_mV[pin]);
return voltage /= 1000;
}
if (mode_ADC[pin]==3)
{
voltage=map(((data/mvDivisor)+2048), ADC_voltage_low_raw[pin], ADC_voltage_high_raw[pin], ADC_voltage_low_mV[pin], ADC_voltage_high_mV[pin]);
return voltage /= 1000;
}
if (mode_ADC[pin]==4)
{
current=map(((data/mvDivisor)+2048), ADC_current_low_raw[pin], ADC_current_high_raw[pin], ADC_current_low_uA[pin], ADC_current_high_uA[pin]);
return ((((current-4000)/20000))*100);
}
if (mode_ADC[pin]==5)
{
voltage=map(((data/mvDivisor)+2048), ADC_voltage_low_raw[pin], ADC_voltage_high_raw[pin], ADC_voltage_low_mV[pin], ADC_voltage_high_mV[pin]);
return (((voltage/10000))*100);
}
if (mode_ADC[pin]==6)
{
voltage=map(((data/mvDivisor)+2048), ADC_voltage_low_raw[pin], ADC_voltage_high_raw[pin], ADC_voltage_low_mV[pin], ADC_voltage_high_mV[pin]);
return (((voltage/5000))*100);
}
if (mode_ADC[pin]==7)
{
current=((data/mvDivisor)+2048);
return current;
}
if (mode_ADC[pin]==8)
{
voltage=((data/mvDivisor)+2048);
return voltage;
}
if ((s & MCP342X_BUSY) == 0);
} while (millis() - start < 500); //allows rollover of millis()
Serial.println("read timeout");
}
//------------------------------------------------------------------------------
// write mcp342x configuration byte
int IndioClass::mcp342xWrite(int config)
{
Wire.beginTransmission(MCP3422_ADDRESS);
Wire.write(config);
Wire.endTransmission();
}
//------------------------------------------------------------------------------
void IndioClass::begin(char addr)
{
_i2caddr = addr;
Wire.begin();
}
void IndioClass::setAddress(char subAddress)
{
Wire.begin();
ADDRESS = 0x4 << 3 | subAddress;
REGISTER_INPUT = 0x0;
REGISTER_OUTPUT = 0x2;
REGISTER_CONFIG = 0x6;
// outputBuffer = 0;
// inputBuffer = 0;
// modeBuffer = 0;
}
void IndioClass::setAddress2(char subAddress)
{
Wire.begin();
ADDRESS = 0x4 << 3 | subAddress;
REGISTER_INPUT = 0x0;
REGISTER_OUTPUT = 0x2;
REGISTER_CONFIG = 0x6;
// outputBuffer2 = 0;
// inputBuffer2 = 0;
// modeBuffer2 = 0;
}
void IndioClass::analogWrite(int pin, float value, bool writeEEPROM )
{
int output;
if(mode_DAC[pin]==1)
{
output = map((value*1000), DAC_current_low_uA[pin], DAC_current_high_uA[pin], DAC_current_low_raw[pin], DAC_current_high_raw[pin]);
}
if(mode_DAC[pin]==2)
{
output = map((value*1000), DAC_voltage_low_mV[pin], DAC_voltage_high_mV[pin], DAC_voltage_low_raw[pin], DAC_voltage_high_raw[pin]);
}
if(mode_DAC[pin]==3)
{
output = map((value*1000), DAC_voltage_low_mV[pin], DAC_voltage_high_mV[pin], DAC_voltage_low_raw[pin], DAC_voltage_high_raw[pin]);
}
if(mode_DAC[pin]==4)
{
output = map(((value+4)*100), DAC_current_low_uA[pin], DAC_current_high_uA[pin], DAC_current_low_raw[pin], DAC_current_high_raw[pin]);
}
if(mode_DAC[pin]==5)
{
output = map((value*100), DAC_voltage_low_mV[pin], DAC_voltage_high_mV[pin], DAC_voltage_low_raw[pin], DAC_voltage_high_raw[pin]);
}
if(mode_DAC[pin]==6)
{
output = map((value*1000), DAC_voltage_low_mV[pin], DAC_voltage_high_mV[pin], DAC_voltage_low_raw[pin], DAC_voltage_high_raw[pin]);
}
if(mode_DAC[pin]==7)
{
output = value;
}
if(mode_DAC[pin]==8)
{
output = value;
}
if (pin == 1)
{
begin(0x60);
}
if (pin == 2)
{
begin(0x61);
}
uint8_t twbrback = TWBR;
TWBR = 12; // 400 khz
Wire.beginTransmission(_i2caddr);
if (writeEEPROM)
{
Wire.write(MCP4726_CMD_WRITEDACEEPROM);
}
else
{
Wire.write(MCP4726_CMD_WRITEDAC);
}
Wire.write(output / 16); // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4)
Wire.write((output % 16) << 4); // Lower data bits (D3.D2.D1.D0.x.x.x.x)
Wire.endTransmission();
TWBR = twbrback;
}
void IndioClass::setADCResolution(int res)
{
INDIO_ADC_RESOLUTION=res;
}
void IndioClass::analogReadMode(int pin, int mode)
{
int res=(INDIO_ADC_RESOLUTION-12)/2;
switch (res) {
case 0:
sample_rate = 5;
mvDivisor = 1;
break;
case 1:
sample_rate = 17;
mvDivisor = 4;
break;
case 2:
sample_rate = 67;
mvDivisor = 16;
break;
case 3:
sample_rate = 267;
mvDivisor = 64;
break;
}
previouspin = 0;
this->setAddress2(0x20);
bitClear(modeBuffer2, 10);
bitClear(modeBuffer2, 11);
bitClear(modeBuffer2, 12);
bitClear(modeBuffer2, 13);
this->flushMode2();
// setup the mode of a pin from PCA9555 call flushMode() or flush() to send it on PCA9555
if(mode == 1)
{
bitWrite(outputBuffer2, pin+9, HIGH);
adcConfig[pin]= MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=1;
}
else if (mode == 2)
{
bitWrite(outputBuffer2, pin+9, LOW);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=2;
}
else if (mode == 3)
{
bitWrite(outputBuffer2, pin+9, LOW);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2 | 1;
mode_ADC[pin]=3;
}
else if (mode == 4)
{
bitWrite(outputBuffer2, pin+9, HIGH);
adcConfig[pin]= MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=4;
}
else if (mode == 5)
{
bitWrite(outputBuffer2, pin+9, LOW);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=5;
}
else if (mode == 6)
{
bitWrite(outputBuffer2, pin+9, LOW);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2 | 1;
mode_ADC[pin]=6;
}
else if (mode == 7)
{
bitWrite(outputBuffer2, pin+9, HIGH);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=7;
}
else if (mode == 8)
{
bitWrite(outputBuffer2, pin+9, LOW);
adcConfig[pin] = MCP342X_START | MCP342X_CONTINUOUS | (pin-1) << 5 | res<< 2;
mode_ADC[pin]=8;
}
this->flushOutput2();
}
void IndioClass::analogWriteMode(int pin, int mode)
{
int tempstore = outputBuffer2;
this->setAddress2(0x20);
bitClear(modeBuffer2, 8);
bitClear(modeBuffer2, 9);
this->flushMode2();
outputBuffer2 = tempstore;
this->flushOutput2();
if(mode == 1)
{
bitSet(outputBuffer2, pin+7);
mode_DAC[pin]=1;
}
else if (mode == 2)
{
bitClear(outputBuffer2, pin+7);
mode_DAC[pin]=2;
}
else if (mode == 3)
{
bitClear(outputBuffer2, pin+7);
mode_DAC[pin]=3;
}
else if (mode == 4)
{
bitSet(outputBuffer2, pin+7);
mode_DAC[pin]=4;
}
else if (mode == 5)
{
bitClear(outputBuffer2, pin+7);
mode_DAC[pin]=5;
}
else if (mode == 6)
{
bitClear(outputBuffer2, pin+7);
mode_DAC[pin]= 6;
}
else if (mode == 7)
{
bitSet(outputBuffer2, pin+7);
mode_DAC[pin]=7;
}
else if (mode == 8)
{
bitClear(outputBuffer2, pin+7);
mode_DAC[pin]=8;
}
this->flushOutput2();
/*if (pin == 1)
{
begin(0x60);
}
if (pin == 2)
{
begin(0x61);
}
*/
}
void IndioClass::digitalMode(int pin, int mode)
{
this->setAddress(0x21);
// setup the mode of a pin from PCA9555 call flushMode() or flush() to send it on PCA9555
if(mode == 1)
{
bitClear(modeBuffer, pin*2-1);
}
else
{
bitSet(modeBuffer, pin*2-2);
}
this->flushMode();
}
void IndioClass::digitalWrite(int pin, int value)
{
this->setAddress(0x21);
bitWrite(outputBuffer, pin*2-1, value);
this->flushOutput();
}
int IndioClass::digitalRead(int pin)
{
this->setAddress(0x21);
// reads a bit from inputBuffer
// reads the value of a pin from PCA9555 it's not the current State. Call flush() or flushInput() before read
this->flushInput();
return (bitRead(inputBuffer,pin*2-2));
}
void IndioClass::flushMode()
{
// send modeBuffer to PCA9555 so you can config the inputs and outputs
gpio_dir(modeBuffer);
}
void IndioClass::flushOutput()
{
// send outputBuffer to PCA9555
gpio_write(outputBuffer);
}
void IndioClass::flushInput()
{
// receive inputBuffer from PCA9555
inputBuffer = gpio_read();
}
void IndioClass::flushMode2()
{
// send modeBuffer to PCA9555 so you can config the inputs and outputs
gpio_dir(modeBuffer2);
}
void IndioClass::flushOutput2()
{
// send outputBuffer to PCA9555
gpio_write(outputBuffer2);
}
void IndioClass::flushInput2()
{
// receive inputBuffer from PCA9555
inputBuffer2 = gpio_read();
}
void IndioClass::flush()
{
// send modeBuffer to PCA9555
flushMode();
// send outputBuffer to PCA9555
flushOutput();
// receive inputBuffer from PCA9555
flushInput();
}
void IndioClass::flush2()
{
// send modeBuffer to PCA9555
flushMode2();
// send outputBuffer to PCA9555
flushOutput2();
// receive inputBuffer from PCA9555
flushInput2();
}
int IndioClass::gpio_read()
{
int data = 0;
// Send input register address
Wire.beginTransmission(ADDRESS);
Wire.write(REGISTER_INPUT);
Wire.endTransmission();
// Connect to device and request two bytes
Wire.beginTransmission(ADDRESS);
Wire.requestFrom(ADDRESS, 2);
if (Wire.available()) {
data = Wire.read();
}
if (Wire.available()) {
data = word(Wire.read(),data);
}
Wire.endTransmission();
return data;
}
void IndioClass::gpio_write(int data)
{
// Send output register address
Wire.beginTransmission(ADDRESS);
Wire.write(REGISTER_OUTPUT);
// Connect to device and send two bytes
Wire.write(0xff & data); // low byte
Wire.write(data >> 8); // high byte
Wire.endTransmission();
}
void IndioClass::gpio_dir(int dir)
{
// Send config register address
Wire.beginTransmission(ADDRESS);
Wire.write(REGISTER_CONFIG);
// Connect to device and send two bytes
Wire.write(0xff & dir); // low byte
Wire.write(dir >> 8); // high byte
Wire.endTransmission();
}
IndioClass Indio;