A port of AD9850/51 code from Arduino source. This port allows direct input of the frequency in Mega Hertz rather than a series of Hex digits as in Liam Goudges program.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /* 
00004  * This program is a port of an AD9851 DDS Arduino sketch by Andrew Smallbone at www.rocketnumbernine.com
00005  * It was later modified for testing the inexpensive AD9850 ebay DDS modules
00006  * Pictures and pinouts at nr8o.dhlpilotcentral.com
00007  * AD9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
00008  * Use freely
00009  * Ported by Alan Matheson on 20 February 2014
00010  */
00011  
00012  
00013  //efine W2_CLK      // connect to AD9850 module2 word load clock pin (CLK) 
00014  //efine W1_CLK 12   // connect to AD9850 module1 word load clock pin (CLK)
00015  //efine FQ_UD p5    // connect to freq update pin (FQ)
00016  //efine DATA 10     // connect to serial data load pin (DATA)
00017  //efine RESET 9     // connect to reset pin (RST).
00018  
00019  #define pulseHigh(pin) {pin = 1; pin = 0; }
00020 
00021  // Pins used are the same as those used in the MBED driver for AD9850 digital synthesizer using hacked SPI interface
00022  // by Liam Goudge Sept 2013
00023  // This program allows the desired frequency in MegaHertz to be directly entered and uses bitbanging rather than the SPI pins.
00024 
00025 
00026 
00027  // configure data pins for output
00028   DigitalOut FQ_UD(p8);
00029   DigitalOut W_CLK(p7);
00030   DigitalOut DATA(p5);
00031   DigitalOut RESET(p15);
00032   
00033   
00034 
00035  
00036  // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
00037 void tfr_byte_module1(char data)
00038 {
00039   for (int i=0; i<8; i++, data>>=1) {
00040   DATA = (data & 0x01);
00041   pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
00042   }
00043 }
00044 
00045 void tfr_byte_module2(char data)
00046 {
00047   for (int i=0; i<8; i++, data>>=1) {
00048     DATA =  (data & 0x01);
00049     pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
00050   }
00051 }
00052 
00053 
00054 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
00055 void sendFrequency(double frequency) {
00056   int32_t freq = frequency *1e6 * 4294967295/125000000;  // note 125 MHz clock on 9850
00057   for (int b=0; b<4; b++, freq>>=8) {
00058     tfr_byte_module1(freq & 0xFF);
00059   }
00060   tfr_byte_module1(0X38);   // Final control byte, all 0 for 9850 chip
00061  }
00062 
00063 
00064 int main()
00065 {
00066 double frequency;
00067 
00068 // Initialise the AD9850 module
00069  pulseHigh(RESET);
00070  pulseHigh(W_CLK);
00071  pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
00072 
00073 
00074 while(1)
00075   {
00076    frequency = 7.080;
00077    sendFrequency(frequency);
00078    pulseHigh(FQ_UD);
00079    wait(5.0);
00080    } 
00081  }
00082 
00083 }
00084 
00085