forked from Ho-Ro/ComponentTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools_misc.c
2281 lines (1842 loc) · 66.1 KB
/
tools_misc.c
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
/* ************************************************************************
*
* misc tools (hardware and software options)
*
* (c) 2012-2021 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define TOOLS_MISC_C
/*
* include header files
*/
/* local includes */
#include "config.h" /* global configuration */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
#include "colors.h" /* color definitions */
/* ************************************************************************
* support functions
* ************************************************************************ */
#ifdef FUNC_PROBE_PINOUT
/*
* display probe pinout
*
* required:
* - Mode
* PROBES_PWM PWM signal
* PROBES_ESR ESR measurement
* PROBES_RCL monitoring RCL
* PROBES_RINGS ring tester
*/
void ProbePinout(uint8_t Mode)
{
uint8_t ID_1 = 0; /* character for probe #1 */
uint8_t ID_2 = 0; /* character for probe #2 */
uint8_t ID_3 = 0; /* character for probe #3 */
LCD_ClearLine2(); /* info goes to line #2 */
/*
* set probe pinout based on mode
*/
switch (Mode)
{
#if defined (SW_PWM_SIMPLE) || defined (SW_PWM_PLUS) || defined (SW_SERVO) || defined (SW_SQUAREWAVE)
case PROBES_PWM:
/* probe #1: Gnd / probe #2: signal / probe #3: Gnd */
ID_1 = '-';
ID_2 = 's';
ID_3 = '-';
break;
#endif
#if defined (SW_ESR_TOOL) || defined (SW_CONTINUITY_CHECK)
case PROBES_ESR:
/* probe #1: + / probe #3: - */
ID_1 = '+';
ID_2 = 0;
ID_3 = '-';
break;
#endif
#if defined (SW_MONITOR_R) || defined (SW_MONITOR_C) || defined (SW_MONITOR_L) || defined(SW_MONITOR_RCL) || defined(SW_MONITOR_RL)
case PROBES_RCL:
/* probe #1: * / probe #3: * */
ID_1 = '*';
ID_2 = 0;
ID_3 = '*';
break;
#endif
#if defined (HW_RING_TESTER) && defined (RING_TESTER_PROBES)
case PROBES_RINGS:
/* probe #1: Vcc / probe #2: pulse out / probe #3: Gnd */
ID_1 = '+';
ID_2 = 'p';
ID_3 = '-';
break;
#endif
}
Show_SimplePinout(ID_1, ID_2, ID_3); /* display pinout */
/* wait for any key press or 5s */
TestKey(5000, CHECK_BAT);
LCD_ClearLine2(); /* clear line #2 */
}
#endif
/* ************************************************************************
* Zener tool / external voltage
* ************************************************************************ */
#if defined (HW_ZENER) && ! defined (ZENER_UNSWITCHED)
/*
* Zener tool (standard mode)
* - hardware option for voltage measurement of Zener diode
* - uses dedicated analog input (TP_ZENER) with voltage divider
* (default 10:1)
* - test push button enables boost converter
*/
void Zener_Tool(void)
{
uint8_t Run = 1; /* control flag */
uint8_t Counter; /* length of key press */
uint8_t Counter2 = 0; /* time between two key presses */
uint16_t U1; /* current voltage */
uint16_t Min = UINT16_MAX; /* minimal voltage */
#ifdef ZENER_DIVIDER_CUSTOM
uint32_t Value; /* value */
#endif
/* show info */
LCD_Clear();
#ifdef UI_COLORED_TITLES
/* display: Zener */
Display_ColoredEEString(Zener_str, COLOR_TITLE);
#else
Display_EEString(Zener_str); /* display: Zener */
#endif
Display_NextLine();
Display_Minus(); /* display "no value" */
/*
* processing loop
*/
while (Run > 0)
{
/*
* manage timing
*/
Counter = 0; /* reset key press time */
MilliSleep(30); /* delay by 30ms */
Counter2++; /* increase delay time counter */
if (Counter2 > 200) /* prevent overflow & timer (about 6s) */
{
Counter2 = 10; /* reset counter (above delay for quick key presses) */
#ifndef BAT_NONE
CheckBattery(); /* and check battery */
#endif
}
/*
* key press triggers measurement
* - also enables boost converter via hardware
* - two short key presses exit tool
*/
while (!(BUTTON_PIN & (1 << TEST_BUTTON))) /* as long as key is pressed */
{
/* get voltage (10:1 voltage divider) */
U1 = ReadU(TP_ZENER); /* read voltage (in mV) */
#ifndef ZENER_DIVIDER_CUSTOM
/* ADC pin is connected to a 10:1 voltage divider */
/* so U1's scale is 10mV */
#endif
#ifdef ZENER_DIVIDER_CUSTOM
/*
* ADC pin is connected to a voltage divider (top: R1 / bottom: R2).
* - U2 = (Uin / (R1 + R2)) * R2
* - Uin = (U2 * (R1 + R2)) / R2
*/
Value = (((uint32_t)(ZENER_R1 + ZENER_R2) * 1000) / ZENER_R2); /* factor (0.001) */
Value *= U1; /* voltage (0.001 mV) */
Value /= 1000; /* scale to mV */
U1 = (uint16_t)Value; /* keep 2 bytes */
#endif
/* display voltage */
if (Counter % 8 == 0) /* every 8 loop runs (240ms) */
{
LCD_ClearLine2(); /* clear line #2 */
#ifndef ZENER_DIVIDER_CUSTOM
Display_Value(U1, -2, 'V'); /* display current voltage */
#else
Display_Value(U1, -3, 'V'); /* display current voltage */
#endif
}
/* data hold */
if (Counter == 0) /* first loop run */
{
Min = UINT16_MAX; /* reset minimum to default */
}
else if (Counter >= 10) /* ensure stable voltage */
{
if (U1 < Min) Min = U1; /* update minimum */
}
/* timing */
MilliSleep(30); /* delay next run / also debounce by 30ms */
Counter++; /* increase key press time counter */
if (Counter > 100) /* prevent overflow & timer (about 3s) */
{
Counter = 12; /* reset counter (above time for short key press) */
#ifndef BAT_NONE
CheckBattery(); /* and check battery */
#endif
}
}
/*
* user interface logic
*/
if (Counter > 0) /* key was pressed */
{
/* detect two quick key presses */
if (Run == 2) /* flag for short key press set */
{
if (Counter2 <= 8) /* short delay between key presses <= 250ms */
{
Run = 0; /* end loop */
}
else /* long delay between key presses */
{
Run = 1; /* reset flag */
}
}
else /* flag not set */
{
if (Counter <= 10) /* short key press <= 300ms */
{
Run = 2; /* set flag */
}
}
/* display hold value */
LCD_ClearLine2();
if (Min != UINT16_MAX) /* got updated value */
{
#ifndef ZENER_DIVIDER_CUSTOM
Display_Value(Min, -2, 'V'); /* display minimal voltage */
#else
Display_Value(Min, -3, 'V'); /* display minimal voltage */
#endif
Display_Space();
Display_EEString(Min_str); /* display: Min */
}
else /* unchanged default */
{
Display_Minus(); /* display "no value" */
}
Counter2 = 0; /* reset delay time */
}
}
}
#endif
#if defined (HW_ZENER) && defined (ZENER_UNSWITCHED)
/*
* Zener tool (alternative mode)
* - hardware option for voltage measurement of Zener diode
* or external voltage
* - uses dedicated analog input (TP_ZENER) with voltage divider
* (default 10:1)
* - boost converter runs all the time or circuit without boost converter
*/
void Zener_Tool(void)
{
uint8_t Run = 1; /* control flag */
uint8_t Test; /* user feedback */
uint16_t U1; /* voltage */
#ifdef ZENER_DIVIDER_CUSTOM
uint32_t Value; /* value */
#endif
/* show info */
LCD_Clear();
#ifdef UI_COLORED_TITLES
/* display: Zener */
Display_ColoredEEString(Zener_str, COLOR_TITLE);
#else
Display_EEString(Zener_str); /* display: Zener */
#endif
/*
* processing loop
*/
while (Run)
{
/* get voltage */
U1 = ReadU(TP_ZENER); /* read voltage (in mV) */
#ifndef ZENER_DIVIDER_CUSTOM
/* ADC pin is connected to a 10:1 voltage divider */
/* so U1's scale is 10mV */
#endif
#ifdef ZENER_DIVIDER_CUSTOM
/*
* ADC pin is connected to a voltage divider (top: R1 / bottom: R2).
* - U2 = (Uin / (R1 + R2)) * R2
* - Uin = (U2 * (R1 + R2)) / R2
*/
Value = (((uint32_t)(ZENER_R1 + ZENER_R2) * 1000) / ZENER_R2); /* factor (0.001) */
Value *= U1; /* voltage (0.001 mV) */
Value /= 1000; /* scale to mV */
U1 = (uint16_t)Value; /* keep 2 bytes */
#endif
/* display voltage */
LCD_ClearLine2(); /* clear line #2 */
#ifndef ZENER_DIVIDER_CUSTOM
Display_Value(U1, -2, 'V'); /* display current voltage */
#else
Display_Value(U1, -3, 'V'); /* display current voltage */
#endif
/* user feedback (1s delay) */
Test = TestKey(1000, CHECK_KEY_TWICE | CHECK_BAT | CURSOR_STEADY);
if (Test == KEY_TWICE) /* two short key presses */
{
Run = 0; /* end processing loop */
}
}
}
#endif
#ifdef HW_PROBE_ZENER
/*
* check for Zener diode
* - hardware option for voltage measurement of Zener diode
* or external voltage
* - uses dedicated analog input (TP_ZENER) with voltage divider
* (default 10:1)
* - boost converter runs all the time or circuit without boost converter
*/
void CheckZener(void)
{
uint16_t U1; /* voltage */
#ifdef ZENER_DIVIDER_CUSTOM
uint32_t Value; /* value */
#endif
/* get voltage */
U1 = ReadU(TP_ZENER); /* read voltage (in mV) */
#ifndef ZENER_DIVIDER_CUSTOM
/* ADC pin is connected to a 10:1 voltage divider */
U1 *= 10; /* voltage (mV) */
#endif
#ifdef ZENER_DIVIDER_CUSTOM
/*
* ADC pin is connected to a voltage divider (top: R1 / bottom: R2).
* - U2 = (Uin / (R1 + R2)) * R2
* - Uin = (U2 * (R1 + R2)) / R2
*/
Value = (((uint32_t)(ZENER_R1 + ZENER_R2) * 1000) / ZENER_R2); /* factor (0.001) */
Value *= U1; /* voltage (0.001 mV) */
Value /= 1000; /* scale to mV */
U1 = (uint16_t)Value; /* keep 2 bytes */
#endif
/* check for valid voltage */
if ((U1 >= ZENER_VOLTAGE_MIN) && (U1 <= ZENER_VOLTAGE_MAX))
{
Check.Found = COMP_ZENER; /* we got a Zener */
Semi.U_1 = U1; /* save voltage (V_Z) */
}
}
#endif
/* ************************************************************************
* ESR tool
* ************************************************************************ */
#ifdef SW_ESR_TOOL
/*
* ESR tool
* - uses probe #1 (pos) and probe #3 (neg)
*/
void ESR_Tool(void)
{
uint8_t Run = 1; /* control flag */
uint8_t Test; /* temp. value */
Capacitor_Type *Cap; /* pointer to cap */
uint16_t ESR; /* ESR (in 0.01 Ohms) */
Check.Diodes = 0; /* disable diode check in cap measurement */
Cap = &Caps[0]; /* pointer to first cap */
#ifdef HW_DISCHARGE_RELAY
/* discharge relay: short circuit probes */
/* ADC_PORT should be 0 */
ADC_DDR = (1 << TP_REF); /* disable relay */
#endif
/* show tool info */
LCD_Clear();
#ifdef UI_COLORED_TITLES
/* display: ESR */
Display_ColoredEEString(ESR_str, COLOR_TITLE);
#else
Display_EEString(ESR_str); /* display: ESR */
#endif
ProbePinout(PROBES_ESR); /* show probes used */
Display_Minus(); /* display "no value" */
while (Run > 0)
{
/*
* short or long key press -> measure
* two short key presses -> exit tool
*/
/* wait for user feedback */
Test = TestKey(0, CURSOR_BLINK | CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_TWICE) /* two short key presses */
{
Run = 0; /* end loop */
}
/* measure cap */
if (Run > 0) /* key pressed */
{
#ifdef HW_DISCHARGE_RELAY
/* discharge relay: remove short circuit */
/* ADC_PORT should be 0 */
ADC_DDR = 0; /* enable relay (via extrenal reference) */
#endif
LCD_ClearLine2(); /* update line #2 */
Display_EEString(Probing_str); /* display: probing... */
MeasureCap(PROBE_1, PROBE_3, 0); /* probe-1 = Vcc, probe-3 = Gnd */
LCD_ClearLine2(); /* update line #2 */
if (Check.Found == COMP_CAPACITOR) /* found capacitor */
{
/* show capacitance */
Display_Value(Cap->Value, Cap->Scale, 'F');
/* show ESR */
Display_Space();
ESR = MeasureESR(Cap);
if (ESR < UINT16_MAX) /* got valid ESR */
{
Display_Value(ESR, -2, LCD_CHAR_OMEGA);
}
else /* no ESR */
{
Display_Minus();
}
}
else /* no capacitor */
{
Display_Minus();
}
#ifdef HW_DISCHARGE_RELAY
ADC_DDR = (1 << TP_REF); /* short circuit probes */
#endif
}
}
#ifdef HW_DISCHARGE_RELAY
ADC_DDR = 0; /* remove short circuit */
#endif
}
#endif
/* ************************************************************************
* rotary encoder check
* ************************************************************************ */
#ifdef SW_ENCODER
/* local constants for direction */
#define DIR_NONE 0b00000000 /* no turn or error */
#define DIR_RIGHT 0b00000001 /* turned to the right */
#define DIR_LEFT 0b00000010 /* turned to the left */
/*
* check rotary encoder
*
* requires:
* - pointer to encoder history
*/
uint8_t CheckEncoder(uint8_t *History)
{
uint8_t Action = DIR_NONE; /* return value */
uint8_t Old_AB; /* old AB state */
uint8_t AB = 0; /* new AB state */
uint8_t Dir; /* turning direction */
uint8_t Steps; /* encoder steps */
uint8_t Temp; /* temporary value */
/* we assume: probe-1 = A / probe-2 = B / probe-3 = Common */
/* set up probes: probe-1 -- Rl -- Vcc / probe-2 -- Rl -- Vcc / Gnd -- probe-3 */
R_PORT = Probes.Rl_1 | Probes.Rl_2; /* pullup via Rl */
R_DDR = Probes.Rl_1 | Probes.Rl_2; /* enable pull-up resistors */
ADC_PORT = 0; /* pull down directly */
ADC_DDR = Probes.Pin_3; /* enable Gnd for probe-3 */
wait500us(); /* settle time */
/* get A & B signals */
Temp = ADC_PIN;
if (Temp & Probes.Pin_1) AB = 0b00000010;
if (Temp & Probes.Pin_2) AB |= 0b00000001;
R_DDR = 0; /* reset probes */
ADC_DDR = 0;
/* unpack history */
Temp = *History;
Old_AB = Temp & 0b00000011; /* old AB state, first 2 bits */
Temp >>=2 ; /* move 2 bits */
Dir = Temp & 0b00000011; /* direction, next 2 bits */
Temp >>= 2; /* move 2 bits */
Steps = Temp; /* steps, remaining 4 bits */
/* update state history */
if (Dir == (DIR_RIGHT | DIR_LEFT)) /* first scan */
{
Old_AB = AB; /* set as last state */
Dir = DIR_NONE; /* reset direction */
}
/* process signals */
if (Old_AB != AB) /* signals changed */
{
/* check if only one bit has changed (Gray code) */
Temp = AB ^ Old_AB; /* get bit difference */
if (!(Temp & 0b00000001)) Temp >>= 1;
if (Temp == 1) /* valid change */
{
/* determine direction */
/* Gray code: 00 01 11 10 */
Temp = 0b10001101; /* expected values for a right turn */
Temp >>= (Old_AB * 2); /* get expected value by shifting */
Temp &= 0b00000011; /* select value */
if (Temp == AB) /* value matches */
Temp = DIR_RIGHT; /* turn to the right */
else /* value mismatches */
Temp = DIR_LEFT; /* turn to the left */
/* detection logic */
if (Temp == Dir) /* turn in same direction */
{
Steps++; /* got another step */
/* for proper detection we need 4 Gray code steps */
if (Steps == 4) /* got 4 steps */
{
LCD_ClearLine2();
/*
* The turning direction determines A and B:
* - right: A = Probe #1 / B = Probe #2
* - left: A = Probe #2 / B = Probe #1
*/
if (Dir == DIR_RIGHT) /* right */
{
Semi.A = Probes.ID_1;
Semi.B = Probes.ID_2;
}
else /* left */
{
Semi.A = Probes.ID_2;
Semi.B = Probes.ID_1;
}
Semi.C = Probes.ID_3; /* Common */
/* display pinout */
Show_SemiPinout('A', 'B', 'C');
Steps = 0; /* reset steps */
Action = Temp; /* signal valid step */
}
}
else /* turn has changed direction */
{
Steps = 1; /* first step for new direction */
}
Dir = Temp; /* update direction */
}
else /* invalid change */
{
Dir = DIR_RIGHT | DIR_LEFT; /* trigger reset of history */
}
}
/* pack new history */
Temp = AB; /* AB state, first 2 bits */
Dir <<= 2; /* direction, next 2 bits */
Temp |= Dir;
Steps <<= 4; /* steps, remaining 4 bits */
Temp |= Steps;
*History = Temp; /* save new history */
return Action;
}
/*
* rotary encoder check
* - uses standard probes
*/
void Encoder_Tool(void)
{
uint8_t Flag; /* flag/counter */
uint8_t History[3]; /* encoder history */
/*
* History:
* - 000000xx AB state
* - 0000xx00 turning direction
* - xxxx0000 steps
*/
/* show info */
LCD_Clear();
#ifdef UI_COLORED_TITLES
/* display: Rotary Encoder */
Display_ColoredEEString(Encoder_str, COLOR_TITLE);
#else
Display_EEString(Encoder_str); /* display: Rotary Encoder */
#endif
/* init array */
for (Flag = 0; Flag <= 2; Flag++)
{
History[Flag] = (DIR_RIGHT | DIR_LEFT) << 2;
}
/* processing loop */
Flag = 5;
while (Flag < 10)
{
wdt_reset();
if (Flag == 5) /* ask user to turn */
{
LCD_ClearLine2();
Display_EEString(TurnRight_str); /* display: Turn right! */
Flag = 0; /* reset flag */
}
UpdateProbes(PROBE_1, PROBE_2, PROBE_3); /* check first pinout */
Flag = CheckEncoder(&History[0]);
if (Flag == 0)
{
UpdateProbes(PROBE_1, PROBE_3, PROBE_2); /* check second pinout */
Flag = CheckEncoder(&History[1]);
}
if (Flag == 0)
{
UpdateProbes(PROBE_2, PROBE_3, PROBE_1); /* check third pinout */
Flag = CheckEncoder(&History[2]);
}
if (Flag > 0) /* detected encoder */
{
/* let the user read or skip the text */
TestKey(3000, CURSOR_STEADY | CHECK_OP_MODE | CHECK_BAT);
Flag = 5; /* reset flag */
}
else /* nothing found yet */
{
if (!(BUTTON_PIN & (1 << TEST_BUTTON))) /* if key is pressed */
{
MilliSleep(100); /* smooth UI */
Flag = 10; /* end loop */
}
}
}
}
/* clean up local constants for direction */
#undef DIR_LEFT
#undef DIR_RIGHT
#undef DIR_NONE
#endif
/* ************************************************************************
* opto coupler check
* ************************************************************************ */
#ifdef SW_OPTO_COUPLER
/*
* check for LED
* - simple wrapper for CheckDiode()
*
* requires:
* - Probe1: ID of positive probe (anode)
* - Probe2: ID of negative probe (cathode)
*/
void Check_LED(uint8_t Probe1, uint8_t Probe2)
{
uint8_t Probe3; /* ID of probe #3 */
uint16_t U1; /* voltage */
/* update all three probes */
Probe3 = GetThirdProbe(Probe1, Probe2); /* get third one */
UpdateProbes(Probe1, Probe2, Probe3); /* update probes */
/* we assume: probe-1 = A / probe2 = C */
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc */
R_PORT = 0; /* set resistor port to Gnd */
R_DDR = Probes.Rl_2; /* pull down probe-2 via Rl */
ADC_DDR = Probes.Pin_1; /* set probe-1 to output */
ADC_PORT = Probes.Pin_1; /* pull-up probe-1 directly */
U1 = ReadU_5ms(Probes.Ch_2); /* voltage at Rl (cathode) */
if (U1 >= 977) /* not just a leakage current (> 1.4mA) */
{
CheckDiode(); /* run standard diode check */
}
}
/*
* check opto couplers
* - uses standard probes
* - pins which have to be connected (common Gnd):
* - LED's cathode and BJT's emitter
* - LED's cathode and TRIAC's MT2
* - supports:
* - BJT
* - TRIAC (with and without zero crossing circuit)
*/
void OptoCoupler_Tool(void)
{
uint8_t Run = 1; /* loop control */
uint8_t Test; /* user input */
uint16_t U1, U2; /* voltages */
uint16_t U3, U4; /* voltages */
uint32_t CTR = 0; /* CTR in % */
/* local constants for status */
#define DETECTED_LED 50
#define DETECTED_BJT 100
#define DETECTED_TRIAC 101
/* init */
/* next-line mode: keep first line and wait for key/timeout */
UI.LineMode = LINE_KEEP | LINE_KEY;
/* display info */
LCD_Clear();
#ifdef UI_COLORED_TITLES
/* display: Opto Coupler */
Display_ColoredEEString(OptoCoupler_str, COLOR_TITLE);
#else
Display_EEString(OptoCoupler_str); /* display: Opto Coupler */
#endif
Display_NL_EEString(Start_str); /* display: Start */
/*
* processing loop
*/
while (Run)
{
/* user input */
/* wait for user feedback */
Test = TestKey(0, CURSOR_BLINK | CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_TWICE) /* two short key presses */
{
Run = 0; /* end loop */
}
if (Run) /* check opto coupler */
{
/* update display */
LCD_Clear();
#ifdef UI_SERIAL_COPY
Display_Serial_On(); /* enable serial output & NL */
#endif
#ifdef UI_COLORED_TITLES
/* display: Opto Coupler */
Display_ColoredEEString(OptoCoupler_str, COLOR_TITLE);
#else
Display_EEString(OptoCoupler_str); /* display: Opto Coupler */
#endif
Display_NextLine();
Test = 0; /* reset status */
/*
* scan for LED
*/
Check.Found = COMP_NONE; /* reset component search */
Check.Diodes = 0; /* reset number of diodes */
/* check all possible probe combinations */
Check_LED(PROBE_1, PROBE_2);
Check_LED(PROBE_2, PROBE_1);
Check_LED(PROBE_1, PROBE_3);
Check_LED(PROBE_3, PROBE_1);
Check_LED(PROBE_2, PROBE_3);
Check_LED(PROBE_3, PROBE_2);
if (Check.Diodes == 1) /* got one */
{
/* update all three probes for remaining checks */
Test = GetThirdProbe(Diodes[0].A, Diodes[0].C); /* get third probe */
UpdateProbes(Diodes[0].A, Diodes[0].C, Test); /* update probes */
Test = DETECTED_LED; /* proceed with other checks */
}
/*
* we assume:
* probe-1 = LED's anode
* probe-2 = LED's cathode & BJT's emitter or TRIAC's MT2
* probe-3 = BJT's collector or TRIAC's MT1
*/
/*
* check for BJT and TRIAC
* - BJT conducts only while LED is lit.
* - TRIAC keeps conducting as long as load current flows.
* Some types with zero crossing circuit got an inhibit voltage
* of about 5V.
*/
if (Test == DETECTED_LED) /* LED detected */
{
/* set probes: probe-2 -- Gnd / probe-3 -- Rl -- Vcc */
ADC_DDR = Probes.Pin_2; /* set probe-2 to output */
ADC_PORT = 0; /* pull down probe-2 directly */
R_DDR = Probes.Rl_1 | Probes.Rl_3; /* select Rl for probe-1 & Rl for probe-3 */
R_PORT = Probes.Rl_3; /* pull up collector via Rl */
U1 = ReadU_5ms(Probes.Ch_3); /* voltage at collector when LED is off */
/* make sure we have no conduction without the LED lit */
if (U1 > 4000) /* allow a leakage current of 1.5mA */
{
/* simulate zero crossing in case of a TRIAC with zero crossing circuit */
R_PORT = Probes.Rl_1; /* turn on LED */
wait1ms(); /* wait a tad */
R_PORT = Probes.Rl_1 | Probes.Rl_3; /* also pull up collector via Rl */
U1 = ReadU_5ms(Probes.Ch_3); /* voltage at collector when LED is on */
R_PORT = Probes.Rl_3; /* turn off LED */
U2 = ReadU_5ms(Probes.Ch_3); /* voltage at collector when LED is off */
/* we should have conduction when the LED is lit */
if (U1 <= 4000) /* more than 1.5mA */
{
if (U2 >= 4000) /* no conduction, allow some leakage current */
{
Test = DETECTED_BJT; /* BJT type */
}
else /* conduction */
{
/* check if both voltages are about the same */
U3 = U1;
U3 /= 8; /* 12.5% */
U4 = U1 - U3; /* lower threshold */
U3 += U1; /* upper threshold */
if ((U2 > U4) && (U2 < U3))
{
Test = DETECTED_TRIAC; /* TRIAC type */
}
}
}
}
R_DDR = Probes.Rl_1; /* set probe-3 to HiZ */
}
/*
* measure CRT for BJT type
*/
if (Test == DETECTED_BJT) /* BJT type */
{
/* change probes: probe-3 -- Vcc */
ADC_DDR = Probes.Pin_2 | Probes.Pin_3; /* set probe-3 to output */
ADC_PORT = Probes.Pin_3; /* pull up probe-3 directly */
/* get voltages at current shunts */
Cfg.Samples = 10; /* just a few samples for 1ms runtime */
R_PORT = Probes.Rl_1; /* turn LED on */
wait1ms(); /* time for propagation delay */
U1 = ReadU(Probes.Ch_1); /* voltage at LED's anode (Rl) */
U2 = ReadU(Probes.Ch_2); /* voltage at emitter (RiL) */
R_PORT = 0; /* turn LED off */
Cfg.Samples = ADC_SAMPLES; /* reset samples to default */
/* calculate LED's If */
/* If = (Vcc - U1) / (RiH + Rl) */
U3 = Cfg.Vcc - U1; /* Vcc - U1 (mV) */
CTR = (uint32_t)U3;
CTR *= 10000; /* scale to 0.0001 mV */
U4 = NV.RiH + (R_LOW * 10); /* RiH + Rl (0.1 Ohms) */
CTR /= U4; /* If = U/R in µA */
U3 = (uint16_t)CTR; /* If in µA */
/* calculate BJT's Ie */
/* Ie = I_total - If = (U2 / RiL) - If */
CTR = (uint32_t)U2; /* U2 (mV) */
CTR *= 10000; /* scale to 0.0001 mV */
CTR /= NV.RiL; /* /RiL in 0.1 Ohms -> I_total (µA) */
CTR -= U3; /* Ie = I_total - If (µA) */
/* calculate CTR */
/* CTR = Ie / If */
CTR *= 100; /* scale up to % */
CTR /= U3; /* Ie / If (%) */
}
/*
* Measure turn-on and turn-off times
* - Unfortunately we can't use the analog comparator in conjunction
* with Timer1, because the 1.1V bandgap reference would limit the
* time measurement to opto couplers with a CTR > 200%.
*/
if (Test == DETECTED_BJT) /* BJT type */
{