-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
840 lines (825 loc) · 27.5 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
//###########################################################################
// Author: Hamid Reza Tanhaei
// DSP Developing
// Device: TMS320F28033PNT
// Clock freq: 60MHz (input osc. freq. = 12MHz)
// ADC: 3MSPS
// FFT: 32point
//
#define Toggle1_Mux GpioCtrlRegs.GPAMUX2.bit.GPIO24
#define Toggle1_Dir GpioCtrlRegs.GPADIR.bit.GPIO24
#define Toggle1_Clr GpioDataRegs.GPACLEAR.bit.GPIO24
#define Toggle1_Set GpioDataRegs.GPASET.bit.GPIO24
#define Toggle1_Dat GpioDataRegs.GPADAT.bit.GPIO24
// Constants:
#define FFT32_Weights {255,0,250,-50,236,-98,212,-142,180,-180,142,-212,98,-236,50,-250,0,-255,-50,-250,-98,-236,-142,-212,-180,-180,-212,-142,-236,-98,-250,-50}
//###########################################################################
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include <stdio.h>
__interrupt void cpu_timer0_isr(void);
//__interrupt void xint1_isr(void);
void Initialize_Routine(void);
void Init_GPIOs(void);
void Init_ADC(void);
void ADC_Capture(void);
void FFT_32(void);
void FFT_16(void);
void Flush_Vectors(void);
void Reset_Device(void);
Uint16 volatile RealTime_Flag;
#pragma DATA_SECTION(perm_32, "VecSpace");
Uint16 perm_32[] = {0,32,16,48,8,40,24,56,4,36,20,52,12,44,28,60};
#pragma DATA_SECTION(perm_16, "VecSpace");
Uint16 perm_16[] = {0,16,8,24,4,20,12,28};
#pragma DATA_SECTION(ADC_Buffer, "VecSpace");
int16 ADC_Buffer[32];
#pragma DATA_SECTION(Weight, "VecSpace");
int16 Weight[32] = FFT32_Weights; // {255,0,250,-50,236,-98,212,-142,180,-180,142,-212,98,-236,50,-250,0,-255,-50,-250,-98,-236,-142,-212,-180,-180,-212,-142,-236,-98,-250,-50};
#pragma DATA_SECTION(fft_draft, "VecSpace");
int32 fft_draft[64];
#pragma DATA_SECTION(FFT_out_32, "VecSpace");
Uint16 FFT_out_32[16];
#pragma DATA_SECTION(FFT_out_16, "VecSpace");
Uint16 FFT_out_16[8];
//=============================================//
void main(void)
{
Init_GPIOs();
//
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
//
InitSysCtrl(); // Step 1. Initialize System Control: PLL, WatchDog, enable Peripheral Clocks
//
DINT; // Disable CPU interrupts
//
InitFlash();
//
InitPieCtrl(); // Initialize the PIE control registers to their default state. The default state is all PIE interrupts disabled and flags are cleared.
//
IER = 0x0000; // Disable CPU interrupts and
IFR = 0x0000; // clear all CPU interrupt flags
//
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2803x_DefaultIsr.c.
InitPieVectTable();
//
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
// InitEPWMs();
//
Init_GPIOs();
Init_ADC();
Initialize_Routine();
//
ServiceDog(); // Reset the watchdog counter
//
EALLOW;
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 0; // GPIO18 = GPIO
SysCtrlRegs.XCLK.bit.XCLKOUTDIV = 3;
EDIS;
//
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
//
// Enable Timer0 for interrupting every 1ms:
CpuTimer0Regs.TIM.all = 0 ;
CpuTimer0Regs.PRD.all = (Uint32) (60000); //1msec
CpuTimer0Regs.TCR.bit.TRB = 1; // Reload timer0
CpuTimer0Regs.TCR.bit.TSS = 0; // Start Timer0
CpuTimer0Regs.TCR.bit.TIE = 1; // Enable timer interrupt
//
for(;;);
}
//=============================================//
void Reset_Device(void)
{
DINT;
EALLOW;
SysCtrlRegs.WDCR = 0x0047; // a random wrong value in order to reset device
EDIS;
}
//=============================================//
void Initialize_Routine(void)
{
// Enable Watchdog
ServiceDog(); // Reset the watchdog counter
EALLOW;
SysCtrlRegs.SCSR = 0; //BIT1;
SysCtrlRegs.WDCR = 0x002F; // WDCLK = OSCCLK/512/64
//
DINT;
InitCpuTimers(); // For this example, only initialize the Cpu Timers
CpuTimer0Regs.TCR.all = 0; // 0x4001; // Use write-only instruction
CpuTimer0Regs.TCR.bit.TIE = 0;
// External Interrupt:
// GpioIntRegs.GPIOXINT1SEL.bit.GPIOSEL = ZC_pin_No; // XINT1 is ZC
// Configure XINT1
// XIntruptRegs.XINT1CR.bit.POLARITY = 3; // both edge (0:Falling edge, 1:Rising edge, 3:both edge)
// Enable XINT1
// XIntruptRegs.XINT1CR.bit.ENABLE = 1; // Enable XINT1
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
//PieVectTable.XINT1 = &xint1_isr; // Zero-Cross-Pulse
PieVectTable.TINT0 = &cpu_timer0_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
//
// Enable XINT1 and XINT2 and TINT0 in the PIE: Group 1 interrupt 4 & 5 &7
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
//PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // XINT1(Zero-Cross-Pulse) // Enable PIE Group 1 INT4
PieCtrlRegs.PIEIER1.bit.INTx7 = 1; // TINT0 (Timer0) // Enable PIE Group 1 INT7
IER |= M_INT1; // Enable CPU INT1
DINT;
// Initialize I2C
// I2caRegs.I2CSAR = 0x0050; // Slave address - EEPROM control code
// I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
// I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
/// I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
// I2caRegs.I2CIER.all = 0x24; // 0x24; // Enable SCD & ARDY interrupts
//I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
// I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
// I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
//
DELAY_US(10000);
}
//============================================//
void Flush_Vectors(void)
{
Uint16 index,i;
//
for (i = 0; i < 32; i++)
{
ADC_Buffer[i] = 0x0800; // Mid value
}
//
//
for (index = 0; index < 64; index++)
{
fft_draft[index] = 0;
}
//
for (i = 0; i < 16; i++)
{
FFT_out_32[i] = 0;
}
//
for (i = 0; i < 8; i++)
{
FFT_out_16[i] = 0;
}
//
//
}
//============================================//
#pragma CODE_SECTION(cpu_timer0_isr,"ramfuncs");
__interrupt void cpu_timer0_isr(void) // timer0 interrupt service
{
ServiceDog(); // Reset the watchdog counter
Toggle1_Set = 1;
ADC_Capture(); //10us
FFT_32(); //140us
Toggle1_Clr = 1;
// stop timer0 , no further count
//CpuTimer0Regs.TCR.bit.TIE = 0;
//CpuTimer0Regs.TCR.bit.TSS = 1;
CpuTimer0Regs.TCR.bit.TIF = 1;
CpuTimer0.InterruptCount++;
// Acknowledge this interrupt to receive more interrupts from group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}
//==============================================//
#pragma CODE_SECTION(FFT_32 , "ramfuncs")
void FFT_32(void)
{
// FFT:
Uint16 p;
Uint16 k;
int32 a_r;
int32 a_i;
int32 b_r;
int32 b_i;
int32 w_r;
int32 w_i;
int32 temp1;
int32 temp2;
int16 temp3;
//int16 temp4;
//index = index << 5;
for (k=0;k<32;k++)
{
temp3 = (ADC_Buffer[k] - 2048);
fft_draft[2*k] = temp3;
fft_draft[2*k+1] = 0 ;// temp4;
}
//index = index >> 5;
for(p=0;p<16;p++)
{
for(k=p;k<32;k=k+32)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+32];
b_i = fft_draft[2*k+32+1];
//w=(exp(-pi*1i*p/16));
w_r = Weight[2*p];
w_i = Weight[2*p+1];
//fft_draft(k+1)=a+b;
fft_draft[2*k] = a_r + b_r;
fft_draft[2*k+1] = a_i + b_i;
// fft_draft(k+16)=(a-b)*w;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+32] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+32+1] = temp2 >> 8;
}
}
//
for (p=0;p<8;p++)
{
for (k=p;k<32;k=k+16)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+16];
b_i = fft_draft[2*k+16+1];
//%t=(exp(-pi*1i*2*p/16));
//%w=Weight(2*p+1);
w_r = Weight[4*p];
w_i = Weight[4*p+1];
//%fft_draft(k+1)=b+fft_draft(k+8+1);
fft_draft[2*k] = a_r + b_r;
fft_draft[2*k+1] = a_i + b_i;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+16] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+16] = temp2 >> 8;
}
}
//
for (p=0;p<4;p++)
{
for (k=p;k<32;k=k+8)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+8];
b_i = fft_draft[2*k+1+8];
// %t=(exp(-pi*1i*4*p/16));
// %t=Weight(4*p+1);
w_r = Weight[8*p];
w_i = Weight[8*p+1];
// %fft_draft(k+1)=b+fft_draft(k+4+1);
fft_draft[2*k] = (a_r + b_r);
fft_draft[2*k+1] = (a_i + b_i);
// %fft_draft(k+4+1)=(b-fft_draft(k+4+1))*t;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+8] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+8] = temp2 >> 8;
}
}
//
//m=1:
for (p=0;p<2;p++)
{
for (k=p;k<32;k=k+4)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+4];
b_i = fft_draft[2*k+1+4];
//t=(exp(-pi*1i*8*p/16));
//t=Weight(8*p+1);
w_r = Weight[16*p];
w_i = Weight[16*p+1];
//fft_draft(k+1)=b+fft_draft(k+2+1);
fft_draft[2*k] = (a_r + b_r);
fft_draft[2*k+1] = (a_i + b_i);
//fft_draft(k+2+1)=(a-fft_draft(k+2+1))*t;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+4] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+4] = temp2 >> 8;
}
}
//m=0:
//for p=0:1:0
p=0;
for (k=p;k<32;k=k+2)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+2];
b_i = fft_draft[2*k+1+2];
//t=(exp(-pi*1i*16*p/16));
//t=Weight(16*p+1);
w_r = Weight[32*p];
w_i = Weight[32*p+1];
//fft_draft(k+1)=a+fft_draft(k+1+1);
fft_draft[2*k] = (a_r + b_r);
fft_draft[2*k+1] = (a_i + b_i);
//fft_draft(k+1+1)=(b-fft_draft(k+1+1))*t;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+2] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+2] = temp2 >> 8;
}
//
//
// Uint16 perm_32[] = {0,32,16,48,8,40,24,56,4,36,20,52,12,44,28,60};
Uint16 pidx;
for (k=0;k<16;k++)
{
pidx = perm_32[k];
temp1 = (fft_draft[pidx] * fft_draft[pidx]) + (fft_draft[pidx+1] * fft_draft[pidx+1]);
//FFT_out[index][k] = (temp1 >> 16); //[k + index] = temp1 >> 16;
// temp1 = sqrt(temp1);
temp1 = (temp1 >> 12); //4096
if (k == 0)
{ temp1 = (temp1 >> 4);} // modify DC index
temp3 = 0;
if (temp1 > 60) // 25mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 25;}
if (temp1 > 240) // 50mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 50;}
if (temp1 > 550) // 75mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 75;}
if (temp1 > 950) // 100mV
{ temp3 = 100;}
if (temp1 > 1500) // 125mV
{ temp3 = 125;}
if (temp1 > 2150) // 150mV
{ temp3 = 150;}
if (temp1 > 3000) // 175mV
{ temp3 = 175;}
if (temp1 > 3800) // 200mV
{ temp3 = 200;}
if (temp1 > 4950) // 225mV
{ temp3 = 225;}
if (temp1 > 6000) // 250mV
{ temp3 = 250;}
if (temp1 > 7400) // 275mV
{ temp3 = 275;}
if (temp1 > 8650) // 300mV
{ temp3 = 300;}
if (temp1 > 11700) // 350mV
{ temp3 = 350;}
if (temp1 > 15300) // 400mV
{ temp3 = 400;}
if (temp1 > 19400) // 450mV
{ temp3 = 450;}
if (temp1 > 24000) // 500mV
{ temp3 = 500;}
if (temp1 > 29000) // 550mV
{ temp3 = 550;}
if (temp1 > 34600) // 600mV
{ temp3 = 600;}
if (temp1 > 40000) // 650mV
{ temp3 = 650;}
if (temp1 > 47000) // 700mV
{ temp3 = 700;}
if (temp1 > 54000) // 750mV
{ temp3 = 750;}
if (temp1 > 61500) // 800mV
{ temp3 = 800;}
if (temp1 > 69400) // 850mV
{ temp3 = 850;}
if (temp1 > 77850) // 900mV
{ temp3 = 900;}
if (temp1 > 86700) // 950mV
{ temp3 = 950;}
if (temp1 > 96100) // 1000mV
{ temp3 = 1000;}
if (temp1 > 116300) // 1100mV
{ temp3 = 1100;}
if (temp1 > 138400) // 1200mV
{ temp3 = 1200;}
if (temp1 > 162400) // 1300mV
{ temp3 = 1300;}
if (temp1 > 188400) // 1400mV
{ temp3 = 1400;}
if (temp1 > 216000) // 1500mV
{ temp3 = 1500;}
FFT_out_32[k] = temp3;
}
}
//=============================================//
/*#pragma CODE_SECTION(FFT_16, "ramfuncs")
void FFT_16(void)
{
// FFT:
Uint16 p;
Uint16 k;
int32 a_r;
int32 a_i;
int32 b_r;
int32 b_i;
int32 w_r;
int32 w_i;
int32 temp1;
int32 temp2;
int16 temp3;
//
for (k=0;k<16;k++)
{
temp3 = (ADC_Buff_M2[(index*4) + k] - 2048);
fft_draft[2*k] = temp3;
fft_draft[2*k+1] = 0 ;
}
for (k=16;k<32;k++)
{
fft_draft[2*k] = 0;
fft_draft[2*k+1] = 0 ;
}
//
for(p=0;p<8;p++)
{
for(k=p;k<16;k=k+16)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+16];
b_i = fft_draft[2*k+16+1];
//w=(exp(-pi*1i*p/16));
w_r = Weight[4*p];
w_i = Weight[4*p+1];
//fft_draft(k+1)=a+b;
fft_draft[2*k] = a_r + b_r;
fft_draft[2*k+1] = a_i + b_i;
// fft_draft(k+16)=(a-b)*w;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+16] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+16+1] = temp2 >> 8;
}
}
//
for (p=0;p<4;p++)
{
for (k=p;k<16;k=k+8)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+8];
b_i = fft_draft[2*k+8+1];
//%t=(exp(-pi*1i*2*p/16));
//%w=Weight(2*p+1);
w_r = Weight[8*p];
w_i = Weight[8*p+1];
//%fft_draft(k+1)=b+fft_draft(k+8+1);
fft_draft[2*k] = a_r + b_r;
fft_draft[2*k+1] = a_i + b_i;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+8] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+8] = temp2 >> 8;
}
}
//
for (p=0;p<2;p++)
{
for (k=p;k<16;k=k+4)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+4];
b_i = fft_draft[2*k+1+4];
// %t=(exp(-pi*1i*4*p/16));
// %t=Weight(4*p+1);
w_r = Weight[16*p];
w_i = Weight[16*p+1];
// %fft_draft(k+1)=b+fft_draft(k+4+1);
fft_draft[2*k] = (a_r + b_r);
fft_draft[2*k+1] = (a_i + b_i);
// %fft_draft(k+4+1)=(b-fft_draft(k+4+1))*t;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+4] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+4] = temp2 >> 8;
}
}
//
//m=1:
p=0;
for (k=p;k<16;k=k+2)
{
a_r = fft_draft[2*k];
a_i = fft_draft[2*k+1];
b_r = fft_draft[2*k+2];
b_i = fft_draft[2*k+1+2];
//t=(exp(-pi*1i*8*p/16));
//t=Weight(8*p+1);
w_r = Weight[32*p];
w_i = Weight[32*p+1];
//fft_draft(k+1)=b+fft_draft(k+2+1);
fft_draft[2*k] = (a_r + b_r);
fft_draft[2*k+1] = (a_i + b_i);
//fft_draft(k+2+1)=(a-fft_draft(k+2+1))*t;
temp1 = ((a_r - b_r) * w_r) - ((a_i - b_i) * w_i);
fft_draft[2*k+2] = temp1 >> 8;
temp2 = ((a_r - b_r) * w_i) + ((a_i - b_i) * w_r);
fft_draft[2*k+1+2] = temp2 >> 8;
}
// Uint16 perm_16[] = {0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30};
Uint16 pidx;
for (k=0;k<8;k++)
{
pidx = perm_16[k];
temp1 = (fft_draft[pidx] * fft_draft[pidx]) + (fft_draft[pidx+1] * fft_draft[pidx+1]);
//FFT_out_16[index][k] = (temp1 >> 16); //[k + index] = temp1 >> 16;
//temp1 = sqrt(temp1); // ???
temp1 = (temp1 >> 10);
temp3 = 0;
if (temp1 > 60) // 25mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 25;}
if (temp1 > 240) // 50mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 50;}
if (temp1 > 550) // 75mV (Vp_p ADCinput, Vref = 1650mV)
{ temp3 = 75;}
if (temp1 > 950) // 100mV
{ temp3 = 100;}
if (temp1 > 1500) // 125mV
{ temp3 = 125;}
if (temp1 > 2150) // 150mV
{ temp3 = 150;}
if (temp1 > 3000) // 175mV
{ temp3 = 175;}
if (temp1 > 3800) // 200mV
{ temp3 = 200;}
if (temp1 > 4950) // 225mV
{ temp3 = 225;}
if (temp1 > 6000) // 250mV
{ temp3 = 250;}
if (temp1 > 7400) // 275mV
{ temp3 = 275;}
if (temp1 > 8650) // 300mV
{ temp3 = 300;}
if (temp1 > 11700) // 350mV
{ temp3 = 350;}
if (temp1 > 15300) // 400mV
{ temp3 = 400;}
if (temp1 > 19400) // 450mV
{ temp3 = 450;}
if (temp1 > 24000) // 500mV
{ temp3 = 500;}
if (temp1 > 29000) // 550mV
{ temp3 = 550;}
if (temp1 > 34600) // 600mV
{ temp3 = 600;}
if (temp1 > 40000) // 650mV
{ temp3 = 650;}
if (temp1 > 47000) // 700mV
{ temp3 = 700;}
if (temp1 > 54000) // 750mV
{ temp3 = 750;}
if (temp1 > 61500) // 800mV
{ temp3 = 800;}
if (temp1 > 69400) // 850mV
{ temp3 = 850;}
if (temp1 > 77850) // 900mV
{ temp3 = 900;}
if (temp1 > 86700) // 950mV
{ temp3 = 950;}
if (temp1 > 96100) // 1000mV
{ temp3 = 1000;}
if (temp1 > 116300) // 1100mV
{ temp3 = 1100;}
if (temp1 > 138400) // 1200mV
{ temp3 = 1200;}
if (temp1 > 162400) // 1300mV
{ temp3 = 1300;}
if (temp1 > 188400) // 1400mV
{ temp3 = 1400;}
if (temp1 > 216000) // 1500mV
{ temp3 = 1500;}
FFT_out_16[index][k] = temp3;
}
}*/
//=============================================//
void Init_GPIOs(void)
{
// Configure GPIOs:
EALLOW;
Toggle1_Mux = 0;
Toggle1_Dir = 1;
Toggle1_Clr = 1;
EDIS;
}
//=============================================//
void Init_ADC(void)
{
EALLOW;
// initialize adc
SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // Return ADC clock to original state
__asm (" NOP");
__asm (" NOP");
AdcRegs.ADCCTL1.bit.RESET = 1;
__asm (" NOP");
__asm (" NOP");
__asm (" NOP");
__asm (" NOP");
AdcRegs.ADCCTL1.bit.ADCPWDN = 1;
AdcRegs.ADCCTL1.bit.ADCBGPWD = 1;
AdcRegs.ADCCTL1.bit.ADCREFPWD = 1;
AdcRegs.ADCCTL1.bit.ADCREFSEL = 1;
AdcRegs.ADCCTL1.bit.INTPULSEPOS = 1;
AdcRegs.ADCCTL1.bit.VREFLOCONV = 0;
AdcRegs.ADCCTL1.bit.TEMPCONV = 0;
//
AdcRegs.ADCCTL2.bit.CLKDIV2EN = 0;
AdcRegs.ADCCTL2.bit.ADCNONOVERLAP = 0;
////////////////////////////////////////
// Analog input signal:
AdcRegs.ADCSOC0CTL.bit.CHSEL=0x9; // B1 pin
AdcRegs.ADCSOC0CTL.bit.ACQPS = 7; // (this + 1 + 6clkcycle(16.66ns)) = sampling time in ns
AdcRegs.ADCSOC1CTL.bit.CHSEL=0x9; // B1 pin
AdcRegs.ADCSOC1CTL.bit.ACQPS = 7;
AdcRegs.INTSEL1N2.bit.INT1CONT = 0;
AdcRegs.INTSEL1N2.bit.INT2CONT = 0;
AdcRegs.ADCINTSOCSEL1.bit.SOC0 = 0;
AdcRegs.ADCINTSOCSEL1.bit.SOC1 = 0;
AdcRegs.INTSEL1N2.bit.INT1SEL = 0; // connect EOC0 to ADCINT1
AdcRegs.INTSEL1N2.bit.INT1E = 1;
AdcRegs.INTSEL1N2.bit.INT2SEL = 1; // connect EOC1 to ADCINT2
AdcRegs.INTSEL1N2.bit.INT2E = 1;
AdcRegs.SOCPRICTL.bit.SOCPRIORITY = 2; // SOC0 & SOC1 highest priority
AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 0x09; // ePWM3, ADCSOCA
AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 0x0A; // ePWM3, ADCSOCB
///////////////////////////////
// AdcRegs.INTSEL1N2.bit.INT2SEL=1;
// AdcRegs.INTSEL1N2.bit.INT2E=1;
EPwm3Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; // TBCLK = SYSCLKOUT
EPwm3Regs.TBCTL.bit.CLKDIV = TB_DIV1;
EPwm3Regs.ETSEL.bit.SOCAEN = 0;
EPwm3Regs.ETSEL.bit.SOCBEN = 0;
EPwm3Regs.ETSEL.bit.SOCASEL = 0b100; //Enable event time-base counter equal to CMPA when the timer is incrementing
EPwm3Regs.ETSEL.bit.SOCBSEL = 0b010; //Enable event time-base counter equal to period (TBCTR = TBPRD)
EPwm3Regs.ETPS.bit.SOCAPRD = 1; // Generate pulse on 1st event
EPwm3Regs.ETPS.bit.SOCBPRD = 1; // Generate pulse on 1st event
EPwm3Regs.TBPRD = 37; // 18 * 16.66 => 300ns sampling // Period = * TBCLK counts
EPwm3Regs.CMPA.half.CMPA = 19; // Compare A TBCLK counts
// EPwm3Regs.TBPRD = 120; // 60 * 16.66 => 1us sampling // Period = * TBCLK counts
// EPwm3Regs.CMPA.half.CMPA = 60; // Compare A TBCLK counts
// EPwm3Regs.CMPB = 30; // Compare B TBCLK counts
EPwm3Regs.TBPHS.half.TBPHS = 0; // Set Phase register to zero
EPwm3Regs.TBCTR = 0; // clear TB counter
EPwm3Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;
EPwm3Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Phase loading disabled
EPwm3Regs.TBCTL.bit.PRDLD = TB_SHADOW;
EPwm3Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;
EPwm3Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm3Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm3Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on TBCTR = Zero
EPwm3Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on TBCTR = Zero
AdcRegs.ADCCTL1.bit.ADCENABLE = 1;
DELAY_US(1000);
EDIS;
}
//=============================================//
#pragma CODE_SECTION(ADC_Capture, "ramfuncs");
void ADC_Capture(void)
{// Start ADC:
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
EPwm3Regs.TBCTR = 0; // init TB counter
EPwm3Regs.ETSEL.bit.SOCAEN = 1;
EPwm3Regs.ETSEL.bit.SOCBEN = 1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[0] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[1] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[2] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[3] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[4] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[5] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[6] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[7] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[8] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[9] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[10] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[11] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[12] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[13] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[14] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[15] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[16] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[17] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[18] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[19] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[20] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[21] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[22] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[23] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[24] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[25] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[26] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[27] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[28] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[29] = AdcResult.ADCRESULT1;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT1 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT1=1;
ADC_Buffer[30] = AdcResult.ADCRESULT0;
//
while(AdcRegs.ADCINTFLG.bit.ADCINT2 == 0){}
AdcRegs.ADCINTFLGCLR.bit.ADCINT2=1;
ADC_Buffer[31] = AdcResult.ADCRESULT1;
//
EPwm3Regs.ETSEL.bit.SOCAEN = 0;
EPwm3Regs.ETSEL.bit.SOCBEN = 0;
}
//=============================================//
//=============================================//
//=============================================//