-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsemi.c
2196 lines (1800 loc) · 71.3 KB
/
semi.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
/* ************************************************************************
*
* semiconductor tests and measurements
*
* (c) 2012-2024 by Markus Reschke
* based on code from Markus Frejek and Karl-Heinz Kübbeler
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define SEMI_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 */
/* ************************************************************************
* support functions
* ************************************************************************ */
/*
* manage pin designators for semiconductors
* - results in smaller firmware than setting the designators in
* each semiconductor check function
*/
void SemiPinDesignators(void)
{
uint8_t A = 0; /* designator for pin A */
uint8_t B = 0; /* designator for pin B */
uint8_t C = 0; /* designator for pin C */
/*
* set pin designators based on semiconductor type
*/
switch (Check.Found)
{
case COMP_BJT: /* BJT */
A = 'B'; /* base */
B = 'C'; /* collector */
C = 'E'; /* emitter */
break;
case COMP_FET: /* FET */
A = 'G'; /* gate */
if (Check.Type & TYPE_SYMMETRICAL) /* symmetrical */
{
B = 'x'; /* drain */
C = 'x'; /* source */
}
else /* normal */
{
B = 'D'; /* drain */
C = 'S'; /* source */
}
break;
case COMP_IGBT: /* IGBT */
A = 'G'; /* gate */
B = 'C'; /* collector */
C = 'E'; /* emitter */
break;
case COMP_THYRISTOR: /* Thyristor / SCR */
case COMP_PUT: /* PUT */
A = 'G'; /* gate */
B = 'A'; /* anode */
C = 'C'; /* cathode */
break;
case COMP_TRIAC: /* TRIAC */
A = 'G'; /* gate */
B = '2'; /* MT2 */
C = '1'; /* MT1 */
break;
#ifdef SW_UJT
case COMP_UJT: /* UJT */
A = 'E'; /* emitter */
B = '2'; /* B2 */
C = '1'; /* B1 */
break;
#endif
}
/* update pin designators */
Semi.DesA = A; /* designator for pin A */
Semi.DesB = B; /* designator for pin B */
Semi.DesC = C; /* designator for pin C */
}
/*
* measure leakage current
* - current through a semiconducter in non-conducting mode
* - result is stored in Semi.I_value & I.scale
*
* requires:
* - mode:
* 0 = normal
* 1 = high current
*/
void GetLeakageCurrent(uint8_t Mode)
{
int8_t Scale; /* exponent of factor (value * 10^x) */
uint32_t Value; /* current */
uint32_t R_Shunt; /* shunt resistor */
uint16_t U_Rl; /* voltage at Rl */
/*
* set up probes:
* - use Rl as current shunt
* - probe-1 = pos / probe-2 = neg / probe-3 = HiZ
* Diode: probe-1 = cathode / probe-2 = anode
* NPN BJT: probe-1 = collector / probe-2 = emitter
* PNP BJT: probe-1 = emitter / probe-2 = collector
* - 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 */
U_Rl = ReadU_5ms(Probes.Ch_2); /* get voltage at Rl */
if (U_Rl > 3) /* > 5µA */
{
/* consider internal resistance of MCU */
R_Shunt = (uint32_t)NV.RiL; /* in 0.1 Ohms */
Scale = -7; /* 100n */
if ((U_Rl > 1400) && (Mode == 1)) /* > 2mA */
{
/*
* for high currents take second measurement using RiL:
* - only if high current mode is enabled
* - with 1mV ADC resolution we get down to 50µA
* - max. current is 5V/40Ohms = 125mA
* - set probes: Gnd -- probe-2 / probe-1 -- Vcc
*/
R_DDR = 0; /* disable resistors */
/* pull down probe-2 directly */
ADC_DDR = Probes.Pin_1 | Probes.Pin_2;
U_Rl = ReadU(Probes.Ch_2); /* get voltage at RiL */
}
else /* keep measurement */
{
/*
* Rl:
* - with 1mV ADC resolution we get down to 1.4µA
* - max. current is 5V/720Ohms = 7mA
*/
/* add Rl */
R_Shunt += (R_LOW * 10); /* in 0.1 Ohms */
}
}
else /* < 5µA */
{
/*
* For low currents we take a second measurement using Rh:
* - with 1mV ADC resolution we get down to 2nA
* - max. current is 5V/470kOhms = 10µA
* - set probes: Gnd -- Rh -- probe-2 / probe-1 -- Vcc
*/
R_DDR = Probes.Rh_2; /* pull down probe-2 via Rh */
U_Rl = ReadU_5ms(Probes.Ch_2); /* get voltage at Rh */
/* neglect MCU's internal resistance */
R_Shunt = R_HIGH;
Scale = -8; /* 10n */
}
/* clean up */
ADC_DDR = 0; /* set ADC port to HiZ mode */
ADC_PORT = 0; /* set ADC port low */
R_DDR = 0; /* set resistor port to HiZ mode */
R_PORT = 0; /* set resistor port low */
/* calculate current */
Value = U_Rl * 100000; /* scale voltage to 10nV */
Value /= R_Shunt; /* I = U/R */
/* save result */
Semi.I_value = Value;
Semi.I_scale = Scale;
}
/* ************************************************************************
* diodes
* ************************************************************************ */
/*
* look for a specific diode
*
* required:
* - probe ID for Anode
* - probe ID for Cathode
*
* returns:
* - NULL if no matching diode was found
* - pointer to Diode_Type if matching diode was found
*/
Diode_Type *SearchDiode(uint8_t A, uint8_t C)
{
Diode_Type *Diode; /* pointer to diode */
uint8_t n; /* counter */
n = Check.Diodes; /* number of diodes */
Diode = &Diodes[0]; /* pointer to first diode */
while (n > 0)
{
if ((A == Diode->A) && (C == Diode->C))
{
break;
}
n--; /* next diode */
Diode++;
}
if (n == 0) /* no match found */
{
Diode = NULL; /* reset pointer */
}
return Diode;
}
/*
* check for diode
*/
void CheckDiode(void)
{
Diode_Type *Diode; /* pointer to diode */
uint16_t U1_Rl; /* Vf #1 with Rl pull-up */
uint16_t U1_Rh; /* Vf #1 with Rh pull-up */
uint16_t U1_Zero; /* Vf #1 zero */
uint16_t U2_Rl; /* Vf #2 with Rl pull-down */
uint16_t U2_Rh; /* Vf #2 with Rh pull-down */
uint16_t U2_Zero; /* Vf #2 zero */
uint16_t U_Diff; /* Vf difference */
wdt_reset(); /* reset watchdog */
DischargeProbes(); /* try to discharge probes */
if (Check.Found == COMP_ERROR) return; /* skip on error */
/*
* DUT could be:
* - simple diode
* - intrinsic diode of a MOSFET or another component
* - flyback diode of a BJT
* - small resistor (< 3k) or inductor
* - capacitor (> around 22µF)
*
* Solution:
* - Vf of a diode rises with the current within some limits (about twice
* for Si and Schottky). Ge, Z-diodes and LEDs are hard to determine.
* Therefore it might be better to filter out other components.
* - For a MOSFET's intrinsic diode we have to make sure that the MOSFET
* is not conducting to be able to get Vf of the protection diode.
* So we discharge the gate and run the measurements twice for assumed
* p and n channel FETs.
* - Take care about the internal voltage drop of the MCU at the cathode
* for high test currents (Rl).
* - Filter out resistors by the used voltage divider:
* k = Rh / (Rl + Ri_H + Ri_L)
* U_Rh = U_Rl / (k - (k - 1) U_Rl / Vcc)
* U_Rl = k U_Rh / (1 + (k - 1) U_Rh / Vcc)
* - Filter out caps by checking the voltage before and after measurement
* with Rh. In 15ms a 22µF cap would be charged from 0 to 7mV, a larger
* cap would have a lower voltage. We have also to consider other effects
* which could increase the voltage.
*
* Hints:
* - Rl drives a current of about 7mA. That's not the best current for
* measuring Vf. The current for Rh is about 10.6µA.
* Most DMMs use 1mA.
*/
/*
* Vf #1, supporting a possible p-channel MOSFET
*/
/* we assume: probe-1 = A / probe2 = C */
/* set probes: Gnd -- probe-2 / probe-1 -- HiZ */
ADC_PORT = 0;
ADC_DDR = Probes.Pin_2; /* pull down cathode directly */
/* R_DDR is set to HiZ by DischargeProbes() */
U1_Zero = ReadU(Probes.Ch_1); /* get voltage at anode */
/* measure voltage across DUT (Vf) with Rh */
/* set probes: Gnd -- probe-2 / probe-1 -- Rh -- Vcc */
R_DDR = Probes.Rh_1; /* enable Rh for probe-1 */
R_PORT = Probes.Rh_1; /* pull up anode via Rh */
PullProbe(Probes.Rl_3, PULL_10MS | PULL_UP); /* discharge gate */
U1_Rh = ReadU_5ms(Probes.Ch_1); /* get voltage at anode */
/* neglect voltage at cathode */
/* measure voltage across DUT (Vf) with Rl */
/* set probes: Gnd -- probe-2 / probe-1 -- Rl -- Vcc */
R_DDR = Probes.Rl_1; /* enable Rl for probe-1 */
R_PORT = Probes.Rl_1; /* pull up anode via Rl */
PullProbe(Probes.Rl_3, PULL_10MS | PULL_UP); /* discharge gate */
U1_Rl = ReadU_5ms(Probes.Ch_1); /* get voltage at anode */
U1_Rl -= ReadU(Probes.Ch_2); /* substract voltage at cathode */
DischargeProbes(); /* try to discharge probes */
if (Check.Found == COMP_ERROR) return; /* skip on error */
/*
* Vf #2, supporting a possible n-channel MOSFET
*/
/* we assume: probe-1 = A / probe2 = C */
/* set probes: Gnd -- probe-2 / probe-1 -- HiZ */
ADC_PORT = 0;
ADC_DDR = Probes.Pin_2; /* pull down cathode directly */
/* R_DDR is set to HiZ by DischargeProbes() */
U2_Zero = ReadU(Probes.Ch_1); /* get voltage at anode */
/* measure voltage across DUT (Vf) with Rh */
/* set probes: Gnd -- Rh -- probe-2 / probe-1 -- Vcc */
ADC_DDR = 0; /* set to HiZ to prepare change */
ADC_PORT = Probes.Pin_1; /* pull up anode directly */
ADC_DDR = Probes.Pin_1; /* enable output */
R_PORT = 0; /* pull down cathode via Rh */
R_DDR = Probes.Rh_2; /* enable Rh for probe-2 */
PullProbe(Probes.Rl_3, PULL_10MS | PULL_DOWN); /* discharge gate */
U2_Rh = ReadU_5ms(Probes.Ch_1); /* get voltage at anode */
U_Diff = ReadU(Probes.Ch_2); /* get voltage at cathode */
if (U2_Rh >= U_Diff) /* prevent underrun */
{
U2_Rh -= U_Diff; /* V_f = U_Anode - U_Cathode */
}
else /* maybe large inductance */
{
U2_Rh = 0; /* simply zero */
}
/* measure voltage across DUT (Vf) with Rl */
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc */
R_DDR = Probes.Rl_2; /* pull down cathode via Rl */
PullProbe(Probes.Rl_3, PULL_10MS | PULL_DOWN); /* discharge gate */
U2_Rl = ReadU_5ms(Probes.Ch_1); /* get voltage at anode */
U2_Rl -= ReadU(Probes.Ch_2); /* substract voltage at cathode */
ADC_DDR = 0; /* stop pulling up */
/*
* process results
*/
/* choose between both measurements */
if (U1_Rl > U2_Rl) /* the higher voltage wins */
{
U_Diff = U1_Rl - U2_Rl; /* difference of U_Rls */
U2_Rl = U1_Rl;
U2_Rh = U1_Rh;
U2_Zero = U1_Zero;
}
else /* keep 2nd data set */
{
U_Diff = U2_Rl - U1_Rl; /* difference of U_Rls */
}
/*
* check difference of U_Rl measurements
* - nearly zero for diodes, BJTs etc.
* - about a diode drop for enhancement-mode MOSFETs
* - >1000mV for depletion-mode FETs partly conducting
*/
if (U_Diff > 1000) return; /* depletion-mode FET */
/*
* U_Rh < 5mV for
* - resistor < 470 Ohm
* - very large cap
*
* The lowest Vf measured with Rh is 9mV for a GY100 (Germanium).
*/
if (U2_Rh <= 5) return; /* small resistor or very large cap */
/*
* U_Zero <= 2 for resistor or diode
* U_Zero > 2 for cap or diode
* if U_Zero > 2 then U_Rh - U_Zero < 100 for cap
*
* Hints:
* If U_Zero > 10 and U_Rh is about U_Zero it's a large cap.
* As larger the cap as lower U_Rl (charging time 15ms).
*/
if (U2_Rh > U2_Zero) /* prevent underrun */
{
U_Diff = U2_Rh - U2_Zero; /* calculate difference */
}
else
{
U_Diff = U2_Zero - U2_Rh; /* calculate difference */
}
if ((U2_Zero > 2) && (U_Diff < 100)) /* capacitor */
{
return;
}
/*
* Check for a resistor:
* - Measure current in forward and reverse direction. Both curents
* should be the same for a resistor.
* - For U_Rh > 40mV we don't need to check for a resistor.
*/
if (U2_Rh < 40) /* resistor (< 3k) */
{
/* get current in forward direction */
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc */
R_PORT = 0;
R_DDR = Probes.Rl_2; /* pull down cathode via Rl */
ADC_PORT = Probes.Pin_1;
ADC_DDR = Probes.Pin_1; /* pull up anode directly */
U1_Rl = ReadU_5ms(Probes.Ch_2); /* get voltage at Rl */
/* get current in reverse direction */
/* set probes: Gnd -- Rl -- probe-1 / probe-2 -- Vcc */
R_DDR = Probes.Rl_1; /* pull down anode via Rl */
ADC_PORT = Probes.Pin_2;
ADC_DDR = Probes.Pin_2; /* pull up cathode directly */
U1_Rh = ReadU_5ms(Probes.Ch_1); /* get voltage at Rl */
/* check if both currents match */
U_Diff = U1_Rl / 20; /* 5% */
U1_Zero = U1_Rl - U_Diff; /* 95% */
U2_Zero = U1_Rl + U_Diff; /* 105% */
if ((U1_Rh > U1_Zero) && (U1_Rh < U2_Zero)) /* within limits */
{
/* could also be an anti-parallel diode with very low Vf */
return; /* resistor */
}
}
/*
* if U_Rl (Vf) is between 0.15V and 4.64V it's a diode
*/
if ((U2_Rl > 150) && (U2_Rl < 4640))
{
/* if we haven't found any other component yet */
if ((Check.Found == COMP_NONE) ||
(Check.Found == COMP_RESISTOR))
{
Check.Found = COMP_DIODE;
/* Check.Type = TYPE_STANDARD; */
/* we don't set Check.Done in case we'll find something different */
}
/* save data */
Diode = &Diodes[Check.Diodes];
Diode->A = Probes.ID_1; /* probe ID for Anode */
Diode->C = Probes.ID_2; /* probe ID for Cathode */
Diode->V_f = U2_Rl; /* Vf for high measurement current */
Diode->V_f2 = U2_Rh; /* Vf for low measurement current */
Check.Diodes++;
}
}
/* ************************************************************************
* BJTs and FETs
* ************************************************************************ */
/*
* verify MOSFET by checking the body diode
*/
void VerifyMOSFET(void)
{
uint8_t Anode;
uint8_t Cathode;
Diode_Type *Diode; /* pointer to diode */
/* set expected body diode */
if (Check.Type & TYPE_N_CHANNEL) /* n-channel */
{
Anode = Semi.C; /* anode at source */
Cathode = Semi.B; /* cathode at drain */
}
else /* p-channel */
{
Anode = Semi.B; /* anode at drain */
Cathode = Semi.C; /* cathode at source */
}
/* search for a diode with reversed polarity */
Diode = SearchDiode(Cathode, Anode); /* search for reversed diode */
if (Diode != NULL) /* got it */
{
/* this can't be a MOSFET, so let's reset */
Check.Found = COMP_NONE;
Check.Type = 0;
Check.Done &= ~(DONE_SEMI); /* unset flag */
}
}
/*
* measure the gate threshold voltage of a depletion-mode MOSFET
*
* requires:
* - Type: n-channel or p-channel
*/
void GetGateThreshold(uint8_t Type)
{
int32_t Ugs = 0; /* gate threshold voltage / Vth */
uint8_t Drain_Rl; /* Rl register bits for drain */
uint8_t Drain_ADC; /* ADC port register bits for drain */
uint8_t PullMode; /* pull-up/down mode */
uint8_t Counter; /* loop counter */
/*
* init variables
*/
if (Type & TYPE_N_CHANNEL) /* n-channel */
{
/* we assume: probe-1 = D / probe-2 = S / probe-3 = G */
/* probe-2 is still pulled down directly */
/* probe-1 is still pulled up via Rl */
Drain_Rl = Probes.Rl_1;
Drain_ADC = Probes.Pin_1;
PullMode = PULL_10MS | PULL_DOWN;
}
else /* p-channel */
{
/* we assume: probe-1 = S / probe-2 = D / probe-3 = G */
/* probe-2 is still pulled down via Rl */
/* probe-1 is still pulled up directly */
Drain_Rl = Probes.Rl_2;
Drain_ADC = Probes.Pin_2;
PullMode = PULL_10MS | PULL_UP;
}
/*
* For low reaction times we use the ADC directly.
*/
/* sanitize register bits for drain to prevent a never-ending loop */
Drain_ADC &= ((1 << TP1) | (1 << TP2) | (1 << TP3));
ADMUX = Probes.Ch_3 | ADC_REF_VCC; /* select probe-3 for ADC input */
/* and use Vcc as reference */
#ifndef ADC_LARGE_BUFFER_CAP
/* buffer cap: 1nF or none at all */
wait100us(); /* time for voltage stabilization */
#else
/* buffer cap: 100nF */
wait10ms(); /* time for voltage stabilization */
#endif
/* sample 10 times */
for (Counter = 0; Counter < 10; Counter++)
{
wdt_reset(); /* reset watchdog */
/* discharge gate via Rl for 10 ms */
PullProbe(Probes.Rl_3, PullMode);
/* pull up/down gate via Rh to slowly charge gate */
R_DDR = Drain_Rl | Probes.Rh_3;
/* wait until FET conducts */
/* any problem will cause a watchdog timeout */
if (Type & TYPE_N_CHANNEL) /* n-channel */
{
/* FET conducts when the voltage at the drain reaches low level */
while (ADC_PIN & Drain_ADC);
}
else /* p-channel */
{
/* FET conducts when the voltage at the drain reaches high level */
while (!(ADC_PIN & Drain_ADC));
}
R_DDR = Drain_Rl; /* set probe-3 to HiZ mode */
/* get voltage of gate */
ADCSRA |= (1 << ADSC); /* start ADC conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* add ADC reading */
if (Type & TYPE_N_CHANNEL) /* n-channel */
{
Ugs += ADCW; /* Ugs = U_g */
}
else /* p-channel */
{
Ugs -= (1023 - ADCW); /* Ugs = - (Vcc - U_g) */
}
}
/* calculate V_th */
Ugs /= 10; /* average of 10 samples */
Ugs *= Cfg.Vcc; /* convert to voltage */
Ugs /= 1024; /* using 10 bit resolution */
/* save data */
Semi.U_2 = (int16_t)Ugs; /* gate threshold voltage (in mV) */
/* update reference source for next ADC run */
Cfg.Ref = ADC_REF_VCC; /* we've used Vcc as reference */
}
/*
* measure hFE of BJT in common collector circuit (emitter follower)
*
* requires:
* - Type: NPN or PNP
*
* returns:
* - hFE
*/
uint32_t Get_hFE_C(uint8_t Type)
{
uint32_t hFE; /* return value */
#ifndef NO_HFE_C_RL
uint32_t hFE2; /* temp. hFE value */
#endif
uint16_t U_R_e; /* voltage across emitter resistor */
uint16_t U_R_b; /* voltage across base resistor */
uint16_t Ri; /* internal resistance of MCU */
#ifdef SW_HFE_CURRENT
int32_t I_e; /* emitter current */
#endif
/*
* measure hFE for a BJT in common collector circuit
* (emitter follower):
* - hFE = (I_e - I_b) / I_b
* - measure the voltages across the resistors and calculate the currents
* (resistor values are well known)
* - hFE = ((U_R_e / R_e) - (U_R_b / R_b)) / (U_R_b / R_b)
*/
#ifndef NO_HFE_C_RL
/*
* First use Rl as base resistor, and then Rh if base voltage is quite low.
* Keep higher hFE value.
*/
/*
* set up probes and get voltages using Rl as base resistor
*/
if (Type == TYPE_NPN) /* NPN */
{
/* we assume: probe-1 = C / probe-2 = E / probe-3 = B */
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc / probe-3 -- Rl -- Vcc */
ADC_DDR = Probes.Pin_1; /* set probe-1 to output */
ADC_PORT = Probes.Pin_1; /* pull up collector directly */
R_DDR = Probes.Rl_2 | Probes.Rl_3; /* select Rl for probe-2 & Rl for probe-3 */
R_PORT = Probes.Rl_3; /* pull up base via Rl */
U_R_e = ReadU_5ms(Probes.Ch_2); /* U_R_e = U_e */
U_R_b = Cfg.Vcc - ReadU(Probes.Ch_3); /* U_R_b = Vcc - U_b */
#ifdef SW_HFE_CURRENT
I_e = (int32_t)U_R_e; /* U_R_e (mV) */
Ri = NV.RiL; /* RiL (0.1 Ohms) */
#endif
}
else /* PNP */
{
/* we assume: probe-1 = E / probe-2 = C / probe-3 = B */
/* set probes: Gnd -- probe-2 / probe-1 -- Rl -- Vcc / Gnd -- Rl -- probe-3 */
ADC_PORT = 0; /* set ADC port low */
ADC_DDR = Probes.Pin_2; /* pull down collector directly */
R_PORT = Probes.Rl_1; /* pull up emitter via Rl */
R_DDR = Probes.Rl_1 | Probes.Rl_3; /* pull down base via Rl */
U_R_e = Cfg.Vcc - ReadU_5ms(Probes.Ch_1); /* U_R_e = Vcc - U_e */
U_R_b = ReadU(Probes.Ch_3); /* U_R_b = U_b */
#ifdef SW_HFE_CURRENT
I_e = -(int32_t)U_R_e; /* negative U_R_e (mV) */
Ri = NV.RiH; /* RiH (0.1 Ohms) */
#endif
}
/*
* Both resistors are about the same (R_e = R_b):
* - hFE = ((U_R_e / R_e) - (U_R_b / R_b)) / (U_R_b / R_b)
* - = (U_R_e - U_R_b) / U_R_b
*/
if (U_R_b == 0) U_R_b = 1; /* prevent division by zero */
hFE = (uint32_t)((U_R_e - U_R_b) / U_R_b);
#ifdef SW_HFE_CURRENT
/* calculate emitter current: I_e = U_R_e / (Rl + Ri) */
/* NPN: Ri = RiL / PNP: Ri = RiH */
I_e *= 10000; /* scale to 0.1 µV (for µA and Ri's 0.1 Ohms */
Ri += (R_LOW * 10); /* Ri + Rl (in 0.1 Ohms) */
I_e /= Ri; /* / (Rl + Ri) */
Semi.U_2 = (int16_t)I_e; /* save I_e (in µA) */
#endif
/*
* for a possible high gain BJT or Darlington change the base resistor
* from Rl to Rh and measure again
*/
if (U_R_b <= 15) /* I_b <= 21 µA */
{
if (Type == TYPE_NPN) /* NPN */
{
/* change probes: probe-3 -- Rh -- Vcc */
R_DDR = Probes.Rl_2 | Probes.Rh_3; /* select Rl for probe-2 & Rh for probe-3 */
R_PORT = Probes.Rh_3; /* pull up base via Rh */
U_R_e = ReadU_5ms(Probes.Ch_2); /* U_R_e = U_e */
U_R_b = Cfg.Vcc - ReadU(Probes.Ch_3); /* U_R_b = Vcc - U_b */
#ifdef SW_HFE_CURRENT
I_e = (int32_t)U_R_e; /* U_R_e (mV) */
#endif
Ri = NV.RiL; /* get internal resistance */
}
else /* PNP */
{
/* change probes: Gnd -- Rh -- probe-3 */
R_DDR = Probes.Rl_1 | Probes.Rh_3; /* pull down base via Rh */
U_R_e = Cfg.Vcc - ReadU_5ms(Probes.Ch_1); /* U_R_e = Vcc - U_e */
U_R_b = ReadU(Probes.Ch_3); /* U_R_b = U_b */
#ifdef SW_HFE_CURRENT
I_e = -(int32_t)U_R_e; /* negative U_R_e (mV) */
#endif
Ri = NV.RiH; /* get internal resistance */
}
/*
* Since I_b is so small vs. I_e we'll neglect it and use
* hFE = I_e / I_b
* = (U_R_e / R_e) / (U_R_b / R_b)
* = (U_R_e * R_b) / (U_R_b * R_e)
*/
if (U_R_b == 0) U_R_b = 1; /* prevent division by zero */
hFE2 = U_R_e * R_HIGH; /* U_R_e * R_b */
hFE2 /= U_R_b; /* / U_R_b */
hFE2 *= 10; /* upscale to 0.1 */
Ri += (R_LOW * 10); /* R_e = Ri + Rl (in 0.1 Ohms) */
hFE2 /= Ri; /* / R_e in 0.1 Ohms */
/* keep the higher hFE */
if (hFE2 > hFE) /* hFE_Rh higher */
{
hFE = hFE2; /* update hFE */
#ifdef SW_HFE_CURRENT
/* calculate emitter current: I_e = U_R_e / (Rl + Ri) */
/* NPN: Ri = RiL / PNP: Ri = RiH */
I_e *= 10000; /* scale to 0.1 µV (for µA and Ri's 0.1 Ohms */
I_e /= Ri; /* / (Rl + Ri) */
Semi.U_2 = (int16_t)I_e; /* update I_e (in µA) */
#endif
}
}
#else
/*
* Skip measurement with Rl as base resistor and use only Rh. A poorly
* designed tester clone causes U_R_b to be too low when using Rl. :(
*/
/*
* set up probes and get voltages using Rh as base resistor
*/
if (Type == TYPE_NPN) /* NPN */
{
/* we assume: probe-1 = C / probe-2 = E / probe-3 = B */
/* set probes: Gnd -- Rl -- probe-2 / probe-1 -- Vcc / probe-3 -- Rh -- Vcc */
ADC_DDR = Probes.Pin_1; /* set probe-1 to output */
ADC_PORT = Probes.Pin_1; /* pull up collector directly */
R_DDR = Probes.Rl_2 | Probes.Rh_3; /* select Rl for probe-2 & Rh for probe-3 */
R_PORT = Probes.Rh_3; /* pull up base via Rh */
U_R_e = ReadU_5ms(Probes.Ch_2); /* U_R_e = U_e */
U_R_b = Cfg.Vcc - ReadU(Probes.Ch_3); /* U_R_b = Vcc - U_b */
#ifdef SW_HFE_CURRENT
I_e = (int32_t)U_R_e; /* U_R_e (mV) */
#endif
Ri = NV.RiL; /* get internal resistance */
}
else /* PNP */
{
/* we assume: probe-1 = E / probe-2 = C / probe-3 = B */
/* set probes: Gnd -- probe-2 / probe-1 -- Rl -- Vcc / Gnd -- Rh -- probe-3 */
ADC_PORT = 0; /* set ADC port low */
ADC_DDR = Probes.Pin_2; /* pull down collector directly */
R_DDR = Probes.Rl_1 | Probes.Rh_3; /* pull down base via Rh */
R_PORT = Probes.Rl_1; /* pull up emitter via Rl */
U_R_e = Cfg.Vcc - ReadU_5ms(Probes.Ch_1); /* U_R_e = Vcc - U_e */
U_R_b = ReadU(Probes.Ch_3); /* U_R_b = U_b */
#ifdef SW_HFE_CURRENT
I_e = -(int32_t)U_R_e; /* negative U_R_e (mV) */
#endif
Ri = NV.RiH; /* get internal resistance */
}
/*
* Since I_b is so small vs. I_e we'll neglect it and use
* hFE = I_e / I_b
* = (U_R_e / R_e) / (U_R_b / R_b)
* = (U_R_e * R_b) / (U_R_b * R_e)
*/
if (U_R_b == 0) U_R_b = 1; /* prevent division by zero */
hFE = U_R_e * R_HIGH; /* U_R_e * R_b */
hFE /= U_R_b; /* / U_R_b */
hFE *= 10; /* upscale to 0.1 */
Ri += (R_LOW * 10); /* R_e = Ri + Rl (in 0.1 Ohms) */
hFE /= Ri; /* / R_e in 0.1 Ohms */
#ifdef SW_HFE_CURRENT
/* calculate emitter current: I_e = U_R_e / (Rl + Ri) */
/* NPN: Ri = RiL / PNP: Ri = RiH */
I_e *= 10000; /* scale to 0.1 µV (for µA and Ri's 0.1 Ohms */
I_e /= Ri; /* / (Rl + Ri) */
Semi.U_2 = (int16_t)I_e; /* save I_e (in µA) */
#endif
#endif
return hFE;
}
/*
* check for BJT, enhancement-mode MOSFET and IGBT
* - sets hFE test circuit type in Semi.Flags (when SW_HFE_CIRCUIT)
*
* requires:
* - BJT_Type: NPN or PNP (also used for FET channel type)
* - U_Rl: voltage across Rl pulled down
*/
void CheckTransistor(uint8_t BJT_Type, uint16_t U_Rl)
{
uint8_t FET_Type; /* MOSFET type */
uint8_t Flag = 0; /* flag */
uint16_t U_R_c; /* voltage across collector resistor */
uint16_t U_R_b; /* voltage across base resistor */
uint16_t BJT_Level; /* voltage threshold for BJT */
uint16_t FET_Level; /* voltage threshold for FET */
uint16_t Ri; /* internal resistance of MCU */
uint32_t hFE_C; /* hFE (common collector) */
uint32_t hFE_E; /* hFE (common emitter) */
#ifdef SW_HFE_CURRENT
int32_t I_c; /* collector current */
#endif
/*
* init, set probes and measure
*/
BackupProbes(); /* save current probe IDs */
if (BJT_Type == TYPE_NPN) /* NPN / n-channel */
{
BJT_Level = 2557; /* voltage across base resistor (5.44µA) */
FET_Level = 3400; /* voltage across drain resistor (4.8mA) */
FET_Type = TYPE_N_CHANNEL;
/*
* we assume
* - BJT: probe-1 = C / probe-2 = E / probe-3 = B
* - FET: probe-1 = D / probe-2 = S / probe-3 = G
* probes already set to:
* Gnd -- probe-2 / probe-1 -- Rl -- Vcc / (probe-3 -- Rl -- Vcc)
* drive base/gate via Rh instead of Rl
*/
R_DDR = Probes.Rl_1 | Probes.Rh_3; /* enable Rl for probe-1 & Rh for probe-3 */
R_PORT = Probes.Rl_1 | Probes.Rh_3; /* pull up collector via Rl and base via Rh */
wait50ms(); /* wait to skip gate charging of a FET */
U_R_c = Cfg.Vcc - ReadU(Probes.Ch_1); /* U_R_c = Vcc - U_c */
U_R_b = Cfg.Vcc - ReadU(Probes.Ch_3); /* U_R_b = Vcc - U_b */
#ifdef SW_HFE_CURRENT
I_c = (int32_t)U_R_c; /* U_R_c (mV) */
#endif
Ri = NV.RiH; /* RiH (0.1 Ohms) */
}
else /* PNP / p-channel */
{
BJT_Level = 977; /* voltage across base resistor (2.1µA) */
FET_Level = 2000; /* voltage across drain resistor (2.8mA) */
FET_Type = TYPE_P_CHANNEL;
/*
* we assume
* - BJT: probe-1 = E / probe-2 = C / probe-3 = B
* - FET: probe-1 = S / probe-2 = D / probe-3 = G
* probes already set to:
* Gnd -- Rl - probe-2 / probe-1 -- Vcc / (probe-3 -- Rl -- Gnd)
* drive base/gate via Rh instead of Rl
*/
R_DDR = Probes.Rl_2 | Probes.Rh_3; /* pull down base via Rh */
U_R_c = ReadU_5ms(Probes.Ch_2); /* U_R_c = U_c */
U_R_b = ReadU(Probes.Ch_3); /* U_R_b = U_b */
#ifdef SW_HFE_CURRENT
I_c = -(int32_t)U_R_c; /* negative U_R_c (mV) */
#endif
Ri = NV.RiL; /* RiL (0.1 Ohms) */
}
/*
* distinguish BJT from enhancement-mode MOSFET or IGBT
*/
if (U_R_b > BJT_Level) /* U_R_b exceeds minimum level of BJT */
{
/*
* A voltage drop across the base resistor Rh means that a current
* is flowing constantly. So this can't be a FET.
*
* Problem: