single line output
[Arduino] / AD9850 / AD9850.ino
1 /*
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
7  * Use freely
8  */
9  
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).
14
15 #define encoder_a 3
16 #define encoder_b 4
17 #define encoder_click 5
18  
19 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
20
21 double user_freq = 1.e6; // 10.e6
22  
23 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line
24 void tfr_byte(byte data)
25 {
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
29   }
30 }
31
32  
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);
38   }
39   tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
40   pulseHigh(FQ_UD);  // Done!  Should see output
41   
42   Serial.print("Freq = ");
43   Serial.print((frequency / 1.e6), 4);
44   Serial.println(" MHz");
45 }
46
47  
48 void setup() {
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);
54  
55   pulseHigh(RESET);
56   pulseHigh(W_CLK);
57   pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
58
59   // encoder
60   pinMode(encoder_a, INPUT);
61   pinMode(encoder_b, INPUT);
62   pinMode(encoder_click, INPUT);
63   
64   Serial.begin(9600);
65   sendFrequency(user_freq);
66 }
67
68 double freq_step = 1.e5; // 0.1 MHz
69 int encoder_state = LOW;
70 int encoder_last  = LOW;
71
72 void loop() {
73   if (Serial.available()) {
74      int inByte = Serial.read();
75      Serial.println(inByte, DEC);
76      if (inByte == 43 || inByte == 61) { // + or =
77        user_freq += freq_step;
78        sendFrequency(user_freq);
79      } else if ( inByte == 45 ) {
80        user_freq -= freq_step; 
81        sendFrequency(user_freq);
82      }
83   }
84
85   encoder_state = digitalRead(encoder_a);
86   if ((encoder_last == LOW) && (encoder_state == HIGH)) {
87     if (digitalRead(encoder_b) == LOW) {
88       user_freq -= freq_step;
89     } else {
90       user_freq += freq_step;
91     }
92     sendFrequency(user_freq);
93   }
94   encoder_last = encoder_state;
95   
96   if(digitalRead(encoder_click) == LOW) sendFrequency( 1.e6 ); // reset to 1MHz
97
98 }