This is a program which can be used for a Direct Digital Synthesizer (DDS), model AD9954. A phase step to a desired final value or a phase ramp from 0 to 359 degree is triggered upon a start signal received by the TeraTerm-Terminal. The RAM is not used in this case.
Revision 6:1cf1523d29f3, committed 2012-11-09
- Comitter:
- ahambi
- Date:
- Fri Nov 09 15:08:10 2012 +0000
- Parent:
- 5:68d93e88377a
- Child:
- 7:32fa7b41a964
- Commit message:
- anna_final_revision;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon Jul 23 16:23:07 2012 +0000 +++ b/main.cpp Fri Nov 09 15:08:10 2012 +0000 @@ -1,25 +1,14 @@ -// serial port configuration:: http://mbed.org/handbook/Windows-serial-configuration - #include "mbed.h" #include "AD9954.h" Serial pc(USBTX, USBRX); DigitalOut myled(LED1); DigitalOut cs2(p11); -DigitalOut cs1(p8);//take cs out of the device class -DigitalOut rst(p9);//take rst out of device class +DigitalOut cs1(p8); +DigitalOut rst(p9); DigitalOut update(p10); DigitalOut trigger(p13); AD9954 myDevice(p5, p6, p7, p8, p9, p10); - - int read_all() - { - pc.printf("\n \n Register 2 = %X ", myDevice.CFR2_read()); - pc.printf("FTWO = %X ", myDevice.FTWO_read()); - pc.printf("Register 1 = %X", myDevice.CFR1_read()); - pc.printf("PHWO = %X \n \n", myDevice.PHWO_read()); - return 0; - } int reset() { @@ -31,56 +20,19 @@ int ch_1() { cs1 = 0; cs2 = 1; return 0; } int ch_2() { cs1 = 1; cs2 = 0; return 0; } -/* -int set_frequency(double freq, double clock) { - uint32_t FTWO = 0xFFFFFFFF & (int)(freq/clock*pow(2.0,32.0)); - trigger = 1; - myDevice.FTW0_write(FTWO); - pc.printf("frequency set to %f \n \r", freq); - trigger = 0; - return 0; - }*/ - -int set_frequency(double freq, double clock) { - uint32_t FTWO1 = 0xFFFFFFFF & (int)((80.0e6-freq/2.0)/clock*pow(2.0,32.0)); - uint32_t FTWO2 = 0xFFFFFFFF & (int)((80.0e6+freq/2.0)/clock*pow(2.0,32.0)); - trigger = 1; - //ch_1(); - //myDevice.FTW0_write(FTWO1); - ch_2(); - myDevice.FTW0_write(FTWO2); - pc.printf("delta frequency set to %f Hz \n \r", freq); - trigger = 0; - return 0;} - -int set_frequency_not(double freq, double clock) { - uint32_t FTWO = 0xFFFFFFFF & (int)(freq/clock*pow(2.0,32.0)); - trigger = 1; - ch_1(); - myDevice.FTW0_write(FTWO); - ch_2(); - myDevice.FTW0_write(FTWO); - pc.printf("both channels on: %f Hz \n \r", freq); - trigger = 0; - return 0;} - int main() { double freq0; - freq0 = 80.0e6; - /*double frequency; - frequency = 79.98e6; // enter frequency in Hz - double frequency_new; - frequency_new = 40.0e3;*/ - double clock = 400.0e6; // clock frequency after multiplier in Hz + freq0 = 80.0e6; // Output frequency of both output channels + double clock = 400.0e6; // Frequency of external clock signal trigger = 0; reset(); - + +// Set output channel 1 and 2 to the frequency "freq0": ch_1(); myDevice.CFR1_write(0x00000200); - uint32_t FTWO = 0xFFFFFFFF & (int)(freq0/clock*pow(2.0,32.0)); myDevice.FTW0_write(FTWO); pc.printf("channel 1 frequency %f \n \r", freq0); @@ -90,61 +42,54 @@ myDevice.CFR1_write(0x00000200); myDevice.FTW0_write(FTWO); - //set_frequency_not(freq0, clock); double phase = 0.0; // in degree uint32_t PHWO = int(phase/360.0*pow(2.0,14.0)); myDevice.PHWO_write(PHWO); - - double frequency_new; - frequency_new = 80.0e3; + + +// This sequency watches the TeraTerm-Terminal and triggers actions, +// when a certain key is pressed: char std; while(1) { pc.scanf("%c",&std); + // Ramp channel 1 or channel 2 by pressing entering "1" or "2" in TeraTerm + if (std == '1') { ch_1(); pc.printf("ch1 selected \r\n"); }; + if (std == '2') { ch_2(); pc.printf("ch2 selected \r\n"); }; + // Enter "p" --> enter the final phase value in degree. The DDS will start to make phase steps 0-->final value with + // a 5 kHz frequency. The signal can be observed with the mbed trigger on pin 13 if (std == 'p') { trigger = 0; - pc.printf("enter new phase: \r \n"); + pc.printf("enter final phase: \r \n"); pc.scanf("%lf", &phase); pc.printf("entered: %lf \r \n", phase); - ch_2(); uint32_t PHWO = rint(phase/360.0*pow(2.0,14.0)); - trigger = 1; - myDevice.PHWO_write(PHWO); - trigger = 0; - pc.printf("new phase: %lf \r \n ", phase); }; + while(1) { + myDevice.PHWO_write(0); + wait_us(200); + trigger = 1; + myDevice.PHWO_write(PHWO); + trigger = 0; + if (pc.readable()) break; + }}; + // Enter "r" --> A phase ramp from 0-->359 degree in 700 steps is started. The ramp takes approximately + // a millisecond. It is repeated until another key is pressed. if (std == 'r') { + pc.printf("entered r -> starting phase ramp\r \n"); int i; - int steps = 1; + int steps = 700; double phase0 = 0; - double phase1 = 50; + double phase1 = 359; double phase_array[steps+1]; uint32_t PHWO [steps+1]; i = 0; while(i<=steps) { phase_array[i] = phase0 + (phase1-phase0)/steps*i; - pc.printf("phase value: %lf", phase_array[i]); PHWO[i]=rint(phase_array[i]/360.0*pow(2.0,14.0)); i += 1; } - // * The Ramp * // while(1) { - ch_2(); - myDevice.PHWO_write(PHWO[0]); - ch_1(); - myDevice.PHWO_write(PHWO[0]); - trigger = 0; - wait(1e-3); - i = 0; - trigger = 1; - while(i<=steps) { - myDevice.PHWO_write(PHWO[i]); - i+=1; - } - wait(10e-3); - ch_1(); - myDevice.PHWO_write(PHWO[0]); - ch_2(); myDevice.PHWO_write(PHWO[0]); trigger = 0; wait(1e-3); @@ -153,65 +98,13 @@ while(i<=steps) { myDevice.PHWO_write(PHWO[i]); i+=1; - } + } + trigger = 0; wait(10e-3); + myDevice.PHWO_write(PHWO[0]); if (pc.readable()) break; - } - // - }; - if (std == 'f') { - pc.printf("enter new delta f in kHz: \r \n"); - pc.scanf("%lf", &frequency_new); - pc.printf("entered: %lf kHz \r \n", frequency_new); - frequency_new = frequency_new*1.0e3; - }; - if (std == 'u') { - pc.printf("entered u. \r\n"); - set_frequency(frequency_new, clock); - }; - if (std == 'd') { - pc.printf("entered d. \r\n"); - set_frequency_not(freq0, clock); - }; + }}; }; - /* - while(1) { - pc.scanf("%lf", &phase); - pc.printf("entered: %lf \n", phase); - uint32_t PHWO = rint(phase/360.0*pow(2.0,14.0)); - myDevice.PHWO_write(PHWO); - pc.printf("new phase: %lf \r", phase); - }; */ - - /* double value; - - pc.scanf("%lf", &value); - - pc.printf("Twice %lf is %lf\n", value, value * 2);*/ - - /*uint32_t PHWO_Array[7]; - - int i = 0; - double delphase = 5.0; - while (i<=7) { - PHWO_Array[i] = fmod((phase+i*delphase),360)/360.0*pow(2.0,14.0); - pc.printf("PHWO_ARRAY %d %f \n", i,phase+i*delphase); - i += 1; - } - i = 0; - while(true) { - //PHWO = rint(phase/360.0*pow(2.0,14.0)); - //PHWO = fmod((phase+i*delphase),360)/360.0*pow(2.0,14.0); - myDevice.PHWO_write(PHWO_Array[i]); - //myDevice.PHWO_write(PHWO); - //pc.printf("PHWO_ARRAY %d %x \n", i, PHWO); - wait(2.0); - i += 1; - if (i == 7){ - i = 0; }; - }*/ - - //read_all(); return 0; } \ No newline at end of file