-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcap.c
1912 lines (1531 loc) · 62.6 KB
/
cap.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
/* ************************************************************************
*
* capacitor measurements
*
* (c) 2012-2023 by Markus Reschke
* based on code from Markus Frejek and Karl-Heinz Kübbeler
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define CAP_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 */
/* ************************************************************************
* ESR measurement
* ************************************************************************ */
#ifdef SW_ESR
/*
* set up timer for delay
* - uses Timer0 as MCU cycle timer
*
* requires:
* - number of MCU cycles
*
* returns:
* - 1 on success
* - 0 on error
*/
uint8_t SetUpDelayTimer(uint8_t Cycles)
{
uint8_t Flag = 0; /* return value */
/*
* adjust cycles for DelayTimer():
* - -4 for function call
* - -3 for starting timer
* - -2 for waiting loop
* - -4 for stopping timer
* - -4 for return
* - total: 17
*/
#define CYCLE_OFFSET 17
if (Cycles < CYCLE_OFFSET) return Flag; /* check for required minimum */
Cycles -= CYCLE_OFFSET; /* substract cycles for DelayTimer() */
#undef CYCLE_OFFSET
/*
* set up Timer0:
* - CTC mode (count up to OCR0A)
* - prescaler 1 to match MCU cycles
*/
TCCR0B = 0; /* stop timer */
TCCR0A = (1 << WGM01); /* set CTC mode, disable output compare pins */
OCR0A = Cycles; /* set number of MCU cycles */
/* todo: check if we have to substract one cycle for setting the flag */
Flag = 1; /* signal success */
return Flag;
}
/*
* start timer and wait for timeout
* (requires prior call of SetUpPulseTimer() for setup)
*/
void DelayTimer(void)
{
TCNT0 = 0; /* reset counter to 0 */
TCCR0B = (1 << CS00); /* start timer by setting prescaler */
while (!(TIFR0 & (1 << OCF0A))); /* wait for output compare A match flag */
TCCR0B = 0; /* stop timer */
TIFR0 = (1 << OCF0A); /* clear flag */
}
/*
* measure ESR
* - tolerates charge up to about 130mV
*
* requires:
* - pointer to cap data structure
*
* returns:
* - ESR in 0.01 Ohm
* - UINT16_MAX on any problem
*/
uint16_t MeasureESR(Capacitor_Type *Cap)
{
uint16_t ESR = UINT16_MAX; /* return value */
uint16_t U_1; /* voltage at probe 1 with pos. pulse unloaded */
uint16_t U_2; /* voltage at probe 2 with pos. pulse loaded */
uint16_t U_3; /* voltage at probe 2 with neg. pulse unloaded */
uint16_t U_4; /* voltage at probe 1 with neg. pulse loaded */
uint8_t Probe1; /* probe #1 */
uint8_t Probe2; /* probe #2 */
uint8_t Bits; /* register bits for ADC */
uint8_t n; /* counter */
uint32_t Sum_1; /* sum #1 */
uint32_t Sum_2; /* sum #2 */
uint32_t Value;
/* check for a capacitor >= 10nF */
if ((Cap == NULL) ||
(CmpValue(Cap->Value, Cap->Scale, 10, -9) < 0)) return ESR;
/*
* Hint:
* - When we would use MilliSleep() we'd have to change the MCU
* sleep mode to idle to keep the timer running in sleep mode.
*/
/*
* init stuff
*/
DischargeProbes(); /* try to discharge probes */
if (Check.Found == COMP_ERROR) return ESR; /* skip on error */
/* Some testers need additional discharging to lower the cap's residual
voltage to a reasonable level. */
DischargeCap(Cap->A, Cap->B); /* additional discharge */
UpdateProbes2(Cap->A, Cap->B); /* update probes */
Probe1 = Probes.Ch_1; /* ADC MUX for probe-1 */
Probe2 = Probes.Ch_2; /* ADC MUX for probe-2 */
Probe1 |= ADC_REF_BANDGAP; /* select bandgap reference */
Probe2 |= ADC_REF_BANDGAP; /* select bandgap reference */
/* register bits to enable and start ADC */
Bits = (1 << ADSC) | (1 << ADEN) | (1 << ADIF) | ADC_CLOCK_DIV;
/* init variables */
Sum_1 = 1; /* 1 to prevent division by zero */
Sum_2 = 1; /* 1 to prevent division by zero */
/*
* We have to create a delay to shift the middle of the current pulse to
* the ADC's S&H. S&H happens at 1.5 ADC clock cycles after starting the
* conversion. We synchronize to a dummy conversion done directly before,
* so we have 2.5 ADC clock cycles to S&H. The required delay between the
* dummy conversion and S&H of the next conversion is:
* MCU cycles for 2.5 ADC clock cycles
* - MCU cycles for waiting loop for completion of dummy conversion (4)
* - MCU cycles for starting next conversion (2)
* - MCU cycles for fixed pre-delay of 10µs
* - MCU cycles for enabling pulse (4)
* - MCU cycles for half-pulse (2µs)
*
* MCU ADC MCU cycles for
* clock clock 2.5 ADC cycles pre-delay half-pulse delay
* ---------------------------------------------------------------
* 8 MHz 125 kHz 160 (20µs) 80 (10µs) 16 (2µs) 54
* 32 (4µs) 38
* 16 MHz 125 kHz 320 (20µs) 160 (10µs) 32 (2µs) 118
* 64 (4µs) 86
* 20 MHz 156.25 320 (16µs) 200 (10µs) 40 (2µs) 70
* 80 (4µs) 30
*
* Skipping the second half-pulse allows us to measure low value caps too.
*/
/* delay for pulse */
/* MCU cycles for one ADC cycle * 2.5 - MCU cycles for 10µs
- MCU cycles for half-pulse - 10 */
U_1 = ((MCU_CYCLES_PER_ADC * 25) / 10) - (MCU_CYCLES_PER_US * 10)
- (MCU_CYCLES_PER_US * 2) - 10;
#if CPU_FREQ == 8000000
/* magic time shift to compensate missing second half-pulse */
U_1 -= 4;
#endif
n = (uint8_t)U_1;
/* set up delay timer */
if (SetUpDelayTimer(n) == 0) return ESR; /* skip on error */
/*
* measurement loop:
* - simulate AC by short positive and negative pulses
* - measure start voltage without DUT
* - measure pulse voltage with DUT
* - pre-charge DUT with a negative pulse of half length to
* to compensate voltage rise by positive charging pulse
* - 16 & 20 MHz MCUs seem to measure higher ESR values
*/
ADC_PORT = 0; /* set ADC port to low */
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
wait10ms(); /* time for voltage stabilization */
U_2 = 50; /* don't start with positive half-pulse */
U_4 = 0; /* start with a negative half-pulse */
n = 255; /* set loop counter */
while (n > 0)
{
wdt_reset(); /* reset watchdog */
/*
* mitigate runaway of cap's charge/voltage
*/
if (U_4 < 50) /* voltage too low for last negative pulse */
{
/* charge cap a little bit more: negative pulse */
/* set probes: GND -- probe-2 / probe-1 -- Rl -- Vcc */
ADC_DDR = Probes.Pin_2; /* pull down probe-2 directly */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable pull up */
wait2us(); /* wait half-pulse */
R_DDR = 0; /* disable any pull up */
R_PORT = 0; /* reset probe resistors */
}
if (U_2 < 50) /* voltage too low for last positive pulse */
{
/* charge cap a little bit more: positive pulse */
/* set probes: GND -- probe-1 / probe-2 -- Rl -- Vcc */
ADC_DDR = Probes.Pin_1; /* pull down probe-1 directly */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable pull up */
wait2us(); /* wait half-pulse */
R_DDR = 0; /* disable any pull up */
R_PORT = 0; /* reset probe resistors */
}
/*
* forward mode, probe-1 only (probe-2 in HiZ mode)
* set probes: GND -- probe-1 -- Rl -- Vcc / probe-2 -- HiZ
* get voltage at probe-1 (voltage at RiL)
*/
ADC_DDR = Probes.Pin_1; /* pull down probe-1 directly to GND */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable resistor */
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* real conversion */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_1 = ADCW; /* save ADC value */
/*
* forward mode, positive charging pulse
* set probes: GND -- probe-1 / probe-2 -- Rl -- Vcc
* get voltage at probe-2 (voltage at DUT, i.e. RiL + ESR)
*/
ADMUX = Probe2; /* set input channel to probe-2 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* read ADC in the mid of a positive charging pulse */
ADCSRA = Bits; /* start conversion with next ADC clock cycle */
wait10us(); /* fixed pre-delay */
DelayTimer(); /* delay for pulse */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable resistor */
wait2us(); /* first half-pulse */
/* S/H happens here */
#if CPU_FREQ < 8000000
wait2us(); /* second half-pulse */
#endif
// R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_2 = ADCW; /* save ADC value */
/*
* reverse mode, probe-2 only (probe-1 in HiZ mode)
* set probes: GND -- probe-2 -- Rl -- Vcc / probe-1 -- HiZ
* get voltage at probe-2 (voltage at RiL)
*/
ADC_DDR = Probes.Pin_2; /* pull down probe-2 directly */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable resistor */
ADMUX = Probe2; /* set input channel to probe-2 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* real conversion */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_3 = ADCW; /* save ADC value */
/*
* reverse mode, negative charging pulse
* set probes: GND -- probe-2 / probe-1 -- Rl -- Vcc
* get voltage at probe-1 (voltage at DUT, i.e. RiL + ESR)
*/
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* read ADC in the mid of a negatve charging pulse */
ADCSRA = Bits; /* start conversion with next ADC clock cycle */
wait10us(); /* fixed pre-delay */
DelayTimer(); /* delay for pulse */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable resistor */
wait2us(); /* first half-pulse */
/* S/H happens here */
#if CPU_FREQ < 8000000
wait2us(); /* second half-pulse */
#endif
// R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_4 = ADCW; /* save ADC value */
/*
* manage measured values
*/
Sum_1 += U_1; /* positive pulse without DUT */
Sum_1 += U_3; /* negative pulse without DUT */
Sum_2 += U_2; /* positive pulse with DUT */
Sum_2 += U_4; /* negative pulse with DUT */
n--; /* next loop run */
}
/* probes: reset to safe mode */
ADC_DDR = 0;
R_PORT = 0;
/*
* process measurements
*/
if (Sum_2 > Sum_1) /* valid measurement */
{
/*
* calculate ESR
* - ESR = U_ESR / I_ESR
* with U_ESR = (U2 or U4) and I_ESR = (U1 or U3) / RiL
* ESR = (U2 or U4) * RiL / (U1 or U3)
* - since we divide (U2 or U4) by (U1 or U3), we don't need to convert
* the ADC value into a voltage and simply desample the sums.
* - so ESR = Sum_2 * RiL / Sum_1
* - for a resolution of 0.01 Ohms we have to scale RiL to 0.01 Ohms
*/
/* voltage across the DUT (raw value) */
Sum_2 -= Sum_1; /* subtract voltage at DUT's low side (RiL) */
/* ESR = Sum_2 * RiL / Sum_1 */
Value = (uint32_t)(NV.RiL * 10); /* RiL in 0.01 Ohms */
Value *= Sum_2; /* sum of raw values for voltage across DUT */
Value /= Sum_1; /* sum of raw values for voltage at RiL */
U_1 = (uint16_t)Value; /* raw ESR (0.01 Ohms) */
/* consider probe resistance */
#ifdef R_MULTIOFFSET
/* get index number for probe pair */
n = GetOffsetIndex(Probes.ID_1, Probes.ID_2);
U_2 = NV.RZero[n];
#else
U_2 = NV.RZero;
#endif
if (U_1 > U_2) /* larger than offset */
{
U_1 -= U_2; /* subtract offset */
ESR = U_1; /* got result */
}
else /* offset problem or zero */
{
/* should only happen for large caps (>1000µF) */
if (CmpValue(Cap->Value, Cap->Scale, 1000, -6) > 0)
{
ESR = 0; /* can't be less than 0 Ohms */
}
}
}
/*
* clean up
*/
/* update reference source for next ADC run */
Cfg.Ref = ADC_REF_BANDGAP; /* we've used the bandgap reference */
DischargeProbes(); /* discharge DUT */
return ESR;
}
#endif
#ifdef SW_OLD_ESR
/*
* set up Timer0 as MCU cycle timer
*
* requires:
* - number of MCU cycles
*
* returns:
* - 1 on success
* - 0 on error
*/
uint8_t SetupDelayTimer(uint8_t Cycles)
{
uint8_t Flag = 0; /* return value */
/*
* adjust cycles for DelayTimer():
* - -4 for function call
* - -3 for starting timer
* - -2 for waiting loop
* - -4 for stopping timer
* - -4 for return
* - total: 17
*/
#define CYCLE_OFFSET 17
if (Cycles < CYCLE_OFFSET) return Flag; /* check for required minimum */
Cycles -= CYCLE_OFFSET; /* substract cycles for DelayTimer() */
#undef CYCLE_OFFSET
/*
* set up timer0:
* - CTC mode (count up to OCR0A)
* - prescaler 1 to match MCU cycles
*/
TCCR0B = 0; /* stop timer */
TCCR0A = (1 << WGM01); /* set CTC mode, disable output compare pins */
OCR0A = Cycles; /* set number of MCU cycles */
/* todo: check if we have to substract one cycle for setting the flag */
Flag = 1; /* signal success */
return Flag;
}
/*
* start timer and wait for timeout
* (requires prior call of SetupDelayTimer() for setup)
*/
void DelayTimer(void)
{
TCNT0 = 0; /* reset counter to 0 */
TCCR0B = (1 << CS00); /* start timer by setting prescaler */
while (!(TIFR0 & (1 << OCF0A))); /* wait for output compare A match flag */
TCCR0B = 0; /* stop timer */
TIFR0 = (1 << OCF0A); /* clear flag */
}
/*
* measure ESR
* - tolerates charge up to about 130mV
*
* requires:
* - pointer to cap data structure
*
* returns:
* - ESR in 0.01 Ohm
* - UINT16_MAX on any problem
*/
uint16_t MeasureESR(Capacitor_Type *Cap)
{
uint16_t ESR = UINT16_MAX; /* return value */
uint16_t U_1; /* voltage at probe 1 with pos. pulse unloaded */
uint16_t U_2; /* voltage at probe 2 with pos. pulse loaded */
uint16_t U_3; /* voltage at probe 2 with neg. pulse unloaded */
uint16_t U_4; /* voltage at probe 1 with neg. pulse loaded */
uint8_t Probe1; /* probe #1 */
uint8_t Probe2; /* probe #2 */
uint8_t Bits; /* register bits for ADC */
uint8_t n; /* counter */
uint32_t Sum_1; /* sum #1 */
uint32_t Sum_2; /* sum #2 */
uint32_t Value;
/* check for a capacitor >= 0.18µF */
if ((Cap == NULL) ||
(CmpValue(Cap->Value, Cap->Scale, 180, -9) < 0)) return ESR;
/*
* init stuff
*/
DischargeProbes(); /* try to discharge probes */
if (Check.Found == COMP_ERROR) return ESR; /* skip on error */
DischargeCap(Cap->A, Cap->B); /* additional discharge */
UpdateProbes2(Cap->A, Cap->B); /* update probes */
Probe1 = Probes.Ch_1; /* ADC MUX for probe-1 */
Probe2 = Probes.Ch_2; /* ADC MUX for probe-2 */
Probe1 |= ADC_REF_BANDGAP; /* select bandgap reference */
Probe2 |= ADC_REF_BANDGAP; /* select bandgap reference */
/* register bits to enable and start ADC */
Bits = (1 << ADSC) | (1 << ADEN) | (1 << ADIF) | ADC_CLOCK_DIV;
/* init variables */
Sum_1 = 1; /* 1 to prevent division by zero */
Sum_2 = 1; /* 1 to prevent division by zero */
/*
* We have to create a delay to shift the middle of the pulse to the ADC's
* S&H. S&H happens at 1.5 ADC clock cycles after starting the conversion.
* We synchronize to a dummy conversion done directly before, so we have
* 2.5 ADC clock cycles to S&H. The time between the completed dummy
* conversion and S&H of the next conversion is:
* 2.5 ADC clock cycles
* - MCU cycles for waiting loop for completion of dummy conversion (4)
* - MCU cycles for starting next conversion (2)
* - 5µs delay
* - MCU cycles for enabling pulse (4)
*
* That time is the first half of the puls. So we have to double the time
* for a full pulse. Half pulse for 8MHz MCU clock is about 13.5µs.
*/
/* delay for pulse */
/* MCU cycles for one ADC cycle * 2.5 - MCU cycles for 5µs - 10 */
U_1 = ((MCU_CYCLES_PER_ADC * 25) / 10) - (MCU_CYCLES_PER_US * 5) - 10;
n = (uint8_t)U_1;
/* set up delay timer */
if (SetupDelayTimer(n) == 0) return ESR; /* skip on error */
/*
* charge capacitor with a negative pulse of half length
* pulse: GND -- probe-2 / probe-1 -- Rl -- Vcc
*/
ADC_PORT = 0; /* set ADC port to low */
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
wait10ms(); /* time for voltage stabilization */
ADC_DDR = Probes.Pin_2; /* pull down probe-2 directly */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable resistor */
DelayTimer(); /* wait 1/2 pulse */
R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
/*
* measurement loop:
* - simulate AC by positive and negative pulses
* - measure start voltage (no load)
* - measure pulse voltage (with load)
*/
n = 255;
while (n > 0)
{
/*
* forward mode, probe-1 only (probe-2 in HiZ mode)
* get voltage at probe-1 (facing Gnd)
* set probes: GND -- probe-1 -- Rl -- Vcc / probe-2 -- HiZ
*/
ADC_DDR = Probes.Pin_1; /* pull down probe-1 directly to GND */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable resistor */
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
wdt_reset(); /* reset watchdog */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* real conversion */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_1 = ADCW; /* save ADC value */
/*
* forward mode, positive charging pulse
* get voltage at probe-2 (facing Vcc)
* set probes: GND -- probe-1 / probe-2 -- Rl -- Vcc
*/
ADMUX = Probe2; /* set input channel to probe-2 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* read ADC in the mid of a positive charging pulse */
ADCSRA = Bits; /* start conversion with next ADC clock cycle */
wait5us();
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable resistor */
DelayTimer(); /* wait 1/2 pulse */
DelayTimer(); /* wait another 1/2 pulse */
R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_2 = ADCW; /* save ADC value */
/*
* prevent runaway of cap's charge
*/
if (U_2 <= 100)
{
/* charge cap a little bit more (positive pulse) */
/* set probes: GND -- probe-1 / probe-2 -- Rl -- Vcc */
/* probe-1 is still pulled down directly */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable pull up */
wait2us();
DelayTimer(); /* wait 1/2 pulse */
R_DDR = 0; /* disable any pull up */
R_PORT = 0; /* reset probe resistors */
}
/*
* reverse mode, probe-2 only (probe-1 in HiZ mode)
* get voltage at probe 2 (facing Gnd)
* set probes: GND -- probe-2 -- Rl -- Vcc / probe-1 -- HiZ
*/
ADC_DDR = Probes.Pin_2; /* pull down probe-2 directly */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable resistor */
ADMUX = Probe2; /* set input channel to probe-2 & set bandgap ref */
wdt_reset(); /* reset watchdog */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* real conversion */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_3 = ADCW; /* save ADC value */
/*
* reverse mode, negative charging pulse
* get voltage at probe-1 (facing Vcc)
* set probes: GND -- probe-2 / probe-1 -- Rl -- Vcc
*/
ADMUX = Probe1; /* set input channel to probe-1 & set bandgap ref */
/* run dummy conversion for ADMUX change */
ADCSRA = Bits; /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/* read ADC in the mid of a negatve charging pulse */
ADCSRA = Bits; /* start conversion with next ADC clock cycle */
wait5us();
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable resistor */
DelayTimer(); /* wait 1/2 pulse */
DelayTimer(); /* wait another 1/2 pulse */
R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
U_4 = ADCW; /* save ADC value */
/*
* prevent runaway of cap's charge
*/
if (U_4 <= 100) /* <= 107mV */
{
/* charge cap a little bit more (negative pulse) */
/* set probes: GND -- probe-2 / probe-1 -- Rl -- Vcc */
/* probe-2 is still pulled down directly */
R_PORT = Probes.Rl_1; /* pull up probe-1 via Rl */
R_DDR = Probes.Rl_1; /* enable pull up */
wait2us();
DelayTimer(); /* wait 1/2 pulse */
R_DDR = 0; /* disable any pull up */
R_PORT = 0; /* reset probe resistors */
}
/*
* manage measured values
*/
U_1 += U_3; /* sum of both measurements without pulses/load */
Sum_1 += U_1; /* add to total no-load sum */
U_2 += U_4; /* sum of both measurements with pulses/load */
Sum_2 += U_2; /* add to total with-load sum */
n--; /* next loop run */
}
/*
* process measurements
*/
/* calculate voltage across the DUT */
if (Sum_2 > Sum_1) /* valid measurement */
{
Sum_2 -= Sum_1; /* subtract voltage at DUT's low side (RiL) */
}
else /* invalid measurement */
{
Sum_2 = 0;
}
/*
* calculate ESR
* - ESR = U_ESR / I_ESR
* with U_ESR = (U2 or U4) and I_ESR = (U1 or U3) / RiL
* ESR = (U2 or U4) * RiL / (U1 or U3)
* - since we divide (U2 or U4) by (U1 or U3), we don't need to convert
* the ADC value into a voltage and simply desample the sums.
* - so ESR = Sum_2 * RiL / Sum_1
* - for a resolution of 0.01 Ohms we have to scale RiL to 0.01 Ohms
*/
Value = (uint32_t)(NV.RiL * 10); /* RiL in 0.01 Ohms */
Value *= Sum_2; /* sum of raw values for voltage across DUT */
Value /= Sum_1; /* sum of raw values for voltage at RiL */
U_1 = (uint16_t)Value; /* raw ESR (0.01 Ohms) */
/* consider probe resistance */
#ifdef R_MULTIOFFSET
/* get index number for probe pair */
n = GetOffsetIndex(Probes.ID_1, Probes.ID_2);
U_2 = NV.RZero[n];
#else
U_2 = NV.RZero;
#endif
if (U_1 > U_2) /* larger than offset */
{
U_1 -= U_2; /* subtract offset */
ESR = U_1; /* we got a valid result */
}
/*
* clean up
*/
/* update reference source for next ADC run */
Cfg.Ref = ADC_REF_BANDGAP; /* we've used the bandgap reference */
DischargeProbes(); /* discharge DUT */
return ESR;
}
#endif
/* ************************************************************************
* capacitance measurements
* ************************************************************************ */
/*
We measure the capacitance by measuring the time needed to charge up the DUT
to a specific voltage using a constant voltage source:
U_c(t) = U_in * (1 - e^(-t/RC))
With ln(e^x) = x we get
C = -t / (R * ln(1 - U_c/U_in)
for an ideal capacitor whithout parallel resistive losses by leakage.
Instead of calculating C directly we'll use pre-calculated tables to speed
up things and keep the firmware small. The tables hold the pre-calculated
values of -1/(R * ln(1 - U_c/U_in) for a specific range of U_c, so we just
have to multiply the time with that stored factor to get C.
Large caps:
- R = 680 + 22 (22 is the internal resistance of the MCU for pull-up)
- U_in = 5V
- values are: (-1 / (R * ln(1 - U_c/U_in))) * 10^9n * 10^-2s * 10^-1
- 10^9n for nF scale
- 10^-2s for charge pulses of 10ms each
- 10^-1 internal scale factor (make values fit in uint16_t)
- bc:
- options: -i -l
- define x (u) { return (-1000000 / (702 * l(1 - u/5000))); }
Small caps:
- R = 470k (neglect internal resistance of uC)
- U_in = 5V
- values are: (-1 / (R * ln(1 - U_c/U_in))) * 10^12p * 10^-4
- 10^12p for pF scale
- 10^-4 internal scale factor (make values fit in uint16_t)
- bc:
- options: -i -l
- define x (u) { return (-100000000 / (470000 * l(1 - u/5000))); }
- We could use 10^-3 as internal scale factor to maximize resolution.
*/
/*
* measure cap >4.7µF between two probe pins
*
* requires:
* - Cap: pointer to capacitor data structure
*
* returns:
* - 3 on success
* - 2 if capacitance is too low
* - 1 if capacitance is too high
* - 0 on any problem
*/
uint8_t LargeCap(Capacitor_Type *Cap)
{
uint8_t Flag = 3; /* return value */
uint8_t TempByte; /* temp. value */
uint8_t Mode; /* measurement mode */
int8_t Scale; /* capacitance scale */
uint16_t TempInt; /* temp. value */
uint16_t Pulses; /* number of charging pulses */
int16_t U_Zero; /* voltage before charging (zero offset) */
int16_t U_temp; /* temporary voltage */
uint16_t U_Cap; /* voltage of DUT */
uint16_t U_Drop = 0; /* voltage drop (self-discharge) */
uint16_t U_leak = 0; /* voltage drop (leakage current) */
uint32_t Raw; /* raw capacitance value */
uint32_t Value; /* corrected capacitance value */
/* set up mode */
Mode = PULL_10MS | PULL_UP; /* start with large cap (>47uF) */
/*
* We charge the DUT with up to 500 pulses each 10ms long until the
* DUT reaches 300mV. The charging is done via Rl. This method is
* suitable for large capacitances from 47uF up to 100mF. If we find a
* lower capacitance we'll switch to 1ms charging pulses and try again
* (4.7µF up to 47µF).
*
* Problem:
* ReadADC() needs about 5ms (44 runs). We charge the DUT for 10ms and
* measure for 5ms. During that time the voltage will drop due to
* resistive losses of the DUT and the measurement itself. So the DUT
* seems to need more time to reach 300mV causing a higher capacitance
* be calculated.
*
* Remark:
* The Analog Input Resistance of the ADC is 100MOhm typically.
*/
large_cap:
/* prepare probes */
DischargeProbes(); /* try to discharge probes */
if (Check.Found == COMP_ERROR) return 0; /* skip on error */
/*
* get zero offset (noise / dielectric absorption)
* - create reference point with a low positive voltage to be able to
* measure also a low negative offset
* - use voltage divider: top RiH + Rl, bottom RiL (about 140mV)
*/
/* set probes: Gnd -- probe-2 -- Rl - Vcc / probe-1 -- HiZ */
ADC_PORT = 0; /* set ADC port to low */
ADC_DDR = Probes.Pin_2; /* pull down probe-2 directly */
R_PORT = Probes.Rl_2; /* pull up probe-2 via Rl */
R_DDR = Probes.Rl_2; /* enable pull-up */
U_Zero = ReadU(Probes.Ch_1); /* get voltage at probe-1 */
U_Zero -= ReadU(Probes.Ch_2); /* - voltage at probe-2 */
/* set probes: Gnd -- probe-2 / probe-1 -- HiZ */
R_PORT = 0; /* set resistor port to low */
R_DDR = 0; /* set resistor port to HiZ */
/* charge DUT with up to 500 pulses until it reaches 300mV */
/* pulse: probe-1 -- Rl -- Vcc */
Pulses = 0; /* reset number of pulses */
TempByte = 1; /* set loop control */
while (TempByte) /* charge loop */
{
Pulses++;
PullProbe(Probes.Rl_1, Mode); /* charging pulse */
U_Cap = ReadU(Probes.Ch_1); /* get voltage */
/* consider zero offset */
U_temp = (int16_t)U_Cap; /* explicit type conversion */
if (U_temp > U_Zero) /* voltage higher than zero offset */
U_temp -= U_Zero; /* subtract zero offset */
else /* shouldn't happen but you never know */
U_temp = 0; /* assume 0V */
U_Cap = (uint16_t)U_temp; /* take result */
/* end loop if charging is too slow */
if ((Pulses == 126) && (U_Cap < 75)) TempByte = 0;
/* end loop if 300mV are reached (cap charged) */
if (U_Cap >= 300) TempByte = 0;
/* end loop if maximum number of pulses is reached (timeout) */
if (Pulses == 500) TempByte = 0;
wdt_reset(); /* reset watchdog */
}
/* if 300mV are not reached DUT isn't a cap or much too large (>100mF) */
/* we can ignore that for mid-sized caps */
if (U_Cap < 300)
{
Flag = 1; /* signal too high capacitance */
}
/* if 1300mV are reached with one pulse, we got a small cap */
if ((Pulses == 1) && (U_Cap > 1300))
{
if (Mode & PULL_10MS) /* 10ms pulses (>47µF) */
{
/* change to smaller cap (4.7 - 47µF) */
Mode = PULL_1MS | PULL_UP; /* set mode to 1ms charging pulses */
goto large_cap; /* and re-run */
}
else /* 1ms pulses (<47µF) */
{
Flag = 2; /* signal low capacitance (<4.7µF) */
}
}
/*
* Check if DUT sustains the charge and get the voltage drop.