-
Notifications
You must be signed in to change notification settings - Fork 6
/
tools_counter.c
1715 lines (1393 loc) · 53.8 KB
/
tools_counter.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
/* ************************************************************************
*
* counter tools (hardware and software options)
*
* (c) 2012-2023 by Markus Reschke
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define TOOLS_COUNTER_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 */
/*
* local variables
*/
/* pulse counter */
#if defined (HW_FREQ_COUNTER_EXT) || defined (HW_EVENT_COUNTER) || defined (HW_LC_METER)
volatile uint32_t Pulses; /* number of pulses */
#elif defined (HW_FREQ_COUNTER_BASIC) || defined (HW_RING_TESTER)
volatile uint16_t Pulses; /* number of pulses */
#endif
/* time counter */
#ifdef HW_EVENT_COUNTER
volatile uint8_t TimeTicks; /* tick counter */
volatile uint16_t TimeCounter; /* time counter */
#endif
/* ************************************************************************
* shared ISRs for counter tools
* - also used by Get_LC_Frequency() in tools_LC_Meter.c
* ************************************************************************ */
#if defined (HW_FREQ_COUNTER_BASIC) || defined (HW_FREQ_COUNTER_EXT) || defined (HW_EVENT_COUNTER) || defined (HW_LC_METER) || defined (HW_RING_TESTER)
/*
* ISR for overflow of Timer0
* - catch overflows of pulse counter
*/
ISR(TIMER0_OVF_vect, ISR_BLOCK)
{
/*
* hints:
* - the TOV0 interrupt flag is cleared automatically
* - interrupt processing is disabled while this ISR runs
* (no nested interrupts)
*/
Pulses += 256; /* add overflow to global counter */
}
#endif
#if defined (HW_FREQ_COUNTER_BASIC) || defined (HW_FREQ_COUNTER_EXT) || defined (HW_LC_METER) || defined (HW_RING_TESTER)
/*
* ISR for match of Timer1's OCR1A (Output Compare Register A)
* - for gate time of frequency counter
*/
ISR(TIMER1_COMPA_vect, ISR_BLOCK)
{
/*
* hints:
* - the OCF1A interrupt flag is cleared automatically
* - interrupt processing is disabled while this ISR runs
* (no nested interrupts)
*/
/* gate time has passed */
TCCR1B = 0; /* disable Timer1 */
TCCR0B = 0; /* disable Timer0 */
/* break TestKey() processing */
Cfg.OP_Control |= OP_BREAK_KEY; /* set break signal */
}
#endif
/* ************************************************************************
* simple frequency counter
* ************************************************************************ */
#ifdef HW_FREQ_COUNTER_BASIC
/*
* basic frequency counter
* - frequency input: T0
* - requires idle sleep mode to keep timers running when MCU is sleeping
*/
void FrequencyCounter(void)
{
uint8_t Flag; /* loop control flag */
uint8_t Test; /* user feedback */
uint8_t Old_DDR; /* old DDR state */
uint8_t Index; /* prescaler table index */
uint8_t Bits; /* prescaler register bits */
uint16_t GateTime; /* gate time in ms */
uint16_t Top; /* top value for timer */
uint32_t Value; /* temporary value */
/* local constants for Flag */
#define RUN_FLAG 1 /* run flag */
#define WAIT_FLAG 2 /* enter/run waiting loop */
#define GATE_FLAG 3 /* gatetime flag */
#define SHOW_FREQ 4 /* display frequency */
/* show info */
LCD_Clear(); /* clear display */
#ifdef UI_COLORED_TITLES
/* display: Freq. Counter */
Display_ColoredEEString(FreqCounter_str, COLOR_TITLE);
#else
Display_EEString(FreqCounter_str); /* display: Freq. Counter */
#endif
/*
* We use Timer1 for the gate time and Timer0 to count pulses of the
* unknown signal. Max. frequency for Timer0 is 1/4 of the MCU clock.
*/
Flag = RUN_FLAG; /* enter measurement loop */
/*
auto ranging
Timer1 top value (gate time)
- top = gatetime * MCU_cycles / prescaler
- gate time in µs
- MCU cycles per µs
- top max. 2^16 - 1
Frequency
- f = pulses / gatetime
- pulses = f * gatetime
range gate time prescaler MCU clock pulses
----------------------------------------------------------
<10kHz 1000ms 1024 > 16MHz <10k
1000ms 256 <= 16MHz <10k
10kHz-100kHz 100ms 64 all 1k-10k
>100kHz 10ms 8 all >1k (<50k)
*/
/* start values for autoranging (assuming high frequency) */
GateTime = 10; /* gate time 10ms */
Index = 1; /* prescaler table index (prescaler 8:1) */
/* set up Timer0 (pulse counter) */
TCCR0A = 0; /* normal mode (count up) */
TIFR0 = (1 << TOV0); /* clear overflow flag */
TIMSK0 = (1 << TOIE0); /* enable overflow interrupt */
/* set up Timer1 (gate time) */
TCCR1A = 0; /* normal mode (count up) */
TIFR1 = (1 << OCF1A); /* clear output compare A match flag */
TIMSK1 = (1 << OCIE1A); /* enable output compare A match interrupt */
/*
* measurement loop
*/
while (Flag > 0)
{
/* set up T0 as input (pin might be shared with display) */
Old_DDR = COUNTER_DDR; /* save current settings */
COUNTER_DDR &= ~(1 << COUNTER_IN); /* signal input */
wait500us(); /* settle time */
/* update prescaler */
Top = DATA_read_word(&T1_Prescaler_table[Index]); /* prescaler value */
Bits = DATA_read_byte(&T1_RegBits_table[Index]); /* prescaler bits */
/* calculate compare value for Timer1 (gate time) */
/* top = gatetime * MCU_cycles / timer prescaler */
Value = GateTime; /* gatetime (in ms) */
/* * MCU cycles per µs and scale gatetime to µs */
Value *= (MCU_CYCLES_PER_US * 1000);
Value /= Top; /* divide by timer prescaler */
Top = (uint16_t)Value; /* use lower 16 bit */
/* start timers */
Pulses = 0; /* reset pulse counter */
Flag = WAIT_FLAG; /* enter waiting loop */
TCNT0 = 0; /* Timer0: reset pulse counter */
TCNT1 = 0; /* Timer1: reset gate time counter */
OCR1A = Top; /* Timer1: set gate time */
TCCR1B = Bits; /* start Timer1: prescaler */
TCCR0B = (1 << CS02) | (1 << CS01); /* start Timer0: clock source T0 on falling edge */
/* wait for timer1 or key press */
while (Flag == WAIT_FLAG)
{
if (TCCR1B == 0) /* Timer1 stopped by ISR */
{
Flag = GATE_FLAG; /* end loop and signal Timer1 event */
}
else /* Timer1 still running */
{
/* wait for user feedback */
Test = TestKey(0, CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_TWICE) /* two short key presses */
{
Flag = 0; /* end processing loop */
}
}
}
/* T0 pin might be shared with display */
COUNTER_DDR = Old_DDR; /* restore old settings */
Cfg.OP_Control &= ~OP_BREAK_KEY; /* clear break signal (just in case) */
/*
* process measurement
*/
if (Flag == GATE_FLAG) /* got measurement */
{
/* total sum of pulses during gate period */
Pulses += TCNT0; /* add counter of Timer0 */
/*
* calculate frequency
* - f = pulses / gatetime
* - 20MHz MCU: 5M pulses per second at maximum
* with 10ms gate time max. 50k pulses
*/
Value = Pulses; /* number of pulses */
Value *= 1000; /* scale to ms */
Value /= GateTime; /* divide by gatetime (in ms) */
Flag = SHOW_FREQ; /* display frequency */
/* autoranging */
if (Pulses > 10000) /* range overrun */
{
if (GateTime > 10) /* upper range limit not reached yet */
{
GateTime /= 10; /* 1000ms -> 100ms -> 10ms */
Index--; /* one prescaler step down */
#if CPU_FREQ > 16000000
if (Index == 3) Index--; /* skip 256, use 64 */
#endif
Flag = RUN_FLAG; /* don't display frequency */
}
}
else if (Pulses < 1000) /* range underrun */
{
if (GateTime < 1000) /* lower range limit not reached yet */
{
GateTime *= 10; /* 1ms -> 10ms -> 100ms -> 1000ms */
Index++; /* one prescaler step up */
#if CPU_FREQ > 16000000
if (Index == 3) Index++; /* skip 256, use 1024 */
#endif
Flag = RUN_FLAG; /* don't display frequency */
}
}
/* prevent display of "0 Hz" */
if (Pulses == 0) /* no signal or f too low */
{
Flag = RUN_FLAG; /* don't display frequency */
}
}
/*
* display frequency (in line #2)
*/
LCD_ClearLine2(); /* clear line #2 */
Display_Char('f'); /* display: f */
Display_Space();
if (Flag == SHOW_FREQ) /* valid frequency */
{
Display_Value(Value, 0, 0); /* display frequency */
Display_EEString(Hertz_str); /* display: Hz */
Flag = RUN_FLAG; /* clear flag */
}
else /* invalid frequency */
{
Display_Minus(); /* display: no value */
}
}
/*
* clean up
*/
TIMSK0 = 0; /* disable all interrupts for Timer0 */
TIMSK1 = 0; /* disable all interrupts for Timer1 */
/* local constants for Flag */
#undef RUN_FLAG
#undef WAIT_FLAG
#undef GATE_FLAG
#undef SHOW_FREQ
}
#endif
/* ************************************************************************
* extended frequency counter
* ************************************************************************ */
#ifdef HW_FREQ_COUNTER_EXT
/*
* extended frequency counter
* - uses frontend with buffer, prescaler and crystal oscillators
* - frequency input: T0
* - control signals
* prescaler - COUNTER_CTRL_DIV
* channel addr #0 - COUNTER_CTRL_CH0
* channel addr #1 - COUNTER_CTRL_CH1
* - prescaler
* 0 - 1:1
* 1 - 16:1 (or 32:1)
* - source channel address
* 00 - buffered frequency input
* 01 - unused
* 10 - HF crystal oscillator
* 11 - LF crystal oscillator
* - requires idle sleep mode to keep timers running when MCU is sleeping
*/
void FrequencyCounter(void)
{
uint8_t Flag; /* loop control flag */
uint8_t Test = 0; /* user feedback */
uint8_t InDir; /* input DDR state */
uint8_t CtrlDir; /* control DDR state */
uint8_t Channel; /* input channel */
uint8_t Range; /* range ID */
uint8_t Div = 0; /* frequency prescaler */
uint8_t Index; /* prescaler table index */
uint8_t Bits = 0; /* prescaler register bits */
uint16_t GateTime = 0; /* gate time in ms */
uint16_t Top = 0; /* top value for timer */
unsigned char *String = NULL; /* string pointer (EEPROM) */
uint32_t MinPulses = 0; /* minimim pulses for range */
// uint32_t MaxPulses = 0; /* maximum pulses for range */
uint32_t Value; /* temporary value */
/* local constants for Flag (bitfield) */
#define RUN_FLAG 0b00000001 /* run flag */
#define WAIT_FLAG 0b00000010 /* wait flag */
#define GATE_FLAG 0b00000100 /* gatetime flag */
#define UPDATE_CHANNEL 0b00001000 /* update source channel */
#define UPDATE_RANGE 0b00010000 /* update measurement range */
#define SHOW_FREQ 0b00100000 /* display frequency */
#define RESCAN_FLAG 0b01000000 /* rescan */
#define SKIP_FREQ 0b10000000 /* skip display of f */
/* show info */
LCD_Clear(); /* clear display */
#ifdef UI_COLORED_TITLES
/* display: Freq. Counter */
Display_ColoredEEString(FreqCounter_str, COLOR_TITLE);
#else
Display_EEString(FreqCounter_str); /* display: Freq. Counter */
#endif
/*
* We use Timer1 for the gate time and Timer0 to count pulses of the
* unknown signal. Max. frequency for Timer0 is 1/4 of the MCU clock.
*/
/*
auto ranging
Timer1 top value (gate time)
- top = gatetime * MCU_cycles / timer prescaler
- gate time in µs
- MCU cycles per µs
- top max. 2^16 - 1
Frequency
- f = f-prescaler * pulses / gatetime
- pulses = f * gatetime / f-prescaler
gate timer MCU frequency
range time prescaler clock prescaler pulses
----------------------------------------------------------------
n/a 3000ms 1024 all
<100kHz 1000ms 1024 > 16MHz 1:1 <100k
1000ms 256 <= 16MHz 1:1 <100k
100kHz-1MHz 100ms 64 all 1:1 10k-100k
>1MHz 100ms 64 all 16:1 >6250 (<500k)
100ms 64 all 32:1 >3125 (<500k)
*/
/* set up control lines */
CtrlDir = COUNTER_CTRL_DDR; /* get current direction */
/* set to output mode */
COUNTER_CTRL_DDR |= (1 << COUNTER_CTRL_DIV) | (1 << COUNTER_CTRL_CH0) | (1 << COUNTER_CTRL_CH1);
/* set up Timer0 (pulse counter) */
TCCR0A = 0; /* normal mode (count up) */
TIFR0 = (1 << TOV0); /* clear overflow flag */
TIMSK0 = (1 << TOIE0); /* enable overflow interrupt */
/* set up Timer1 (gate time) */
TCCR1A = 0; /* normal mode (count up) */
TIFR1 = (1 << OCF1A); /* clear output compare A match flag */
TIMSK1 = (1 << OCIE1A); /* enable output compare A match interrupt */
/* set start values */
Channel = 0; /* source channel: ext. frequency */
Range = 2; /* start with highest range */
Flag = RUN_FLAG | UPDATE_CHANNEL | UPDATE_RANGE; /* set control flags */
#ifdef UI_QUARTZ_CRYSTAL
Check.Symbol = SYMBOL_CRYSTAL; /* set symbol ID */
#endif
/*
* processing loop
*/
while (Flag > 0)
{
/*
* set and display source channel
*/
if (Flag & UPDATE_CHANNEL) /* update channel settings */
{
switch (Channel) /* get channel specifics */
{
case 0: /* buffered frequency input */
String = (unsigned char *)FreqInput_str;
Test = 0;
break;
case 1: /* high frequency crystal oscillator */
String = (unsigned char *)HF_Crystal_str;
Test = (1 << COUNTER_CTRL_CH1);
break;
case 2: /* low frequency crystal oscillator */
String = (unsigned char *)LF_Crystal_str;
Test = (1 << COUNTER_CTRL_CH1) | (1 << COUNTER_CTRL_CH0);
break;
}
/* set source channel */
InDir = COUNTER_CTRL_PORT; /* get current state */
InDir &= ~((1 << COUNTER_CTRL_CH1) | (1 << COUNTER_CTRL_CH0)); /* clear channel lines */
InDir |= Test; /* set channel lines */
COUNTER_CTRL_PORT = InDir; /* update port */
/* display source channel (in line #3) */
LCD_ClearLine3(); /* clear line #3 */
Display_EEString(CounterChannel_str); /* display: "Ch" */
Display_Space();
Display_EEString(String); /* display channel name */
#ifdef UI_QUARTZ_CRYSTAL
if (Channel == 0) /* buffered frequency input */
{
Clear_Symbol(4); /* clear symbol in line #4 */
}
else /* HF or LF crystal oscillator */
{
Display_FancySemiPinout(4); /* display crystal symbol in line #4 */
}
#endif
/* restart scan in top range */
Range = 2; /* select top range */
Flag |= UPDATE_RANGE; /* update range */
Flag &= ~(RESCAN_FLAG | SKIP_FREQ); /* reset rescan */
Flag &= ~UPDATE_CHANNEL; /* clear flag */
}
/*
* settings for ranges
*/
if (Flag & UPDATE_RANGE) /* update range settings */
{
switch (Range) /* get range specifics */
{
case 0: /* <100kHz */
Div = 1; /* frequency prescaler 1:1 */
#if CPU_FREQ <= 16000000
Index = 3; /* table index 3: 256:1 */
#else
Index = 4; /* table index 4: 1024:1 */
#endif
GateTime = 1000; /* gate time: 1000ms */
MinPulses = 0; /* lower limit: none */
// MaxPulses = 100000; /* upper limit: 100k */
break;
case 1: /* 100kHz-1MHz */
Div = 1; /* frequency prescaler 1:1 */
Index = 2; /* table index 2: 64:1 */
GateTime = 100; /* gate time: 100ms */
MinPulses = 10000; /* lower limit: 10k */
// MaxPulses = 100000; /* upper limit: 100k */
break;
case 2: /* >1MHz */
Div = FREQ_COUNTER_PRESCALER; /* frequency prescaler 16:1 or 32:1 */
Index = 2; /* table index 2: 64:1 */
GateTime = 100; /* gate time: 100ms */
#if FREQ_COUNTER_PRESCALER == 16
MinPulses = 6250; /* lower limit: 6250 */
#elif FREQ_COUNTER_PRESCALER == 32
MinPulses = 3125; /* lower limit: 3125 */
#endif
// MaxPulses = 0; /* upper limit: none */
break;
}
/* update Timer1 prescaler */
Top = DATA_read_word(&T1_Prescaler_table[Index]); /* prescaler value */
Bits = DATA_read_byte(&T1_RegBits_table[Index]); /* prescaler bits */
/* calculate compare value for Timer1 (gate time) */
/* top = gatetime * MCU_cycles / timer prescaler */
Value = GateTime; /* gatetime (in ms) */
/* * MCU cycles per µs and scale gatetime to µs */
Value *= (MCU_CYCLES_PER_US * 1000);
Value /= Top; /* divide by timer prescaler */
Top = (uint16_t)Value; /* use lower 16 bit */
/* update frequency counter prescaler */
if (Div == FREQ_COUNTER_PRESCALER) /* 16:1 / 32:1 */
{
/* enable frequency prescaler */
COUNTER_CTRL_PORT |= (1 << COUNTER_CTRL_DIV); /* set pin high */
}
else /* 1:1 */
{
/* disable frequency prescaler */
COUNTER_CTRL_PORT &= ~(1 << COUNTER_CTRL_DIV); /* set pin low */
}
Flag &= ~UPDATE_RANGE; /* clear flag */
}
/* set up T0 as input */
InDir = COUNTER_DDR & (1 << COUNTER_IN); /* get current direction */
COUNTER_DDR &= ~(1 << COUNTER_IN); /* set to input mode */
wait500us(); /* settle time */
/* start timers */
Flag |= WAIT_FLAG; /* enter waiting loop */
Pulses = 0; /* reset pulse counter */
TCNT0 = 0; /* Timer0: reset pulse counter */
TCNT1 = 0; /* Timer1: reset gate time counter */
OCR1A = Top; /* Timer1: set gate time */
TCCR1B = Bits; /* start Timer1: prescaler */
TCCR0B = (1 << CS02) | (1 << CS01); /* start Timer0: clock source T0 on falling edge */
/*
* wait for timer1 or user feedback
*/
while (Flag & WAIT_FLAG)
{
if (TCCR1B == 0) /* Timer1 stopped by ISR */
{
Flag |= GATE_FLAG; /* signal Timer1 event */
Flag &= ~WAIT_FLAG; /* end waiting loop */
}
else /* Timer1 still running */
{
/* wait for user feedback */
Test = TestKey(0, CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_SHORT) /* short key press */
{
/* select next source channel */
if (Channel < 2) Channel++; /* next channel */
else Channel = 0; /* overrun */
Flag |= UPDATE_CHANNEL; /* update channel */
Flag &= ~WAIT_FLAG; /* end waiting loop */
}
else if (Test == KEY_TWICE) /* two short key presses */
{
Flag = 0; /* end processing loop */
}
#ifdef HW_KEYS
else if (Test == KEY_RIGHT) /* right key */
{
if (Channel < 2) Channel++; /* next channel */
else Channel = 0; /* overrun */
Flag |= UPDATE_CHANNEL; /* update channel */
Flag &= ~WAIT_FLAG; /* end waiting loop */
}
else if (Test == KEY_LEFT) /* left key */
{
if (Channel > 0) Channel--; /* previous channel */
else Channel = 2; /* underrun */
Flag |= UPDATE_CHANNEL; /* update channel */
Flag &= ~WAIT_FLAG; /* end waiting loop */
}
#endif
}
}
if (InDir) /* restore old setting for T0 */
{
COUNTER_DDR |= (1 << COUNTER_IN); /* set to output mode */
}
Cfg.OP_Control &= ~OP_BREAK_KEY; /* clear break signal (just in case) */
/*
* process measurement
*/
if (Flag & GATE_FLAG) /* got measurement */
{
/* total sum of pulses during gate period */
Pulses += TCNT0; /* add counter of Timer0 */
/*
* autoranging
*/
if (Pulses < MinPulses) /* range underrun */
{
if (Range > 0) /* not lowest range yet */
{
Range--; /* change to lower range */
Flag |= UPDATE_RANGE; /* update range */
}
}
#if 0
else if (Pulses > MaxPulses) /* range overrun */
{
if (Range < 2) /* not highest range yet */
{
Range++; /* change to higher range */
Flag |= UPDATE_RANGE; /* update range */
}
}
#endif
/* show frequency only when not switching ranges */
if (! (Flag & UPDATE_RANGE)) /* no change of range */
{
/* prevent display of "0 Hz" (no signal or f too low) */
if (Pulses) /* got signal */
{
Flag |= SHOW_FREQ; /* show frequency */
}
/* manage rescan */
Flag &= ~(RESCAN_FLAG | SKIP_FREQ); /* reset flags */
if (Range < 2) /* not top range */
{
Range = 2; /* change to top range */
Flag |= (UPDATE_RANGE | RESCAN_FLAG); /* update range and rescan */
}
}
/*
* calculate frequency
* - f = pulses * f-prescaler / gatetime
* - 20MHz MCU: 5M pulses per second at maximum
* with 100ms gate time max. 500k pulses
*/
if (Div == 1) /* f-prescaler 1:1 */
{
/* no overflow possible */
Pulses *= 1000; /* scale to ms */
Pulses /= GateTime; /* divide by gatetime (in ms) */
}
else /* f-prescaler 16:1 or 32:1 */
{
/* prevent overflow */
Pulses *= 100; /* scale to 10ms */
Pulses *= Div; /* * f-prescaler */
Pulses /= (GateTime / 10); /* divide by gatetime (in 10ms) */
}
Flag &= ~GATE_FLAG; /* clear flag */
}
/*
* display frequency (in line #2)
*/
if (! (Flag & SKIP_FREQ)) /* update frequency display */
{
LCD_ClearLine2(); /* clear line #2 */
Display_Char('f'); /* display: f */
Display_Space();
if (Flag & SHOW_FREQ) /* valid frequency */
{
/* determine prefix */
Test = 0; /* dot position */
Index = 0; /* unit char */
if (Pulses >= 1000000) /* f >= 1MHz */
{
Test = 6; /* 10^6 */
Index = 'M'; /* M for mega */
}
else if (Pulses >= 1000) /* f >= 1kHz */
{
Test = 3; /* 10^3 */
Index = 'k'; /* k for kilo */
}
/* display frequency */
Display_FullValue(Pulses, Test, Index);
Display_EEString(Hertz_str); /* display: "Hz" */
Flag &= ~SHOW_FREQ; /* clear flag */
}
else /* invalid frequency */
{
Display_Minus(); /* display: no value */
}
/* manage rescan */
if (Flag & RESCAN_FLAG) /* in rescan mode */
{
/* prevent any updates while in rescan mode */
Flag |= SKIP_FREQ; /* skip frequency display */
}
}
}
/*
* clean up
*/
TIMSK0 = 0; /* disable all interrupts for Timer0 */
TIMSK1 = 0; /* disable all interrupts for Timer1 */
/* filter control lines which were in input mode */
CtrlDir ^= (1 << COUNTER_CTRL_DIV) | (1 << COUNTER_CTRL_CH0) | (1 << COUNTER_CTRL_CH1);
CtrlDir &= (1 << COUNTER_CTRL_DIV) | (1 << COUNTER_CTRL_CH0) | (1 << COUNTER_CTRL_CH1);
COUNTER_CTRL_DDR &= ~CtrlDir; /* set former direction */
/* local constants for Flag */
#undef RUN_FLAG
#undef WAIT_FLAG
#undef GATE_FLAG
#undef UPDATE_CHANNEL
#undef UPDATE_RANGE
#undef SHOW_FREQ
#undef RESCAN_FLAG
#undef SKIP_FREQ
}
#endif
/* ************************************************************************
* ring tester for high Q chokes and transformers
* ************************************************************************ */
#ifdef HW_RING_TESTER
/*
* ring tester (LOPT/FBT tester)
* - uses frontend for ring detection
* - counter input: T0
* - control via probes (RING_TESTER_PROBES)
* probe #1: +5V
* probe #2: pulse output
* probe #3: Gnd
* - control via dedicated pin (RING_TESTER_PIN)
* RINGTESTER_OUT: pulse output
* - requires idle sleep mode to keep timers running when MCU is sleeping
*/
void RingTester(void)
{
uint8_t Flag; /* loop control flag */
uint8_t Test; /* user feedback */
uint8_t Old_DDR; /* old DDR state */
/* local constants for Flag (bitfield) */
#define RUN_FLAG 1 /* run flag */
#define WAIT_FLAG 2 /* enter/run waiting loop */
#define GATE_FLAG 3 /* gatetime flag */
#define SHOW_RINGS 4 /* display rings */
/* show info */
LCD_Clear(); /* clear display */
#ifdef UI_COLORED_TITLES
/* display: Ring Tester */
Display_ColoredEEString(RingTester_str, COLOR_TITLE);
#else
Display_EEString(RingTester_str); /* display: Ring Tester */
#endif
#ifdef RING_TESTER_PROBES
ProbePinout(PROBES_RINGTESTER); /* show probes used */
#endif
/*
* working priciple:
* - a shorted or bad inductor has a low Q value
* - a trigger pulse causes a damped oscillation in a tank circuit
* - the higher the inductor's Q the longer the oscillation lasts
* - so we simply count the number of rings until the oscillation
* fades away
*/
/*
* set up pulse output
*/
#ifdef RING_TESTER_PIN
/* use dedicated pin */
RINGTESTER_PORT &= ~(1 << RINGTESTER_OUT); /* low by default */
RINGTESTER_DDR |= (1 << RINGTESTER_OUT); /* enable output */
#endif
#ifdef RING_TESTER_PROBES
/* set probes: probe #1 - Vcc / probe #2 - Rl - pulse out / probe #3 - Gnd */
R_PORT = 0; /* pull down probe #2 */
R_DDR = (1 << R_RL_2); /* enable Rl for probe #2 */
ADC_PORT = (1 << TP1); /* pull up #1, pull down #3 */
ADC_DDR = (1 << TP1) | (1 << TP3); /* enable direct pull for #1 and #3 */
#endif
/*
* We use Timer1 for the gate time and Timer0 to count rings.
* Max. frequency for Timer0 is 1/4 of the MCU clock.
*/
Flag = RUN_FLAG; /* enter measurement loop */
/*
Fixed gate time of 10ms.
Timer1 top value (gate time)
- top = gatetime * MCU_cycles / prescaler
- gate time in µs
- MCU cycles per µs
- top max. 2^16 - 1
Using prescaler 8:1 - register bits: (1 << CS11)
*/
/* set up Timer0 (ring counter) */
TCCR0A = 0; /* normal mode (count up) */
TIFR0 = (1 << TOV0); /* clear overflow flag */
TIMSK0 = (1 << TOIE0); /* enable overflow interrupt */
/* set up Timer1 (gate time) */
TCCR1A = 0; /* normal mode (count up) */
TIFR1 = (1 << OCF1A); /* clear output compare A match flag */
TIMSK1 = (1 << OCIE1A); /* enable output compare A match interrupt */
/* top = gatetime * MCU_cycles / timer prescaler */
OCR1A = (uint16_t)((10000UL * MCU_CYCLES_PER_US) / 8);
/*
* measurement loop
*/
while (Flag > 0)
{
/* set up T0 as input (pin might be shared with display) */
Old_DDR = COUNTER_DDR; /* save current settings */
COUNTER_DDR &= ~(1 << COUNTER_IN); /* signal input */
wait500us(); /* settle time */
/* start timers */
Pulses = 0; /* reset pulse counter */
Flag = WAIT_FLAG; /* enter waiting loop */
TCNT0 = 0; /* Timer0: reset ring counter */
TCNT1 = 0; /* Timer1: reset gate time counter */
TCCR1B = (1 << CS11); /* start Timer1: prescaler 8:1 */
TCCR0B = (1 << CS02) | (1 << CS01); /* start Timer0: clock source T0 on falling edge */
/*
* create trigger pulse (2 ms)
* - will create one pseudo ring since timers are already running
*/
#ifdef RING_TESTER_PIN
RINGTESTER_PORT |= (1 << RINGTESTER_OUT); /* set pin high */
wait2ms(); /* wait 2 ms */
RINGTESTER_PORT &= ~(1 << RINGTESTER_OUT); /* set pin low */
#endif
#ifdef RING_TESTER_PROBES
R_PORT = (1 << R_RL_2); /* pull up probe #2 via Rl */
wait2ms(); /* wait 2 ms */
R_PORT = 0; /* pull down probe #2 */
#endif
/* wait for timer1 or key press */
while (Flag == WAIT_FLAG)
{
if (TCCR1B == 0) /* Timer1 stopped by ISR */
{
Flag = GATE_FLAG; /* end loop and signal Timer1 event */
}
else /* Timer1 still running */
{
/* wait for user feedback */
Test = TestKey(0, CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_TWICE) /* two short key presses */
{
Flag = 0; /* end processing loop */
}
}
}
/* T0 pin might be shared with display */
COUNTER_DDR = Old_DDR; /* restore old settings */
Cfg.OP_Control &= ~OP_BREAK_KEY; /* clear break signal (just in case) */
/*
* process measurement
*/
if (Flag == GATE_FLAG) /* got measurement */
{
/* total sum of rings during gate period */
Pulses += TCNT0; /* add counter of Timer0 */
/* consider first/pseudo ring created by trigger pulse */
if (Pulses > 0) /* sanity check */
{
Pulses--; /* subtract one ring */
}
Flag = SHOW_RINGS; /* display rings */
}