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.
main.cpp@1:44d13dabc8e9, 2014-02-10 (annotated)
- Committer:
- alan_matheson
- Date:
- Mon Feb 10 00:17:37 2014 +0000
- Revision:
- 1:44d13dabc8e9
- Parent:
- 0:bea0000c5526
AD9850 program ported from Arduino code by Alan Matheson
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alan_matheson | 0:bea0000c5526 | 1 | #include "mbed.h" |
alan_matheson | 0:bea0000c5526 | 2 | |
alan_matheson | 0:bea0000c5526 | 3 | /* |
alan_matheson | 1:44d13dabc8e9 | 4 | * This program is a port of an AD9851 DDS Arduino sketch by Andrew Smallbone at www.rocketnumbernine.com |
alan_matheson | 1:44d13dabc8e9 | 5 | * It was later modified for testing the inexpensive AD9850 ebay DDS modules |
alan_matheson | 0:bea0000c5526 | 6 | * Pictures and pinouts at nr8o.dhlpilotcentral.com |
alan_matheson | 1:44d13dabc8e9 | 7 | * AD9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf |
alan_matheson | 0:bea0000c5526 | 8 | * Use freely |
alan_matheson | 1:44d13dabc8e9 | 9 | * Ported by Alan Matheson on 20 February 2014 |
alan_matheson | 0:bea0000c5526 | 10 | */ |
alan_matheson | 0:bea0000c5526 | 11 | |
alan_matheson | 0:bea0000c5526 | 12 | |
alan_matheson | 0:bea0000c5526 | 13 | //efine W2_CLK // connect to AD9850 module2 word load clock pin (CLK) |
alan_matheson | 0:bea0000c5526 | 14 | //efine W1_CLK 12 // connect to AD9850 module1 word load clock pin (CLK) |
alan_matheson | 0:bea0000c5526 | 15 | //efine FQ_UD p5 // connect to freq update pin (FQ) |
alan_matheson | 0:bea0000c5526 | 16 | //efine DATA 10 // connect to serial data load pin (DATA) |
alan_matheson | 0:bea0000c5526 | 17 | //efine RESET 9 // connect to reset pin (RST). |
alan_matheson | 0:bea0000c5526 | 18 | |
alan_matheson | 0:bea0000c5526 | 19 | #define pulseHigh(pin) {pin = 1; pin = 0; } |
alan_matheson | 0:bea0000c5526 | 20 | |
alan_matheson | 1:44d13dabc8e9 | 21 | // Pins used are the same as those used in the MBED driver for AD9850 digital synthesizer using hacked SPI interface |
alan_matheson | 1:44d13dabc8e9 | 22 | // by Liam Goudge Sept 2013 |
alan_matheson | 1:44d13dabc8e9 | 23 | // This program allows the desired frequency in MegaHertz to be directly entered and uses bitbanging rather than the SPI pins. |
alan_matheson | 1:44d13dabc8e9 | 24 | |
alan_matheson | 0:bea0000c5526 | 25 | |
alan_matheson | 0:bea0000c5526 | 26 | |
alan_matheson | 0:bea0000c5526 | 27 | // configure data pins for output |
alan_matheson | 0:bea0000c5526 | 28 | DigitalOut FQ_UD(p8); |
alan_matheson | 0:bea0000c5526 | 29 | DigitalOut W_CLK(p7); |
alan_matheson | 0:bea0000c5526 | 30 | DigitalOut DATA(p5); |
alan_matheson | 0:bea0000c5526 | 31 | DigitalOut RESET(p15); |
alan_matheson | 0:bea0000c5526 | 32 | |
alan_matheson | 0:bea0000c5526 | 33 | |
alan_matheson | 0:bea0000c5526 | 34 | |
alan_matheson | 0:bea0000c5526 | 35 | |
alan_matheson | 0:bea0000c5526 | 36 | // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line |
alan_matheson | 0:bea0000c5526 | 37 | void tfr_byte_module1(char data) |
alan_matheson | 0:bea0000c5526 | 38 | { |
alan_matheson | 0:bea0000c5526 | 39 | for (int i=0; i<8; i++, data>>=1) { |
alan_matheson | 0:bea0000c5526 | 40 | DATA = (data & 0x01); |
alan_matheson | 0:bea0000c5526 | 41 | pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high |
alan_matheson | 0:bea0000c5526 | 42 | } |
alan_matheson | 0:bea0000c5526 | 43 | } |
alan_matheson | 0:bea0000c5526 | 44 | |
alan_matheson | 0:bea0000c5526 | 45 | void tfr_byte_module2(char data) |
alan_matheson | 0:bea0000c5526 | 46 | { |
alan_matheson | 0:bea0000c5526 | 47 | for (int i=0; i<8; i++, data>>=1) { |
alan_matheson | 0:bea0000c5526 | 48 | DATA = (data & 0x01); |
alan_matheson | 0:bea0000c5526 | 49 | pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high |
alan_matheson | 0:bea0000c5526 | 50 | } |
alan_matheson | 0:bea0000c5526 | 51 | } |
alan_matheson | 0:bea0000c5526 | 52 | |
alan_matheson | 0:bea0000c5526 | 53 | |
alan_matheson | 0:bea0000c5526 | 54 | // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32 |
alan_matheson | 0:bea0000c5526 | 55 | void sendFrequency(double frequency) { |
alan_matheson | 0:bea0000c5526 | 56 | int32_t freq = frequency *1e6 * 4294967295/125000000; // note 125 MHz clock on 9850 |
alan_matheson | 0:bea0000c5526 | 57 | for (int b=0; b<4; b++, freq>>=8) { |
alan_matheson | 0:bea0000c5526 | 58 | tfr_byte_module1(freq & 0xFF); |
alan_matheson | 0:bea0000c5526 | 59 | } |
alan_matheson | 0:bea0000c5526 | 60 | tfr_byte_module1(0X38); // Final control byte, all 0 for 9850 chip |
alan_matheson | 0:bea0000c5526 | 61 | } |
alan_matheson | 0:bea0000c5526 | 62 | |
alan_matheson | 0:bea0000c5526 | 63 | |
alan_matheson | 0:bea0000c5526 | 64 | int main() |
alan_matheson | 0:bea0000c5526 | 65 | { |
alan_matheson | 0:bea0000c5526 | 66 | double frequency; |
alan_matheson | 0:bea0000c5526 | 67 | |
alan_matheson | 0:bea0000c5526 | 68 | // Initialise the AD9850 module |
alan_matheson | 0:bea0000c5526 | 69 | pulseHigh(RESET); |
alan_matheson | 0:bea0000c5526 | 70 | pulseHigh(W_CLK); |
alan_matheson | 0:bea0000c5526 | 71 | pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10 |
alan_matheson | 0:bea0000c5526 | 72 | |
alan_matheson | 0:bea0000c5526 | 73 | |
alan_matheson | 0:bea0000c5526 | 74 | while(1) |
alan_matheson | 0:bea0000c5526 | 75 | { |
alan_matheson | 1:44d13dabc8e9 | 76 | frequency = 7.080; |
alan_matheson | 1:44d13dabc8e9 | 77 | sendFrequency(frequency); |
alan_matheson | 1:44d13dabc8e9 | 78 | pulseHigh(FQ_UD); |
alan_matheson | 1:44d13dabc8e9 | 79 | wait(5.0); |
alan_matheson | 1:44d13dabc8e9 | 80 | } |
alan_matheson | 0:bea0000c5526 | 81 | } |
alan_matheson | 0:bea0000c5526 | 82 | |
alan_matheson | 0:bea0000c5526 | 83 | } |
alan_matheson | 0:bea0000c5526 | 84 | |
alan_matheson | 0:bea0000c5526 | 85 |