ddsFreq.c 3.76 KB
#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;
}