forked from Robotics68/Teensy-LTC2946
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LTC2946.cpp
849 lines (666 loc) · 23 KB
/
LTC2946.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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*!
Revised Version
7/4/2020, J. Krause
Reformatted code into a single class, capable of functioning with Teensy 3.6.
-Has full functionality for continuous read of VIN, Current and Power.
-Has limited functionality for Snapshot mode (VIN, Current) (to be done in future if time permits)
-Incorporated experimentally determined constants (one constant to correlate measured with actual value).
-No limit functionality at this time.
-I2C error transaction checking via a single function to check and clear past errors (use in an if statement)
*/
/*!
LTC2946: 12-Bit Wide Range Power, Charge and Energy Monitor
The LTC®2946 is a rail-to-rail system monitor that measures
current, voltage, power, charge and energy. It features an
operating range of 2.7V to 100V and includes a shunt regulator
for supplies above 100V. The current measurement common mode
range of 0V to 100V is independent of the input supply.
A 12-bit ADC measures load current, input voltage and an
auxiliary external voltage. Load current and internally
calculated power are integrated over an external clock or
crystal or internal oscillator time base for charge and energy.
An accurate time base allows the LTC2946 to provide measurement
accuracy of better than ±0.6% for charge and ±1% for power and
energy. Minimum and maximum values are stored and an overrange
alert with programmable thresholds minimizes the need for software
polling. Data is reported via a standard I2C interface.
Shutdown mode reduces power consumption to 15uA.
*/
#include <Arduino.h>
#include <stdint.h>
#include "LTC2946.h"
#include <i2c_t3.h>
//#include <Wire.h>
LTC2946::LTC2946(uint8_t wire_num,uint8_t wire_addr) //!constructor
{
I2C_WIRE = wire_num;
I2C_ADDRESS = wire_addr;
}
void LTC2946::Setup()
{
if(I2C_WIRE == 0){
Wire.begin();
}else if(I2C_WIRE == 1){
Wire1.begin();
}else if(I2C_WIRE == 2){
Wire2.begin();
}else if(I2C_WIRE == 3){
Wire3.begin();
}
}
bool LTC2946::ErrorCheck()
//Check for past errors in I2C handshake. Resets tracking variable upon read.
{
if(I2C_ACK == 0){
return(true);
}else{
I2C_ACK = 0;
return(false);
}
}
//! Set the constants for converting RAW to values
void LTC2946::SetVINConst(float vin_const){VIN_CONST = vin_const;}
void LTC2946::SetAmperageConst(float i_const){CURRENT_CONST = i_const;}
void LTC2946::SetPowerConst(float w_const){POWER_CONST = w_const;}
void LTC2946::SetContinuous()
// Set default LTC2946 values for Continuous capture mode
{
int8_t ack = 0;
LTC2946_mode = 0;
ack = LTC2946_write(LTC2946_CTRLA_REG, CTRLA);
I2C_ACK |= ack;
}
void LTC2946::SetSnapShot()
{
LTC2946_mode = 1;
}
void LTC2946::EnableConversion(bool state)
{
use_conversion = state;
}
void LTC2946::EnableLegacy(bool state)
{
use_legacy = state;
}
float LTC2946::ReadVIN()
{
int8_t ack = 0;
uint16_t VIN_code;
float VIN_Return = 0;
//Continuous Request
if(LTC2946_mode == 0)
{
ack |= LTC2946_read_12_bits(LTC2946_VIN_MSB_REG, &VIN_code);
}
//Snapshot Request
else if(LTC2946_mode == 1)
{
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_VDD;
ack |= LTC2946_write(LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_STATUS2_REG, &busy);
}
while (0x8 & busy);
ack |= LTC2946_read_12_bits(LTC2946_VIN_MSB_REG, &VIN_code);
}
//Conversion
if(use_conversion)
{
if(use_legacy)
{
//Legacy conversion
VIN_Return = LTC2946_VIN_code_to_voltage(VIN_code);
}
else
{
//Experimental conversion
VIN_Return = (float)VIN_code*VIN_CONST;
}
}
else
{
//Return RAW value
VIN_Return = (float)VIN_code;
}
//update error
I2C_ACK |= ack;
return(VIN_Return);
}
float LTC2946::ReadCurrent()
{
int8_t ack = 0;
uint16_t current_code;
float current_Return = 0;
//Continuous Request
if(LTC2946_mode == 0)
{
ack |= LTC2946_read_12_bits(LTC2946_DELTA_SENSE_MSB_REG, ¤t_code);
}
//Snapshot Request
else if(LTC2946_mode == 1)
{
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_DELTA_SENSE;
ack |= LTC2946_write(LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_STATUS2_REG, &busy); //!< Check to see if conversion is still in process
}
while (0x8 & busy);
ack |= LTC2946_read_12_bits(LTC2946_DELTA_SENSE_MSB_REG, ¤t_code);
}
//Conversion
if(use_conversion)
{
if(use_legacy)
{
//Legacy conversion
current_Return = LTC2946_code_to_current(current_code);
}
else
{
//Experimental conversion
current_Return = (float)current_code*CURRENT_CONST;
}
}
else
{
//Return RAW value
current_Return = (float)current_code;
}
//update error
I2C_ACK |= ack;
return(current_Return);
}
float LTC2946::ReadPower()
{
int8_t ack = 0;
uint32_t power_code;
float power_Return = 0;
//Continuous Request
if(LTC2946_mode == 0)
{
ack |= LTC2946_read_24_bits(LTC2946_POWER_MSB2_REG, &power_code);
}
//Snapshot Request
else if(LTC2946_mode == 1)
{
//Not available Yet
}
//Conversion
if(use_conversion)
{
if(use_legacy)
{
//Legacy conversion
//Not available yet
}
else
{
//Experimental conversion
power_Return = (float)power_code*POWER_CONST;
}
}
else
{
//Return RAW value
power_Return = (float)power_code;
}
//update error
I2C_ACK |= ack;
return(power_Return);
}
/*
Here is where I would add provisions for limits and colombs and joules, if I ever find time for that
*/
// Write an 8-bit code to the LTC2946.
int8_t LTC2946::LTC2946_write(uint8_t adc_command, uint8_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
// ack = i2c_write_byte_data(i2c_address, adc_command, code);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
Wire.write(code);
ack = Wire.endTransmission(false);
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
Wire1.write(code);
ack = Wire1.endTransmission(false);
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
Wire2.write(code);
ack = Wire2.endTransmission(false);
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
Wire3.write(code);
ack = Wire3.endTransmission(false);
}
return ack;
}
// Write a 16-bit code to the LTC2946.
int8_t LTC2946::LTC2946_write_16_bits(uint8_t adc_command, uint16_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
uint8_t b[2];
uint16_t w;
} data;
data.w = code;
// ack = i2c_write_word_data(i2c_address, adc_command, code);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
Wire.write(data.b[1]);
Wire.write(data.b[0]);
ack = Wire.endTransmission(false);
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
Wire1.write(data.b[1]);
Wire1.write(data.b[0]);
ack = Wire1.endTransmission(false);
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
Wire2.write(data.b[1]);
Wire2.write(data.b[0]);
ack = Wire2.endTransmission(false);
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
Wire3.write(data.b[1]);
Wire3.write(data.b[0]);
ack = Wire3.endTransmission(false);
}
return(ack);
}
// Write a 24-bit code to the LTC2946.
int8_t LTC2946::LTC2946_write_24_bits(uint8_t adc_command, uint32_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
int32_t MY_int32; //!< 32-bit signed integer to be converted to four bytes
uint32_t MY_uint32; //!< 32-bit unsigned integer to be converted to four bytes
uint8_t MY_byte[4]; //!< 4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer
} data;
data.MY_int32 = code;
// ack = i2c_write_block_data(i2c_address, adc_command, (uint8_t) 3, data.LT_byte);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
Wire.write(data.MY_byte[2]);
Wire.write(data.MY_byte[1]);
Wire.write(data.MY_byte[0]);
ack = Wire.endTransmission(false);
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
Wire1.write(data.MY_byte[2]);
Wire1.write(data.MY_byte[1]);
Wire1.write(data.MY_byte[0]);
ack = Wire1.endTransmission(false);
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
Wire2.write(data.MY_byte[2]);
Wire2.write(data.MY_byte[1]);
Wire2.write(data.MY_byte[0]);
ack = Wire2.endTransmission(false);
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
Wire3.write(data.MY_byte[2]);
Wire3.write(data.MY_byte[1]);
Wire3.write(data.MY_byte[0]);
ack = Wire3.endTransmission(false);
}
return(ack);
}
int8_t LTC2946::LTC2946_write_32_bits(uint8_t adc_command, uint32_t code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
int32_t MY_int32; //!< 32-bit signed integer to be converted to four bytes
uint32_t MY_uint32; //!< 32-bit unsigned integer to be converted to four bytes
uint8_t MY_byte[4]; //!< 4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer
} data;
data.MY_int32 = code;
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
Wire.write(data.MY_byte[3]);
Wire.write(data.MY_byte[2]);
Wire.write(data.MY_byte[1]);
Wire.write(data.MY_byte[0]);
ack = Wire.endTransmission(false);
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
Wire1.write(data.MY_byte[3]);
Wire1.write(data.MY_byte[2]);
Wire1.write(data.MY_byte[1]);
Wire1.write(data.MY_byte[0]);
ack = Wire1.endTransmission(false);
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
Wire2.write(data.MY_byte[3]);
Wire2.write(data.MY_byte[2]);
Wire2.write(data.MY_byte[1]);
Wire2.write(data.MY_byte[0]);
ack = Wire2.endTransmission(false);
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
Wire3.write(data.MY_byte[3]);
Wire3.write(data.MY_byte[2]);
Wire3.write(data.MY_byte[1]);
Wire3.write(data.MY_byte[0]);
ack = Wire3.endTransmission(false);
}
// ack = i2c_write_block_data(i2c_address, adc_command, (uint8_t) 4, data.LT_byte);
return(ack);
}
// Reads an 8-bit adc_code from LTC2946
int8_t LTC2946::LTC2946_read(uint8_t adc_command, uint8_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
// ack = i2c_read_byte_data(i2c_address, adc_command, adc_code);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
ack = Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, (uint8_t)1);
*adc_code = Wire.read();
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
ack = Wire1.endTransmission(false);
Wire1.requestFrom(I2C_ADDRESS, (uint8_t)1);
*adc_code = Wire1.read();
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
ack = Wire2.endTransmission(false);
Wire2.requestFrom(I2C_ADDRESS, (uint8_t)1);
*adc_code = Wire2.read();
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
ack = Wire3.endTransmission(false);
Wire3.requestFrom(I2C_ADDRESS, (uint8_t)1);
*adc_code = Wire3.read();
}
return ack;
}
// Reads a 12-bit adc_code from LTC2946
int8_t LTC2946::LTC2946_read_12_bits(uint8_t adc_command, uint16_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
// Use union type defined in Linduino.h to combine two uint8_t's (8-bit unsigned integers) into one uint16_t (unsigned 16-bit integer)
// Then, shift by 4 bits and return in *adc_code
int8_t ack;
union
{
uint8_t b[2];
uint16_t w;
} data;
// ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
ack = Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire.read();
data.b[0] = Wire.read();
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
ack = Wire1.endTransmission(false);
Wire1.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire1.read();
data.b[0] = Wire1.read();
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
ack = Wire2.endTransmission(false);
Wire2.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire2.read();
data.b[0] = Wire2.read();
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
ack = Wire3.endTransmission(false);
Wire3.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire3.read();
data.b[0] = Wire3.read();
}
//Serial.print(data.b[1],BIN); Serial.print(" | "); Serial.print(data.b[0],BIN); Serial.print(" | "); Serial.print(data.w,BIN); Serial.print(" | ");
*adc_code = data.w;
*adc_code >>= 4;
return ack;
}
// Reads a 16-bit adc_code from LTC2946
int8_t LTC2946::LTC2946_read_16_bits(uint8_t adc_command, uint16_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
uint8_t b[2];
uint16_t w;
} data;
// ack = i2c_read_word_data(i2c_address, adc_command, adc_code);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
ack = Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire.read();
data.b[0] = Wire.read();
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
ack = Wire1.endTransmission(false);
Wire1.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire1.read();
data.b[0] = Wire1.read();
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
ack = Wire2.endTransmission(false);
Wire2.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire2.read();
data.b[0] = Wire2.read();
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
ack = Wire3.endTransmission(false);
Wire3.requestFrom(I2C_ADDRESS, (uint8_t)2);
data.b[1] = Wire3.read();
data.b[0] = Wire3.read();
}
*adc_code = data.w;
return ack;
}
// Reads a 24-bit adc_code from LTC2946
int8_t LTC2946::LTC2946_read_24_bits(uint8_t adc_command, uint32_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
int32_t MY_int32; //!< 32-bit signed integer to be converted to four bytes
uint32_t MY_uint32; //!< 32-bit unsigned integer to be converted to four bytes
uint8_t MY_byte[4]; //!< 4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer
} data;
// ack = i2c_read_block_data(i2c_address, adc_command, (uint8_t)3, data.LT_byte);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
ack = Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, (uint8_t)3);
data.MY_byte[2] = Wire.read();
data.MY_byte[1] = Wire.read();
data.MY_byte[0] = Wire.read();
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
ack = Wire1.endTransmission(false);
Wire1.requestFrom(I2C_ADDRESS, (uint8_t)3);
data.MY_byte[2] = Wire1.read();
data.MY_byte[1] = Wire1.read();
data.MY_byte[0] = Wire1.read();
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
ack = Wire2.endTransmission(false);
Wire2.requestFrom(I2C_ADDRESS, (uint8_t)3);
data.MY_byte[2] = Wire2.read();
data.MY_byte[1] = Wire2.read();
data.MY_byte[0] = Wire2.read();
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
ack = Wire3.endTransmission(false);
Wire3.requestFrom(I2C_ADDRESS, (uint8_t)3);
data.MY_byte[2] = Wire3.read();
data.MY_byte[1] = Wire3.read();
data.MY_byte[0] = Wire3.read();
}
*adc_code = 0x0FFFFFF & data.MY_int32;
return(ack);
}
// Reads a 32-bit adc_code from LTC2946
int8_t LTC2946::LTC2946_read_32_bits(uint8_t adc_command, uint32_t *adc_code)
// The function returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack;
union
{
int32_t MY_int32; //!< 32-bit signed integer to be converted to four bytes
uint32_t MY_uint32; //!< 32-bit unsigned integer to be converted to four bytes
uint8_t MY_byte[4]; //!< 4 bytes (unsigned 8-bit integers) to be converted to a 32-bit signed or unsigned integer
} data;
// ack = i2c_read_block_data(i2c_address, adc_command, (uint8_t) 4, data.LT_byte);
if(I2C_WIRE == 0){
Wire.beginTransmission(I2C_ADDRESS);
Wire.write(adc_command);
ack = Wire.endTransmission(false);
Wire.requestFrom(I2C_ADDRESS, (uint8_t)4);
data.MY_byte[3] = Wire.read();
data.MY_byte[2] = Wire.read();
data.MY_byte[1] = Wire.read();
data.MY_byte[0] = Wire.read();
}else if(I2C_WIRE == 1){
Wire1.beginTransmission(I2C_ADDRESS);
Wire1.write(adc_command);
ack = Wire1.endTransmission(false);
Wire1.requestFrom(I2C_ADDRESS, (uint8_t)4);
data.MY_byte[3] = Wire1.read();
data.MY_byte[2] = Wire1.read();
data.MY_byte[1] = Wire1.read();
data.MY_byte[0] = Wire1.read();
}else if(I2C_WIRE == 2){
Wire2.beginTransmission(I2C_ADDRESS);
Wire2.write(adc_command);
ack = Wire2.endTransmission(false);
Wire2.requestFrom(I2C_ADDRESS, (uint8_t)4);
data.MY_byte[3] = Wire2.read();
data.MY_byte[2] = Wire2.read();
data.MY_byte[1] = Wire2.read();
data.MY_byte[0] = Wire2.read();
}else if(I2C_WIRE == 3){
Wire3.beginTransmission(I2C_ADDRESS);
Wire3.write(adc_command);
ack = Wire3.endTransmission(false);
Wire3.requestFrom(I2C_ADDRESS, (uint8_t)4);
data.MY_byte[3] = Wire3.read();
data.MY_byte[2] = Wire3.read();
data.MY_byte[1] = Wire3.read();
data.MY_byte[0] = Wire3.read();
}
*adc_code = 0xFFFFFFFF & data.MY_int32;
return(ack);
}
// Calculate the LTC2946 VIN voltage
float LTC2946::LTC2946_VIN_code_to_voltage(uint16_t adc_code)
// Returns the VIN Voltage in Volts
//Legacy code
{
float voltage;
voltage = (float)adc_code*LTC2946_VIN_lsb; //! 1) Calculate voltage from code and lsb
return(voltage);
}
// Calculate the LTC2946 ADIN voltage
float LTC2946::LTC2946_ADIN_code_to_voltage(uint16_t adc_code)
// Returns the ADIN Voltage in Volts
//Legacy code
{
float adc_voltage;
adc_voltage = (float)adc_code*LTC2946_ADIN_lsb; //! 1) Calculate voltage from code and ADIN lsb
return(adc_voltage);
}
// Calculate the LTC2946 current with a sense resistor
float LTC2946::LTC2946_code_to_current(uint16_t adc_code)
// Returns the LTC2946 current in Amps
//Legacy code
{
float voltage, current;
voltage = (float)adc_code*LTC2946_DELTA_SENSE_lsb; //! 1) Calculate voltage from ADC code and delta sense lsb
current = voltage/resistor; //! 2) Calculate current, I = V/R
return(current);
}
// Calculate the LTC2946 power
float LTC2946::LTC2946_code_to_power(int32_t adc_code)
// Returns The LTC2946 power in Watts
//Legacy code
{
float power;
power = (float)adc_code*LTC2946_Power_lsb/resistor; //! 1) Calculate Power using Power lsb and resistor
return(power);
}
// Calculate the LTC2946 energy
float LTC2946::LTC2946_code_to_energy(int32_t adc_code)
// Returns the LTC2946 energy in Joules
//Legacy code
{
float energy_lsb, energy;
energy_lsb=(float)(LTC2946_Power_lsb/resistor)*65536*LTC2946_TIME_lsb; //! 1) Calculate Energy lsb from Power lsb and Time lsb
energy = adc_code*energy_lsb; //! 2) Calculate Energy using Energy lsb and adc code
return(energy);
}
// Calculate the LTC2946 Coulombs
float LTC2946::LTC2946_code_to_coulombs(int32_t adc_code)
// Returns the LTC2946 Coulombs
//Legacy code
{
float coulomb_lsb, coulombs;
coulomb_lsb=(float)(LTC2946_DELTA_SENSE_lsb/resistor)*16*LTC2946_TIME_lsb; //! 1) Calculate Coulomb lsb Current lsb and Time lsb
coulombs = adc_code*coulomb_lsb; //! 2) Calculate Coulombs using Coulomb lsb and adc code
return(coulombs);
}
//Calculate the LTC2946 Time in Seconds
float LTC2946::LTC2946_code_to_time(float time_code)
//Legacy code
{
float seconds;
seconds = LTC2946_TIME_lsb * time_code;
return seconds;
}