Saturday, 10 March 2018

Si5351 DDS VFO/BFO Example Software

#include <Wire.h>                         // I2C comms
#include <LiquidCrystal_I2C.h>
#include <si5351.h>

// Define Constants and Vaviables
static const long bandStart = 1000000;     // start of 80m
static const long bandEnd =   30000000;    // end of 80m
static const long bandInit =  3690000;     // where to initially set the frequency
static const long LSB_IF_freq = 9001500;   // filter centre freq + 1500Hz
static const long LSB_BFO_freq = 9001500;  // filter centre freq + 1500Hz
volatile long oldfreq = 0;
volatile long freq = bandInit ;           
volatile long radix = 1000;                // how much to change the frequency by, clicking the rotary encoder will change this.
volatile int updatedisplay = 0;

// Rotary encoder pins and other inputs
static const int pushPin = 4;
static const int rotBPin = 3;
static const int rotAPin = 2;

// Rotary encoder variables, used by interrupt routines
volatile int rotState = 0;
volatile int rotAval = 1;
volatile int rotBval = 1;

// Instantiate the Objects
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Si5351 si5351;

void setup()
{
  // Set up frequency and radix switches
  pinMode(rotAPin, INPUT);
  pinMode(rotBPin, INPUT);
  pinMode(pushPin, INPUT);

  // Set up pull-up resistors on inputs
  digitalWrite(rotAPin, HIGH);
  digitalWrite(rotBPin, HIGH);
  digitalWrite(pushPin, HIGH);

  // Set up interrupt pins
  attachInterrupt(digitalPinToInterrupt(rotAPin), ISRrotAChange, CHANGE);
  attachInterrupt(digitalPinToInterrupt(rotBPin), ISRrotBChange, CHANGE);

  // Initialize the display
  lcd.begin();
  lcd.backlight();
  UpdateDisplay();
  delay(1000);

  // Initialize the DDS
  si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
  si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
  si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA);
  si5351.drive_strength(SI5351_CLK2, SI5351_DRIVE_8MA);
  si5351.set_freq((freq * 100ULL), SI5351_PLL_FIXED, SI5351_CLK0);
  si5351.set_freq((LSB_BFO_freq * 100ULL), SI5351_PLL_FIXED, SI5351_CLK2);
}


void loop()
{
  if (freq != oldfreq)
  {
    UpdateDisplay();
    SendFrequency();
    oldfreq = freq;
  }

  if (digitalRead(pushPin) == LOW)
  {
    delay(10);
    while (digitalRead(pushPin) == LOW)
    {
      if (updatedisplay == 1)
      {
        UpdateDisplay();
        updatedisplay = 0;
      }
    }
    delay(50);
  }
}


// Interrupt routines
void ISRrotAChange()
{
  if (digitalRead(rotAPin))
  {
    rotAval = 1;
    UpdateRot();
  }
  else
  {
    rotAval = 0;
    UpdateRot();
  }
}


void ISRrotBChange()
{
  if (digitalRead(rotBPin))
  {
    rotBval = 1;
    UpdateRot();
  }
  else
  {
    rotBval = 0;
    UpdateRot();
  }
}


