![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Basic AD9933
Dependencies: mbed
main.cpp@0:c5d5930ea5e7, 2014-11-26 (annotated)
- Committer:
- pc445
- Date:
- Wed Nov 26 18:21:15 2014 +0000
- Revision:
- 0:c5d5930ea5e7
AD9833
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pc445 | 0:c5d5930ea5e7 | 1 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 2 | // AD9833 control code |
pc445 | 0:c5d5930ea5e7 | 3 | // Create by Po-Cheng Chen 07-04-2014 |
pc445 | 0:c5d5930ea5e7 | 4 | // Version 0.1 |
pc445 | 0:c5d5930ea5e7 | 5 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 6 | |
pc445 | 0:c5d5930ea5e7 | 7 | #include "mbed.h" |
pc445 | 0:c5d5930ea5e7 | 8 | #include "SPI.h" |
pc445 | 0:c5d5930ea5e7 | 9 | #include "DigitalOut.h" |
pc445 | 0:c5d5930ea5e7 | 10 | |
pc445 | 0:c5d5930ea5e7 | 11 | SPI AD9833_SPI(p5, p6, p7); //mosi, miso, sclk --> don't need MISO |
pc445 | 0:c5d5930ea5e7 | 12 | DigitalOut fSync(p8); //use p8 as FSync data pin (same as chip select) |
pc445 | 0:c5d5930ea5e7 | 13 | |
pc445 | 0:c5d5930ea5e7 | 14 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 15 | // SPI initialization |
pc445 | 0:c5d5930ea5e7 | 16 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 17 | void SPI_INITIALIZATION() |
pc445 | 0:c5d5930ea5e7 | 18 | { |
pc445 | 0:c5d5930ea5e7 | 19 | //Setup the spi for 16 bit data, with 25MHz clock rate, mode 2 |
pc445 | 0:c5d5930ea5e7 | 20 | // Note: mBed is MSB first SPI protocol |
pc445 | 0:c5d5930ea5e7 | 21 | AD9833_SPI.format(16,2); |
pc445 | 0:c5d5930ea5e7 | 22 | AD9833_SPI.frequency(25000000); |
pc445 | 0:c5d5930ea5e7 | 23 | fSync = 1; |
pc445 | 0:c5d5930ea5e7 | 24 | wait_ms(10); |
pc445 | 0:c5d5930ea5e7 | 25 | } |
pc445 | 0:c5d5930ea5e7 | 26 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 27 | // SPI writing |
pc445 | 0:c5d5930ea5e7 | 28 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 29 | void write_SPI(short dat) |
pc445 | 0:c5d5930ea5e7 | 30 | { |
pc445 | 0:c5d5930ea5e7 | 31 | fSync = 0; |
pc445 | 0:c5d5930ea5e7 | 32 | AD9833_SPI.write(dat); |
pc445 | 0:c5d5930ea5e7 | 33 | fSync = 1; |
pc445 | 0:c5d5930ea5e7 | 34 | } |
pc445 | 0:c5d5930ea5e7 | 35 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 36 | // Set desired frequency |
pc445 | 0:c5d5930ea5e7 | 37 | // Comments:Calculate the desired frequency accrodingly |
pc445 | 0:c5d5930ea5e7 | 38 | // Do it in Matlab or mBed? |
pc445 | 0:c5d5930ea5e7 | 39 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 40 | void setFreq(long FREQ) |
pc445 | 0:c5d5930ea5e7 | 41 | { |
pc445 | 0:c5d5930ea5e7 | 42 | int freq_MSB; //define freq MSB reg value |
pc445 | 0:c5d5930ea5e7 | 43 | int freq_LSB; //define freq LSB reg value |
pc445 | 0:c5d5930ea5e7 | 44 | |
pc445 | 0:c5d5930ea5e7 | 45 | long freq_cal; //define freq calculated value |
pc445 | 0:c5d5930ea5e7 | 46 | float freq_val = 0.00000000; //define ferq calculate tempotary value |
pc445 | 0:c5d5930ea5e7 | 47 | |
pc445 | 0:c5d5930ea5e7 | 48 | //calculate the frqe reg value |
pc445 | 0:c5d5930ea5e7 | 49 | //((desired frequency)/(reference frequency)) x 0x10000000. |
pc445 | 0:c5d5930ea5e7 | 50 | freq_val = (((float)(frequency))/25000000); |
pc445 | 0:c5d5930ea5e7 | 51 | freq_cal = freq_val*0x10000000; |
pc445 | 0:c5d5930ea5e7 | 52 | |
pc445 | 0:c5d5930ea5e7 | 53 | freq_MSB = (int)((freq_cal & 0xFFFC000)>>14); // shift 14 bits |
pc445 | 0:c5d5930ea5e7 | 54 | freq_LSB = (int)(freq_cal & 0x3FFF); |
pc445 | 0:c5d5930ea5e7 | 55 | |
pc445 | 0:c5d5930ea5e7 | 56 | freq_MSB = freq_MSB | 0x4000; //assign freq reg address |
pc445 | 0:c5d5930ea5e7 | 57 | freq_LSB = freq_LSB | 0x4000; |
pc445 | 0:c5d5930ea5e7 | 58 | /////////////////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 59 | //print the data from the serial port to verifiy the function |
pc445 | 0:c5d5930ea5e7 | 60 | printf("freq_LSB 0x%x\n\r", freq_LSB); |
pc445 | 0:c5d5930ea5e7 | 61 | //print the data from the serial port to verifiy the function |
pc445 | 0:c5d5930ea5e7 | 62 | printf("freq_MSB 0x%x\n\r", freq_MSB); |
pc445 | 0:c5d5930ea5e7 | 63 | /////////////////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 64 | write_SPI(0x2100); //write control reg - apply reset |
pc445 | 0:c5d5930ea5e7 | 65 | write_SPI(freq_LSB); //write freq reg - LSB |
pc445 | 0:c5d5930ea5e7 | 66 | write_SPI(freq_MSB); //write freq reg - MSB |
pc445 | 0:c5d5930ea5e7 | 67 | write_SPI(0xC000); //write phase reg - 0 for now |
pc445 | 0:c5d5930ea5e7 | 68 | write_SPI(0x2000); //write control reg - disable reset |
pc445 | 0:c5d5930ea5e7 | 69 | } |
pc445 | 0:c5d5930ea5e7 | 70 | |
pc445 | 0:c5d5930ea5e7 | 71 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 72 | // Main function |
pc445 | 0:c5d5930ea5e7 | 73 | // Comments:include the command line from Matlab |
pc445 | 0:c5d5930ea5e7 | 74 | // Control from COM port |
pc445 | 0:c5d5930ea5e7 | 75 | ///////////////////////////////////////////////////////////////////////// |
pc445 | 0:c5d5930ea5e7 | 76 | int main() |
pc445 | 0:c5d5930ea5e7 | 77 | { |
pc445 | 0:c5d5930ea5e7 | 78 | SPI_INITIALIZATION(); // initialized the SPI |
pc445 | 0:c5d5930ea5e7 | 79 | setFreq(100000); |
pc445 | 0:c5d5930ea5e7 | 80 | } |