2 * A simple single freq AD9850 Arduino test script
3 * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com
4 * Modified for testing the inexpensive AD9850 ebay DDS modules
5 * Pictures and pinouts at nr8o.dhlpilotcentral.com
6 * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf
10 #define W_CLK 8 // Pin 8 - connect to AD9850 module word load clock pin (CLK)
11 #define FQ_UD 9 // Pin 9 - connect to freq update pin (FQ)
12 #define DATA 10 // Pin 10 - connect to serial data load pin (DATA)
13 #define RESET 11 // Pin 11 - connect to reset pin (RST).
17 #define encoder_click 5
19 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
21 double user_freq = 1.e6; // 10.e6
23 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
24 void tfr_byte(byte data)
26 for (int i=0; i<8; i++, data>>=1) {
27 digitalWrite(DATA, data & 0x01);
28 pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high
33 // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
34 void sendFrequency(double frequency) {
35 int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850
36 for (int b=0; b<4; b++, freq>>=8) {
37 tfr_byte(freq & 0xFF);
39 tfr_byte(0x000); // Final control byte, all 0 for 9850 chip
40 pulseHigh(FQ_UD); // Done! Should see output
42 Serial.print("Freq = ");
43 Serial.print((frequency / 1.e6), 4);
44 Serial.println(" MHz");
49 // configure arduino data pins for output
50 pinMode(FQ_UD, OUTPUT);
51 pinMode(W_CLK, OUTPUT);
52 pinMode(DATA, OUTPUT);
53 pinMode(RESET, OUTPUT);
57 pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10
60 pinMode(encoder_a, INPUT);
61 pinMode(encoder_b, INPUT);
62 pinMode(encoder_click, INPUT);
65 sendFrequency(user_freq);
68 double freq_step = 1.e5; // 0.1 MHz
69 int encoder_state = LOW;
70 int encoder_last = LOW;
73 if (Serial.available()) {
74 int inByte = Serial.read();
75 Serial.println(inByte, DEC);
76 if (inByte == 43 || inByte == 61 or inByte == 67) { // + = left
77 user_freq += freq_step;
78 sendFrequency(user_freq);
79 } else if ( inByte == 45 || inByte == 68) { // - right
80 user_freq -= freq_step;
81 sendFrequency(user_freq);
82 } else if ( inByte == 65 ) { // up
83 user_freq -= freq_step * 10;
84 sendFrequency(user_freq);
85 } else if ( inByte == 66 ) { // down
86 user_freq += freq_step * 10;
87 sendFrequency(user_freq);
88 } else if ( inByte >= 48 && inByte <= 57) { // 0 .. 9
89 user_freq = ( inByte - 48 ) * 1.e6;
90 sendFrequency(user_freq);
94 encoder_state = digitalRead(encoder_a);
95 if ((encoder_last == LOW) && (encoder_state == HIGH)) {
96 if (digitalRead(encoder_b) == LOW) {
97 user_freq -= freq_step;
99 user_freq += freq_step;
101 sendFrequency(user_freq);
103 encoder_last = encoder_state;
105 if(digitalRead(encoder_click) == LOW) sendFrequency( 1.e6 ); // reset to 1MHz