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.
main.cpp@7:32fa7b41a964, 2012-11-09 (annotated)
- Committer:
- ahambi
- Date:
- Fri Nov 09 15:45:19 2012 +0000
- Revision:
- 7:32fa7b41a964
- Parent:
- 6:1cf1523d29f3
menu added;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ahambi | 7:32fa7b41a964 | 1 | // ******************************************* |
ahambi | 7:32fa7b41a964 | 2 | // *** phase ramps & steps without the RAM *** |
ahambi | 7:32fa7b41a964 | 3 | // ******************************************* |
ahambi | 7:32fa7b41a964 | 4 | |
ahambi | 0:930cad3922ee | 5 | #include "mbed.h" |
ahambi | 0:930cad3922ee | 6 | #include "AD9954.h" |
ahambi | 0:930cad3922ee | 7 | |
ahambi | 0:930cad3922ee | 8 | Serial pc(USBTX, USBRX); |
ahambi | 0:930cad3922ee | 9 | DigitalOut myled(LED1); |
ahambi | 0:930cad3922ee | 10 | DigitalOut cs2(p11); |
ahambi | 6:1cf1523d29f3 | 11 | DigitalOut cs1(p8); |
ahambi | 6:1cf1523d29f3 | 12 | DigitalOut rst(p9); |
ahambi | 0:930cad3922ee | 13 | DigitalOut update(p10); |
ahambi | 0:930cad3922ee | 14 | DigitalOut trigger(p13); |
ahambi | 0:930cad3922ee | 15 | AD9954 myDevice(p5, p6, p7, p8, p9, p10); |
ahambi | 0:930cad3922ee | 16 | |
ahambi | 0:930cad3922ee | 17 | int reset() |
ahambi | 0:930cad3922ee | 18 | { |
ahambi | 0:930cad3922ee | 19 | rst = 1; |
ahambi | 0:930cad3922ee | 20 | wait(0.1); |
ahambi | 0:930cad3922ee | 21 | rst = 0 ; |
ahambi | 0:930cad3922ee | 22 | return 0; |
ahambi | 0:930cad3922ee | 23 | } |
ahambi | 0:930cad3922ee | 24 | |
ahambi | 0:930cad3922ee | 25 | int ch_1() { cs1 = 0; cs2 = 1; return 0; } |
ahambi | 0:930cad3922ee | 26 | int ch_2() { cs1 = 1; cs2 = 0; return 0; } |
ahambi | 0:930cad3922ee | 27 | |
ahambi | 0:930cad3922ee | 28 | int main() { |
ahambi | 0:930cad3922ee | 29 | |
ahambi | 7:32fa7b41a964 | 30 | pc.printf("*************************************************** \n \r"); |
ahambi | 7:32fa7b41a964 | 31 | pc.printf("*** Phase steps and slow phase ramp without RAM *** \n \r"); |
ahambi | 7:32fa7b41a964 | 32 | pc.printf("*************************************************** \n \r"); |
ahambi | 7:32fa7b41a964 | 33 | pc.printf("Enter: \n \r"); |
ahambi | 7:32fa7b41a964 | 34 | pc.printf("'1' or '2' --> which output channel should be ramped. \n \r"); |
ahambi | 7:32fa7b41a964 | 35 | pc.printf("'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 \n \r"); |
ahambi | 7:32fa7b41a964 | 36 | pc.printf("'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.\n \r"); |
ahambi | 7:32fa7b41a964 | 37 | |
ahambi | 0:930cad3922ee | 38 | double freq0; |
ahambi | 6:1cf1523d29f3 | 39 | freq0 = 80.0e6; // Output frequency of both output channels |
ahambi | 6:1cf1523d29f3 | 40 | double clock = 400.0e6; // Frequency of external clock signal |
ahambi | 0:930cad3922ee | 41 | trigger = 0; |
ahambi | 0:930cad3922ee | 42 | reset(); |
ahambi | 6:1cf1523d29f3 | 43 | |
ahambi | 6:1cf1523d29f3 | 44 | // Set output channel 1 and 2 to the frequency "freq0": |
ahambi | 0:930cad3922ee | 45 | ch_1(); |
ahambi | 4:c3a96bc6ab19 | 46 | |
ahambi | 0:930cad3922ee | 47 | myDevice.CFR1_write(0x00000200); |
ahambi | 0:930cad3922ee | 48 | uint32_t FTWO = 0xFFFFFFFF & (int)(freq0/clock*pow(2.0,32.0)); |
ahambi | 0:930cad3922ee | 49 | myDevice.FTW0_write(FTWO); |
ahambi | 0:930cad3922ee | 50 | pc.printf("channel 1 frequency %f \n \r", freq0); |
ahambi | 4:c3a96bc6ab19 | 51 | |
ahambi | 0:930cad3922ee | 52 | |
ahambi | 0:930cad3922ee | 53 | ch_2(); |
ahambi | 0:930cad3922ee | 54 | |
ahambi | 0:930cad3922ee | 55 | myDevice.CFR1_write(0x00000200); |
ahambi | 0:930cad3922ee | 56 | myDevice.FTW0_write(FTWO); |
ahambi | 0:930cad3922ee | 57 | |
ahambi | 0:930cad3922ee | 58 | double phase = 0.0; // in degree |
ahambi | 0:930cad3922ee | 59 | uint32_t PHWO = int(phase/360.0*pow(2.0,14.0)); |
ahambi | 0:930cad3922ee | 60 | myDevice.PHWO_write(PHWO); |
ahambi | 6:1cf1523d29f3 | 61 | |
ahambi | 6:1cf1523d29f3 | 62 | |
ahambi | 6:1cf1523d29f3 | 63 | // This sequency watches the TeraTerm-Terminal and triggers actions, |
ahambi | 6:1cf1523d29f3 | 64 | // when a certain key is pressed: |
ahambi | 0:930cad3922ee | 65 | |
ahambi | 0:930cad3922ee | 66 | char std; |
ahambi | 0:930cad3922ee | 67 | while(1) { |
ahambi | 0:930cad3922ee | 68 | pc.scanf("%c",&std); |
ahambi | 6:1cf1523d29f3 | 69 | // Ramp channel 1 or channel 2 by pressing entering "1" or "2" in TeraTerm |
ahambi | 6:1cf1523d29f3 | 70 | if (std == '1') { ch_1(); pc.printf("ch1 selected \r\n"); }; |
ahambi | 6:1cf1523d29f3 | 71 | if (std == '2') { ch_2(); pc.printf("ch2 selected \r\n"); }; |
ahambi | 6:1cf1523d29f3 | 72 | // Enter "p" --> enter the final phase value in degree. The DDS will start to make phase steps 0-->final value with |
ahambi | 6:1cf1523d29f3 | 73 | // a 5 kHz frequency. The signal can be observed with the mbed trigger on pin 13 |
ahambi | 0:930cad3922ee | 74 | if (std == 'p') { |
ahambi | 0:930cad3922ee | 75 | trigger = 0; |
ahambi | 6:1cf1523d29f3 | 76 | pc.printf("enter final phase: \r \n"); |
ahambi | 0:930cad3922ee | 77 | pc.scanf("%lf", &phase); |
ahambi | 0:930cad3922ee | 78 | pc.printf("entered: %lf \r \n", phase); |
ahambi | 0:930cad3922ee | 79 | uint32_t PHWO = rint(phase/360.0*pow(2.0,14.0)); |
ahambi | 6:1cf1523d29f3 | 80 | while(1) { |
ahambi | 6:1cf1523d29f3 | 81 | myDevice.PHWO_write(0); |
ahambi | 6:1cf1523d29f3 | 82 | wait_us(200); |
ahambi | 6:1cf1523d29f3 | 83 | trigger = 1; |
ahambi | 6:1cf1523d29f3 | 84 | myDevice.PHWO_write(PHWO); |
ahambi | 6:1cf1523d29f3 | 85 | trigger = 0; |
ahambi | 6:1cf1523d29f3 | 86 | if (pc.readable()) break; |
ahambi | 6:1cf1523d29f3 | 87 | }}; |
ahambi | 6:1cf1523d29f3 | 88 | // Enter "r" --> A phase ramp from 0-->359 degree in 700 steps is started. The ramp takes approximately |
ahambi | 6:1cf1523d29f3 | 89 | // a millisecond. It is repeated until another key is pressed. |
ahambi | 1:d5557c1daab5 | 90 | if (std == 'r') { |
ahambi | 6:1cf1523d29f3 | 91 | pc.printf("entered r -> starting phase ramp\r \n"); |
ahambi | 2:4d8f768aeb0d | 92 | int i; |
ahambi | 6:1cf1523d29f3 | 93 | int steps = 700; |
ahambi | 4:c3a96bc6ab19 | 94 | double phase0 = 0; |
ahambi | 6:1cf1523d29f3 | 95 | double phase1 = 359; |
ahambi | 5:68d93e88377a | 96 | double phase_array[steps+1]; |
ahambi | 5:68d93e88377a | 97 | uint32_t PHWO [steps+1]; |
ahambi | 2:4d8f768aeb0d | 98 | i = 0; |
ahambi | 5:68d93e88377a | 99 | while(i<=steps) { |
ahambi | 2:4d8f768aeb0d | 100 | phase_array[i] = phase0 + (phase1-phase0)/steps*i; |
ahambi | 2:4d8f768aeb0d | 101 | PHWO[i]=rint(phase_array[i]/360.0*pow(2.0,14.0)); |
ahambi | 2:4d8f768aeb0d | 102 | i += 1; |
ahambi | 2:4d8f768aeb0d | 103 | } |
ahambi | 5:68d93e88377a | 104 | while(1) { |
ahambi | 5:68d93e88377a | 105 | myDevice.PHWO_write(PHWO[0]); |
ahambi | 5:68d93e88377a | 106 | trigger = 0; |
ahambi | 5:68d93e88377a | 107 | wait(1e-3); |
ahambi | 5:68d93e88377a | 108 | i = 0; |
ahambi | 5:68d93e88377a | 109 | trigger = 1; |
ahambi | 5:68d93e88377a | 110 | while(i<=steps) { |
ahambi | 4:c3a96bc6ab19 | 111 | myDevice.PHWO_write(PHWO[i]); |
ahambi | 4:c3a96bc6ab19 | 112 | i+=1; |
ahambi | 6:1cf1523d29f3 | 113 | } |
ahambi | 6:1cf1523d29f3 | 114 | trigger = 0; |
ahambi | 5:68d93e88377a | 115 | wait(10e-3); |
ahambi | 6:1cf1523d29f3 | 116 | myDevice.PHWO_write(PHWO[0]); |
ahambi | 5:68d93e88377a | 117 | if (pc.readable()) break; |
ahambi | 6:1cf1523d29f3 | 118 | }}; |
ahambi | 0:930cad3922ee | 119 | }; |
ahambi | 0:930cad3922ee | 120 | |
ahambi | 0:930cad3922ee | 121 | return 0; |
ahambi | 0:930cad3922ee | 122 | } |