-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
1974 lines (1764 loc) · 54 KB
/
main.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
//*****************************************************************************
//
// File Name : 'main.c'
// Title : AVR DDS2 signal generator
// Author : Scienceprog.com - Copyright (C) 2008
// Created : 2008-03-09
// Revised : 2008-03-09
// Version : 2.0
// Target MCU : Atmel AVR series ATmega16
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <inttypes.h>
#include "lcd_lib.h"
// define R2R port
#define R2RPORT PORTA
#define R2RDDR DDRA
// define button port and dedicated pins (PD0-4)
#define BPORT PORTD
#define BPIN PIND
#define BDDR DDRD
#define DOWN 0
#define LEFT 1
#define START 2
#define RIGHT 3
#define UP 4
#define OPT 6
#define BPORT2 PORTB
#define BDDR2 DDRB
#define BTN_INT 2
// define Highs Speed (HS) signal output (PD5)
#define HSDDR DDRD
#define HSPORT PORTD
#define HSPIN PIND
#define HS 5
// define eeprom addresses
#define EE_CONFIG 0
#define EE_INIT E2END
#define CPU_FREQ 16000000ul
#define OUT_TICKS 10
#define OUT_SYNC_TICKS 15
#define SWEEP_OUT_TICKS 9
#define ACC_FRAC_BITS 24
#define SWEEP_ACC_FRAC_BITS 16
#define SIGNAL_BUFFER_SIZE 256
#define MIN_FREQ 0.0 // minimum DDS frequency
#define MAX_FREQ 250000.0 // maximum DDS frequency
#define MIN_FREQ_STEP 0.001 // minimum DDS frequency step
#define MAX_FREQ_STEP 10000.0 // maximum DDS frequency step
#define MIN_FREQ_INC 0.0 // minimum sweep frequency increment
#define MAX_FREQ_INC 100.0 // maximum sweep frequency increment
#define MIN_FREQ_CAL 0.09
#define MAX_FREQ_CAL 1.01
#define STEP_FREQ_CAL 0.000001
#define MIN_PULSE 0.001 // minimum pulse duration, ms
#define MAX_PULSE 1000.0 // maximum pulse duration, ms
void timer2Init(void);
void timer2Start(void);
void timer2Stop(void);
void timer1Start(uint8_t);
void timer1StartPwm(uint16_t);
void timer1Stop(void);
inline void static signalOut(const uint8_t *, uint8_t, uint8_t, uint8_t, uint8_t);
inline void static signalWithSyncOut(const uint8_t *, uint8_t, uint8_t, uint8_t, uint8_t);
inline void static randomSignalOut(const uint8_t *);
inline void static sweepOut(const uint8_t *, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
// button processing
typedef void (ButtonHandlerFn_t)(void);
void processButton(void);
void buttonNop(void);
void menu_onUp(void);
void menu_onDown(void);
void menu_onOpt(void);
void optMenu_onUp(void);
void optMenu_onDown(void);
void optMenu_onOpt(void);
void signal_onLeft(void);
void signal_onRight(void);
void signal_onStart(void);
void freqStep_onLeft(void);
void freqStep_onRight(void);
void freqMode_onLeft(void);
void freqMode_onRight(void);
void noise_onStart(void);
void pulse_onStart(void);
void pulse_onLeft(void);
void pulse_onRight(void);
void hs_onLeft(void);
void hs_onRight(void);
void hs_onStart(void);
void pwm_onUp(void);
void pwm_onDown(void);
void pwm_onLeft(void);
void pwm_onRight(void);
void pwm_onStart(void);
void pwmHs_onUp(void);
void pwmHs_onDown(void);
void pwmHs_onLeft(void);
void pwmHs_onRight(void);
void pwmHs_onStart(void);
void sweep_onUp(void);
void sweep_onDown(void);
void sweep_onLeft(void);
void sweep_onRight(void);
void sweep_onStart(void);
void offLevel_onLeft(void);
void offLevel_onRight(void);
void syncOut_onLeft(void);
void syncOut_onRight(void);
void syncOut_onOpt(void);
void trigger_onLeft(void);
void trigger_onRight(void);
void calFreq_onLeft(void);
void calFreq_onRight(void);
void calFreq_onStart(void);
// menu processing
typedef void (MenuItemEnterHandlerFn_t)(void);
void signal_updateDisplay(void);
void noise_updateDisplay(void);
void pulse_updateDisplay(void);
void freqStep_updateDisplay(void);
void freqMode_updateDisplay(void);
void hs_updateDisplay(void);
void pwm_updateDisplay(void);
void pwmHs_updateDisplay(void);
void sweep_updateDisplay(void);
void offLevel_updateDisplay(void);
void syncOut_updateDisplay(void);
void trigger_updateDisplay(void);
void calFreq_updateDisplay(void);
// adjust LCDsendChar() function for strema
static int LCDsendstream(char c, FILE *stream);
// set output stream to LCD
static FILE lcd_str = FDEV_SETUP_STREAM(LCDsendstream, NULL, _FDEV_SETUP_WRITE);
struct ButtonHandlers {
ButtonHandlerFn_t * onUp;
ButtonHandlerFn_t * onDown;
ButtonHandlerFn_t * onLeft;
ButtonHandlerFn_t * onRight;
ButtonHandlerFn_t * onStart;
ButtonHandlerFn_t * onOpt;
};
struct MenuEntry {
const char * title;
const void * data;
MenuItemEnterHandlerFn_t * updateDisplay;
struct ButtonHandlers buttonHandlers;
};
enum SyncOut {
SyncOut_Off,
SyncOut_Single,
SyncOut_Multiple,
SyncOut_Trigger,
SyncOut_End
};
enum FreqMode {
FreqMode_Exact,
FreqMode_Jitter
};
struct Config {
uint8_t menuEntry; // active or last active main menu entry
double freq; // frequency value, Hz
double freqCal; // frequence calibration coefficient
double freqEnd; // end frequency for sweep, Hz
double freqInc; // frequency increment for sweep, Hz
uint8_t hsFreq; // high speed frequency [1..8 MHz]
double freqStep; // frequency step value, Hz
enum FreqMode freqMode;
uint16_t pwmFreq; // PWM freq [61..62500 Hz]
uint8_t pwmDuty;
uint8_t offLevel; // output value when then generator if off
double pulse; // pulse duration, ms
enum SyncOut syncOut;
double triggerDelay; // deleay after trigger detection, ms
};
struct Config config = {
.menuEntry = 0,
.freq = 1000.0,
.freqCal = 1.0000,
.freqEnd = 20000.0,
.freqInc = 0.1,
.hsFreq = 1, // default 1MHz HS signal freq
.freqStep = 100.0,
.pwmFreq = 62500,
.pwmDuty = 127,
.offLevel = 0x80, // middle of the scale
.pulse = 1.0,
.syncOut = SyncOut_Off,
.triggerDelay = 0.0,
};
volatile bool running; // generator on/off
//define signals
const uint8_t SINE_WAVE[] PROGMEM = {
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c
};
const uint8_t SQUARE_WAVE[] PROGMEM = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
};
const uint8_t SAWTOOTH_WAVE[] PROGMEM = {
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,
0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x3b,0x3c,0x3d,0x3e,0x3f,
0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,
0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,
0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,
0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7e,0x7f,
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,
0xa0,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xab,0xac,0xad,0xae,0xaf,
0xb0,0xb1,0xb2,0xb3,0xb4,0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xbb,0xbc,0xbd,0xbe,0xbf,
0xc0,0xc1,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xcb,0xcc,0xcd,0xce,0xcf,
0xd0,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xdb,0xdc,0xdd,0xde,0xdf,
0xe0,0xe1,0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xeb,0xec,0xed,0xee,0xef,
0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
};
const uint8_t REV_SAWTOOTH_WAVE[] PROGMEM = {
0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf3,0xf2,0xf1,0xf0,
0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0,
0xdf,0xde,0xdd,0xdc,0xdb,0xda,0xd9,0xd8,0xd7,0xd6,0xd5,0xd4,0xd3,0xd2,0xd1,0xd0,
0xcf,0xce,0xcd,0xcc,0xcb,0xca,0xc9,0xc8,0xc7,0xc6,0xc5,0xc4,0xc3,0xc2,0xc1,0xc0,
0xbf,0xbe,0xbd,0xbc,0xbb,0xba,0xb9,0xb8,0xb7,0xb6,0xb5,0xb4,0xb3,0xb2,0xb1,0xb0,
0xaf,0xae,0xad,0xac,0xab,0xaa,0xa9,0xa8,0xa7,0xa6,0xa5,0xa4,0xa3,0xa2,0xa1,0xa0,
0x9f,0x9e,0x9d,0x9c,0x9b,0x9a,0x99,0x98,0x97,0x96,0x95,0x94,0x93,0x92,0x91,0x90,
0x8f,0x8e,0x8d,0x8c,0x8b,0x8a,0x89,0x88,0x87,0x86,0x85,0x84,0x83,0x82,0x81,0x80,
0x7f,0x7e,0x7d,0x7c,0x7b,0x7a,0x79,0x78,0x77,0x76,0x75,0x74,0x73,0x72,0x71,0x70,
0x6f,0x6e,0x6d,0x6c,0x6b,0x6a,0x69,0x68,0x67,0x66,0x65,0x64,0x63,0x62,0x61,0x60,
0x5f,0x5e,0x5d,0x5c,0x5b,0x5a,0x59,0x58,0x57,0x56,0x55,0x54,0x53,0x52,0x51,0x50,
0x4f,0x4e,0x4d,0x4c,0x4b,0x4a,0x49,0x48,0x47,0x46,0x45,0x44,0x43,0x42,0x41,0x40,
0x3f,0x3e,0x3d,0x3c,0x3b,0x3a,0x39,0x38,0x37,0x36,0x35,0x34,0x33,0x32,0x31,0x30,
0x2f,0x2e,0x2d,0x2c,0x2b,0x2a,0x29,0x28,0x27,0x26,0x25,0x24,0x23,0x22,0x21,0x20,
0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18,0x17,0x16,0x15,0x14,0x13,0x12,0x11,0x10,
0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00,
};
const uint8_t TRIANGLE_WAVE[] PROGMEM = {
0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
0xff,0xfd,0xfb,0xf9,0xf7,0xf5,0xf3,0xf1,0xef,0xef,0xeb,0xe9,0xe7,0xe5,0xe3,0xe1,
0xdf,0xdd,0xdb,0xd9,0xd7,0xd5,0xd3,0xd1,0xcf,0xcf,0xcb,0xc9,0xc7,0xc5,0xc3,0xc1,
0xbf,0xbd,0xbb,0xb9,0xb7,0xb5,0xb3,0xb1,0xaf,0xaf,0xab,0xa9,0xa7,0xa5,0xa3,0xa1,
0x9f,0x9d,0x9b,0x99,0x97,0x95,0x93,0x91,0x8f,0x8f,0x8b,0x89,0x87,0x85,0x83,0x81,
0x7f,0x7d,0x7b,0x79,0x77,0x75,0x73,0x71,0x6f,0x6f,0x6b,0x69,0x67,0x65,0x63,0x61,
0x5f,0x5d,0x5b,0x59,0x57,0x55,0x53,0x51,0x4f,0x4f,0x4b,0x49,0x47,0x45,0x43,0x41,
0x3f,0x3d,0x3b,0x39,0x37,0x35,0x33,0x31,0x2f,0x2f,0x2b,0x29,0x27,0x25,0x23,0x21,
0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x11,0x0f,0x0f,0x0b,0x09,0x07,0x05,0x03,0x01
};
const uint8_t ECG_WAVE[] PROGMEM = {
73,74,75,75,74,73,73,73,73,72,71,69,68,67,67,67,
68,68,67,65,62,61,59,57,56,55,55,54,54,54,55,55,
55,55,55,55,54,53,51,50,49,49,52,61,77,101,132,
169,207,238,255,254,234,198,154,109,68,37,17,5,
0,1,6,13,20,28,36,45,52,57,61,64,65,66,67,68,68,
69,70,71,71,71,71,71,71,71,71,72,72,72,73,73,74,
75,75,76,77,78,79,80,81,82,83,84,86,88,91,93,96,
98,100,102,104,107,109,112,115,118,121,123,125,
126,127,127,127,127,127,126,125,124,121,119,116,
113,109,105,102,98,95,92,89,87,84,81,79,77,76,75,
74,73,72,70,69,68,67,67,67,68,68,68,69,69,69,69,
69,69,69,70,71,72,73,73,74,74,75,75,75,75,75,75,
74,74,73,73,73,73,72,72,72,71,71,71,71,71,71,71,
70,70,70,69,69,69,69,69,70,70,70,69,68,68,67,67,
67,67,66,66,66,65,65,65,65,65,65,65,65,64,64,63,
63,64,64,65,65,65,65,65,65,65,64,64,64,64,64,64,
64,64,65,65,65,66,67,68,69,71,72,73
};
const uint8_t SINE_WAVE_FROM_ZERO[] PROGMEM = { //sine 256 values, start from 0
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0a,0x0c,0x0d,0x0f,0x10,0x12,0x13,0x15,0x17,0x19,0x1b,0x1d,0x1f,0x21,0x23,
0x25,0x27,0x2a,0x2c,0x2e,0x31,0x33,0x36,0x38,0x3b,0x3e,0x40,0x43,0x46,0x49,0x4c,
0x4f,0x51,0x54,0x57,0x5a,0x5d,0x60,0x63,0x67,0x6a,0x6d,0x70,0x73,0x76,0x79,0x7c,
0x80,0x83,0x86,0x89,0x8c,0x8f,0x92,0x95,0x98,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,
0xb0,0xb3,0xb6,0xb9,0xbc,0xbf,0xc1,0xc4,0xc7,0xc9,0xcc,0xce,0xd1,0xd3,0xd5,0xd8,
0xda,0xdc,0xde,0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xed,0xef,0xf0,0xf2,0xf3,0xf5,
0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfc,0xfd,0xfe,0xfe,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfe,0xfd,0xfc,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,
0xf6,0xf5,0xf3,0xf2,0xf0,0xef,0xed,0xec,0xea,0xe8,0xe6,0xe4,0xe2,0xe0,0xde,0xdc,
0xda,0xd8,0xd5,0xd3,0xd1,0xce,0xcc,0xc9,0xc7,0xc4,0xc1,0xbf,0xbc,0xb9,0xb6,0xb3,
0xb0,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x98,0x95,0x92,0x8f,0x8c,0x89,0x86,0x83,
0x80,0x7c,0x79,0x76,0x73,0x70,0x6d,0x6a,0x67,0x63,0x60,0x5d,0x5a,0x57,0x54,0x51,
0x4f,0x4c,0x49,0x46,0x43,0x40,0x3e,0x3b,0x38,0x36,0x33,0x31,0x2e,0x2c,0x2a,0x27,
0x25,0x23,0x21,0x1f,0x1d,0x1b,0x19,0x17,0x15,0x13,0x12,0x10,0x0f,0x0d,0x0c,0x0a,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x03,0x02,0x01,0x01
};
const uint8_t NOISE_SIGNAL[] PROGMEM = {
0x0a,0x0e,0x2d,0x73,0xc4,0x40,0xaa,0x8f,0xdd,0xf3,0x6b,0x97,0xb9,0x8d,0x77,0x57,
0xe3,0x52,0x93,0x3f,0x25,0x07,0x99,0x5f,0x8b,0x37,0x30,0x7b,0x3a,0x89,0xc6,0xae,
0x4e,0x58,0xe4,0x4b,0x48,0x05,0xd6,0xf2,0x5c,0x44,0xef,0xf8,0x69,0xf6,0x92,0x56,
0x1d,0x96,0xab,0x2f,0x88,0x35,0xf5,0x36,0x83,0xfc,0x8e,0x60,0xe0,0xda,0xa8,0x5b,
0xdf,0x7e,0x4d,0x3b,0x38,0x91,0x2b,0xfa,0x21,0xc2,0x23,0x0d,0x2e,0xce,0x3c,0xb6,
0x03,0x32,0xed,0x86,0xe6,0x0f,0x55,0x6a,0x34,0xb8,0x70,0x45,0x49,0x9b,0x76,0xbc,
0x18,0x5a,0x41,0x46,0x28,0xfd,0x2c,0xb0,0xea,0xb2,0xde,0x65,0xbb,0x10,0x59,0xf1,
0x9d,0xb7,0x29,0xd4,0xeb,0x42,0x85,0x6f,0x39,0xd5,0x26,0x90,0x7f,0xa7,0xe8,0xd9,
0x98,0xc1,0x8c,0x11,0x62,0xad,0x81,0x66,0x0c,0x5d,0x19,0x01,0x1e,0xc8,0x87,0xe1,
0x2a,0xd2,0x24,0xd1,0x43,0xe7,0x4f,0x68,0xc0,0xaf,0x5e,0x9e,0x84,0xe2,0x50,0xcb,
0x1a,0xc3,0xb4,0x74,0x04,0xac,0x64,0xa0,0x13,0xd3,0x31,0x00,0x9c,0xfe,0x4a,0xb3,
0x78,0x15,0x3e,0xee,0x94,0x7c,0x1c,0x72,0xa1,0x20,0x9f,0x95,0xcf,0x3d,0x82,0xb5,
0xbd,0x54,0xa6,0x47,0x6e,0x75,0xc7,0x1b,0xd7,0x09,0x16,0xf0,0x12,0x02,0xb1,0x06,
0x4c,0xcd,0xa9,0xa2,0x6c,0xa5,0x61,0xca,0x7d,0x1f,0x22,0x17,0x14,0xc5,0xd8,0x6d,
0x8a,0xf7,0x51,0xa3,0xfb,0xf4,0x63,0xbf,0x79,0xc9,0x27,0xec,0x7a,0x9a,0xbe,0x80,
0xff,0xe5,0xba,0xcc,0x0b,0xdb,0xdc,0xf9,0x67,0xe9,0xa4,0x08,0xd0,0x71,0x33,0x53
};
const uint8_t * const SIGNALS[] PROGMEM = {
SINE_WAVE,
SQUARE_WAVE,
TRIANGLE_WAVE,
SAWTOOTH_WAVE,
REV_SAWTOOTH_WAVE,
ECG_WAVE
};
const char SINE_TITLE[] PROGMEM = " Sine ";
const char SQUARE_TITLE[] PROGMEM = " Square ";
const char TRIANGLE_TITLE[] PROGMEM = " Triangle ";
const char SAW_TITLE[] PROGMEM = " SawTooth ";
const char REV_SAW_TITLE[] PROGMEM = " Rev SawTooth ";
const char ECG_TITLE[] PROGMEM = " ECG ";
const char FREQ_STEP_TITLE[] PROGMEM = " Freq Step ";
const char FREQ_MODE_TITLE[] PROGMEM = " Freq Mode ";
const char NOISE_TITLE[] PROGMEM = " Noise ";
const char PULSE_TITLE[] PROGMEM = " Pulse ";
const char HS_TITLE[] PROGMEM = " High Speed ";
const char PWM_TITLE[] PROGMEM = " PWM ";
const char PWM_HS_TITLE[] PROGMEM = " PWM (HS) ";
const char SWEEP_TITLE[] PROGMEM = " Sweep ";
const char SWEEP_END_TITLE[] PROGMEM = " Sweep End";
const char SWEEP_INC_TITLE[] PROGMEM = " Sweep Step";
const char OFF_LEVEL_TITLE[] PROGMEM = " Off Level ";
const char SYNC_OUT_TITLE[] PROGMEM = " Sync Output ";
const char TRIGGER_TITLE[] PROGMEM = " Trigger Delay ";
const char CAL_FREQ_TITLE[] PROGMEM = " Calibrate Freq ";
const struct MenuEntry MENU[] PROGMEM = {
{
SINE_TITLE,
SINE_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
SQUARE_TITLE,
SQUARE_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
TRIANGLE_TITLE,
TRIANGLE_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
SAW_TITLE,
SAWTOOTH_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
REV_SAW_TITLE,
REV_SAWTOOTH_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
ECG_TITLE,
ECG_WAVE,
signal_updateDisplay,
{
menu_onUp,
menu_onDown,
signal_onLeft,
signal_onRight,
signal_onStart,
menu_onOpt,
}
},
{
NOISE_TITLE,
NULL,
noise_updateDisplay,
{
menu_onUp,
menu_onDown,
buttonNop,
buttonNop,
noise_onStart,
menu_onOpt,
}
},
{
PULSE_TITLE,
NULL,
pulse_updateDisplay,
{
menu_onUp,
menu_onDown,
pulse_onLeft,
pulse_onRight,
pulse_onStart,
menu_onOpt,
}
},
{
HS_TITLE,
NULL,
hs_updateDisplay,
{
menu_onUp,
menu_onDown,
hs_onLeft,
hs_onRight,
hs_onStart,
menu_onOpt,
}
},
{
PWM_TITLE,
NULL,
pwm_updateDisplay,
{
pwm_onUp,
pwm_onDown,
signal_onLeft,
signal_onRight,
pwm_onStart,
menu_onOpt,
}
},
{
PWM_HS_TITLE,
NULL,
pwmHs_updateDisplay,
{
pwmHs_onUp,
pwmHs_onDown,
pwmHs_onLeft,
pwmHs_onRight,
pwmHs_onStart,
menu_onOpt,
}
},
{
SWEEP_TITLE,
NULL,
sweep_updateDisplay,
{
sweep_onUp,
sweep_onDown,
sweep_onLeft,
sweep_onRight,
sweep_onStart,
menu_onOpt,
}
},
};
static const uint8_t MENU_SIZE = (sizeof(MENU)/sizeof(MENU[0]));
const struct MenuEntry OPT_MENU[] PROGMEM = {
{
FREQ_STEP_TITLE,
NULL,
freqStep_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
freqStep_onLeft,
freqStep_onRight,
optMenu_onOpt,
optMenu_onOpt,
}
},
{
FREQ_MODE_TITLE,
NULL,
freqMode_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
freqMode_onLeft,
freqMode_onRight,
optMenu_onOpt,
optMenu_onOpt,
}
},
{
OFF_LEVEL_TITLE,
NULL,
offLevel_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
offLevel_onLeft,
offLevel_onRight,
optMenu_onOpt,
optMenu_onOpt,
}
},
{
SYNC_OUT_TITLE,
NULL,
syncOut_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
syncOut_onLeft,
syncOut_onRight,
syncOut_onOpt,
syncOut_onOpt,
}
},
{
TRIGGER_TITLE,
NULL,
trigger_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
trigger_onLeft,
trigger_onRight,
optMenu_onOpt,
optMenu_onOpt,
}
},
{
CAL_FREQ_TITLE,
NULL,
calFreq_updateDisplay,
{
optMenu_onUp,
optMenu_onDown,
calFreq_onLeft,
calFreq_onRight,
calFreq_onStart,
optMenu_onOpt,
}
},
};
static const uint8_t OPT_MENU_SIZE = (sizeof(OPT_MENU)/sizeof(OPT_MENU[0]));
//various LCD strings
const char MNON[] PROGMEM = "ON ";
const char MNOFF[] PROGMEM = "OFF";
const char MNDIS[] PROGMEM = "DIS";
const char MNTRIG[] PROGMEM = "TRG";
const char RND[] PROGMEM = " Random";
enum Button {
Button_None,
Button_Up,
Button_Right,
Button_Down,
Button_Left,
Button_Start,
Button_Opt,
};
struct ButtonState {
uint16_t now;
uint16_t pressedTime;
uint16_t autoTime;
bool autoRepeat;
volatile enum Button pressed;
volatile bool processed;
};
struct ButtonState buttonState;
static const uint16_t BUTTON_UNBOUNCE = 20;
static const uint16_t BUTTON_AUTO_START = 100;
static const uint16_t BUTTON_AUTO_REPEAT = 8;
uint8_t optMenuEntryNum = (uint8_t)-1; // active opt-menu entry or -1 if not in the opt-menu
struct MenuEntry menuEntry; // copy of active menu entry
struct ButtonHandlers * buttonHandlers;
uint8_t submenuLevel = 0; // used by the seep only
uint8_t signalBuffer[SIGNAL_BUFFER_SIZE]
__attribute__ ((aligned(SIGNAL_BUFFER_SIZE)))
__attribute__ ((section (".noinit")));
// adjust LCD stream fuinction to use with printf()
static int LCDsendstream(char c , FILE *stream) {
LCDsendChar(c);
return 0;
}
inline uint32_t delayMsToCount(double ms) {
return (double)(CPU_FREQ / 1000) * ms / 6;
}
inline void delayCount(uint32_t count) {
asm volatile(
// takes 6 cycles
"1:" "\n\t"
"subi %[c0], 1 ; 1 c" "\n\t"
"sbci %[c1], 0 ; 1 c" "\n\t"
"sbci %[c2], 0 ; 1 c" "\n\t"
"sbci %[c3], 0 ; 1 c" "\n\t"
"brne 1b ; 2 c" "\n\t"
:
: [c3] "d"((uint8_t)(count >> 24)),
[c2] "d"((uint8_t)(count >> 16)),
[c1] "d"((uint8_t)(count >> 8)),
[c0] "d"((uint8_t)count)
);
}
// initialize Timer2 (used for button reading)
void timer2Init(void) {
TCNT2 = 0x00;
}
void timer2Start(void) {
TCCR2 |= (1 << CS22) | (1 << CS21); // prescaller 256 => ~244 interrupts/s
TIMSK |= (1 << TOV2); // enable overflow interrupts
}
void timer2Stop(void) {
TCCR0 &= ~((1 << CS22) | (1 << CS21)); // stop
TIMSK &= ~(1 << TOV2); // disable overflow interrupts
}
// External interrupts service routines
// used to stop DDS in the inline ASM by setting
// CPHA bit in SPCR register
ISR(INT0_vect) {
SPCR |= (1 << CPHA);
}
ISR(INT1_vect) {
SPCR |= (1 << CPHA);
}
ISR(INT2_vect) {
SPCR |= (1 << CPHA);
}
// called every 4.1 ms, takes ~4 us
void checkButtons(void) {
++buttonState.now;
enum Button newButton;
if(bit_is_clear(BPIN, UP))
newButton = Button_Up;
else if(bit_is_clear(BPIN, RIGHT))
newButton = Button_Right;
else if(bit_is_clear(BPIN, DOWN))
newButton = Button_Down;
else if(bit_is_clear(BPIN, LEFT))
newButton = Button_Left;
else if(bit_is_clear(BPIN, START))
newButton = Button_Start;
else if(bit_is_clear(BPIN, OPT))
newButton = Button_Opt;
else
newButton = Button_None;
if(buttonState.pressed != newButton) {
bool ignore = false;
if(newButton == Button_None) {
if((buttonState.now - buttonState.pressedTime) < BUTTON_UNBOUNCE)
ignore = true;
}
if(!ignore) {
buttonState.pressedTime = buttonState.now;
buttonState.pressed = newButton;
buttonState.processed = false;
buttonState.autoTime = buttonState.now;
buttonState.autoRepeat = false;
}
}
else if(buttonState.pressed != Button_None) {
if( (!buttonState.autoRepeat && ((buttonState.now - buttonState.pressedTime) >= BUTTON_AUTO_START))
|| ( buttonState.autoRepeat && ((buttonState.now - buttonState.pressedTime) >= BUTTON_AUTO_REPEAT)))
{
buttonState.processed = false;
buttonState.autoTime = buttonState.now;
buttonState.autoRepeat = true;
}
}
}
void saveSettings(void) {
eeprom_update_block(&config, EE_CONFIG, sizeof(config));
}
void loadSettings(void) {
if(eeprom_read_byte((uint8_t*)EE_INIT) != 'T') {
// save the initial hard-coded values
saveSettings();
eeprom_write_byte((uint8_t*)EE_INIT, 'T'); // marks once that eeprom init is done
}
eeprom_read_block(&config, EE_CONFIG, sizeof(config));
}
inline bool isHsOutputEnabled(void)
{
return (config.syncOut != SyncOut_Trigger);
}
inline void setHsDirection(void)
{
if(isHsOutputEnabled()) {
HSDDR |= _BV(HS); // configure HS as output
}
else {
HSDDR &= ~_BV(HS); // configure HS as input
}
}
inline void syncPulse(void)
{
HSPORT |= (1 << HS);
HSPORT &= ~(1 << HS);
}
void buttonNop(void) {
}
void onNewMenuEntry(void) {
memcpy_P(&menuEntry, &MENU[config.menuEntry], sizeof(menuEntry));
buttonHandlers = &menuEntry.buttonHandlers;
LCDclr();
CopyStringtoLCD(menuEntry.title, 0, 0);
menuEntry.updateDisplay();
}
void menu_onUp(void) {
if(!running) {
if(config.menuEntry == 0) config.menuEntry = MENU_SIZE - 1;
else --config.menuEntry;
onNewMenuEntry();
}
}
void menu_onDown(void) {
if(!running) {
++config.menuEntry;
if(config.menuEntry == MENU_SIZE) config.menuEntry = 0;
onNewMenuEntry();
}
}
void onNewOptMenuEntry(void) {
memcpy_P(&menuEntry, &OPT_MENU[optMenuEntryNum], sizeof(menuEntry));
buttonHandlers = &menuEntry.buttonHandlers;
LCDclr();
CopyStringtoLCD(menuEntry.title, 0, 0);
menuEntry.updateDisplay();
}
void menu_onOpt(void) {
if(!running) {
optMenuEntryNum = 0;
onNewOptMenuEntry();
}
}
void optMenu_onUp(void) {
if(!running) {
if(optMenuEntryNum == 0) optMenuEntryNum = OPT_MENU_SIZE - 1;
else --optMenuEntryNum;
onNewOptMenuEntry();
}
}
void optMenu_onDown(void) {
if(!running) {
++optMenuEntryNum;
if(optMenuEntryNum == OPT_MENU_SIZE) optMenuEntryNum = 0;
onNewOptMenuEntry();
}
}
void optMenu_onOpt(void) {
optMenuEntryNum = (uint8_t)-1;
onNewMenuEntry();
}
void displaySignalStatus(void) {
if(running)
CopyStringtoLCD((config.syncOut == SyncOut_Trigger) ? MNTRIG : MNON, 13, 1);
else
CopyStringtoLCD(MNOFF, 13, 1);
}
void showFreq(double freq) {
LCDGotoXY(0, 1);
printf("%10.3fHz", freq);
}
void signal_updateDisplay(void) {
showFreq(config.freq);
displaySignalStatus();
}
void signal_onLeft(void) {
config.freq -= config.freqStep;
if(config.freq < MIN_FREQ)
config.freq = MIN_FREQ;
signal_updateDisplay();
}
void signal_onRight(void) {
config.freq += config.freqStep;
if(config.freq > MAX_FREQ)
config.freq = MAX_FREQ;
signal_updateDisplay();
}
void disableMenu(void) {
while(buttonState.pressed != Button_None); // wait until button release, otherwise the release interrupt will stop the generation
GICR |= (1 << INT0) | (1 << INT1) | (1 << INT2); // set external interrupts to enable stop or modify
timer2Stop(); // menu inactive
}
void enableMenu(void) {
GICR &= ~((1 << INT0) | (1 << INT1) | (1 << INT2)); // stop external interrupts
timer2Start(); // menu active
}
void signal_start(void) {
saveSettings();
running = true;
menuEntry.updateDisplay();
disableMenu();
}
uint32_t freqToAcc(double freq, uint8_t ticks) {
double resolution = (double)CPU_FREQ / ticks / ((uint32_t)1 << ACC_FRAC_BITS) / SIGNAL_BUFFER_SIZE;
return freq/(resolution / config.freqCal);
}
double accToFreq(uint32_t acc, uint8_t ticks) {
double resolution = (double)CPU_FREQ / ticks / ((uint32_t)1 << ACC_FRAC_BITS) / SIGNAL_BUFFER_SIZE;
return (double)acc * (resolution / config.freqCal);
}
void signal_recheckButtons(void) {
enableMenu();
while(buttonState.pressed != Button_None) {
processButton();
}
disableMenu();
}
inline bool waitTrigger(void) {
if(config.syncOut != SyncOut_Trigger) return true;
HSDDR &= ~_BV(HS); // configure HS as input
SPCR &= ~(1 << CPHA);
uint32_t count = delayMsToCount(config.triggerDelay);
if(count == 0) {
while(true) {
if(bit_is_set(HSPIN, HS)) return true;
if(bit_is_set(SPCR, CPHA)) return false;
}
}
else {
while(true) {
if(bit_is_set(HSPIN, HS)) break;
if(bit_is_set(SPCR, CPHA)) return false;
}
delayCount(count);
return true;
}
}
void signal_continue(bool tryToCorrect) {
uint32_t ticks = (config.syncOut == SyncOut_Multiple) ? OUT_SYNC_TICKS : OUT_TICKS;
uint32_t acc = freqToAcc(config.freq, ticks);
if(acc == 0) acc = 1;
if(tryToCorrect && config.freqMode == FreqMode_Jitter) {
// try to minimize jitter
uint64_t k = ((uint64_t)UINT32_MAX + 1) / acc;
acc = ((uint64_t)UINT32_MAX + 1) / k;
double freq = accToFreq(acc, ticks);
showFreq(freq);
}
SPCR &= ~(1 << CPHA); // clear CPHA bit in SPCR register to allow DDS
switch(config.syncOut) {
case SyncOut_Single:
syncPulse();
// continue
case SyncOut_Off: