Control of a AD9851 DDS or Midnight Design DDS60 from serial terminal

Dependencies:   mbed

Fork of Serial_HelloWorld_Mbed by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 #include "mbed.h"
00002 /** @file main.cpp
00003  *
00004  * Serial controlled AD9851 demo
00005  * Mainly intended for DDS60 board but should be adaptable by changing XTAL
00006  * Note uses a software serial protocol resembling SPI(0)
00007  * AD9851 is LSB first and is believed to still accept clocks when LOAD is high
00008  * Initial start up is odd due to need to leave parallel mode, and to prevent 
00009  * loading junk if the device is already in serial mode the first "LOAD" must 
00010  * come after a valid serial word. In this case first 8 then 40 zeros are sent 
00011  * on program start
00012  *
00013  * Note using a high-idling select line and a high-idling clock but
00014  * according to datasheet both are rising-edge triggered
00015  */
00016 
00017 #define xtal 180000000
00018 
00019 DigitalOut mosi(p5);
00020 DigitalOut sck(p7,1);
00021 DigitalOut sel(p22,1); //default 1 to try to avoid false edges
00022 
00023 ///LSB-first serial send
00024 void ssend(unsigned char count, unsigned int prog)
00025 {
00026     
00027     while(count)
00028     {
00029         mosi=prog&1;     
00030         sck=0;
00031         wait_us(1);
00032         sck=1;
00033         wait_us(1);
00034         count--;
00035         prog=prog>>1;
00036     }
00037 }        
00038     
00039 /** calculate divisor value
00040  * calculates the sum frequency*(1<<32)/xtal
00041  * fails if frequency>xtal or if frequency > maxint
00042  */
00043 unsigned int CalcFreq(unsigned int freq,unsigned int ref)
00044 {
00045     unsigned char count;
00046     unsigned int result=0;
00047     for(count=0;count< 32;count++)
00048     {
00049       result = result << 1;
00050       freq=freq << 1;
00051       if (freq >= ref)
00052       {
00053         freq = freq - ref;
00054         result++;
00055       }
00056     }
00057     return(result);
00058 } 
00059 Serial pc(USBTX, USBRX); // tx, rx
00060 
00061 
00062 int freq;
00063 int prog;
00064 char c;
00065 int main() {
00066     // zero the AD9851 to prevent erroneous operation        
00067     sck=1;
00068     sel=0;
00069     ssend(8,0);
00070     sel=1;
00071     wait_us(1);
00072     sel=0;
00073     ssend(40,0);
00074     sel=1;
00075 
00076     while(1) 
00077     {
00078         pc.printf("AD9851 control\r\n");
00079         pc.printf("Enter Frequency\r\n");
00080         freq=0;
00081         c=0;
00082         while(c!=13) {
00083             c=pc.getc();
00084             if (c>=48 & c<58) {
00085                 freq = freq*10 + c-48;
00086                 pc.printf("\r%i",freq);
00087             } else {
00088                 if (c==0x7f)
00089                 {
00090                     freq = freq / 10;
00091                     pc.printf("\r%i \b",freq);
00092                 }
00093             }
00094         }
00095         if (freq)
00096         {
00097             prog=CalcFreq(freq,xtal); //converts from MHz to internal control word
00098             pc.printf("\r\nControl word: 0x01%08x\r\n",prog);//
00099             sel=0;
00100             ssend(32,prog);
00101             ssend(8,0x01);
00102             sel=1;
00103         }
00104     }
00105 }