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
|
/*
**************************************************************************
*Program : DDS_20121217.cpp
**************************************************************************
*/
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <math.h>
// System Clock set as Hz. User settable.
#define SysClk 18000000
// Scalar set as 2^32-1. Special scalar defined by Scalar Devices (No pun).
#define MagicNum 0xFFFFFFFF
// Mega scalar (multiply by 1,000,000)
#define Mega *1000000
// -------------------------------------------------------------------------
// Define structures to be used
// ============================
struct ddscontrol
{
unsigned char SixTimesRefClk:1 ; // D0 - Six Times (6x) Reference clock multiplier.
unsigned char Parallel_Serial:1 ; // D1 - Serial mode (active high).
unsigned char Power_Down:1 ; // D2 - Software power down (active high).
unsigned char Phase_Shift:5 ; // D7..D3 - Not used with a single chip.
} ;
struct ddsword
{
ddscontrol control ; // Control (one byte).
unsigned long freq ; // Set Frequency (quad byte).
} ;
// Assign sub-routines/functions to be used.
void display( ddsword show ) ;
// -------------------------------------------------------------------------
// **** Core Program matter ****
// =============================
int main()
{
ddsword dds ;
float req_freq ;
unsigned long
ireq_freq ,
iMagic ,
iSysClk ,
itestresult ;
// Set up the DDS control byte
dds.control.Phase_Shift = 1 ; // 5-bit phase (D7..D3)
dds.control.Power_Down = 0 ; // 1-bit power down flag (D2).
dds.control.Parallel_Serial = 0 ; // 1-bit serial/parallel load bit (D1).
dds.control.SixTimesRefClk = 1 ; // 1-bit 6x set bit (D0).
printf( "SIZEOF( dds.control ) = %i \n", sizeof( dds.control ) ) ;
printf( "SIZEOF( dds.freq ) = %i \n", sizeof( dds.freq ) ) ;
printf( "SIZEOF( dds ) = %i \n\n", sizeof( dds ) ) ;
printf( "dds.control = %02X\n\n" , dds.control ) ;
// Request output frequency.
printf( "Enter desired Frequency (MHz): ") ;
scanf( "%f", &req_freq ) ;
// Assign the defined values to working variables.
ireq_freq = (unsigned long) req_freq*1000000 ; // Convert to MHz to Hz.
iSysClk = SysClk ; // Save as Hz int variable.
iMagic = MagicNum ; // Save as long integer variable.
printf( "\n\nifrequency = \t%10li Hz\n", ireq_freq ) ;
printf( "System Clock = \t%10li Hz\n", iSysClk ) ;
printf( "Magic Number = \t 0x%8lX \n\n", iMagic ) ;
dds.freq = iMagic / iSysClk * ireq_freq ;
printf( "dds.freq = \t %7u\n\n", dds.freq ) ;
printf( "SIZEOF( dds.freq ) = \t%i bytes\n\n", sizeof( dds.freq ) ) ;
display( dds ) ;
system( "PAUSE" );
return 0 ;
} ;
// -------------------------------------------------------------------------
// **** Subroutines/Functions ****
// ===============================
// -- Display - Routine to display results (what else?)
void display( ddsword show )
{
int i ;
printf( "SIZEOF( show ) = \t%i bytes\n\n", sizeof( show ) ) ;
printf( "Complete 40-bit data\n" ) ;
printf( "\tshow.control\t how.freq\n" ) ;
printf( "\t%2X \t %2X\n", show.control, show.freq ) ;
printf( "\t%7i \t %u \n\n", show.control, show.freq ) ;
printf( "show.control = %i ", show.control ) ;
/*
for ( i=39; i>=32; i-- )
{
if ( show.control & 1<<i ) printf( "1" ) ;
else printf ( "0" ) ;
if ( i%4 == 0 ) printf( " " ) ;
};
*/
for ( i=31; i>=0; i-- )
{
if ( show.freq & 1<<i ) printf( "1" ) ;
else printf ( "0" ) ;
if ( i%4 == 0 ) printf( " " ) ;
};
printf ( "\n" ) ;
} ;
|