-
Notifications
You must be signed in to change notification settings - Fork 9
/
DC2156.ino
1849 lines (1642 loc) · 72.3 KB
/
DC2156.ino
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
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
DC2156A
LTC2946: 12-Bit Wide Range Power, Charge and Energy Monitor
@verbatim
Setting the Alert Thresholds:
1. Select the Alert option from the main menu.
2. Select the desired Thresholds to be changed. Then enter the minimum and maximum
values.
3. Select the Enable and Disable Alert Option and Enable the desired alerts.
4. Lastly, go back to the main menu and start reading values in Continuous Mode
or Snapshot Mode.
Note: Alerts only respond when conversion is done. Therefore, in continuous
mode the alerts will constantly be updated.
Reading and Clearing an Alert:
1. Select the Alert option from the main menu.
2. Select the Read and Clear Alerts option. This reads all faults that occured
and clears the alerts.
NOTES
Setup:
Set the terminal baud rate to 115200 and select the newline terminator.
Requires a power supply.
Refer to demo manual DC2156A.
USER INPUT DATA FORMAT:
decimal : 1024
hex : 0x400
octal : 02000 (leading 0 "zero")
binary : B10000000000
float : 1024.0
@endverbatim
REVISION HISTORY
$Revision: 2348 $
$Date: 2014-04-14 13:46:45 -0700 (Mon, 14 Apr 2014) $
Copyright (c) 2013, Linear Technology Corp.(LTC)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of Linear Technology Corp.
The Linear Technology Linduino is not affiliated with the official Arduino team.
However, the Linduino is only possible because of the Arduino team's commitment
to the open-source community. Please, visit http://www.arduino.cc and
http://store.arduino.cc , and consider a purchase that will help fund their
ongoing work.
*/
/*! @file
@ingroup LTC2946
*/
#include <Arduino.h>
#include <stdint.h>
#include "Linduino.h"
#include "LT_I2C.h"
#include "LT_SPI.h"
#include "UserInterface.h"
#include "QuikEval_EEPROM.h"
#include "LTC2946.h"
#include <Wire.h>
#include <SPI.h>
// Function Declaration
void print_title(); // Print the title block
void print_prompt(); // Print the Prompt
void store_alert_settings(); // Store the alert settings to the EEPROM
int8_t restore_alert_settings(); // Read the alert settings from EEPROM
int8_t menu_1_continuous_mode(uint8_t CTRLA, uint8_t VOLTAGE_SEL, float scale);
int8_t menu_2_snapshot_mode(uint8_t VOLTAGE_SEL, float scale);
int8_t menu_2_snapshot_mode_menu_1_SENSE();
int8_t menu_2_snapshot_mode_menu_2_ADIN(float scale);
int8_t menu_2_snapshot_mode_menu_3_VDD();
int8_t menu_2_snapshot_mode_menu_4_Current();
int8_t menu_3_alert(uint8_t VOLTAGE_SEL, float scale);
int8_t menu_3_alert_menu_1_set_power_alerts(uint8_t VOLTAGE_SEL, float scale);
int8_t menu_3_alert_menu_2_set_current_alerts();
int8_t menu_3_alert_menu_3_set_vin_alerts();
int8_t menu_3_alert_menu_4_set_adin_alerts(float scale);
int8_t menu_3_alert_menu_5_enable_disable_alerts();
int8_t menu_3_alert_menu_6_read_clear_faults();
int8_t menu_4_shutdown(uint8_t &CTRLB, uint8_t &shutdown);
int8_t menu_5_settings(uint8_t &CTRLA, uint8_t &CTRLB, uint8_t &VOLTAGE_SEL, uint8_t &GPIO_CFG, uint8_t &GPIO3_CTRL, float &scale);
int8_t menu_5_settings_menu_1_configure_CTRLA_reg(uint8_t CTRLA, uint8_t VOLTAGE_SEL);
int8_t menu_5_settings_menu_2_configure_CTRLB_reg(uint8_t CTRLB);
int8_t menu_5_settings_menu_3_configure_GPIO(uint8_t GPIO_CFG, uint8_t GPIO3_CTRL);
int8_t menu_6_clear_min_max();
#define CONTINUOUS_MODE_DISPLAY_DELAY 2000 //!< The delay between readings
const float resistor = .02; //!< resistor value on demo board
const float CLK_FREQ = 4E6; //!< CLK Frequency
// LSB Weights
const float LTC2946_ADIN_lsb = 5.001221E-04; //!< Typical ADIN lsb weight in volts
const float LTC2946_DELTA_SENSE_lsb = 2.5006105E-05; //!< Typical Delta lsb weight in volts
const float LTC2946_VIN_lsb = 2.5006105E-02; //!< Typical VIN lsb weight in volts
const float LTC2946_Power_lsb = 6.25305E-07; //!< Typical POWER lsb weight in V^2 VIN_lsb * DELTA_SENSE_lsb
const float LTC2946_ADIN_DELTA_SENSE_lsb = 1.25061E-08; //!< Typical sense lsb weight in V^2 *ADIN_lsb * DELTA_SENSE_lsb
const float LTC2946_INTERNAL_TIME_lsb = 4101.00/250000.00; //!< Internal TimeBase lsb. Use LTC2946_TIME_lsb if an external CLK is used. See Settings menu for how to calculate Time LSB.
static float LTC2946_TIME_lsb = 16.39543E-3; //!< Static variable which is based off of the default clk frequency of 250KHz.
// Error string
const char ack_error[] = "Error: No Acknowledge. Check I2C Address."; //!< Error message
// Global variables
static int8_t demo_board_connected; //!< Set to 1 if the board is connected
static uint8_t alert1_code = 0; //!< Value stored or read from ALERT1 register. Shared between loop() and restore_alert_settings()
static uint8_t alert2_code = 0; //!< Value stored or read from ALERT2 register. Shared between loop() and restore_alert_settings()
static bool internalCLK = true;
//! Initialize Linduino
void setup()
{
char demo_name[] = "DC2156"; // Demo Board Name stored in QuikEval EEPROM
quikeval_I2C_init(); //! Configure the EEPROM I2C port for 100kHz
quikeval_I2C_connect(); //! Connects to main I2C port
Serial.begin(115200); //! Initialize the serial port to the PC
print_title(); //! Print Title
demo_board_connected = discover_demo_board(demo_name);
if(!demo_board_connected){
Serial.println(F("Demo board not detected, will attempt to proceed"));
demo_board_connected = true;
}
if (demo_board_connected)
{
restore_alert_settings();
print_prompt();
}
}
//! Repeats Linduino loop
void loop()
{
int8_t ack = 0; // I2C acknowledge indicator
static uint8_t user_command; // The user input command
static uint8_t CTRLA = LTC2946_CHANNEL_CONFIG_V_C_3|LTC2946_SENSE_PLUS|LTC2946_OFFSET_CAL_EVERY|LTC2946_ADIN_GND; //! Set Control A register to default value.
static uint8_t CTRLB = LTC2946_DISABLE_ALERT_CLEAR<C2946_DISABLE_SHUTDOWN<C2946_DISABLE_CLEARED_ON_READ<C2946_DISABLE_STUCK_BUS_RECOVER<C2946_ENABLE_ACC<C2946_DISABLE_AUTO_RESET; //! Set Control B Register to default value
static uint8_t GPIO_CFG = LTC2946_GPIO1_OUT_LOW |LTC2946_GPIO2_IN_ACC|LTC2946_GPIO3_OUT_ALERT; //! Set GPIO_CFG Register to Default value
static uint8_t GPIO3_CTRL = LTC2946_GPIO3_OUT_HIGH_Z; //! Set GPIO3_CTRL to Default Value
static uint8_t VOLTAGE_SEL = LTC2946_SENSE_PLUS; //! Set Voltage selection to default value.
static uint8_t shutdown = 0; //! Set Shutdown = 1 to put part in shutdown. This is done through menu item 4.
static float scale = 102.4/2/046; //! Stores division ration for resistive divider on ADIN pin. Configured inside "Settings" menu.
if (demo_board_connected) //! Do nothing if the demo board is not connected
{
if (Serial.available()) //! Do nothing if serial is not available
{
user_command = read_int(); //! Read user input command
if (user_command != 'm')
Serial.println(user_command);
Serial.println();
ack = 0;
switch (user_command) //! Prints the appropriate submenu
{
case 1:
ack |= menu_1_continuous_mode(CTRLA, VOLTAGE_SEL, scale); //! Continuous Mode Measurement
break ;
case 2:
ack |= menu_2_snapshot_mode(VOLTAGE_SEL, scale); //! SnapShot Mode Measurement
break;
case 3:
ack |= menu_3_alert(VOLTAGE_SEL, scale); //! Alert and Threshold Menu
break;
case 4:
ack |= menu_4_shutdown(&CTRLB, &shutdown); //! Toggle Shutdown Down Mode
break;
case 5:
menu_5_settings(&CTRLA, &CTRLB, &VOLTAGE_SEL, &GPIO_CFG, &GPIO3_CTRL, &scale); // Settings
break;
case 6:
ack |= menu_6_clear_min_max(); // Clear Min/Max
break;
default:
Serial.println("Incorrect Option");
break;
}
if (ack != 0)
Serial.println(ack_error);
Serial.print(F("*************************"));
print_prompt();
}
}
}
// Function Definitions
//! Print the title block
void print_title()
{
Serial.println(F("\n*****************************************************************"));
Serial.print(F("* DC2156 Demonstration Program *\n"));
Serial.print(F("* *\n"));
Serial.print(F("* This program communicates with the LTC2946 12-Bit Wide Range *\n"));
Serial.print(F("* I2C Energy and Power Monitor found on the DC2156 demo board. *\n"));
Serial.print(F("* Set the baud rate to 115200 and select the newline terminator.*\n"));
Serial.print(F("* *\n"));
Serial.print(F("*****************************************************************\n"));
}
//! Print the Prompt
void print_prompt()
{
Serial.print(F("\n1-Continuous Mode\n"));
Serial.print(F("2-Snapshot Mode\n"));
Serial.print(F("3-Alert and Threshold Menu\n"));
Serial.print(F("4-Toggle Shutdown Mode\n"));
Serial.print(F("5-Settings\n"));
Serial.print(F("6-Clear Min/Max\n\n"));
Serial.print(F("Enter a command: "));
}
//! Continuous Mode.
int8_t menu_1_continuous_mode(uint8_t CTRLA, //!< CTRLA Register sets the mode in which Continious measurements are made. Configured in "Settings" menu.
uint8_t VOLTAGE_SEL, //!< VOLTAGE_SEL variable represents the voltage channel selected. Scaling is done if ADIN channel is selected and resistive dividers are present.
float scale) //!< Stores division ratio for resistive divider on ADIN pin. Configured inside "Settings" menu.
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
uint8_t LTC2946_mode;
int8_t ack = 0;
LTC2946_mode = CTRLA; //! Set the configuration of the CTRLA Register.
Serial.println();
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_CTRLA_REG, LTC2946_mode); //! Sets the LTC2946 to continuous mode
//! Measurement Loop. Keeps Measuring until 'm' is pressed.
//! Measurement is done by first reading the adc code and then converting it to the respective value.
do
{
if (VOLTAGE_SEL != LTC2946_ADIN)
{
Serial.print(F("*************************\n\n"));
uint32_t power_code, max_power_code, min_power_code;
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_POWER_MSB2_REG, &power_code);
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_POWER_MSB2_REG, &max_power_code);
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_POWER_MSB2_REG, &min_power_code);
float power, max_power, min_power; // Store power results
power = LTC2946_code_to_power(power_code, resistor, LTC2946_Power_lsb);
max_power = LTC2946_code_to_power(max_power_code, resistor, LTC2946_Power_lsb);
min_power = LTC2946_code_to_power(min_power_code, resistor, LTC2946_Power_lsb);
Serial.print(F("****Power: "));
Serial.print(power, 4);
Serial.print(F(" W\n"));
Serial.print(F("Max Power: "));
Serial.print(max_power, 4);
Serial.print(F(" W\n"));
Serial.print(F("Min Power: "));
Serial.print(min_power, 4);
Serial.print(F(" W\n"));
}
else
{
Serial.print(F("*************************"));
Serial.println();
Serial.println();
uint32_t power_code, max_power_code, min_power_code;
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_POWER_MSB2_REG, &power_code);
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_POWER_MSB2_REG, &max_power_code);
ack |= LTC2946_read_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_POWER_MSB2_REG, &min_power_code);
float power, max_power, min_power;
power = LTC2946_code_to_power(power_code, resistor, LTC2946_ADIN_DELTA_SENSE_lsb) * scale;
max_power = LTC2946_code_to_power(max_power_code, resistor, LTC2946_ADIN_DELTA_SENSE_lsb) * scale;
min_power = LTC2946_code_to_power(min_power_code, resistor, LTC2946_ADIN_DELTA_SENSE_lsb) * scale;
Serial.print(F("\n***ADIN Power: "));
Serial.print(power, 4);
Serial.print(F(" W\n"));
Serial.print(F("Max ADIN Power: "));
Serial.print(max_power, 4);
Serial.print(F(" W\n"));
Serial.print(F("Min ADIN Power: "));
Serial.print(min_power, 4);
Serial.print(F(" W\n"));
}
uint16_t current_code, max_current_code, min_current_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_DELTA_SENSE_MSB_REG, ¤t_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_DELTA_SENSE_MSB_REG, &max_current_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_DELTA_SENSE_MSB_REG, &min_current_code);
float current, max_current, min_current;
current = LTC2946_code_to_current(current_code, resistor, LTC2946_DELTA_SENSE_lsb);
max_current = LTC2946_code_to_current(max_current_code, resistor, LTC2946_DELTA_SENSE_lsb);
min_current = LTC2946_code_to_current(min_current_code, resistor, LTC2946_DELTA_SENSE_lsb);
Serial.print(F("\n****Current: "));
Serial.print(current, 4);
Serial.print(F(" A\n"));
Serial.print(F("Max Current: "));
Serial.print(max_current, 4);
Serial.print(F(" A\n"));
Serial.print(F("Min Current: "));
Serial.print(min_current, 4);
Serial.print(F(" A\n"));
uint16_t VIN_code, max_VIN_code, min_VIN_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_VIN_MSB_REG, &VIN_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_VIN_MSB_REG, &max_VIN_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_VIN_MSB_REG, &min_VIN_code);
float VIN, max_VIN, min_VIN;
VIN = LTC2946_VIN_code_to_voltage(VIN_code , LTC2946_VIN_lsb);
max_VIN = LTC2946_VIN_code_to_voltage(max_VIN_code, LTC2946_VIN_lsb);
min_VIN = LTC2946_VIN_code_to_voltage(min_VIN_code, LTC2946_VIN_lsb);
Serial.print(F("\n****VIN: "));
Serial.print(VIN, 4);
Serial.print(F(" V\n"));
Serial.print(F("Max VIN: "));
Serial.print(max_VIN, 4);
Serial.print(F(" V\n"));
Serial.print(F("Min VIN: "));
Serial.print(min_VIN, 4);
Serial.print(F(" V\n"));
uint16_t ADIN_code, max_ADIN_code, min_ADIN_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_ADIN_MSB_REG, &ADIN_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_ADIN_MSB_REG, &max_ADIN_code);
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_ADIN_MSB_REG, &min_ADIN_code);
float ADIN, max_ADIN, min_ADIN;
ADIN = LTC2946_ADIN_code_to_voltage(ADIN_code, LTC2946_ADIN_lsb)*scale;
max_ADIN = LTC2946_ADIN_code_to_voltage(max_ADIN_code, LTC2946_ADIN_lsb)*scale;
min_ADIN = LTC2946_ADIN_code_to_voltage(min_ADIN_code, LTC2946_ADIN_lsb)*scale;
Serial.print(F("\n****ADIN: "));
Serial.print(ADIN, 4);
Serial.print(F(" V\n"));
Serial.print(F("Max ADIN: "));
Serial.print(max_ADIN, 4);
Serial.print(F(" V\n"));
Serial.print(F("Min ADIN: "));
Serial.print(min_ADIN, 4);
Serial.print(F(" V\n"));
uint32_t energy_code;
ack |= LTC2946_read_32_bits(LTC2946_I2C_ADDRESS, LTC2946_ENERGY_MSB3_REG, &energy_code);
uint32_t charge_code;
ack |= LTC2946_read_32_bits(LTC2946_I2C_ADDRESS, LTC2946_CHARGE_MSB3_REG, &charge_code);
uint32_t time_code;
ack |= LTC2946_read_32_bits(LTC2946_I2C_ADDRESS, LTC2946_TIME_COUNTER_MSB3_REG, &time_code);
float energy,charge,time;
if(internalCLK)
{
energy = LTC2946_code_to_energy(energy_code,resistor,LTC2946_Power_lsb, LTC2946_INTERNAL_TIME_lsb);
charge = LTC2946_code_to_coulombs(charge_code,resistor,LTC2946_DELTA_SENSE_lsb, LTC2946_INTERNAL_TIME_lsb);
time = LTC2946_code_to_time(time_code, LTC2946_INTERNAL_TIME_lsb);
}
else
{
energy = LTC2946_code_to_energy(energy_code,resistor,LTC2946_Power_lsb, LTC2946_TIME_lsb);
charge = LTC2946_code_to_coulombs(charge_code,resistor,LTC2946_DELTA_SENSE_lsb, LTC2946_TIME_lsb);
time = LTC2946_code_to_time(time_code, LTC2946_TIME_lsb);
}
Serial.print(F("**********Accumulators**********\n"));
Serial.print(F("\n****Energy: "));
Serial.print(energy, 4);
Serial.print(F(" J\n"));
Serial.print(F("****Charge: "));
Serial.print(charge, 4);
Serial.print(F(" C\n"));
Serial.print(F("****Time: "));
Serial.print(time, 4);
Serial.print(F(" s\n\n"));
Serial.print(F("********************************\n"));
Serial.print(F("m-Main Menu\n\n"));
Serial.flush();
delay(CONTINUOUS_MODE_DISPLAY_DELAY);
}
while (Serial.available() == false);
read_int(); // clears the Serial.available
return(ack);
}
//! Snapshot Mode Menu
int8_t menu_2_snapshot_mode(uint8_t VOLTAGE_SEL, //!< VOLTAGE_SEL variable represents the voltage channel selected. Scaling is done if ADIN channel is selected and resistive dividers are present.
float scale) //!< Stores division ratio for resistive divider on ADIN pin. Configured inside "Settings" menu.
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
//! Print submenu
Serial.print(F("*************************\n\n"));
Serial.print(F("Snapshot Mode\n"));
Serial.print(F("1-SENSE+\n"));
Serial.print(F("2-ADIN\n"));
Serial.print(F("3-VDD\n"));
Serial.print(F("4-Current\n"));
Serial.print(F("m-Main Menu\n"));
uint8_t user_command;
do
{
Serial.print(F("\n\nEnter a Command: "));
user_command = read_int();
if (user_command == 'm')
Serial.println("m");
else
{
Serial.println(user_command);
}
if (user_command != 'm')
{
Serial.print(F("*************************\n\n"));
Serial.print(F("Snapshot Mode\n"));
Serial.print(F("1-SENSE+\n"));
Serial.print(F("2-ADIN\n"));
Serial.print(F("3-VDD\n"));
Serial.print(F("4-Current\n"));
Serial.print(F("m-Main Menu\n"));
}
Serial.println();
switch (user_command)
{
case 1: // SENSE+ - Snapshot Mode
ack |= menu_2_snapshot_mode_menu_1_SENSE();
break;
case 2: // VIN - Snapshot Mode
ack |= menu_2_snapshot_mode_menu_2_ADIN(scale);
break;
case 3: // ADIN - Snapshot Mode
ack |= menu_2_snapshot_mode_menu_3_VDD();
break;
case 4: //Current - Snapshot Mode
ack |= menu_2_snapshot_mode_menu_4_Current();
break;
default:
if (user_command != 'm')
Serial.println("Incorrect Option");
break;
}
if (user_command != 'm')
{
Serial.print(F("*************************\n\n"));
Serial.print(F("Snapshot Mode\n"));
Serial.print(F("1-SENSE+\n"));
Serial.print(F("2-ADIN\n"));
Serial.print(F("3-VDD\n"));
Serial.print(F("4-Current\n"));
Serial.print(F("m-Main Menu\n"));
}
}
while (!((user_command == 'm') || (ack)));
return(ack);
}
//! SENSE+ - Snapshot mode
int8_t menu_2_snapshot_mode_menu_1_SENSE()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_SENSE_PLUS;
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_I2C_ADDRESS, LTC2946_STATUS2_REG, &busy);
}
while (0x8 & busy);
uint16_t voltage_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_VIN_MSB_REG, &voltage_code);
float voltage;
voltage = LTC2946_VIN_code_to_voltage(voltage_code, LTC2946_VIN_lsb);
Serial.print(F("SENSE+: "));
Serial.print(voltage, 4);
Serial.print(F(" V\n\n"));
return(ack);
}
//! ADIN - Snapshot Mode
int8_t menu_2_snapshot_mode_menu_2_ADIN(float scale)
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_ADIN;
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_I2C_ADDRESS, LTC2946_STATUS2_REG, &busy);
}
while (0x8 & busy);
uint16_t ADIN_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_ADIN_MSB_REG, &ADIN_code);
float ADIN;
ADIN = LTC2946_ADIN_code_to_voltage(ADIN_code, LTC2946_ADIN_lsb)*scale;
Serial.print(F("ADIN: "));
Serial.print(ADIN, 4);
Serial.print(F(" V\n\n"));
return(ack);
}
//! VDD - Snapshot Mode
int8_t menu_2_snapshot_mode_menu_3_VDD()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_VDD;
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_I2C_ADDRESS, LTC2946_STATUS2_REG, &busy);
}
while (0x8 & busy);
uint16_t VDD_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_VIN_MSB_REG, &VDD_code);
float VDD;
VDD = LTC2946_VIN_code_to_voltage(VDD_code, LTC2946_VIN_lsb);
Serial.print(F("VDD: "));
Serial.print(VDD, 4);
Serial.print(F(" V\n\n"));
return(ack);
}
//! Current - Snapshot Mode
int8_t menu_2_snapshot_mode_menu_4_Current()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
int8_t LTC2946_mode;
LTC2946_mode = LTC2946_CHANNEL_CONFIG_SNAPSHOT | LTC2946_DELTA_SENSE;
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_CTRLA_REG, LTC2946_mode);
uint8_t busy;
do
{
ack |= LTC2946_read(LTC2946_I2C_ADDRESS, LTC2946_STATUS2_REG, &busy); //!< Check to see if conversion is still in process
}
while (0x8 & busy);
uint16_t current_code;
ack |= LTC2946_read_12_bits(LTC2946_I2C_ADDRESS, LTC2946_DELTA_SENSE_MSB_REG, ¤t_code);
float current;
current = LTC2946_code_to_current(current_code, resistor, LTC2946_DELTA_SENSE_lsb);
Serial.print(F("Current: "));
Serial.print(current, 4);
Serial.print(F(" A\n\n"));
return(ack);
}
//! Alert and Threshold Menu
int8_t menu_3_alert(uint8_t VOLTAGE_SEL, //!< Voltage Selection Variable.
float scale) //!< Stores division ratio for resistive divider on ADIN pin. Configured inside "Settings" menu.
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
int8_t user_command;
do
{
//Print Sub-Menu
Serial.print(F("*************************\n\n"));
Serial.print(F("1-Set Power Alert Thresholds\n"));
Serial.print(F("2-Set Current Alert Thresholds\n"));
Serial.print(F("3-Set VIN Alert Thresholds\n"));
Serial.print(F("4-Set ADIN Alert Thresholds\n"));
Serial.print(F("5-Enable and Disable Alerts\n"));
Serial.print(F("6-Read and Clear Alerts\n"));
Serial.print(F("7-Store Alert Settings to EEPROM\n"));
Serial.print(F("m-Main Menu\n\n"));
Serial.print(F("Enter a command: "));
user_command = read_int();
if (user_command == 'm')
Serial.println("m");
else
Serial.println(user_command);
Serial.println();
switch (user_command)
{
case 1:
ack |= menu_3_alert_menu_1_set_power_alerts(VOLTAGE_SEL, scale); // Set Power Alert Thresholds
break;
case 2:
ack |= menu_3_alert_menu_2_set_current_alerts(); // Set Current Alert Thresholds
break;
case 3:
ack |= menu_3_alert_menu_3_set_vin_alerts(); // Set VIN Alert Thresholds
break;
case 4:
ack |= menu_3_alert_menu_4_set_adin_alerts(scale); // Set ADIN Alert Thresholds
break;
case 5:
ack |= menu_3_alert_menu_5_enable_disable_alerts(); // Enable/Disable Alert Menu
break;
case 6:
ack |= menu_3_alert_menu_6_read_clear_faults(); // Read Fault Register
break;
case 7:
// Read current min/max alarm and store to EEPROM
store_alert_settings();
break;
default:
if (user_command != 'm')
Serial.println("Incorrect Option");
break;
}
}
while (!((user_command == 'm') || (ack)));
return(ack);
}
//! Set Power Alert Thresholds
int8_t menu_3_alert_menu_1_set_power_alerts(uint8_t VOLTAGE_SEL, //!< Choose whether power multiplier uses ADIN pin or SENSE pin as voltage input (A0 bit in CONTROL Register A)
float scale) //!< Scale value based on resistive divider on the ADIN pin.
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
Serial.print(F("Enter Max Power Threshold:"));
float max_power_threshold;
max_power_threshold = read_float();
Serial.println(max_power_threshold, 4);
int32_t max_power_threshold_code;
if (VOLTAGE_SEL != LTC2946_ADIN)
max_power_threshold_code = (max_power_threshold / LTC2946_Power_lsb) * resistor;
else
max_power_threshold_code = ((1.0/scale)*(max_power_threshold / LTC2946_ADIN_DELTA_SENSE_lsb)) * resistor;
ack |= LTC2946_write_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_POWER_THRESHOLD_MSB2_REG, max_power_threshold_code);
Serial.print(F("Enter Min Power Threshold:"));
float min_power_threshold;
min_power_threshold = read_float();
Serial.println(min_power_threshold, 4);
int32_t min_power_threshold_code;
if (VOLTAGE_SEL != LTC2946_ADIN)
min_power_threshold_code = (min_power_threshold / LTC2946_Power_lsb) * resistor;
else
min_power_threshold_code = ((1.0/scale)*(min_power_threshold / LTC2946_ADIN_DELTA_SENSE_lsb)) * resistor;
ack |= LTC2946_write_24_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_POWER_THRESHOLD_MSB2_REG, min_power_threshold_code);
return(ack);
}
//! Set Current Alert Thresholds
int8_t menu_3_alert_menu_2_set_current_alerts()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
Serial.print(F("Enter Max Current Threshold:"));
float max_current_threshold;
max_current_threshold = read_float();
Serial.println(max_current_threshold, 4);
int32_t max_current_threshold_code;
max_current_threshold_code = (max_current_threshold / LTC2946_DELTA_SENSE_lsb) * resistor;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_DELTA_SENSE_THRESHOLD_MSB_REG, (max_current_threshold_code << 4));
Serial.print(F("Enter Min Current Threshold:"));
float min_current_threshold;
min_current_threshold = read_float();
Serial.println(min_current_threshold, 4);
int32_t min_current_threshold_code;
min_current_threshold_code = (min_current_threshold / LTC2946_DELTA_SENSE_lsb) * resistor;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_DELTA_SENSE_THRESHOLD_MSB_REG, (min_current_threshold_code << 4));
return(ack);
}
//! Set VIN Alert Thresholds
int8_t menu_3_alert_menu_3_set_vin_alerts()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
Serial.print(F("Enter Max VIN Threshold:"));
float max_VIN_threshold;
max_VIN_threshold = read_float();
Serial.println(max_VIN_threshold, 4);
int32_t max_VIN_threshold_code;
max_VIN_threshold_code = max_VIN_threshold / LTC2946_VIN_lsb;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_VIN_THRESHOLD_MSB_REG, (max_VIN_threshold_code << 4));
Serial.print(F("Enter Min VIN Threshold:"));
float min_VIN_threshold;
min_VIN_threshold = read_float();
Serial.println(min_VIN_threshold, 4);
int32_t min_VIN_threshold_code;
min_VIN_threshold_code = min_VIN_threshold / LTC2946_VIN_lsb;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_VIN_THRESHOLD_MSB_REG, (min_VIN_threshold_code << 4));
return(ack);
}
//! Set ADIN Alert Thresholds
int8_t menu_3_alert_menu_4_set_adin_alerts(float scale //!< Scale value based on resistive divider on the ADIN pin.
)
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
Serial.print(F("Enter Max ADIN Threshold:"));
float max_ADIN_threshold;
max_ADIN_threshold = read_float();
Serial.println(max_ADIN_threshold, 4);
int32_t max_ADIN_threshold_code;
max_ADIN_threshold_code = ((1.0/scale)*max_ADIN_threshold) / LTC2946_ADIN_lsb;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MAX_ADIN_THRESHOLD_MSB_REG, (max_ADIN_threshold_code << 4));
Serial.print(F("Enter Min ADIN Threshold:"));
float min_ADIN_threshold;
min_ADIN_threshold = read_float();
Serial.println(min_ADIN_threshold, 4);
int32_t min_ADIN_threshold_code;
min_ADIN_threshold_code = ((1.0/scale)*min_ADIN_threshold) / LTC2946_ADIN_lsb;
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_ADIN_THRESHOLD_MSB_REG, (min_ADIN_threshold_code << 4));
return(ack);
}
//! Enable/Disable Alert Menu
int8_t menu_3_alert_menu_5_enable_disable_alerts()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
uint8_t user_command;
do
{
Serial.print(F("*************************\n\n"));
Serial.print(F("1-Enable/Disable ALERT1 Alerts\n"));
Serial.print(F("2-Enable/Disable ALERT2 Alerts\n"));
user_command = read_int();
if (user_command == 'm')
Serial.println('m');
else
Serial.println(user_command);
switch (user_command)
{
case 1: // Enable/Disable Alerts in ALERT1 Register
ack |= menu_3_alert_menu_5_enable_disable_alerts_menu_1_ALERT1();
break;
case 2: // Enable/Disable Alerts in ALERT2 Register
ack |= menu_3_alert_menu_5_enable_disable_alerts_menu_2_ALERT2();
break;
default:
if (user_command != 'm')
Serial.println("Incorrect Option");
break;
}
}
while (user_command != 'm');
return(ack);
}
//! Enable/Disable Alert in ALERT1 Register
int8_t menu_3_alert_menu_5_enable_disable_alerts_menu_1_ALERT1()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
uint8_t user_command;
do
{
//Print Sub-Menu
Serial.print(F("*************************\n\n"));
Serial.print(F("1-Enable/Disable Power Alerts\n"));
Serial.print(F("2-Enable/Disable Current Alerts\n"));
Serial.print(F("3-Enable/Disable VIN Alerts\n"));
Serial.print(F("4-Enable/Disable ADIN Alerts\n"));
Serial.print(F("m-Alert Menu\n\n"));
Serial.print(F("Enter a command:"));
user_command = read_int();
if (user_command == 'm')
Serial.println('m');
else
Serial.println(user_command);
if (!(user_command == 0))
{
Serial.print(F("1-Enable, 2-Disable\n"));
}
switch (user_command)
{
case 1:
// ENABLE/Disable Power Alerts
Serial.print(F("Max Power :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MAX_POWER_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MAX_POWER_ALERT;
Serial.print(F("Min Power :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MIN_POWER_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MIN_POWER_ALERT;
break;
case 2:
// ENABLE/Disable Current Alerts
Serial.print(F("Max Current :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MAX_I_SENSE_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MAX_I_SENSE_ALERT;
Serial.print(F("Min Current :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MIN_I_SENSE_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MIN_I_SENSE_ALERT;
break;
case 3:
// ENABLE/Disable VIN Alerts
Serial.print(F("Max VIN :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MAX_VIN_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MAX_VIN_ALERT;
Serial.print(F("Min VIN :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MIN_VIN_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MIN_VIN_ALERT;
break;
case 4:
// ENABLE/Disable ADIN Alerts
Serial.print(F("Max ADIN :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MAX_ADIN_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MAX_ADIN_ALERT;
Serial.print(F("Min ADIN :"));
user_command = read_int();
if ((user_command > 2) || (user_command < 0))
user_command = 2;
Serial.println(user_command);
if (user_command == 1)
alert1_code = alert1_code | LTC2946_ENABLE_MIN_ADIN_ALERT;
else
alert1_code = alert1_code & LTC2946_DISABLE_MIN_ADIN_ALERT;
break;
default:
if (user_command != 'm')
Serial.println("Incorrect Option");
break;
}
}
while (user_command != 'm');
ack |= LTC2946_write(LTC2946_I2C_ADDRESS, LTC2946_ALERT1_REG, alert1_code);
return(ack);
}
// Enable/Disable alerts in ALERT2 register
int8_t menu_3_alert_menu_5_enable_disable_alerts_menu_2_ALERT2()
//! @return Returns the state of the acknowledge bit after the I2C address write. 0=acknowledge, 1=no acknowledge.
{
int8_t ack = 0;
uint8_t user_command;
do
{
//Print Menu
Serial.print(F("*************************\n\n"));
Serial.print(F("1-Enable/Disable ADC Conversion Alert\n"));
Serial.print(F("2-Enable/Disable GPIO1 Input Alert\n"));
Serial.print(F("3-Enable/Disable GPIO2 Input Alert\n"));
Serial.print(F("4-Enable/Disable Stuck-Bus Timeout Wake-up Alert\n"));
Serial.print(F("5-Enable/Disable Energy Overflow Alert\n"));
Serial.print(F("6-Enable/Disable Charge Overflow Alert\n"));
Serial.print(F("7-Enable/Disable Time Counter Overflow Alert\n"));
Serial.print(F("m-Alert Menu\n\n"));
Serial.print(F("Enter a command:"));
user_command = read_int();
if (user_command == 'm')