Blame view

src/ddsFreq.c 3.76 KB
dfa91a3bc   pyb   libad9915 : add i...
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
  #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
  ",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
  ",d1);
  
  	long double M = f0 / d1 ;
  	long double N = fs / d1 ;
  	//printf(" M = f0 / d1 = %24.16Lf
  ",M);
  	//printf(" N = fs / d1 = %24.16Lf
  ",N);
  
  
  	long double ftw = M * scalbn(1.0,32);
  	ftw /= N;
  	//printf("ftw = %24.16Lf
  ",ftw);
  
  	
  	//printf("X=floor(ftw) = %24.16Lf
  ",(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
  ",Y);
  	
  	long double d2 = pgcd (Y,N);
  	//printf(" d2 = %24.16Lf
  ",d2);
  
  	long double A = Y / d2;
  	long double B = N / d2;
  	//printf("A = Y/d2 = %24.16Lf
  ",A);
  	//printf("B = N/d2 = %24.16Lf
  ",B);
  	
  	//printf("f0 = (M/N) * fclk= %24.32Lf
  ",fs*M/N);
  	//printf("f0 = (X+A/B)2**32= %24.32Lf
  ",fs*(floor(ftw)+A/B)/scalbn(1.0,32));
  	
  #ifdef debug
  printf("
   so finally the programmed registers are :
  "
          "ftw= 0x%08x         A= 0x%08x            B= 0x%08x
  "
          "",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
  printf("uint ftw = %d
  ",(unsigned int)ftw);
  
  printf("
   0x%08x 0x%08x 0x%08x 
  
  ",(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)
  {
2146e89f7   bachi   ddsFreq.c : suppr...
94
  	//checkSize();
dfa91a3bc   pyb   libad9915 : add i...
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
  	long double f0,fs;		
  	//printf("long = %lu
  ",sizeof (long));
  	//printf("float = %lu
  ",sizeof (float));
  	//printf("double = %lu
  ",sizeof (double));
  	//printf("long double = %lu
  ",sizeof (long double));
  
  	printf("
  Desired 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
  ",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
  ",d1);
  
  	long double M = f0 / d1 ;
  	long double N = fs / d1 ;
  	printf(" M = f0 / d1 = %24.16Lf
  ",M);
  	printf(" N = fs / d1 = %24.16Lf
  ",N);
  
  
  	long double ftw = M * scalbn(1.0,32);
  	ftw /= N;
  	printf("ftw = %24.16Lf
  ",ftw);
  
  	
  	printf("X=floor(ftw) = %24.16Lf
  ",(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
  ",Y);
  	
  	long double d2 = pgcd (Y,N);
  	printf(" d2 = %24.16Lf
  ",d2);
  
  	long double A = Y / d2;
  	long double B = N / d2;
  	printf("A = Y/d2 = %24.16Lf
  ",A);
  	printf("B = N/d2 = %24.16Lf
  ",B);
  	
  	printf("f0 = (M/N) * fclk= %24.32Lf
  ",fs*M/N);
  	printf("f0 = (X+A/B)2**32= %24.32Lf
  ",fs*(floor(ftw)+A/B)/scalbn(1.0,32));
  	
  printf("
   so finally the programmed registers are :
  "
          "ftw= 0x%08x         A= 0x%08x            B= 0x%08x
  "
          "",(unsigned int)ftw,(unsigned int)A,(unsigned int)B);
  //printf("uint ftw = %d
  ",(unsigned int)ftw);
  
  printf("
   0x%08x 0x%08x 0x%08x 
  
  ",(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;
  }