/* * A simple single freq AD9850 Arduino test script * Original AD9851 DDS sketch by Andrew Smallbone at www.rocketnumbernine.com * Modified for testing the inexpensive AD9850 ebay DDS modules * Pictures and pinouts at nr8o.dhlpilotcentral.com * 9850 datasheet at http://www.analog.com/static/imported-files/data_sheets/AD9850.pdf * Use freely */ #define W_CLK 8 // Pin 8 - connect to AD9850 module word load clock pin (CLK) #define FQ_UD 9 // Pin 9 - connect to freq update pin (FQ) #define DATA 10 // Pin 10 - connect to serial data load pin (DATA) #define RESET 11 // Pin 11 - connect to reset pin (RST). #define encoder_a 3 #define encoder_b 4 #define encoder_click 5 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); } double user_freq = 1.e6; // 10.e6 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line void tfr_byte(byte data) { for (int i=0; i<8; i++, data>>=1) { digitalWrite(DATA, data & 0x01); pulseHigh(W_CLK); //after each bit sent, CLK is pulsed high } } // frequency calc from datasheet page 8 = * /2^32 void sendFrequency(double frequency) { int32_t freq = frequency * 4294967295/125000000; // note 125 MHz clock on 9850 for (int b=0; b<4; b++, freq>>=8) { tfr_byte(freq & 0xFF); } tfr_byte(0x000); // Final control byte, all 0 for 9850 chip pulseHigh(FQ_UD); // Done! Should see output Serial.print("Freq = "); Serial.print((frequency / 1.e6), 4); Serial.println(" MHz"); } void setup() { // configure arduino data pins for output pinMode(FQ_UD, OUTPUT); pinMode(W_CLK, OUTPUT); pinMode(DATA, OUTPUT); pinMode(RESET, OUTPUT); pulseHigh(RESET); pulseHigh(W_CLK); pulseHigh(FQ_UD); // this pulse enables serial mode - Datasheet page 12 figure 10 // encoder pinMode(encoder_a, INPUT); pinMode(encoder_b, INPUT); pinMode(encoder_click, INPUT); Serial.begin(9600); sendFrequency(user_freq); } double freq_step = 1.e5; // 0.1 MHz int encoder_state = LOW; int encoder_last = LOW; void loop() { if (Serial.available()) { int inByte = Serial.read(); Serial.println(inByte, DEC); if (inByte == 43 || inByte == 61) { // + or = user_freq += freq_step; sendFrequency(user_freq); } else if ( inByte == 45 ) { user_freq -= freq_step; sendFrequency(user_freq); } } encoder_state = digitalRead(encoder_a); if ((encoder_last == LOW) && (encoder_state == HIGH)) { if (digitalRead(encoder_b) == LOW) { user_freq -= freq_step; } else { user_freq += freq_step; } sendFrequency(user_freq); } encoder_last = encoder_state; if(digitalRead(encoder_click) == LOW) sendFrequency( 1.e6 ); // reset to 1MHz }