Friday 4 April 2014

Update to AD9850 DDS Arduino code

Thanks to M0XPD!!! He has published a DDS.h library to handle the programming of the AD9850 module with three simple methods:

#include "DDS.h"

// pin connections
#define RESET 0     // Pin  RST
#define DATA 1      // Pin  DATA
#define FQ_UD 2     // Pin  FQ
#define W_CLK 3     // Pin  CLK

#define XTALFREQ nnnnn // actual freq of AD9850 module xtal if not exactly 125MHz

// DDS object
DDS dds(W_CLK, FQ_UD, DATA, RESET);

double frequency = 7100000;

void setup()
{
  dds.init();
  dds.trim(XTALFREQ);
  dds.setFrequency(frequency); // set the frequency, use a double
}


Here is an example sketch using the DDS library

// DDS sketch using DDS library from M0XPD, uses soft SPI.

#include "DDS.h"
#include "LiquidCrystal.h"

// pin connections
#define RESET 0     // Pin  RST
#define DATA 1      // Pin  DATA
#define FQ_UD 2     // Pin  FQ
#define W_CLK 3     // Pin  CLK

#define D4 4        // LCD data pins
#define D5 5
#define D6 6
#define D7 7
#define RS 8        // LCD RS * EN pins
#define EN 9

#define BUTTONS A0   // buttons read , analog level

// names of bands
char bandName[][5] = {"160", "80", "60", "40", "30", "20", "17", "15", "12", "10"};

// array lookup of lower, current and upper band limits
#define LOWER 0
#define CURRENT 1
#define UPPER 2

// band limits
double freqRange[10][3] = {
    {1810000, 1900000, 2000000},
    {3500000, 3600000, 3800000},
    {5258000, 5100000,5406500},
    {7000000, 7100000, 7200000},
    {10100000, 10300000, 10150000},
    {14000000, 14200000, 14350000},
    {18068000, 18050000, 18168000},
    {21000000, 21250000, 21450000},
    {24890000, 24700000, 24990000},
    {28000000, 28500000, 29700000}
};

// DDS object
DDS dds(W_CLK, FQ_UD, DATA, RESET);
// LCD object
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

// band number
int band;

// actual frequency
double frequency;

void setup()
{
  dds.init();
  dds.trim(124970000); // crystal is not exactly 125MHz

  lcd.begin(16, 2);
  
  band = 0; // 160m band
  frequency = freqRange[band][CURRENT]; // current freq, start or last on this band
}

void loop()
{
  int x;
  
  x = analogRead(BUTTONS);
  if (x < 100) // Right
  {
    while(analogRead(BUTTONS) < 100);
    if(frequency < freqRange[band][UPPER]) frequency += 1000;
  }
  else if (x < 200) 
  {
    while(analogRead(BUTTONS) < 200);
    if(frequency < freqRange[band][UPPER]) frequency += 50;
  }
  else if (x < 400)
  {
    while(analogRead(BUTTONS) < 400);
    if(frequency > freqRange[band][LOWER]) frequency -= 50;
  }
  else if (x < 600)
  {
    while(analogRead(BUTTONS) < 600);
    if(frequency > freqRange[band][LOWER]) frequency -= 1000;
  }
  else if (x < 800)
  {
    while(analogRead(BUTTONS) < 800);
    freqRange[band][CURRENT] = frequency; // save previous band freq
    if(band < 9) band++;
    else band = 0;
    frequency = freqRange[band][CURRENT]; // load current band last freq
  }
  
  lcdDisplay(bandName[band], frequency);
  dds.setFrequency(frequency); // output frequency
}

// display LCD band & freq
void lcdDisplay(char* b, double f)
{

  lcd.setCursor(0,0); // write band & freq
  lcd.print("Band = ");
  lcd.setCursor(0, 1);
  lcd.print("Freq = ");
  
  delay(100); // stop flicker
  lcd.setCursor(7,0); // clear band data
  lcd.print("   ");
  lcd.setCursor(7,0); // write band data
  lcd.print(b);

  lcd.setCursor(7, 1); // clear freq data
  lcd.print("        ");
  lcd.setCursor(7, 1); // write freq data
  lcd.print(f/1000);
}

No comments: