-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaDCLoad.cpp
More file actions
3867 lines (3410 loc) · 119 KB
/
aDCLoad.cpp
File metadata and controls
3867 lines (3410 loc) · 119 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
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
/**
*
* \copyright Copyright (C) 2014-2015 F1RMB, Daniel Caujolle-Bert <f1rmb.daniel@gmail.com>
* \copyright Copyright (C) 2014 Lee Wiggins <lee@wigweb.com.au>
*
* \license
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.<br><br>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.<br><br>
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
*
* \warning
<h1><center> BIG FAT WARNING </center></h1>
<center>Should be compiled with <b> "-Os" </b> flag.</center>
<center>Bootloader <b><span style="text-decoration:underline;color:red;">couldn't</span></b> be flashed, <b>ISP programming ONLY</b></center>
<center> -=- use <i>Code::Blocks</i> or the included <i>Makefile</i> to compile -=- </center>
*/
#warning !!! BIG FAT WARNING !!!
#warning !!! Should be compiled with "-Os" !!!
#include <Arduino.h>
#include <avr/pgmspace.h>
#include <LiquidCrystal.h>
#include <SPI.h>
#include <EEPROM.h>
#include <ClickEncoder.h>
#include <TimerOne.h>
#ifndef RESISTANCE
#include <TimerThree.h>
#endif
#include "aDCLoad.h"
/** \file aDCLoad.cpp
\author F1RMB, Daniel Caujolle-Bert <f1rmb.daniel@gmail.com>
\author Lee Wiggins <lee@wigweb.com.au>
*/
// A bit of user's manual
/**
* \page mainpage Arduino Programmable Constant Current Power Resistance Load
*
* \section origlee Original README.md from Lee Wiggins
*
* This is all of the code, datasheets and design files for [my instructable](http://www.instructables.com/id/Arduino-Programmable-Constant-Current-Power-Resist/ "Arduino Programmable Constant Current Power Resistance Load")
*
* * Arduino - It contains the Arduino code that we will be talking about here, within the dummy load folder. It also contains all of the 3rd party libraries I have used.
* * Datasheets - It contains all of the datasheets for the major components used within the project.
* * DesignSpark - I have used the opensource schematic and PCB design software for this project, its a fantastic free tool that has no limitations and I find it easier to uses than Eagle - http://www.rs-online.com/designspark/electronics/eng/page/designspark-pcb-home-page The rev 1 folder contains all of my initial designs, please don't use this as there are 2 or 3 errors in the footprints plus I have completely revised the layout for rev 2, please only use these files. the gerber files are in there should you wish to have your own board done. See the next step for more information on this.
* * LTSpice - This contains all of the LtSpice files from simulating the operation of the MOSFET.
*
* Please checkout my instructable as it describes all of this code and the operation of the dummy load.
*
*
* \section adddaniel Informations on this code from Daniel Caujolle-Bert
*
* \todo write that!
* \todo notes on Code::Blocks, flashing with avrdude/Makefile, provided HEX file, and so on
*/
#warning WRITE THE STUFF ABOVE
/**
* \page fuses ATmega32U4 fuses settings
*
* Unlike the Arduino™ Leornardo board, the ATmega32U4 MCU used in this DC Load needs some special fuses settings.
*
* The following command line defines them to the correct values:
*
* \code avrdude -F -p atmega32u4 -C /etc/avrdude.conf -v -e -V -c usbasp -P usb -U lfuse:w:0xFF:m -U hfuse:w:0xD1:m -U efuse:w:0xCB:m -F \endcode
*
* You can also invoke the provided <i>Makefile</i>, as:
*
* \code make fuses \endcode
*
*/
/** \page mods26 Hardware modifications for version 2.6
*
*
* Since the <b>Pulse Transient Time</b> and the <b>Input Relay</b> features introduction, few small hardware modifications are required:
* + The LCD cabling (<b>!!! on the LCD's PCB only !!!</b>) should be modified as below:
* * resolder the cable from pins <b>d0</b>, <b>d1</b>, <b>d2</b> and <b>d3</b> to <b>d4</b>, <b>d5</b>, <b>d6</b> and <b>d7</b>, accordingly,
* + Build the small input relay PCB (files available with source code),
* + Get <b>GND</b> and <b>+5V</b> from the LCD connector (on the DC Load board, first and second pins),
* and connect them to the relay's PCB,
* + Get the <b>+12V</b> DC from your power supply,
* + connect old <b>d4</b> and <b>d5</b> (from the DC Load board LCD connector) to the relay PCB as <b>Relay</b> and <b>Button</b>, accordingly.
*
*/
/**
* \page gui User interface overview
*
*
* - The DC load control is done using a simple rotary encoder, which integrates a push button (see \ref enhance26 below for 2.6 specific enhancement).
*
* <br>
*
* - When the DC Load displays the input value (left aligned values), a single encoder detents turns the DC Load's display in settings mode (right aligned values), without any setting value changes.
*
* <br>
*
* - There are two display modes, <b>input values</b> and <b>settings values</b>.
* <br>
* When you rotate the encoder, the DC Load switches automatically to <b>settings mode</b>.
* <br>
* You just need to rotate the encoder to define the desired value, accordingly to the focus : <b>Current</b>, <b>Power</b> or <b>Pulse Transient Time</b>.
*
* <br>
*
* - In both display modes, a double click changes the focus (delimited by '<b>[</b>' and '<b>]</b>' symbols) to the next value parameter,
* Current (<b>I</b>), Power (<b>P</b>) or Pulse Transient Time (<b>t</b>).
*
* <br>
*
* - A simple click changes the tuning step.
* <br>
* Next to the '<b>]</b>' delimiter symbol, an icon displays the tuning step multiplier, as following:
* <center>
* Multiplier | Glyph
* -----------|-------
* x1 | \image html x1.png "" \image latex x1.png ""
* x10 | \image html x10.png "" \image latex x10.png ""
* x100 | \image html x100.png "" \image latex x100.png ""
* x1000 | \image html x1k.png "" \image latex x1k.png ""
* </center>
*
* <br>
*
* - By default, the DC Load displays <b>Input Voltage</b>, <b>Current load</b>, <b>Power dissipation</b>, <b>Pulse Transient Time</b> values and <b>heatsink temperature</b>.
* \note The Voltage is measured on the input connectors of the DC Load and may differs from the measured value out of the power supply source.
*
* <br>
*
* - The <b>Pulse Transient Time</b> permits you to define a pulse duration, from <b>0mA</b> to the defined loading current value. After this peak, the DC Load will switch to <b>0mA</b> loading current, for same duration.
* This will cycle endlessly until you set the <b>Pulse Transient Time</b> to <b>0s</b>.
*
* \image html aDCLoad-Pulse.png "" \image latex aDCLoad-Pulse.png ""
*
* \note The maximum duration is <b>8.192s</b>
*
* <br>
*
*
* - After 3 seconds in settings mode, without any encoder action, the DC Load returns to the <b>input values</b> display mode.
*
* <br>
*
* - According to the actual status of the DC Load, some icons may be shown:
* <center>
* Feature | Glyph
* --------|------
* Logging is running | \image html Logging.png "" \image latex Logging.png ""
* Encoder is locked | \image html Lock.png "" \image latex Lock.png ""
* USB remote controlled | \image html USB.png "" \image latex USB.png ""
* Over-Current alam | \image html OC.png "" \image latex OC.png ""
* Over-Voltage alarm | \image html OV.png "" \image latex OV.png ""
* Over-Temperature alarm (<small>in place of "°C"</small>)| \image html OT.png "" \image latex OT.png ""
* </center>
*
* <br>
*
* - To access to the options configuration, you need to press the button for more than 3 seconds.
* In this <i>window</i>, you can enable or disable the <b>backlight's auto-dimmer</b> and the <b>rotary encoder's
* auto-lock</b> features.
* <br>
* A double click changes the option focus, a simple click changes the option enability and a long press exits the options <i>window</i>.
*
* <br>
*
* - When <b>auto-lock</b> is turned on and triggered, a double click unlocks the rotary encoder (the key icon disappear).
*
* <br>
*
* - When <b>auto-dimmer</b> is turned on and triggered, any rotary encoder action will turn the backlight on, without any change to
* the defined settings.
*
* <br>
*
* - There are 3 differents kind of alarms:
* -# <b>OC</b> for over-current:<br>
* When <b>Over-Current</b> is triggered, Current setting is defined to 0mA, <b>OC</b> icon is displayed.<br>
* Over-Current alarm will be cleared once the encoder is used to set a new Current value.
* <br><br>
* -# <b>OV</b> for over-voltage:<br>
* When <b>Over-Voltage</b> is trig<gered, Current setting is defined to 0mA, <b>OV</b> icon is displayed.<br>
* Encoder will have no action until the input voltage drops below to its maximum value (24V).
* <br><br>
* -# <b>OT</b> the over-temperature:<br>
* When <b>Over-Temperature</b> is triggered, Current setting is defined to 0mA, <b>OT</b> icon is displayed.<br>
* Encoder will have no action until the internal temperature drops below to its maximum value (80°C).
*
* <br>
*
* - The DC Load can be remotely controlled, see \ref remote
*
* <br>
*
* \section enhance26 Extra control since v2.6
* - Since version 2.6, the DC Load uses a mechanical input relay. It's driven by a push button or a remote command.
* This permits to isolate the DC Load and the DUT.
* On any alarm, the input relay will disengage the DUT, to keep both devices in a safe state.
*
* See \ref mods26
*
*/
/**
* \page remote Remote Commands
* See also \ref logging.
*
* \note Serial port configuration: <b>57600</b>,<b>8</b>,<b>N</b>,<b>1</b>
* \warning Commands and arguments are <b>case sensitive</b>, <b>ALL</b> in <b>UPCASE</b>
*
* \section idn Get Identification
* * <b>:*IDN?:</b>
* - Returns firmware informations
*
* See \ref retval.
*
* \section curget Current setting getter
* * <b>:ISET?:</b>
* - Returns current setting (in <b>mA</b>)
*
* See \ref retval.
*
* \section curset Current setting setter
* * <b>:ISET:<i>value</i></b>
* - Set current <b><i>value</i></b> (in <b>mA</b>)
*
* See \ref retval.
*
* \section calinfo Calibration values getter
* * <b>:CAL?:</b>
* - Returns the stored calibration values for <b>V</b>, <b>C</b>, <b>D</b> or <b>VD</b>.<br>
*
* See \ref retval.
* <br>See \ref cal.
*
* \section cal Calibration
* * <b>:CAL:<i>toggle</i></b>
* - Turns <b><i>ON</i></b> or <b><i>OFF</i></b> the logging feature.<br>
* * <b>:CAL:<i>section</i>:<i>slope</i>,<i>offset</i></b>
* - <b><i>section</i></b> could be <b>V</b>, <b>C</b>, <b>D</b> or <b>VD</b>, standing for <b>V</b><i>oltage</i>, <b>C</b><i>urrent</i>, <b>D</b><i>AC</i> and <b>V</b><i>oltage</i> <b>D</b><i>rop</i>.
- <b><i>slope</i></b> and <b><i>offset</i></b> are floating point values, with US period decimal separator ('.'). These values could be calculated using the <i>LibreOffice</i>'s spreadsheet file <i>aDCLoadCalibration.ods</i>.
* * <b>:CAL:SAVE</b>
* - Backup calibation datas into EEPROM.
*
* See \ref retval.
* <br> See \ref calibration.
*
* \section dac DAC value setter (calibration purpose)
* * <b>:DAC:<i>value</i></b>
* - Set DAC value (from <b>0</b> to <b>4095</b>).
*
* \note This command has no effect outside calibration mode
*
* See \ref cal.
* <br> See \ref calibration.
*
* \section curread Current readed getter
* * <b>:I?:</b>
* - Returns current readed from the load (in <b>mA</b>)
*
* See \ref retval.
*
* \section volread Voltage readed getter
* * <b>:U?:</b>
* - Returns voltage readed from the load (in <b>mV</b>)
*
* See \ref retval.
*
* \section logsingle Logging enability
* * <b>:LOG?:</b>
* - Printout if logging is <b><i>ON</i></b> or <b><i>OFF</i></b>.
*
* See \ref retval.
* <br> See \ref logging.
*
* \section logrun Logging enability
* * <b>:LOG:<i>toggle</i></b>
* - Turns <b><i>ON</i></b> or <b><i>OFF</i></b> the logging feature.<br>
*
* \note If <b><i>toggle</i></b> value is not specified, a single logging line is returned.
*
* See \ref retval.
* <br> See \ref logging.
*
* \section pulseinfo Pulse value getter
* * <b>:PUL?:</b>
* - Returns pulse time value (in <b>ms</b>)
*
* See \ref retval.
*
* \section pulse Pulse value setter
*
* * <b>:PUL:<i>value</i></b>
* - Set pulse time <b><i>value</i></b> (in <b>ms</b>)
*
* See \ref retval.
*
* \section relayinfo Input Relay status getter
*
* * <b>:INP?:</b>
* - Printout if the input relay is <b><i>ON</i></b> or <b><i>OFF</i></b>.
*
* See \ref retval.
* <br>See \ref mods26.
*
* \section relay Input Relay status setter
*
* * <b>:INP:<i>toggle</i></b>
* - Turns <b><i>ON</i></b> or <b><i>OFF</i></b> the input relay.<br>
*
* See \ref retval.
* <br>See \ref mods26.
*
* \section retval Return value
* <b>:<i>value</i>:<i>status</i>:</b>
* - Where:
* * <b><i>value</i></b> if any expected. <b>INVALID</b> on unknown command.
* * <b><i>status</i></b> could be <b>OK</b> on success or <b>ERR</b> on failure.
*
*/
/**
* \page logging Logging data format
* See also \ref remote
* \note fields are comma separated
*
* \section logform CSV logging format
* <b><i>timestamp</i></b>,<b><i>voltage</i></b>,<b><i>current sets</i></b>,<b><i>current read</i></b>,<b><i>temperature</i></b><b><i>\\r\\n</i></b>
* - Where:
* + <b><i>timestamp</i></b> in <b>hundred of milliseconds</b>,
* + <b><i>voltage</i></b> in <b>mV</b>,
* + <b><i>current sets</i></b> in <b>mA</b>,
* + <b><i>current read</i></b> in <b>mA</b>,
* + <b><i>temperature</i></b> in <b>Celcius degrees</b>.
*/
/**
* \page calibration Calibration Process
*
*
* + __Prerequisites__:<br><br>
*
* - __Hardware__:
* - Amp-meter,
* - Volt-meter,
* - Power supply (<b>0..24V</b>, <b>8A</b>)
* <br><br>
* - __Software__:
* * A serial terminal emulator (e.g. “<i>HyperTerminal</i>” or “<i>Tera Term</i>” on Windows, “<i>minicom</i>” or “<i>cutecom</i>” on Linux).
* * The calibration spreadsheet file <b>aDCLoadCalibration.ods</b>
* * A software able to open the calibration spreadsheet, like “<i>LibreOffice</i>“, “<i>OpenOffice</i>“ and so on.
*
* The serial communication settings are: <b>57600</b>, <b>8</b>, <b>N</b>, <b>1</b>
*
* <br>
* + __Process Description__:<br><br>
*
* - __Step 1: Maximum Current__
*
* Select “<i>Step 1</i>” tab in the calibration spreadsheet file.
*
* Connect the amp-meter and the power supply to the DC load, for current measurements.
* Open your serial terminal emulator, connect the DC load, then type:
* \code :CAL:ON \endcode
* \code :DAC:4095 \endcode
* Write down the <b>mA</b> value readed on the amp-meter to the “<b><i>mA<sub>max</sub></i></b>” column.
* Now, type:
* \code :DAC:0 \endcode
* Edit the aDCLoad.h source file, browse down the file, looking for the following line:
* \code static const float CURRENT_MAXIMUM = 7.845; ///< Maximum value of load current (A) \endcode
* If necessary, change the 7.845 value to the one you've got on your amp-meter (don't forget to convert it from <b>mA</b>
* to <b>A</b>), then reflash the board with new code (using <i>Code::Blocks IDE</i> or the provided <i>Makefile</i>,
* running “<i>make burn</i>” command).
*
* Remember, if you have to reflash the board, that could be only done using ICSP programming. There is no bootloader flashed on the MCU, due to flash space restriction.
*
* Calibration step 1 is now done.<br><br>
*
* - __Step 2: Voltage__
*
* Select “<i>Step 2</i>” tab in the calibration spreadsheet file.
*
* Connect your power supply to the DC load, sets to <b>0V</b>. The DC Load should the sets to <b>0mA</b>.
* If you've reflashed the firmware or didn't go through the step 1, then, in the serial terminal emulator, type:
* \code :CAL:ON \endcode
* Set your power supply voltage output for each value in “<b><i>V<sub>set</sub></i></b>” column, and write down the readed
* value in “<b><i>V<sub>read</sub></i></b>” column.
*
* Once you went through the whole array, the calibration string should be entered into the serial terminal emulator, like:
* \code :CAL:V:x.xxx,y.yyy \endcode
* Please note that the decimal separator <b>HAS TO BE</b> a period ('.'), as in US format.
*
* Calibration step 2 is now done.<br><br>
*
* - __Step 3: Current__
*
* Select “<i>Step 3</i>” tab in the calibration spreadsheet file.
*
* Connect the amp-meter and the power supply to the DC load, for current measurements.
* Sets the output voltage to <b>5V</b>.
*
* Using the DAC command, try to adjust its value to match each value in the “<b><i>A Amp-Meter</i></b>” column,
* and write down the readed value, on the LCD or serial terminal emulator output, into the “<b><i>A LCD/Term.</i></b>” column.
*
* You can change the values in the “<b><i>A Amp-Meter</i></b>” column to strictly match the ones you're reading on the amp-meter.
*
* The DAC command syntax is :
* \code :DAC:value \endcode where value is an integer from 0 to 4095.<br><br>
* Once you went through the whole array, set DAC value to <b>0</b>:
* \code :DAC:0 \endcode
* The calibration string should be entered into the serial terminal emulator, like:
* \code :CAL:C:x.xxx,y.yyy \endcode
* Please note that the decimal separator <b>HAS TO BE</b> a period ('.'), as in US format.
*
* Calibration step 3 is now done.<br><br>
*
* - __Step 4: DAC__
*
* Select “<i>Step 4</i>” tab in the calibration spreadsheet file.
*
* Connect the amp-meter and the power supply to the DC load, for current measurements.
* Sets the output voltage to <b>5V</b>.
*
* Set the DAC value for each value in “<b><i>Steps</i></b>” column, and write down the readed value on the
* amp-meter into the “<b><i>mA<sub>read</sub></i></b>” column.
*
* The DAC command syntax is :
* \code :DAC:value \endcode where value is an integer from 0 to 4095.<br><br>
* Once you went through the whole array, set DAC value to <b>0</b>:
* \code :DAC:0 \endcode
* The calibration string should be entered into the serial terminal emulator, like:
* \code :CAL:D:x.xxx,y.yyy \endcode
* Please note that the decimal separator <b>HAS TO BE</b> a period ('.'), as in US format.
*
* Calibration step 4 is now done.<br><br>
*
* - __Step 5: Voltage Drop__
*
* Select “<i>Step 5</i>” tab in the calibration spreadsheet file.
*
* Connect the amp-meter, the volt-meter and the power supply to the DC load, for current <b>AND</b> voltage measurements.
* Sets the output voltage to <b>5V</b> (the last entry in the array should be set around <b>12V</b>).
*
* Using the DAC command, try to adjust the its value to match each value in the “<b><i>mA</i></b>” column on the amp-meter,
* and write down the voltage readed value on the volt-meter into the “<b><i>mV meter</i></b>” column, and the readed value
* on the LCD and/or serial terminal emulator to the “<b><i>mV LCD/Term.</i></b>” column.
*
* The DAC command syntax is :
* \code :DAC:value \endcode where value is an integer from 0 to 4095.<br><br>
* You can change the values in the “<b><i>mA</i></b>” column to strictly match the ones you're reading on the amp-meter.
*
* For the last row on the array, set the output voltage to around <b>12V</b>.
*
* Once you went through the whole array, set DAC value to <b>0</b>:
* \code :DAC:0 \endcode
* The calibration string should be entered into the serial terminal emulator, like:
* \code :CAL:VD:x.xxx,y.yyy \endcode
* Please note that the decimal separator <b>HAS TO BE</b> a period ('.'), as in US format.
*
* Calibration step 5 is now done.<br><br>
*
* - __Last Step: Backup__
*
* Once the full calibration is done, you <b>HAVE</b> to save the values into the EEPROM, using the following command:
* \code :CAL:SAVE \endcode <br><br>
* ### Now, the calibration is done. You can use your DC load.
*
*
*/
aDCEngine *pThis = NULL; // Hackish, used by Timer3 ISR.
/**
*** Implement our serial print function to save ~300ko
**/
// Prototype
#if 1
void serialPrint(unsigned long, int = DEC);
/** \brief Serial printing
*
* \param c char
* \return void
*
*/
void serialWrite(char c)
{
Serial.print(c);
}
/** \brief Serial printing
*
* \param str char[]
* \return void
*
*/
void serialPrint(const char str[])
{
#if 1
Serial.print(str);
#else
char *p = (char *)str;
while(*p != '\0')
{
serialWrite(*p);
p++;
}
#endif
}
#if 0
/** \brief Serial printing
*
* \param ifsh const __FlashStringHelper*
* \return void
*
*/
void serialPrint(const __FlashStringHelper *ifsh)
{
Serial.print(ifsh);
#if 0
const char *__attribute__((progmem)) p = (const char * ) ifsh;
while (1)
{
unsigned char c = pgm_read_byte(p++);
if (c == 0)
break;
serialWrite(c);
}
#endif
}
#endif
/** \brief Serial printing
*
* \param c char
* \return void
*
*/
void serialPrint(char c)
{
serialWrite(c);
}
/** \brief Serial printing
*
* \param n int
* \param base int16_t
* \return void
*
*/
void serialPrint(int n, int16_t base = DEC)
{
serialPrint((unsigned long) n, base);
}
/** \brief Flush serial buffer (TX)
*
* \return void
*
*/
void serialFlush()
{
Serial.flush();
}
/** \brief Serial printing
*
* \return void
*
*/
void serialPrintln()
{
serialPrint("\r\n");
serialFlush();
}
/** \brief Serial printing
*
* \param n unsigned long
* \param base int
* \return void
*
*/
void serialPrint(unsigned long n, int base)
{
Serial.print(n, base);
}
/** \brief Serial printing
*
* \param n double
* \param digits int
* \return void
*
*/
void serialPrint(double n, int digits)
{
Serial.print(n, digits);
}
/** \brief Serial printing
*
* \param n double
* \param digits int
* \return void
*
*/
void serialPrintln(double n, int digits)
{
Serial.println(n, digits);
#if 0
serialPrint(n, digits);
serialPrintln();
#endif
}
#if 0
/** \brief Serial printing
*
* \param ifsh const __FlashStringHelper*
* \return void
*
*/
void serialPrintln(const __FlashStringHelper *ifsh)
{
serialPrint(ifsh);
serialPrintln();
}
#endif
/** \brief Serial printing
*
* \param c char
* \return void
*
*/
void serialPrintln(char c)
{
serialWrite(c);
serialPrintln();
}
/** \brief Get the number of bytes (characters) available for reading from the serial port
*
* \return int16_t
*
*/
int16_t serialAvailable()
{
return Serial.available();
}
/** \brief Reads incoming serial data
*
* \return uint8_t
*
*/
uint8_t serialRead()
{
return static_cast<uint8_t>(Serial.read());
}
#endif
#if 0
/**
*** Numerical utils
**/
int8_t getNumericalLength(int16_t n)
{
char buf[16];
return (static_cast<int8_t>(snprintf(buf, sizeof(buf) - 1, "%d", n)));
}
int8_t getNumericalLength(uint16_t n)
{
char buf[16];
return (static_cast<int8_t>(snprintf(buf, sizeof(buf) - 1, "%u", n)));
}
#endif
/**
*** Our float to string format function
**/
/** \brief Get float string length, according to decimal man length
*
* \param n float : <b> Float number to analyse </b>
* \param len uint8_t : <b> decimal max length (3 as default) </b>
* \return int8_t : <b> numeric value length </b>
*
*/
int8_t getNumericalLength(float n, uint8_t len = 3)
{
char buf[32];
if (len > 11)
len = 11;
return strlen(dtostrf(n, 1, len, buf));
}
/**
*** Float rounding function
**/
/** \brief Float number rounding, extensively used
*
* We just get rid of +4 decimal values
*
* \param f float : <b> Float to rounding </b>
* \return float : <b> rounded value </b>
*
*/
float floatRounding(float f)
{
return ((f / 1000.000) * 1000.000);
}
/** \brief Constructor
*
* \param parent aDCEngine* : <b> Pointer to aDCEngine parent </b>
*/
aDCSettings::aDCSettings(aDCEngine *parent) :
m_Parent(parent),
#ifdef SIMU
m_readVoltage(24.000),
#else
m_readVoltage(0),
#endif
m_setsCurrent(0), m_readCurrent(0),
#ifdef RESISTANCE
m_setsResistance(0), m_readResistance(0),
#else
m_setsPulse(0),
#endif
m_setsPower(0), m_readPower(0),
#ifdef SIMU
m_readTemperature(20),
#else
m_readTemperature(0),
#endif
m_fanSpeed(0xFF),
m_currentDAC(0xFF),
m_operationMode(OPERATION_MODE_READ),
m_mode(SELECTION_MODE_CURRENT),
m_encoderPos(0),
m_dispMode(DISPLAY_MODE_VALUES),
m_lockTick(0), m_operationTick(0),
m_features(0x0),
m_datas(0xFFFF)
#ifndef RESISTANCE
, m_pulseEnabled(false),
m_pulseHigh(true)
#endif
{
// Calibration zeroing
for (uint8_t i = 0; i < CALIBRATION_MAX; i++)
{
m_calibrationValues[i].slope = 1.0;
m_calibrationValues[i].offset = 0.0;
}
_eepromRestore();
syncData(DATA_IN_CALIBRATION); // Reset Calibration Bit (all bits are set to 1 on startup)
}
/** \brief Destructor
*/
aDCSettings::~aDCSettings()
{
}
// Voltage
/** \brief Voltage setter
*
* \param v float : <b> voltage </b>
* \return aDCSettings::SettingError_t
*
*/
aDCSettings::SettingError_t aDCSettings::setVoltage(float v)
{
SettingError_t err = _setValue(aDCSettings::OPERATION_MODE_READ, DATA_VOLTAGE, v, m_readVoltage, m_readVoltage, VOLTAGE_MAXIMUM);
// Over-Voltage protection triggered
if (err == SETTING_ERROR_OVERSIZED)
enableAlarm(FEATURE_OVP);
return err;
}
/** \brief Voltage getter
*
* \return float : <b> voltage </b>
*
*/
float aDCSettings::getVoltage()
{
return m_readVoltage;
}
// Current
/** \brief Current setter
*
* \param v float : <b> current </b>
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return aDCSettings::SettingError_t
*
*/
aDCSettings::SettingError_t aDCSettings::setCurrent(float v, OperationMode_t mode)
{
return _setValue(mode, (mode == OPERATION_MODE_SET) ? DATA_CURRENT_SETS : DATA_CURRENT_READ, v, m_setsCurrent, m_readCurrent, CURRENT_MAXIMUM);
}
/** \brief Current getter
*
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return float : <b> current </b>
*
*/
float aDCSettings::getCurrent(OperationMode_t mode)
{
return (mode == OPERATION_MODE_SET) ? m_setsCurrent : m_readCurrent;
}
// Resistance
#ifdef RESISTANCE
/** \brief Resistance setter
*
* \param v float : <b> resistance </b>
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return aDCSettings::SettingError_t
*
*/
aDCSettings::SettingError_t aDCSettings::setResistance(float v, OperationMode_t mode)
{
return _setValue(mode, (mode == OPERATION_MODE_SET) ? DATA_RESISTANCE_SETS : DATA_RESISTANCE_READ, v, m_setsResistance, m_readResistance, RESISTANCE_MAXIMUM);
}
/** \brief Resistance getter
*
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return float : <b> resistance </b>
*
*/
float aDCSettings::getResistance(OperationMode_t mode)
{
return (mode == OPERATION_MODE_SET) ? m_setsResistance : m_readResistance;
}
#else
/** \brief Pulse setter
*
* \param v float : <b> pulse (ms) </b>
* \return aDCSettings::SettingError_t
*
*/
aDCSettings::SettingError_t aDCSettings::setPulse(float v)
{
SettingError_t err = _setValue(aDCSettings::OPERATION_MODE_SET, DATA_PULSE_SETS, v, m_setsPulse, m_setsPulse, PULSE_MAXIMUM);
if (err == SETTING_ERROR_VALID)
{
// Set Timer3 period
// Enable Pulse ?
if (m_setsPulse == 0.0)
{
if (m_pulseEnabled)
{
m_pulseEnabled = false;
Timer3.setPeriod((PULSE_MAXIMUM * 1000.000) * 1000); ///< Sleep well honey
m_pulseHigh = true;
}
}
else
{
if (!m_pulseEnabled)
m_pulseEnabled = true;;
Timer3.setPeriod(static_cast<unsigned long>((m_setsPulse * 1000.000) * 1000)); ///< Wake up son!
}
}
return err;
}
/** \brief Pulse getter
*
* \return float : <b> pulse (ms) </b>
*
*/
float aDCSettings::getPulse()
{
return m_setsPulse;
}
/** \brief Returns pulse enability
*
* \return bool
*
*/
bool aDCSettings::isPulseEnabled()
{
return m_pulseEnabled;
}
/** \brief Set pulse enability.
*
* \param enable bool : <b> enability </b>
* \return void
*
*/
void aDCSettings::enablePulse(bool enable)
{
m_pulseEnabled = enable;
}
/** \brief Returns if pulse is high (load should operate)
*
* \return bool
*
*/
bool aDCSettings::isPulseHigh()
{
return m_pulseHigh;
}
/** \brief Set pulse high/low state value
*
* \param high bool : <b> pulse state </b>
* \return void
*
*/
void aDCSettings::setPulseHigh(bool high)
{
m_pulseHigh = high;
}
#endif
// Power
/** \brief Power setter
*
* \param v float : <b> power </b>
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return aDCSettings::SettingError_t
*
*/
aDCSettings::SettingError_t aDCSettings::setPower(float v, OperationMode_t mode)
{
return _setValue(mode, (mode == OPERATION_MODE_SET) ? DATA_POWER_SETS : DATA_POWER_READ, v, m_setsPower, m_readPower, POWER_MAXIMUM);
}
/** \brief Power getter
*
* \param mode OperationMode_t : <b> READ/SET mode storage access </b>
* \return float : <b> power </b>
*
*/
float aDCSettings::getPower(OperationMode_t mode)
{
return (mode == OPERATION_MODE_SET) ? m_setsPower : m_readPower;
}
/** \brief Update values setting (Current, Resistance, Power) according to selection mode. Sanity checking is also performed.
*
* \param v float : <b> updated value </b>
* \param mode SelectionMode_t : <b> selection mode (CURRENT, RESISTANCE, POWER) </b>
* \return void
*
*/
void aDCSettings::updateValuesFromMode(float v, SelectionMode_t mode)
{
float voltage = getVoltage();
float currentSets = getCurrent(aDCSettings::OPERATION_MODE_SET);
switch (mode)
{
case SELECTION_MODE_CURRENT:
switch (setCurrent(v, aDCSettings::OPERATION_MODE_SET))