<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Arduino%3A_DDS_Source_Code</id>
	<title>Arduino: DDS Source Code - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Arduino%3A_DDS_Source_Code"/>
	<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Arduino:_DDS_Source_Code&amp;action=history"/>
	<updated>2026-04-22T10:26:43Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=Arduino:_DDS_Source_Code&amp;diff=46421&amp;oldid=prev</id>
		<title>Onnowpurbo: Created page with &quot; /*  Main code by Richard Visokey AD7C - www.ad7c.com  Revision 2.0 - November 6th, 2013  */    // Include the library code  #include &lt;LiquidCrystal.h&gt;  #include &lt;rotary.h&gt;  #...&quot;</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Arduino:_DDS_Source_Code&amp;diff=46421&amp;oldid=prev"/>
		<updated>2016-10-24T03:03:25Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; &lt;span class=&quot;autocomment&quot;&gt;Main code by Richard Visokey AD7C - www.ad7c.com  Revision 2.0 - November 6th, 2013: &lt;/span&gt;    // Include the library code  #include &amp;lt;LiquidCrystal.h&amp;gt;  #include &amp;lt;rotary.h&amp;gt;  #...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; /*&lt;br /&gt;
 Main code by Richard Visokey AD7C - www.ad7c.com&lt;br /&gt;
 Revision 2.0 - November 6th, 2013&lt;br /&gt;
 */&lt;br /&gt;
 &lt;br /&gt;
 // Include the library code&lt;br /&gt;
 #include &amp;lt;LiquidCrystal.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;rotary.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;EEPROM.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 //Setup some items&lt;br /&gt;
 #define W_CLK 8   // Pin 8 - connect to AD9850 module word load clock pin (CLK)&lt;br /&gt;
 #define FQ_UD 9   // Pin 9 - connect to freq update pin (FQ)&lt;br /&gt;
 #define DATA 10   // Pin 10 - connect to serial data load pin (DATA)&lt;br /&gt;
 #define RESET 11  // Pin 11 - connect to reset pin (RST) &lt;br /&gt;
 #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }&lt;br /&gt;
 Rotary r = Rotary(2,3); // sets the pins the rotary encoder uses.  Must be interrupt pins.&lt;br /&gt;
 LiquidCrystal lcd(12, 13, 7, 6, 5, 4); // I used an odd pin combination because I need pin 2 and 3 for the interrupts.&lt;br /&gt;
 int_fast32_t rx=7076000; // Base (starting) frequency of VFO.  This only loads once.  To force load again see ForceFreq variable below.&lt;br /&gt;
 int_fast32_t rx2=1; // variable to hold the updated frequency&lt;br /&gt;
 int_fast32_t increment = 10; // starting VFO update increment in HZ.&lt;br /&gt;
 int_fast32_t iffreq = 5000000; // Intermedite Frequency - Amount to subtract (-) from base frequency. ********************************************&lt;br /&gt;
 int buttonstate = 0;&lt;br /&gt;
 int buttonstate2 = 0;&lt;br /&gt;
 int GoIF = 1;&lt;br /&gt;
 String hertz = &amp;quot;10 Hz&amp;quot;;&lt;br /&gt;
 int  hertzPosition = 5;&lt;br /&gt;
 byte ones,tens,hundreds,thousands,tenthousands,hundredthousands,millions ;  //Placeholders&lt;br /&gt;
 String freq; // string to hold the frequency&lt;br /&gt;
 int_fast32_t timepassed = millis(); // int to hold the arduino miilis since startup&lt;br /&gt;
 int memstatus = 1;  // value to notify if memory is current or old. 0=old, 1=current.  &lt;br /&gt;
&lt;br /&gt;
 int ForceFreq = 1;  // Change this to 0 after you upload and run a working sketch to activate the EEPROM memory.  YOU MUST PUT THIS BACK TO 0 AND UPLOAD  THE SKETCH AGAIN AFTER STARTING FREQUENCY IS SET!&lt;br /&gt;
