forked from mausi-mick/AD9833-Simple-Function-Generator-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCCS_AD9833_AD9850_Nano_00.ino
2240 lines (1908 loc) · 72.7 KB
/
CCS_AD9833_AD9850_Nano_00.ino
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
//
//---------------------------------------------------------------------------------------
// Thanks:
//
// Peter Balch : https://www.instructables.com/Signal-Generator-AD9833/ : AD9833 Source, flow-control, sweep, ...
// fhdm-devels : https://www.instructables.com/Simple-AD9833-Based-Signal-Generator/ : PinChangeInterrupt
// GreatScottLab: https://www.instructables.com/DIY-FunctionWaveform-Generator/ : HD44780 display
// https://omerk.github.io/lcdchargen/ : Pixel-Generator HD44780 LCD Modules
// Interrupt-based (switches (PCI) by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
//---------------------------------------------------------------------------------------
// subject to the GNU General Public License
//---------------------------------------------------------------------------------------
// Developped with Arduino IDE 1.8.4 / 1.8.13
//---------------------------------------------------------------------------------------
//
// CCS_AD9833_Nano_00: 2022_01_05: copy from CCS_NanO_08 and AD9833_PCI_Nano_HD44780_02
// CCS_AD9833_Nano_01: 2022_01_08: copy from 00. Test freq-Lo with Steps like current-steps
// CCS_AD9833_Nano_02: 2022_01_22: copy from 01. Test current-source
// CCS_AD9833_Nano_03: 2022_01_25: copy from 02 and AD9850_Nano_PCI_01: Integration Sine-Gen. AD9850
// CCS_AD9833_AD9850_Nano_00: 2022_01_25: copy from CCS_AD9833_Nano_03:
//
#include <SPI.h> // for DAC and AD9833
#include <AD9833.h> // test
#include <MCP4822.h> // DAC 12 bit
#include <Encoder.h>
#include <Wire.h> // Wire for I2C
#include <LiquidCrystal_I2C.h> // HD55780 LCD1602
//------------------------ Encoder with ext. Interrupt:
const int ENC_CW = 2; // ext.Int. Pin D2 <<-- change with D3 wrong direction ???
const int ENC_CCW = 3; // ext.int. Pin D3 <<-- change with D2 wrong direction ???
//------------------------ switches with Pin-Change-Interrupt (PCI):
const int RESET = 4; // AD9850 Modul
const int DATA = 5; // AD9850 Modul
const int FQ_UD = 6; // AD9850 Modul
const int W_CLK = 7; // AD9850 Modul
const int SWITCH_R = 8; // switch a resistor parallel to Rm for increasing current ##################
const int DAC_CS = 9; // MCP4822 12-bit Dac
//------------------------ AD9833 SPI-Pins
const int SG_FSYNC = 10; // AD9833 SPI-CS
const int SG_DATA = 11; // AD9833 SPI-MOSI
const int SG_CLK = 13; // AD9833 SPI-SCLK
const int ENC_A0 = A0; // PCINT8 : Pin A0 // rotary encoder steps
const int ENC_A1 = A1; // PCINT9 : Pin A1 // rotary encoder steps
const int ENC_BUT = A2; // PCINT10 : Pin A2
#define AD9850_CLOCK 125000000 // Module AD9850 crystal frequency.
AD9833 ad9(SG_FSYNC); // SCK and MOSI must CLK and DAT pins on the AD9833 for SPI
LiquidCrystal_I2C lcd(0x27, 16, 2); // HEX-Address 0x27 (or 0x3F), 16 rows , 2 lines_I2C lcd(0x27, 20, 4)"
MCP4822 dac= MCP4822(DAC_CS); // CS-Pin 9, AD9833 on Pin 10
Encoder myEnc(ENC_CW,ENC_CCW);
volatile int EncPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
volatile int oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
volatile byte enc_dir = 'r';
volatile uint8_t but_p = 0,but_c=0;
volatile unsigned long incr = 0;
volatile long unsigned int freq_s = 1000,freq=1000; // Set initial frequency.
volatile long unsigned int freqOld = freq;
volatile uint8_t s_Hz=0,s_step=0,s_upd = 1;
volatile int stepPointer = 0;
//-----------------------------------------------------------------------------
// Global Constants
//-----------------------------------------------------------------------------
const byte numberOfDigits = 8; // number of digits in the frequency
const uint16_t wft_s = 0x2000; // sine
const uint16_t wft_t = 0x2002; // triangle
const uint16_t wft_q = 0x2028; // square
const uint16_t wft_h = 0x2020; // halfsquare
//const int wSine = 0b0000000000000000;
//const int wTriangle = 0b0000000000000010;
//const int wSquare = 0b0000000000101000;
const int step_t[] = {1,2,3,5,6,9,10,15,30}; // step-sine 0...8
const uint16_t sweep_t[] = {1000,2000,5000,10000,20000,50000}; // sweep-pos
const uint16_t delay_t[] = {100,200,500,1,2,5,10,20,50,100,200,500,1000}; // delay
const long f_st[] = { 0,10,100,1000,10000,100000,1000000}; // strt frequency
const char* stepText[] = {" 1 Hz", " 2 Hz", " 5 Hz", " 10 Hz", " 20 Hz", " 50 Hz", " 100 Hz", " 200 Hz", " 500 Hz",
" 1 kHz", " 2 kHz", " 5 kHz", " 10 kHz", " 20 kHz", " 50 kHz", "100 kHz", "200 kHz", "500 kHz"};
String units = stepText[stepPointer];
// 0 1 2 3 4 5 6 7 8 9 10 11 12
const char com_t[] = {'S','T','Q','L','C','H','M','P','D','G','R','X',' '}; // Menu-Commands:
// 0 'S': start Sine
// 1 'T': start Triangle
// 2 'Q': start Square
// 3 'L': set Freq.-Low
// 4 'C': set Freq.-Low scroll contin. or steps
// 5 'H': set Freq.-High
// 6 'M': swap Freq. Low <--> High
// 7 'P': set sweep param. up/down, step-nr
// 8 'D': set / change Delay sweep
// 9 'G': sweep
//10 'R': Reset AD9833
//11 'X': return
const char *str_com[]= {"Sine ","Triangle ","Square ",
"set Low freq. ","scroll Stepw >=1","set High freq. ",
"swap Lo/Hi freq.","set sweep param.","set delay sweep ",
"sweep ","Reset AD9833 ","return "};
// HD44780 special-signs
const byte C_C_1_Arrow_D[8] = { // from: https://omerk.github.io/lcdchargen/
0b01110, 0b01010, 0b01010, 0b01010, 0b01010, 0b11011, 0b01110, 0b00100};
const byte C_C_2_Arrow_U[8] = {
0b00100, 0b01010, 0b11011, 0b01010, 0b01010, 0b01010, 0b01010, 0b01110};
const byte C_C_3_Arrow_L[8] = {
0b00010, 0b00100, 0b01111, 0b11000, 0b01111, 0b00100, 0b00010, 0b00000};
const byte C_C_4_Arrow_R[8] = {
0b01000, 0b00100, 0b11110, 0b00001, 0b11110, 0b00100, 0b01000, 0b00000};
const byte C_C_5_Tilde[8] = {
0b00000, 0b00000, 0b00000, 0b01000, 0b10101, 0b00010, 0b00000, 0b00000};
/*
const byte C_C_5_Line_D[8] = {
0b00000, 0b00000, 0b11111, 0b00000, 0b11111, 0b00000, 0b00000, 0b00000};
const byte C_C_6_Sine_1[8] = { // Sinus oben
0b01110, 0b11011, 0b10001, 0b00000, 0b00000, 0b00000, 0b00000, 0b00000};
const byte C_C_7_Sine_2[8] = {// Sinus oben links nach unten
0b00000, 0b00000, 0b10000, 0b11000, 0b01110, 0b00011, 0b00001, 0b00000};
const byte C_C_8_Sine_3[8] = {// Sinus unten
0b00000, 0b00000, 0b00000, 0b00000, 0b00000, 0b10001, 0b11011, 0b01110};
const byte C_C_9_Sine_4[8] = {// Sinus von unten links nach oben rechts
0b00000, 0b00000, 0b00011, 0b01111, 0b01100, 0b11000, 0b10000, 0b00000};
*/
//-----------------------------------------------------------------------------
// Global Variables
//-----------------------------------------------------------------------------
uint8_t freqSave[numberOfDigits] = {0,0,1,0,0,0,0,0}; // 100 Hz
uint8_t freqSGLo[numberOfDigits] = {0,0,1,0,0,0,0,0}; // 100 Hz
uint8_t freqSGHi[numberOfDigits] = {0,0,0,0,2,0,0,0}; // 20000 Hz
uint8_t func_t[] = {0,1,2,3,4};
uint8_t digi_t[] = {0,0,0,0};
uint32_t time_t[] = {0,1,2,5,10,20,50,100,200,500}; // µs 0...9
uint32_t time_m[] = {1,2,5,10,20,50,100,200,500,800}; // ms 0...9 // 10 ... 19
uint32_t time_s[] = {1,2,5,10,20,30,50,60,100,120}; // s 0...9 // 20 ... 29
int32_t fact_t[] = {1,2,5,10,20,50,100,200,500,1000};
long dig4_0_t[] = {0,0,0,0,0};
long ilm=0,ilt=0,ifu=9,istep=0,zconst,fox=100,fstart=0,f_lowo=100,gainy=10,oldPos=-999,newPos=0;
int8_t pos_i=15,pos_o=15,dig_o=0,dig_n=0,s_sel,ila=2,ido=10,ioo=0,igo=10,line_d=0,sel_c=0,hz_nr=0,sweep_r=0;;
int16_t iso=10,ist=0,zwave_t = wft_s; // sine
int32_t time_del=10,delay_s=0,off_s=0,off_r=0;
unsigned long time_f,time_l,time_d,time_n;
long fact=1,fact_o=1,iconst=0,fac_i=0,offs=0;
uint8_t s_first=0,digi,s_ms=0,s_del=0,s_stepa=0,s_par=0,s_end=0,s_kHz=0,s_sweep=0;
uint8_t s_low=0,sel_o=0,dmax=0,freq_zw=0,tab_nr=0,s_sine=0;
//int waveType = wSine;
char c = ' ',com_o = 'S'; // old command
int i_del=3,i_sw=2,grad,step_s=1,stap=0,endp=1,iao,ieo; // 4 ?
uint8_t posc_t[] = {7, 5};
int delay_micros = 1000; // 1ms
int sweep_nr = 5000;
int SG_iSweep,SG_nSweep;
int16_t sinx = 0,ils;
float wert=0.0,gainf=1.0;
#define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
// transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
void tfr_byte(byte data) {
for (int i = 0; i < 8; i++, data >>= 1) {
digitalWrite(DATA, data & 0x01);
pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
}
}
void sendFrequency(double frequency) {
int32_t freq1 = frequency * 4294967295/AD9850_CLOCK; // note 125 MHz clock on 9850
for (int b = 0; b < 4; b++, freq1 >>= 8) {
tfr_byte(freq1 & 0xFF);
}
tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
pulseHigh(FQ_UD); // Done! Should see output
}
//-----------------------------------------------------------------------------
// Main routines
// The setup function
//-----------------------------------------------------------------------------
void setup() {
// Serial.begin(115200); // Open serial port
pinMode(ENC_CW, INPUT_PULLUP);
pinMode(ENC_CCW, INPUT_PULLUP);
pinMode(ENC_A0, INPUT_PULLUP); // A0 PCINT8
pinMode(ENC_A1, INPUT_PULLUP); // A1 PCINT9
pinMode(ENC_BUT, INPUT_PULLUP); // A2 PCINT10
cli();
// Configure interrupt and enable for rotary encoder.
// PCICR |= (1 << PCIE2); // Port: D
// PCMSK2 |= (1 << PCINT18) | (1 << PCINT19); // D2, D3
PCICR = 0b00000010; // Port: C / PCIE1: Pin Change Interrupt Enable 1
PCMSK1 = 0b00000111; // Enable Pin Change Interrupt for A0,A1,A2 // - A3
sei();
/*
PCICR = 0b00000100; // PCIE2: Pin Change Interrupt Enable 2 // Port D (D0...D7)
//PMMSK2 = 0b00011100; // Enable Pin Change Interrupt for D2 - D4
//PMMSK2 = 0b00001100; // Enable Pin Change Interrupt for D2 - D3
PCMSK2 = 0b00010000; // Enable Pin Change Interrupt for D4
*/
pinMode(FQ_UD, OUTPUT); // Configure pins for output to AD9850 module.
pinMode(W_CLK, OUTPUT);
pinMode(DATA, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(SWITCH_R, OUTPUT);
digitalWrite(SWITCH_R, LOW); // sets the digital pin off
pinMode(DAC_CS, OUTPUT);
digitalWrite(DAC_CS, HIGH); // sets the digital pin HIGH inactiv
lcd.begin(); // HD44780
// HD44780 special signs
lcd.createChar(1, C_C_1_Arrow_D); //########### Down
lcd.createChar(2, C_C_2_Arrow_U); //########### Up
lcd.createChar(3, C_C_3_Arrow_L); //########### Left
lcd.createChar(4, C_C_4_Arrow_R); //########### Right
lcd.createChar(5, C_C_5_Tilde); //########### Tilde
//lcd.createChar(5, C_C_5_Line_D); //########### Line
//lcd.createChar(6, C_C_6_Sine_1); //########### Sine Top
//lcd.createChar(7, C_C_7_Sine_2); //########### Sine Down
//lcd.createChar(1, C_C_8_Sine_3); //########### Sine Bottom
//lcd.createChar(2, C_C_9_Sine_4); //########### Sine Up
lcd.backlight(); //switch-on backlight, lcd.noBacklight(); switch-off backlight
int8_t ilx=0,ily=1,ilz;
for (uint8_t ill = 0;ill<5;ill++) {
lcd.setCursor(0,ilx); lcd.print("Const.Cur.Source");
lcd.setCursor(0,ily); lcd.print("AD9833 Func.Gen.");
ilz = ily;
ily = ilx;
ilx = ilz;
delay(500);
}
// SPI.beginTransaction (SPISettings (8000000, MSBFIRST, SPI_MODE0)); // for AD9833 max 12.5 MHz, MCP4822: 20 MHz
// SPI.begin();
delay(200);
sel_c = 11;// "Sine " at start-display
oldEncPos = 999;
fillBlank(0,0,16);
fillBlank(0,1,16);
}
void startDAC() {
SPI.beginTransaction (SPISettings (20000000, MSBFIRST, SPI_MODE0)); // for AD9833 max 12.5 MHz, MCP4822: 20 MHz
SPI.begin();
dac.begin();
// Configure analog outputs
dac.off_B();
dac.setGain2X_A(); // _AB();
delay(200);
}
ISR (PCINT1_vect) { // Port C A0...A5 ??
// If interrupt is triggered by the button
if (!digitalRead(ENC_BUT)) but_p = 1; // Pin A2: Push-Button pressed
}
/*
ISR (PCINT2_vect) {
// If interrupt is triggered by the button
if (!digitalRead(ENC_BUT)) but_p = 1;; // Pin4: Push-Button pressed
}
*/
void fillBlank(uint8_t col, uint8_t line, uint8_t nr) {
if (nr == 0) return;
lcd.setCursor(col,line);
for (uint8_t l=0;l<nr;l++) {
lcd.print(" ");
}
}
//-----------------------------------------------------------------------------
//returns 10^y
//-----------------------------------------------------------------------------
unsigned long Power(int y) {
unsigned long t = 1;
for (byte i = 0; i < y; i++)
t = t * 10;
return t;
}
//-----------------------------------------------------------------------------
//calculate the frequency from the array.
//-----------------------------------------------------------------------------
unsigned long calcFreq(byte* freqSG) {
unsigned long i = 0;
for (byte x = 0; x < numberOfDigits; x++)
i = i + freqSG[x] * Power(x);
return i;
}
//-----------------------------------------------------------------------------
// calculate freq-array from the frequency (8-digits)
//-----------------------------------------------------------------------------
void printFreq(long freq) {
long fo=freq,pot;
int8_t zw=0,tab_l=numberOfDigits-1;// 8 tab-length - 1: 7... 0
dmax = 0;
for (int8_t ilc=tab_l;ilc>=0;ilc--) { //4...0
// freqSGLo[ilc] = 0;
pot = 1;
if (ilc > 0) pot = Power(ilc);
zw = fo/pot;
if (zw > 0) {
if (dmax == 0) dmax = ilc;
fo = fo - zw*pot;
// freqSGLo[ilc] = zw;
lcd.print(zw);
}
else {
if (dmax == 0) lcd.print(" ");
else lcd.print(zw);
}
}
}
//-----------------------------------------------------------------------------
// calculate iconst-array from the iconst (5-digits)
//-----------------------------------------------------------------------------
void printIconst(long ic) {
long fo=ic,pot;
int8_t zw=0,tab_l=5-1; // 5=tab-length - 1: 4 ... 0
dmax = 0;
for (int8_t ilc=tab_l;ilc>=0;ilc--) { //4...0
dig4_0_t[ilc] = 0;
pot = 1;
if (ilc > 0) pot = Power(ilc);
zw = fo/pot;
if (zw > 0) {
if (dmax == 0) dmax = ilc;
fo = fo - zw*pot;
dig4_0_t[ilc] = zw;
lcd.print(zw);
}
else {
if (dmax == 0) lcd.print(" ");
else lcd.print(zw);
}
}
}
//-----------------------------------------------------------------------------
// calculate iconst-array from the iconst (5-digits)
//-----------------------------------------------------------------------------
void printIstep(long ist) {
if (ist >= 1000)
lcd.print(ist);
else if (ist >= 100) {
lcd.print(" ");
lcd.print(ist);
}
else if (ist >= 10) {
lcd.print(" ");
lcd.print(ist);
}
else {
lcd.print(" ");
lcd.print(ist);
}
}
/*
//-----------------------------------------------------------------------------
// SG_WriteRegister
//-----------------------------------------------------------------------------
void SG_WriteRegister(word dat) {
digitalWrite(SG_CLK, LOW);
digitalWrite(SG_CLK, HIGH);
digitalWrite(SG_FSYNC, LOW);
for (byte i = 0; i < 16; i++) {
if (dat & 0x8000)
digitalWrite(SG_DATA, HIGH);
else
digitalWrite(SG_DATA, LOW);
dat = dat << 1;
digitalWrite(SG_CLK, HIGH);
digitalWrite(SG_CLK, LOW);
}
digitalWrite(SG_CLK, LOW); //#HIGH
digitalWrite(SG_FSYNC, HIGH);
digitalWrite(SG_FSYNC, HIGH);
}
//-----------------------------------------------------------------------------
// SG_Reset
//-----------------------------------------------------------------------------
void SG_Reset() {
delay(100);
SG_WriteRegister(0x100);
delay(100);
}
//-----------------------------------------------------------------------------
// SG_freqReset
// reset the SG regs then set the frequency and wave type
//-----------------------------------------------------------------------------
void SG_freqReset(long frequency, int wave) {
long fl = frequency * (0x10000000 / 25000000.0);
SG_WriteRegister(0x2100);
SG_WriteRegister((int)(fl & 0x3FFF) | 0x4000);
SG_WriteRegister((int)((fl & 0xFFFC000) >> 14) | 0x4000);
SG_WriteRegister(0xC000);
SG_WriteRegister(wave);
waveType = wave;
}
//-----------------------------------------------------------------------------
// SG_freqSet
// set the SG frequency regs
//-----------------------------------------------------------------------------
void SG_freqSet(long frequency, int wave) {
long fl = frequency * (0x10000000 / 25000000.0);
SG_WriteRegister(0x2000 | wave);
SG_WriteRegister((int)(fl & 0x3FFF) | 0x4000);
SG_WriteRegister((int)((fl & 0xFFFC000) >> 14) | 0x4000);
}
*/
//-----------------------------------------------------------------------------
// SG_StepSweep
// increment the FG frequency
//-----------------------------------------------------------------------------
void SG_StepSweep(void) {
if (SG_iSweep > SG_nSweep) SG_iSweep = 0;
long f = exp((log(calcFreq(freqSGHi)) - log(calcFreq(freqSGLo)))*SG_iSweep/SG_nSweep + log(calcFreq(freqSGLo))) +0.5;
if (s_sine == 0) {
ad9.ApplySignal(zwave_t,REG0,f); //
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
}
else sendFrequency(f); // AD9850
// SG_freqSet(f, waveType);
SG_iSweep++;
}
//-----------------------------------------------------------------------------
// Sweep
// sweeps siggen freq continuously
// takes n mS for whole sweep
// SDC regs are saved and restored
// stops when push-button is pressed
//-----------------------------------------------------------------------------
void Sweep(int n) {
int fmin,fmax;
long f;
delay(100);
but_p = 0;
fmin = calcFreq(freqSGLo);
fmax = calcFreq(freqSGHi);
int i= n-1;
if (s_sine == 0) ad9.EnableOutput(true); //
if (sweep_r == 0) { // up
while (but_p == 0) {
for ( i=n-1;i>=0;i--) {
f=exp((log(fmax) - log(fmin)) * i/(n-1) + log(fmin)) + 0.5; //??
if (s_sine == 0) ad9.ApplySignal(zwave_t,REG0,f);
else sendFrequency(f); // AD9850
delayMicroseconds(delay_micros); //delay(1);
if (but_p == 1) goto end_sw;
}
}
}
else if (sweep_r == 1) { // down
while (but_p == 0) {
for ( i=0;i<=n-1;i++) {
f=exp((log(fmax) - log(fmin)) * i/(n-1) + log(fmin)) + 0.5; //??
if (s_sine == 0) ad9.ApplySignal(zwave_t,REG0,f);
else sendFrequency(f); // AD9850
delayMicroseconds(delay_micros); //delay(1);
if (but_p == 1) goto end_sw;
}
}
}
else if (sweep_r == 2) { // up and down
i = 0;
do {
f = exp((log(fmax) - log(fmin))*i/(n-1) + log(fmin)) + 0.5; //
if (s_sine == 0) ad9.ApplySignal(zwave_t,REG0,f); //
else sendFrequency(f); // AD9850
delayMicroseconds(delay_micros); //delay(1);
i++;
if (i >= n) i = 0;
if (but_p == 1) goto end_sw;
} while (but_p == 0) ;//!Serial.available());
}
end_sw:
if (s_sine == 0) {
ad9.ApplySignal(zwave_t,REG0,fmin); // T
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
}
else sendFrequency(fmin); // AD9850
//SG_freqSet(calcFreq(freqSGLo), waveType);
}
//-----------------------------------------------------------------------------
//
//
//-----------------------------------------------------------------------------
void controlAD9833() {
s_end = 0;
lcd.setCursor(0,1);lcd.print(" ");
sel_c = 11; // start with sine ???
while(s_end == 0) {
but_p = 0;
s_first = 0;
while(but_p == 0) { // not pressed
EncPos = myEnc.read(); //###################################
if (oldEncPos != EncPos) {
if (s_first == 0) s_first = 1;
else {
s_first = 0;
if (oldEncPos < EncPos) sel_c++;
else sel_c--;
if (sel_c < 0) sel_c = 11;
else if (sel_c > 11) sel_c = 0;
fillBlank(0,1,5); //lcd.setCursor(0,1);lcd.print(" ");
lcd.setCursor(0,1);lcd.print(str_com[sel_c]);
}
oldEncPos = EncPos;
}
} // while(but_p
TabCommand(com_t[sel_c]); // execute commands ####################################
// if (s_end == 1) return;
}
}
//-----------------------------------------------------------------------------
// TabCommand
// if a byte is available in teh serial input buffer
// execute it as a command
//-----------------------------------------------------------------------------
void TabCommand(char com_x) {
int8_t itt;
c = com_x; // globale variable
switch_s:
switch (com_x) {
case 'S': // 0 run sine
lcd.setCursor(0,1); lcd.print(str_com[0]);
freq_Lo_to_Display(); // Hz, MHz;
if (s_sine == 0) { // AD9833
ad9.ApplySignal(wft_s,REG0,fox); // SINE_WAVE
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
}
else sendFrequency(fox); // AD9850
lcd.setCursor(0,1); lcd.print("Sine start ");
zwave_t = wft_s;
delay(50);
com_o = 'S';
sel_o = 0;
break;
case 'T': // 1 run triangle
if (s_sine == 1) break;
lcd.setCursor(0,1); lcd.print(str_com[1]);
freq_Lo_to_Display();// Hz, MHz
ad9.ApplySignal(wft_t,REG0,fox); // TRIANGLE_WAVE
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
lcd.setCursor(0,1); lcd.print("Triangle start ");
zwave_t = wft_t;
delay(50);
com_o = 'T';
sel_o = 1;
break;
case 'Q': // 2 run Square
if (s_sine == 1) break;
lcd.setCursor(0,1);lcd.print(str_com[2]);
freq_Lo_to_Display();// Hz, MHz
ad9.ApplySignal(wft_q,REG0,fox); // SQUARE_WAVE
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
lcd.setCursor(0,1); lcd.print("Sqare start ");
zwave_t = wft_q;
delay(50);
com_o = 'Q';
sel_o = 2;
break;
case 'L': // 3 set Fequency Low
// lcd.setCursor(0,1);//lcd.print(str_com[3]);
lcd.setCursor(10,0);lcd.print(" Hz ");
s_low = 0;
changeFreqLoHi();
f_lowo = fox;
for (itt=0;itt<numberOfDigits;itt++) { // 0 ... 7
freqSave[itt] = freqSGLo[itt];
}
com_x = com_o;
sel_c = sel_o;
goto switch_s; //####################
break;
case 'C': // 4 set Fequency Low continually or steps
// lcd.setCursor(0,1);//lcd.print(str_com[3]);
lcd.setCursor(10,0);lcd.print(" Hz ");
istep = 0;
for(uint8_t ilg = 0;ilg<=3;ilg++) {
digi_t[ilg] = 0;
}
s_low = 3;
changeFreqLoContin(); //###################################
fox = f_lowo;
for (itt=0;itt<numberOfDigits;itt++) { // 0 ... 7
freqSGLo[itt] = freqSave[itt];
}
com_x = com_o;
sel_c = sel_o;
goto switch_s; //####################
break;
case 'H': // 5 set Fequency High
// lcd.setCursor(0,1);//lcd.print(str_com[4]);
lcd.setCursor(10,0);lcd.print(" Hz ");
s_low = 1;
changeFreqLoHi(); //##########################
com_x = com_o;
sel_c = sel_o;
goto switch_s; //####################
break;
/* case 'F': // 5 set Fequency Low and High
// lcd.setCursor(0,1);//lcd.print(str_com[3]);
lcd.setCursor(10,0);lcd.print(" Hz ");
s_low = 2;
changeFreqLoHi();
com_x = com_o;
sel_c = sel_o;
goto switch_s; //####################
break;
*/
case 'M': // 6 swap frequencys high and low
lcd.setCursor(0,1);lcd.print("swap High ");lcd.write(3);lcd.write(4);lcd.print(" Low");
lcd.setCursor(0,0);lcd.print(" ");
for (int i=7; i>=0; i--) {
freq_zw = freqSGHi[i]; // save old High
freqSGHi[i] = freqSGLo[i];
freqSGLo[i] = freq_zw;
lcd.print(freq_zw);
// Serial.println(freq_zw);
}
freq_zw = posc_t[0]; // cursor-position Low/High
posc_t[0] = posc_t[1];
posc_t[1] = freq_zw;
lcd.print(" Hz ");
if (s_sine == 0) {
ad9.ApplySignal(zwave_t,REG0,fox);
ad9.EnableOutput(true); // Turn ON the output - it defaults to OFF
}
else sendFrequency(fox); // AD9850
freq_Lo_to_Display(); // frequency in Hz, MHz
lcd.setCursor(14,0);lcd.print("ok");
delay(500);
lcd.setCursor(14,0);lcd.print(" ");
if (com_o == ' ' ) com_o = 'S';
com_x = com_o;
goto switch_s; //####################
break;
case 'P': // 7 set param. sweep up/down and step-nr
lcd.setCursor(0,1);lcd.print(str_com[7]);// lcd.print("sweep");
changeSweepNr();
lcd.setCursor(14,1);lcd.print("ok");
delay(1000);
fillBlank(10,0,7);
fillBlank(10,1,6);
// if (com_o == ' ' ) com_o = 'S';
com_x = 'G';
goto switch_s; //####################
break;
case 'D': // 8 set delay
lcd.setCursor(0,1);lcd.print(str_com[8]); // lcd.print("delay(ms)"); //\344s/ms)"); // \344 = µ
lcd.setCursor(10,1);lcd.print(" ");
changeDelay();
lcd.setCursor(14,1);lcd.print("ok");
delay(1000);
lcd.setCursor(10,0);lcd.print(" ");
lcd.setCursor(14,1);lcd.print(" ");
// if (com_o == ' ' ) com_o = 'S';
com_x = 'G'; //com_o;
goto switch_s; //####################
break;
case 'G': // 9 sweep up/down, steps
lcd.setCursor(0,1);lcd.print(str_com[9]);
lcd.setCursor(6,1);
if (sweep_r == 0) lcd.write(2); // up
else if (sweep_r == 1) lcd.write(1); // down
else lcd.write(5); // Tilde// direction: up,down, Tilde
Sweep(sweep_nr);
lcd.setCursor(14,0);lcd.print(" ");
if (com_o == ' ' ) com_o = 'S';
if (com_o == 'G' ) com_o = 'S';
com_x = com_o;
goto switch_s; //####################
break;
case 'R': // 10 Reset AD9833
if (s_sine == 1) break;
lcd.setCursor(0,1);lcd.print(str_com[10]); // "Reset ");
fillBlank(0,0,16);
ad9.Reset();
break; // SigGen reset
case 'X': // 11 Return main
lcd.setCursor(0,1);lcd.print(str_com[11]); // "Return ");
fillBlank(0,0,16);
delay(200);
s_end = 1; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<######################################
fillBlank(0,1,16);
return;
break; // SigGen reset
default: // return;
break;
}
}
/*
//-----------------------------------------------------------------------------
// InitSigGen
//-----------------------------------------------------------------------------
void InitSigGen(void) {
pinMode(SG_DATA, OUTPUT);
pinMode(SG_CLK, OUTPUT);
pinMode(SG_FSYNC, OUTPUT);
digitalWrite(SG_FSYNC, HIGH);
digitalWrite(SG_CLK, LOW); //#HIGH
SG_Reset();
SG_freqReset(calcFreq(freqSGLo), waveType);
}
*/
void changeDelay() {
int z_micros = 0;
oldEncPos = 999;
EncPos = 0;
but_p = 0;
lcd.setCursor(0,0); lcd.blink();
lcd.setCursor(0,0); lcd.print("delay: ");
lcd.setCursor(7,0); lcd.print(delay_t[i_del]);
if (i_del < 3) lcd.print(" \344s");
else lcd.print(" ms");
// while(1==1) {
while (but_p == 0) { // button-switch not pressed
EncPos = myEnc.read(); //###################################
if (oldEncPos != EncPos) {
if (oldEncPos < EncPos) enc_dir = 'r';
else enc_dir = 'l';
if (enc_dir == 'r') if (i_del < 12) i_del++;
if (enc_dir == 'l') if (i_del > 0) i_del--;
lcd.setCursor(7,0);lcd.print(" ");
lcd.setCursor(7,0);lcd.print(delay_t[i_del]);
if (i_del < 3) lcd.print(" \344s");
else lcd.print(" ms");
if (i_del <= 3) z_micros = delay_t[i_del]; // µs
else if (i_del <= 11) z_micros = delay_t[i_del] * 1000; // ms
else z_micros = delay_t[i_del] * 1000000; // s
oldEncPos = EncPos;
lcd.setCursor(7,0);
}
} // while
delay_micros = z_micros;
return;
// } // while
}
void changeSweepNr() {
int z_number = 0;
uint8_t s_switch=0;
oldEncPos = 999;
EncPos = 0;
but_p = 0;
sweep_r = 2;
lcd.setCursor(0,1); lcd.print(" ...steps: ");
lcd.setCursor(0,0); lcd.print("sweep-up/down: ");
lcd.setCursor(15,0); lcd.blink();
lcd.setCursor(15,0);
if (sweep_r == 0) lcd.write(2); // up
else if (sweep_r == 1) lcd.write(1); // down
else lcd.write(5); // Tilde
while (but_p == 0) { // button-switch not pressed
EncPos = myEnc.read(); //###################################
if (oldEncPos != EncPos) {
if (s_switch == 0) s_switch = 1;
else {
s_switch = 0;
lcd.setCursor(15,0);
if (enc_dir == 'r') sweep_r++;
if (enc_dir == 'l') sweep_r--;
if (sweep_r < 0) sweep_r = 2;
if (sweep_r > 2) sweep_r = 0;
if (sweep_r == 0) lcd.write(2); // up
else if (sweep_r == 1) lcd.write(1); // down
else lcd.write(5); // Tilde
}
oldEncPos = EncPos;
}
}
oldEncPos = 999;
// lcd.print(" nr");
// while(1==1) {
but_p = 0;
lcd.setCursor(11,1);
s_switch = 0;
while (but_p == 0) { // button-switch not pressed
EncPos = myEnc.read(); //###################################
if (oldEncPos != EncPos) {
if (s_switch == 0) s_switch = 1;
else {
s_switch = 0;
if (oldEncPos < EncPos) enc_dir = 'r';
else enc_dir = 'l';
if (enc_dir == 'r') if (i_sw < 5) i_sw++;
if (enc_dir == 'l') if (i_sw > 0) i_sw--;
fillBlank(11,1,5); //lcd.setCursor(7,0);lcd.print(" ");
if (sweep_t[i_sw] >= 10000) lcd.setCursor(11,1);
else lcd.setCursor(12,1);
lcd.print(sweep_t[i_sw]);
z_number = sweep_t[i_sw];
}
oldEncPos = EncPos;
lcd.setCursor(11,1);
}
} // while
sweep_nr = z_number;
return;
// } // while
}
void changeFreqLoHi() {
uint8_t il;
fillBlank(0,0,16); //lcd.setCursor(0,0);lcd.print(" ");
fillBlank(10,1,6); //lcd.setCursor(10,1);lcd.print(" ");
lcd.setCursor(0,0); lcd.print("Lo");
for (il=0;il<numberOfDigits;il++) {
lcd.print(freqSGLo[numberOfDigits-1-il]);
}
if (s_low != 3) {
lcd.print(" Hz ");
lcd.setCursor(0,1); lcd.print("Hi");
for (il=0;il<numberOfDigits;il++) {
lcd.print(freqSGHi[numberOfDigits-1-il]);
}
lcd.print(" Hz ");
}
//else fillBlank(0,1,9); // Lo
line_d = 0;
if (s_low == 3) { // continually rotation or Steps ################
fox = f_lowo;
return;
}
if (s_low == 0 or s_low == 2) scanPos(); // Low- Frequency #############################
if (s_low == 0) return;
line_d = 1;
scanPos(); // High-Frequency #############################
s_low = 0; // ??
}
void scanPos() { // decimal-positions of Low-frequency
uint8_t ilp;
long ild;
oldEncPos = 999;
EncPos = 999;//0;
pos_i = posc_t[line_d]; //?
while(1==1) {
but_p = 0;
s_first = 0;
lcd.setCursor( pos_i,line_d); lcd.blink();
while (but_p == 0) { // button-switch not pressed
EncPos = myEnc.read(); //###################################
if (oldEncPos != EncPos) {
if (s_first == 0) s_first = 1; // first switch / teeth
else {
s_first = 0;
if (oldEncPos < EncPos) { // 'r'
if (pos_i < 9) pos_i++; //
else {
pos_i = 2;
lcd.setCursor(2,line_d); // lcd.blink();
}
}
else pos_i--; // 'l'
lcd.setCursor( pos_i,line_d); // lcd.blink();
if (pos_i < 2) {
// lcd.blink_off();
if (pos_i == 1) pos_i = 1;
if (pos_i <= 0) { // == 0 is ok
but_p = 0;
while (but_p == 0) { } // button not pressed
but_p = 0;
goto pos_end;
}
}
}
oldEncPos = EncPos;