ddsFreq.c
3.76 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include "ddsFreq.h"
#include "ad9915.h"
//#define debug
long double pgcd ( long double m , long double n)
{
if (fmod(m,n) ==0)
return n;
return pgcd(n,fmod(m,n));
}
int setddsFreq(int fd, int f_dds, long double f0, long double fs)
{
#ifdef debug
printf("f0=%Le\tfs=%Le\n",f0,fs);
#endif
if (fs<0 || f0<0) return EXIT_FAILURE;
struct frac f1;
f1.numerator = f0;
f1.denominator = fs;
long double d1 = pgcd (f1.numerator , f1.denominator );
//printf(" d1 = %24.16Lf\n",d1);
long double M = f0 / d1 ;
long double N = fs / d1 ;
//printf(" M = f0 / d1 = %24.16Lf\n",M);
//printf(" N = fs / d1 = %24.16Lf\n",N);
long double ftw = M * scalbn(1.0,32);
ftw /= N;
//printf("ftw = %24.16Lf\n",ftw);
//printf("X=floor(ftw) = %24.16Lf\n",(long double)floor(ftw));
long double Y = M * scalbn(1.0,32);
Y -= N * floor(ftw);
//printf("Y=M*2**32-X*N = %24.16Lf\n",Y);
long double d2 = pgcd (Y,N);
//printf(" d2 = %24.16Lf\n",d2);
long double A = Y / d2;
long double B = N / d2;
//printf("A = Y/d2 = %24.16Lf\n",A);
//printf("B = N/d2 = %24.16Lf\n",B);
//printf("f0 = (M/N) * fclk= %24.32Lf\n",fs*M/N);
//printf("f0 = (X+A/B)2**32= %24.32Lf\n",fs*(floor(ftw)+A/B)/scalbn(1.0,32));
#ifdef debug
printf("\n so finally the programmed registers are :\n"
"ftw= 0x%08x A= 0x%08x B= 0x%08x\n"
"",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
printf("uint ftw = %d\n",(unsigned int)ftw);
printf("\n 0x%08x 0x%08x 0x%08x \n\n",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
//setFreqMM(fd,f_dds,(unsigned int)ftw, (unsigned int)A, (unsigned int)B);
#endif
setFreqMM(fd,f_dds,(unsigned int)ftw, 2*(unsigned int)B, 2*(unsigned int)A); //??why *2??
return EXIT_SUCCESS;
}
int ddsFreq(int fd, int f_dds)
{
//checkSize();
long double f0,fs;
//printf("long = %lu\n",sizeof (long));
//printf("float = %lu\n",sizeof (float));
//printf("double = %lu\n",sizeof (double));
//printf("long double = %lu\n",sizeof (long double));
printf("\nDesired output Frequency (MHz) : ");
scanf("%Lf",&f0);
printf("DDS frequency Clock (MHz) : ");
scanf("%Lf",&fs);
//long double f0=atof(argv[1]); //desired output freq
//long double fs=atof(argv[2]); //dds_clk
//int toto = (int)strtol(argv[3],NULL,0);
//printf("toto=%d\t%x\n",toto,toto);
if (fs<0 || f0<0) return EXIT_FAILURE;
struct frac f1;
f1.numerator = f0;
f1.denominator = fs;
long double d1 = pgcd (f1.numerator , f1.denominator );
printf(" d1 = %24.16Lf\n",d1);
long double M = f0 / d1 ;
long double N = fs / d1 ;
printf(" M = f0 / d1 = %24.16Lf\n",M);
printf(" N = fs / d1 = %24.16Lf\n",N);
long double ftw = M * scalbn(1.0,32);
ftw /= N;
printf("ftw = %24.16Lf\n",ftw);
printf("X=floor(ftw) = %24.16Lf\n",(long double)floor(ftw));
long double Y = M * scalbn(1.0,32);
Y -= N * floor(ftw);
printf("Y=M*2**32-X*N = %24.16Lf\n",Y);
long double d2 = pgcd (Y,N);
printf(" d2 = %24.16Lf\n",d2);
long double A = Y / d2;
long double B = N / d2;
printf("A = Y/d2 = %24.16Lf\n",A);
printf("B = N/d2 = %24.16Lf\n",B);
printf("f0 = (M/N) * fclk= %24.32Lf\n",fs*M/N);
printf("f0 = (X+A/B)2**32= %24.32Lf\n",fs*(floor(ftw)+A/B)/scalbn(1.0,32));
printf("\n so finally the programmed registers are :\n"
"ftw= 0x%08x A= 0x%08x B= 0x%08x\n"
"",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
//printf("uint ftw = %d\n",(unsigned int)ftw);
printf("\n 0x%08x 0x%08x 0x%08x \n\n",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
//setFreqMM(fd,f_dds,(unsigned int)ftw, (unsigned int)A, (unsigned int)B);
setFreqMM(fd,f_dds,(unsigned int)ftw, 2*(unsigned int)B, 2*(unsigned int)A);
return EXIT_SUCCESS;
}