Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 // MBED driver for AD9851 digital synthesizer using hacked SPI interface 00002 // Original code for AD9850 by Liam Goudge Sept 2013 00003 // Modified for AD9851 and KL25Z by Mike Richards Oct 2013 00004 00005 #include "mbed.h" 00006 00007 00008 SPI device (PTD2, PTD3, PTD1); // Set the KL25Z SPI pins 00009 DigitalOut CS(PTD0); // Use pin PTD0 as a fake Chip select (AD9851's FQ_UD line) 00010 DigitalOut ADReset(PTA13); // Use Pin PTA13 as the reset line for AD9851 00011 int targetFrq; // 32-bit frequency word for the AD9851 00012 int phase; // Phase and multiplier word for AD9851 00013 00014 Serial pc(USBTX, USBRX); // tx, rx for debug terminal 00015 00016 int reverseBits (int source) 00017 { 00018 // Unfortunately need to invert bit order of the desired frequency setting since MBED only allows for MSB first from SPI. We need LSB first for AD9851 00019 int mask=0;; 00020 int i=0; 00021 int target=0; 00022 int bitTarget=0x80000000; // Hard-wired for 32-bit inversion 00023 00024 for (i=0;i<32;i++) { // ditto 00025 mask=1<<i; 00026 bitTarget=1<<(31-i); // ditto 00027 00028 if (source & mask) 00029 target=target | bitTarget; 00030 } 00031 return target; 00032 } 00033 00034 void writeSPI(int frq, int phase) 00035 { 00036 // Send the 40-bit packet. NB: KL25Z only supports 8-bit SPI so send five 8-bit packets 00037 00038 device.format(8,0); // 8-bits per packet, mode 0 (CPOL=0 CPHA=0) 00039 device.frequency(1000000); //SPI clock set to 1MHz 00040 00041 wait_ms(1); 00042 00043 // First do chip select. Need to use a GPIO to fake the chip select since MBED doesn't allow to set positive logic CS signal 00044 CS=1; // assert chip select (a.k.a FQ_UD frequency update input to AD9851) 00045 wait_ms(1); 00046 CS=0; 00047 00048 // Write 5 x 8-bit packets for the frequency word 00049 device.write(frq>>24); // Packet 1 00050 device.write(frq>>16); // Packet 2 00051 device.write(frq>>8); // Packet 3 00052 device.write(frq); // Packet 4 00053 device.write(phase); // Packet 5 - Finish with the phase, multiplier and factory settings byte 00054 00055 // Now pulse FQ_UD again to load the word into the DDS 00056 CS=0; 00057 wait_ms(1); 00058 CS=1; 00059 wait_ms(1); 00060 CS=0; 00061 } 00062 00063 00064 int main() 00065 { 00066 00067 00068 int DesFrq = 5000000; // Set the desired output frequency in Hz 00069 float Mult = 23.86093; // Multiplier for boards using a 30MHz clock oscillator and 6x multiplier 00070 targetFrq= DesFrq * Mult ; // targetFrq is the 32-bit frequency word for the AD9851 00071 phase = 0x80; // Set the 6x clock multiplier for systems with 30MHz reference oscillator. 00072 00073 // Reset the AD9851. Active high logic. Minimum reset period 5 clock cycles 00074 ADReset=0; 00075 wait_ms(5); 00076 ADReset=1; 00077 wait_ms(5); 00078 ADReset=0; 00079 00080 00081 while(1) 00082 { 00083 00084 writeSPI(reverseBits(targetFrq),phase); // Transfer the frequency settings to the AD9851 00085 00086 00087 00088 } 00089 00090 00091 }
Generated on Thu Jul 21 2022 14:57:40 by
1.7.2