Control AD8933 waveform generator chip from mBed interfacing with MATLAB
Dependencies: mbed
main.cpp@0:15b3f9279b88, 2014-07-07 (annotated)
- Committer:
- pc445
- Date:
- Mon Jul 07 02:03:22 2014 +0000
- Revision:
- 0:15b3f9279b88
communication with Matlab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pc445 | 0:15b3f9279b88 | 1 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 2 | // AD9833 control code |
pc445 | 0:15b3f9279b88 | 3 | // Create by Po-Cheng Chen 07-04-2014 |
pc445 | 0:15b3f9279b88 | 4 | // Version 0.2 (MATLAB) |
pc445 | 0:15b3f9279b88 | 5 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 6 | |
pc445 | 0:15b3f9279b88 | 7 | #include "mbed.h" |
pc445 | 0:15b3f9279b88 | 8 | #include "SPI.h" |
pc445 | 0:15b3f9279b88 | 9 | #include "DigitalOut.h" |
pc445 | 0:15b3f9279b88 | 10 | |
pc445 | 0:15b3f9279b88 | 11 | SPI AD9833_SPI(p5, p6, p7); //mosi, miso, sclk --> don't need MISO |
pc445 | 0:15b3f9279b88 | 12 | DigitalOut fSync(p8); //use p8 as FSync data pin (same as chip select) |
pc445 | 0:15b3f9279b88 | 13 | |
pc445 | 0:15b3f9279b88 | 14 | Serial pc(USBTX, USBRX); //define serial USB port |
pc445 | 0:15b3f9279b88 | 15 | DigitalOut myled1(LED1), myled2(LED2), myled3(LED3), myled4(LED4); //define LEDs |
pc445 | 0:15b3f9279b88 | 16 | |
pc445 | 0:15b3f9279b88 | 17 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 18 | // ASCII to Interger function |
pc445 | 0:15b3f9279b88 | 19 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 20 | unsigned int ascii2int(char c) |
pc445 | 0:15b3f9279b88 | 21 | { |
pc445 | 0:15b3f9279b88 | 22 | unsigned int i; |
pc445 | 0:15b3f9279b88 | 23 | i = 0; |
pc445 | 0:15b3f9279b88 | 24 | switch(c) { |
pc445 | 0:15b3f9279b88 | 25 | case 0x20: |
pc445 | 0:15b3f9279b88 | 26 | i = 0; |
pc445 | 0:15b3f9279b88 | 27 | break; |
pc445 | 0:15b3f9279b88 | 28 | case '0' : |
pc445 | 0:15b3f9279b88 | 29 | i = 0; |
pc445 | 0:15b3f9279b88 | 30 | break; |
pc445 | 0:15b3f9279b88 | 31 | case '1' : |
pc445 | 0:15b3f9279b88 | 32 | i = 1; |
pc445 | 0:15b3f9279b88 | 33 | break; |
pc445 | 0:15b3f9279b88 | 34 | case '2' : |
pc445 | 0:15b3f9279b88 | 35 | i = 2; |
pc445 | 0:15b3f9279b88 | 36 | break; |
pc445 | 0:15b3f9279b88 | 37 | case '3' : |
pc445 | 0:15b3f9279b88 | 38 | i = 3; |
pc445 | 0:15b3f9279b88 | 39 | break; |
pc445 | 0:15b3f9279b88 | 40 | case '4' : |
pc445 | 0:15b3f9279b88 | 41 | i = 4; |
pc445 | 0:15b3f9279b88 | 42 | break; |
pc445 | 0:15b3f9279b88 | 43 | case '5' : |
pc445 | 0:15b3f9279b88 | 44 | i = 5; |
pc445 | 0:15b3f9279b88 | 45 | break; |
pc445 | 0:15b3f9279b88 | 46 | case '6' : |
pc445 | 0:15b3f9279b88 | 47 | i = 6; |
pc445 | 0:15b3f9279b88 | 48 | break; |
pc445 | 0:15b3f9279b88 | 49 | case '7' : |
pc445 | 0:15b3f9279b88 | 50 | i = 7; |
pc445 | 0:15b3f9279b88 | 51 | break; |
pc445 | 0:15b3f9279b88 | 52 | case '8' : |
pc445 | 0:15b3f9279b88 | 53 | i = 8; |
pc445 | 0:15b3f9279b88 | 54 | break; |
pc445 | 0:15b3f9279b88 | 55 | case '9' : |
pc445 | 0:15b3f9279b88 | 56 | i = 9; |
pc445 | 0:15b3f9279b88 | 57 | break; |
pc445 | 0:15b3f9279b88 | 58 | default: |
pc445 | 0:15b3f9279b88 | 59 | i = 0; |
pc445 | 0:15b3f9279b88 | 60 | break; |
pc445 | 0:15b3f9279b88 | 61 | } |
pc445 | 0:15b3f9279b88 | 62 | return i; |
pc445 | 0:15b3f9279b88 | 63 | } |
pc445 | 0:15b3f9279b88 | 64 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 65 | // 8 digit function |
pc445 | 0:15b3f9279b88 | 66 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 67 | unsigned int digit8(char c7, char c6, char c5, char c4, |
pc445 | 0:15b3f9279b88 | 68 | char c3, char c2, char c1, char c0) |
pc445 | 0:15b3f9279b88 | 69 | { |
pc445 | 0:15b3f9279b88 | 70 | unsigned int k7 = 10000000*ascii2int(c7); |
pc445 | 0:15b3f9279b88 | 71 | unsigned int k6 = 1000000*ascii2int(c6); |
pc445 | 0:15b3f9279b88 | 72 | unsigned int k5 = 100000*ascii2int(c5); |
pc445 | 0:15b3f9279b88 | 73 | unsigned int k4 = 10000*ascii2int(c4); |
pc445 | 0:15b3f9279b88 | 74 | unsigned int k3 = 1000*ascii2int(c3); |
pc445 | 0:15b3f9279b88 | 75 | unsigned int k2 = 100*ascii2int(c2); |
pc445 | 0:15b3f9279b88 | 76 | unsigned int k1 = 10*ascii2int(c1); |
pc445 | 0:15b3f9279b88 | 77 | unsigned int k0 = ascii2int(c0); |
pc445 | 0:15b3f9279b88 | 78 | unsigned int k = k7 + k6 + k5 + k4 + k3 + k2 + k1 + k0; |
pc445 | 0:15b3f9279b88 | 79 | return k; |
pc445 | 0:15b3f9279b88 | 80 | } |
pc445 | 0:15b3f9279b88 | 81 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 82 | // SPI initialization |
pc445 | 0:15b3f9279b88 | 83 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 84 | void SPI_INITIALIZATION() |
pc445 | 0:15b3f9279b88 | 85 | { |
pc445 | 0:15b3f9279b88 | 86 | //Setup the spi for 16 bit data, with 25MHz clock rate, mode 2 |
pc445 | 0:15b3f9279b88 | 87 | // Note: mBed is MSB first SPI protocol |
pc445 | 0:15b3f9279b88 | 88 | AD9833_SPI.format(16,2); |
pc445 | 0:15b3f9279b88 | 89 | AD9833_SPI.frequency(25000000); |
pc445 | 0:15b3f9279b88 | 90 | fSync = 1; |
pc445 | 0:15b3f9279b88 | 91 | wait_ms(10); |
pc445 | 0:15b3f9279b88 | 92 | } |
pc445 | 0:15b3f9279b88 | 93 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 94 | // SPI writing |
pc445 | 0:15b3f9279b88 | 95 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 96 | void write_SPI(short dat) |
pc445 | 0:15b3f9279b88 | 97 | { |
pc445 | 0:15b3f9279b88 | 98 | fSync = 0; |
pc445 | 0:15b3f9279b88 | 99 | AD9833_SPI.write(dat); |
pc445 | 0:15b3f9279b88 | 100 | fSync = 1; |
pc445 | 0:15b3f9279b88 | 101 | } |
pc445 | 0:15b3f9279b88 | 102 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 103 | // Calculate desired frequency |
pc445 | 0:15b3f9279b88 | 104 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 105 | long calFreq(long freq) |
pc445 | 0:15b3f9279b88 | 106 | { |
pc445 | 0:15b3f9279b88 | 107 | long freq_cal; //define freq calculated value |
pc445 | 0:15b3f9279b88 | 108 | float freq_val = 0.00000000; //define ferq calculate tempotary value |
pc445 | 0:15b3f9279b88 | 109 | |
pc445 | 0:15b3f9279b88 | 110 | //calculate the frqe reg value |
pc445 | 0:15b3f9279b88 | 111 | //((desired frequency)/(reference frequency)) x 0x10000000. |
pc445 | 0:15b3f9279b88 | 112 | freq_val = (((float)(freq))/25000000); |
pc445 | 0:15b3f9279b88 | 113 | freq_cal = freq_val*0x10000000; |
pc445 | 0:15b3f9279b88 | 114 | return freq_cal; |
pc445 | 0:15b3f9279b88 | 115 | } |
pc445 | 0:15b3f9279b88 | 116 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 117 | // Set frequency register |
pc445 | 0:15b3f9279b88 | 118 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 119 | void setFreq(long frequency) |
pc445 | 0:15b3f9279b88 | 120 | { |
pc445 | 0:15b3f9279b88 | 121 | int freq_MSB; //define freq MSB reg value |
pc445 | 0:15b3f9279b88 | 122 | int freq_LSB; //define freq LSB reg value |
pc445 | 0:15b3f9279b88 | 123 | |
pc445 | 0:15b3f9279b88 | 124 | long freq_cal = calFreq(frequency); //calculate the frequency register |
pc445 | 0:15b3f9279b88 | 125 | |
pc445 | 0:15b3f9279b88 | 126 | freq_MSB = (int)((freq_cal & 0xFFFC000)>>14); // shift 14 bits |
pc445 | 0:15b3f9279b88 | 127 | freq_LSB = (int)(freq_cal & 0x3FFF); |
pc445 | 0:15b3f9279b88 | 128 | |
pc445 | 0:15b3f9279b88 | 129 | freq_MSB = freq_MSB | 0x4000; //assign freq reg address |
pc445 | 0:15b3f9279b88 | 130 | freq_LSB = freq_LSB | 0x4000; |
pc445 | 0:15b3f9279b88 | 131 | /////////////////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 132 | //print the data from the serial port to verifiy the function |
pc445 | 0:15b3f9279b88 | 133 | //printf("freq_LSB 0x%x\n\r", freq_LSB); |
pc445 | 0:15b3f9279b88 | 134 | //printf("freq_MSB 0x%x\n\r", freq_MSB); |
pc445 | 0:15b3f9279b88 | 135 | /////////////////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 136 | write_SPI(0x2100); //write control reg - apply reset |
pc445 | 0:15b3f9279b88 | 137 | write_SPI(freq_LSB); //write freq reg - LSB |
pc445 | 0:15b3f9279b88 | 138 | write_SPI(freq_MSB); //write freq reg - MSB |
pc445 | 0:15b3f9279b88 | 139 | write_SPI(0xC000); //write phase reg - (0 for now) |
pc445 | 0:15b3f9279b88 | 140 | write_SPI(0x2000); //write control reg - disable reset |
pc445 | 0:15b3f9279b88 | 141 | } |
pc445 | 0:15b3f9279b88 | 142 | |
pc445 | 0:15b3f9279b88 | 143 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 144 | // Main function |
pc445 | 0:15b3f9279b88 | 145 | // Comments:include the command line from Matlab |
pc445 | 0:15b3f9279b88 | 146 | // Control from COM port |
pc445 | 0:15b3f9279b88 | 147 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:15b3f9279b88 | 148 | int main() |
pc445 | 0:15b3f9279b88 | 149 | { |
pc445 | 0:15b3f9279b88 | 150 | char buf[128]; // assign buffer for the command from PC |
pc445 | 0:15b3f9279b88 | 151 | SPI_INITIALIZATION(); // initialized the SPI |
pc445 | 0:15b3f9279b88 | 152 | while(true) { |
pc445 | 0:15b3f9279b88 | 153 | if (pc.readable()) { |
pc445 | 0:15b3f9279b88 | 154 | //WAV AAAA BBBB CCCC DDDD EEEE FFFF GGGG HHHH END \n \0 |
pc445 | 0:15b3f9279b88 | 155 | //A, B, C, D ,E, F, G, H are frequency numbers |
pc445 | 0:15b3f9279b88 | 156 | //need 3 + (32) + 3 + 1 + 1 = 40 |
pc445 | 0:15b3f9279b88 | 157 | pc.gets(buf, 16); |
pc445 | 0:15b3f9279b88 | 158 | if ((buf[0] == 'W')&&(buf[1] == 'A')&&(buf[2] == 'V')&&(buf[11] == 'E')&&(buf[12] == 'N')&&(buf[13] == 'D')) { |
pc445 | 0:15b3f9279b88 | 159 | myled1 = !myled1; |
pc445 | 0:15b3f9279b88 | 160 | int freq_pc = digit8(buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10]); |
pc445 | 0:15b3f9279b88 | 161 | setFreq(freq_pc); |
pc445 | 0:15b3f9279b88 | 162 | |
pc445 | 0:15b3f9279b88 | 163 | } // end of if( buffer read) |
pc445 | 0:15b3f9279b88 | 164 | } // end of if(pc.readable()) |
pc445 | 0:15b3f9279b88 | 165 | }// end of while() |
pc445 | 0:15b3f9279b88 | 166 | } // end of main() |