![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@0:bea0000c5526, 2014-02-09 (annotated)
- Committer:
- alan_matheson
- Date:
- Sun Feb 09 23:13:55 2014 +0000
- Revision:
- 0:bea0000c5526
- Child:
- 1:44d13dabc8e9
Initial port of AD9850 program from Arduino
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 | 0:bea0000c5526 | 4 | * A simple single freq AD9850 Arduino test script |
alan_matheson | 0:bea0000c5526 | 5 | * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com |
alan_matheson | 0:bea0000c5526 | 6 | * Modified for testing the inexpensive AD9850 ebay DDS modules |
alan_matheson | 0:bea0000c5526 | 7 | * Pictures and pinouts at nr8o.dhlpilotcentral.com |
alan_matheson | 0:bea0000c5526 | 8 | * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf |
alan_matheson | 0:bea0000c5526 | 9 | * Use freely |
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 | 0:bea0000c5526 | 21 | |
alan_matheson | 0:bea0000c5526 | 22 | |
alan_matheson | 0:bea0000c5526 | 23 | // configure data pins for output |
alan_matheson | 0:bea0000c5526 | 24 | DigitalOut FQ_UD(p8); |
alan_matheson | 0:bea0000c5526 | 25 | DigitalOut W_CLK(p7); |
alan_matheson | 0:bea0000c5526 | 26 | DigitalOut DATA(p5); |
alan_matheson | 0:bea0000c5526 | 27 | DigitalOut RESET(p15); |
alan_matheson | 0:bea0000c5526 | 28 | |
alan_matheson | 0:bea0000c5526 | 29 | |
alan_matheson | 0:bea0000c5526 | 30 | |
alan_matheson | 0:bea0000c5526 | 31 | |
alan_matheson | 0:bea0000c5526 | 32 | // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line |
alan_matheson | 0:bea0000c5526 | 33 | void tfr_byte_module1(char data) |
alan_matheson | 0:bea0000c5526 | 34 | { |
alan_matheson | 0:bea0000c5526 | 35 | for (int i=0; i<8; i++, data>>=1) { |
alan_matheson | 0:bea0000c5526 | 36 | DATA = (data & 0x01); |
alan_matheson | 0:bea0000c5526 | 37 | pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high |
alan_matheson | 0:bea0000c5526 | 38 | } |
alan_matheson | 0:bea0000c5526 | 39 | } |
alan_matheson | 0:bea0000c5526 | 40 | |
alan_matheson | 0:bea0000c5526 | 41 | void tfr_byte_module2(char data) |
alan_matheson | 0:bea0000c5526 | 42 | { |
alan_matheson | 0:bea0000c5526 | 43 | for (int i=0; i<8; i++, data>>=1) { |
alan_matheson | 0:bea0000c5526 | 44 | DATA = (data & 0x01); |
alan_matheson | 0:bea0000c5526 | 45 | pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high |
alan_matheson | 0:bea0000c5526 | 46 | } |
alan_matheson | 0:bea0000c5526 | 47 | } |
alan_matheson | 0:bea0000c5526 | 48 | |
alan_matheson | 0:bea0000c5526 | 49 | |
alan_matheson | 0:bea0000c5526 | 50 | // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32 |
alan_matheson | 0:bea0000c5526 | 51 | void sendFrequency(double frequency) { |
alan_matheson | 0:bea0000c5526 | 52 | int32_t freq = frequency *1e6 * 4294967295/125000000; // note 125 MHz clock on 9850 |
alan_matheson | 0:bea0000c5526 | 53 | for (int b=0; b<4; b++, freq>>=8) { |
alan_matheson | 0:bea0000c5526 | 54 | tfr_byte_module1(freq & 0xFF); |
alan_matheson | 0:bea0000c5526 | 55 | } |
alan_matheson | 0:bea0000c5526 | 56 | tfr_byte_module1(0X38); // Final control byte, all 0 for 9850 chip |
alan_matheson | 0:bea0000c5526 | 57 | } |
alan_matheson | 0:bea0000c5526 | 58 | |
alan_matheson | 0:bea0000c5526 | 59 | |
alan_matheson | 0:bea0000c5526 | 60 | int main() |
alan_matheson | 0:bea0000c5526 | 61 | { |
alan_matheson | 0:bea0000c5526 | 62 | double frequency; |
alan_matheson | 0:bea0000c5526 | 63 | |
alan_matheson | 0:bea0000c5526 | 64 | // Initialise the AD9850 module |
alan_matheson | 0:bea0000c5526 | 65 | pulseHigh(RESET); |
alan_matheson | 0:bea0000c5526 | 66 | pulseHigh(W_CLK); |
alan_matheson | 0:bea0000c5526 | 67 | pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10 |
alan_matheson | 0:bea0000c5526 | 68 | |
alan_matheson | 0:bea0000c5526 | 69 | |
alan_matheson | 0:bea0000c5526 | 70 | |
alan_matheson | 0:bea0000c5526 | 71 | |
alan_matheson | 0:bea0000c5526 | 72 | |
alan_matheson | 0:bea0000c5526 | 73 | while(1) |
alan_matheson | 0:bea0000c5526 | 74 | { |
alan_matheson | 0:bea0000c5526 | 75 | frequency = 10.0034; |
alan_matheson | 0:bea0000c5526 | 76 | { |
alan_matheson | 0:bea0000c5526 | 77 | sendFrequency(frequency); |
alan_matheson | 0:bea0000c5526 | 78 | pulseHigh(FQ_UD); |
alan_matheson | 0:bea0000c5526 | 79 | wait(5.0); |
alan_matheson | 0:bea0000c5526 | 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 |