&lt;br /&gt;
 void setup() {&lt;br /&gt;
   pinMode(A0,INPUT); // Connect to a button that goes to GND on push&lt;br /&gt;
   pinMode(A5,INPUT); // IF sense **********************************************&lt;br /&gt;
   digitalWrite(A0,HIGH);&lt;br /&gt;
   digitalWrite(A5,HIGH);&lt;br /&gt;
   lcd.begin(16, 2);&lt;br /&gt;
   PCICR |= (1 &amp;lt;&amp;lt; PCIE2);&lt;br /&gt;
   PCMSK2 |= (1 &amp;lt;&amp;lt; PCINT18) | (1 &amp;lt;&amp;lt; PCINT19);&lt;br /&gt;
   sei();&lt;br /&gt;
   pinMode(FQ_UD, OUTPUT);&lt;br /&gt;
   pinMode(W_CLK, OUTPUT);&lt;br /&gt;
   pinMode(DATA, OUTPUT);&lt;br /&gt;
   pinMode(RESET, OUTPUT); &lt;br /&gt;
   pulseHigh(RESET);&lt;br /&gt;
   pulseHigh(W_CLK);&lt;br /&gt;
   pulseHigh(FQ_UD);  // this pulse enables serial mode on the AD9850 - Datasheet page 12.&lt;br /&gt;
   lcd.setCursor(hertzPosition,1);    &lt;br /&gt;
   lcd.print(hertz); &lt;br /&gt;
  &lt;br /&gt;
   // Load the stored frequency  &lt;br /&gt;
   if (ForceFreq == 0) {&lt;br /&gt;
     freq = String(EEPROM.read(0))+String(EEPROM.read(1))+String(EEPROM.read(2))+String(EEPROM.read(3))+String(EEPROM.read(4))+String(EEPROM.read(5))+String(EEPROM.read(6));&lt;br /&gt;
     rx = freq.toInt();  &lt;br /&gt;
   }&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 void loop() {    &lt;br /&gt;
   // Update the display and frequency if the new Freq NEQ the old Freq  &lt;br /&gt;
   if (rx != rx2){   &lt;br /&gt;
         showFreq();&lt;br /&gt;
         sendFrequency(rx);&lt;br /&gt;
         rx2 = rx;        &lt;br /&gt;
       } &lt;br /&gt;
         &lt;br /&gt;
   // Rotate through the rate of tuning as you hold down the button&lt;br /&gt;
   buttonstate = digitalRead(A0);&lt;br /&gt;
   if(buttonstate == LOW) {&lt;br /&gt;
         setincrement();        &lt;br /&gt;
     };&lt;br /&gt;
     &lt;br /&gt;
   // Check for PIN low to drive IF offset Freq&lt;br /&gt;
   buttonstate = digitalRead(A5);  &lt;br /&gt;
     if (buttonstate != buttonstate2){&lt;br /&gt;
         if(buttonstate == LOW) {       &lt;br /&gt;
               lcd.setCursor(15,1);&lt;br /&gt;
               lcd.print(&amp;quot;.&amp;quot;); &lt;br /&gt;
               GoIF = 0; &lt;br /&gt;
               buttonstate2 = buttonstate; &lt;br /&gt;
               sendFrequency(rx);         &lt;br /&gt;
           }&lt;br /&gt;
         else{&lt;br /&gt;
             lcd.setCursor(15,1);&lt;br /&gt;
             lcd.print(&amp;quot; &amp;quot;);&lt;br /&gt;
             GoIF = 1;&lt;br /&gt;
             buttonstate2 = buttonstate;&lt;br /&gt;
             sendFrequency(rx);       &lt;br /&gt;
           };&lt;br /&gt;
     };&lt;br /&gt;
     &lt;br /&gt;
     // Write the frequency to memory if not stored and 2 seconds have passed since the last frequency change.&lt;br /&gt;
     if(memstatus == 0){   &lt;br /&gt;
       if(timepassed+2000 &amp;lt; millis()){&lt;br /&gt;
         storeMEM();&lt;br /&gt;
         }&lt;br /&gt;
       }  &lt;br /&gt;
     &lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  &lt;br /&gt;
 // Interrupt routine to catch the rotary encoder&lt;br /&gt;
 ISR(PCINT2_vect) {&lt;br /&gt;
   unsigned char result = r.process();&lt;br /&gt;
   if (result) {    &lt;br /&gt;
     if (result == DIR_CW){rx=rx+increment;}&lt;br /&gt;
     else {rx=rx-increment;};       &lt;br /&gt;
       if (rx &amp;gt;=30000000){rx=rx2;}; // UPPER VFO LIMIT&lt;br /&gt;
       if (rx &amp;lt;=1000000){rx=rx2;}; // LOWER VFO LIMIT&lt;br /&gt;
   } &lt;br /&gt;
 }  &lt;br /&gt;
 &lt;br /&gt;
 // frequency calc from datasheet page 8 = &amp;lt;sys clock&amp;gt; * &amp;lt;frequency tuning word&amp;gt;/2^32&lt;br /&gt;
 void sendFrequency(double frequency) {  &lt;br /&gt;
    if (GoIF == 1){frequency=frequency-iffreq;}; //If pin = low, subtract the IF frequency.&lt;br /&gt;
   int32_t freq = frequency * 4294967295/125000000;  // note 125 MHz clock on 9850.  You can make &amp;#039;slight&amp;#039; tuning variations here by adjusting the clock frequency.&lt;br /&gt;
   for (int b=0; b&amp;lt;4; b++, freq&amp;gt;&amp;gt;=8) {&lt;br /&gt;
     tfr_byte(freq &amp;amp; 0xFF);&lt;br /&gt;
   }&lt;br /&gt;
   tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip&lt;br /&gt;
   pulseHigh(FQ_UD);  // Done!  Should see output&lt;br /&gt;
 }&lt;br /&gt;
 // transfers a byte, a bit at a time, LSB first to the 9850 via serial DATA line&lt;br /&gt;
 void tfr_byte(byte data)&lt;br /&gt;
 {&lt;br /&gt;
   for (int i=0; i&amp;lt;8; i++, data&amp;gt;&amp;gt;=1) {&lt;br /&gt;
     digitalWrite(DATA, data &amp;amp; 0x01);&lt;br /&gt;
     pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high&lt;br /&gt;
   }&lt;br /&gt;
 } &lt;br /&gt;
 &lt;br /&gt;
 void setincrement(){&lt;br /&gt;
   if(increment == 10){increment = 50; hertz = &amp;quot;50 Hz&amp;quot;; hertzPosition=5;}&lt;br /&gt;
   else if (increment == 50){increment = 100;  hertz = &amp;quot;100 Hz&amp;quot;; hertzPosition=4;}&lt;br /&gt;
   else if (increment == 100){increment = 500; hertz=&amp;quot;500 Hz&amp;quot;; hertzPosition=4;}&lt;br /&gt;
   else if (increment == 500){increment = 1000; hertz=&amp;quot;1 Khz&amp;quot;; hertzPosition=6;}&lt;br /&gt;
   else if (increment == 1000){increment = 2500; hertz=&amp;quot;2.5 Khz&amp;quot;; hertzPosition=4;}&lt;br /&gt;
   else if (increment == 2500){increment = 5000; hertz=&amp;quot;5 Khz&amp;quot;; hertzPosition=6;}&lt;br /&gt;
   else if (increment == 5000){increment = 10000; hertz=&amp;quot;10 Khz&amp;quot;; hertzPosition=5;}&lt;br /&gt;
   else if (increment == 10000){increment = 100000; hertz=&amp;quot;100 Khz&amp;quot;; hertzPosition=4;}&lt;br /&gt;
   else if (increment == 100000){increment = 1000000; hertz=&amp;quot;1 Mhz&amp;quot;; hertzPosition=6;}  &lt;br /&gt;
   else{increment = 10; hertz = &amp;quot;10 Hz&amp;quot;; hertzPosition=5;};  &lt;br /&gt;
    lcd.setCursor(0,1);&lt;br /&gt;
    lcd.print(&amp;quot;                &amp;quot;);&lt;br /&gt;
    lcd.setCursor(hertzPosition,1); &lt;br /&gt;
    lcd.print(hertz); &lt;br /&gt;
    delay(250); // Adjust this delay to speed up/slow down the button menu scroll speed.&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 void showFreq(){&lt;br /&gt;
     millions = int(rx/1000000);&lt;br /&gt;
     hundredthousands = ((rx/100000)%10);&lt;br /&gt;
     tenthousands = ((rx/10000)%10);&lt;br /&gt;
     thousands = ((rx/1000)%10);&lt;br /&gt;
     hundreds = ((rx/100)%10);&lt;br /&gt;
     tens = ((rx/10)%10);&lt;br /&gt;
     ones = ((rx/1)%10);&lt;br /&gt;
     lcd.setCursor(0,0);&lt;br /&gt;
     lcd.print(&amp;quot;                &amp;quot;);&lt;br /&gt;
    if (millions &amp;gt; 9){lcd.setCursor(1,0);}&lt;br /&gt;
    else{lcd.setCursor(2,0);}&lt;br /&gt;
     lcd.print(millions);&lt;br /&gt;
     lcd.print(&amp;quot;.&amp;quot;);&lt;br /&gt;
     lcd.print(hundredthousands);&lt;br /&gt;
     lcd.print(tenthousands);&lt;br /&gt;
     lcd.print(thousands);&lt;br /&gt;
     lcd.print(&amp;quot;.&amp;quot;);&lt;br /&gt;
     lcd.print(hundreds);&lt;br /&gt;
     lcd.print(tens);&lt;br /&gt;
     lcd.print(ones);&lt;br /&gt;
     lcd.print(&amp;quot; MHz  &amp;quot;);&lt;br /&gt;
     timepassed = millis();&lt;br /&gt;
     memstatus = 0; // Trigger memory write&lt;br /&gt;
 }; &lt;br /&gt;
 &lt;br /&gt;
 void storeMEM(){&lt;br /&gt;
   //Write each frequency section to a EPROM slot.  Yes, it&amp;#039;s cheating but it works!&lt;br /&gt;
    EEPROM.write(0,millions);&lt;br /&gt;
    EEPROM.write(1,hundredthousands);&lt;br /&gt;
    EEPROM.write(2,tenthousands);&lt;br /&gt;
    EEPROM.write(3,thousands);&lt;br /&gt;
    EEPROM.write(4,hundreds);       &lt;br /&gt;
    EEPROM.write(5,tens);&lt;br /&gt;
    EEPROM.write(6,ones);   &lt;br /&gt;
    memstatus = 1;  // Let program know memory has been written&lt;br /&gt;
 };&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>