void UpdateRot()
{
  switch (rotState)
  {

    case 0:                                         // Idle state, look for direction
      if (!rotBval)
        rotState = 1;                               // CW 1
      if (!rotAval)
        rotState = 11;                              // CCW 1
      break;

    case 1:                                         // CW, wait for A low while B is low
      if (!rotBval)
      {
        if (!rotAval)
        {
          // either increment radixindex or freq
          if (digitalRead(pushPin) == LOW)
          {
            updatedisplay = 1;
            if (radix == 1000000)
              radix = 100000;
            else if (radix == 100000)
              radix = 10000;
            else if (radix == 10000)
              radix = 1000;
            else if (radix == 1000)
              radix = 100;
            else if (radix == 100)
              radix = 10;
            else if (radix == 10)
              radix = 1;
            else
              radix = 1000000;
          }
          else
          {
            freq = (freq + radix);
            if (freq > bandEnd)
              freq = bandEnd;
          }
          rotState = 2;                             // CW 2
        }
      }
      else if (rotAval)
        rotState = 0;                               // It was just a glitch on B, go back to start
      break;

    case 2:                                         // CW, wait for B high
      if (rotBval)
        rotState = 3;                               // CW 3
      break;

    case 3:                                         // CW, wait for A high
      if (rotAval)
        rotState = 0;                               // back to idle (detent) state
      break;

    case 11:                                        // CCW, wait for B low while A is low
      if (!rotAval)
      {
        if (!rotBval)
        {
          // either decrement radixindex or freq
          if (digitalRead(pushPin) == LOW)
          {
            updatedisplay = 1;
            if (radix == 1)
              radix = 10;
            else if (radix == 10)
              radix = 100;
            else if (radix == 100)
              radix = 1000;
            else if (radix == 1000)
              radix = 10000;
            else if (radix == 10000)
              radix = 100000;
            else if (radix == 100000)
              radix = 1000000;
            else
              radix = 1;
          }
          else
          {
            freq = (freq - radix);
            if (freq < bandStart)
              freq = bandStart;
          }
          rotState = 12;                            // CCW 2
        }
      }
      else if (rotBval)
        rotState = 0;                               // It was just a glitch on A, go back to start
      break;

    case 12:                                        // CCW, wait for A high
      if (rotAval)
        rotState = 13;                              // CCW 3
      break;

    case 13:                                        // CCW, wait for B high
      if (rotBval)
        rotState = 0;                               // back to idle (detent) state
      break;
  }
}


void UpdateDisplay()
{
  lcd.setCursor(0, 0);
  lcd.print("        ");
  lcd.setCursor(0, 0);
  lcd.print(freq);
  lcd.setCursor(10, 0);
  lcd.print("ZL2CTM");
  lcd.setCursor(10, 1);
  lcd.print("80m");

  lcd.setCursor(0, 1);
  lcd.print("        ");
  lcd.setCursor(0, 1);
  if (freq > 9999999)
  {
    if (radix == 1)
      lcd.print("       -");
    if (radix == 10)
      lcd.print("      -");
    if (radix == 100)
      lcd.print("     -");
    if (radix == 1000)
      lcd.print("    -");
    if (radix == 10000)
      lcd.print("   -");
    if (radix == 100000)
      lcd.print("  -");
    if (radix == 1000000)
      lcd.print(" -");
  }
  if (freq <= 9999999)
  {
    if (radix == 1)
      lcd.print("      -");
    if (radix == 10)
      lcd.print("     -");
    if (radix == 100)
      lcd.print("    -");
    if (radix == 1000)
      lcd.print("   -");
    if (radix == 10000)
      lcd.print("  -");
    if (radix == 100000)
      lcd.print(" -");
    if (radix == 1000000)
      lcd.print("-");
  }
}


void SendFrequency()
{
  // VFO
  si5351.set_freq(((LSB_IF_freq - freq - 70) * 100ULL), SI5351_PLL_FIXED, SI5351_CLK0);

  // BFO
  si5351.set_freq((LSB_BFO_freq * 100ULL), SI5351_PLL_FIXED, SI5351_CLK2);
}

3 comments:

  1. Hi Charlie
    I tried this Ardunio sketch it come up with errors ,
    lcd.begin();
    // Initialize the DDS
    si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0);
    i am using Ardunio 1.8.5 version
    ??
    email us Charlie

    73's
    Dave

    ReplyDelete
    Replies
    1. lcd.init() instead of lcd.begin()

      si5351.init(SI5351_CRYSTAL_LOAD_8PF,0,0) - note the extra comma and zero

      Delete
    2. Thanks Russel. I'm using the Si5351 from Jason Mildrum. I did note that his library calls are different from some of the others, which makes sense. Looks like we have different LCD libraries too.

      Thanks again.

      Charlie

      Delete