Advertisement
angga_funkadelic

OpenSource DDS Controller

Jan 3rd, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.40 KB | None | 0 0
  1. /*
  2. ###########################
  3. DDS Controller
  4. by Angga Sudrahanggara Arly
  5. www.ngoprekradio.com
  6. 2015
  7. ###########################
  8. */
  9.  
  10. // DDS pin & setup
  11.  #define W_CLK 11      
  12.  #define FQ_UD 12      
  13.  #define DATA 13      
  14.  #define RESET 0
  15.  #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
  16.  
  17.  
  18. // Inisialisasi  rotary encoder
  19. int encoderPin1 = 2;
  20. int encoderPin2 = 3;
  21. volatile int lastEncoded = 0;
  22. volatile long encoderValue = 0;
  23. long lastencoderValue = 0;
  24. int lastMSB = 0;
  25. int lastLSB = 0;
  26. // analog button
  27. int a=0;
  28. // Inisialisasi LCD 16x2 ( sesuaikan angka pin dibawah )
  29. // oya tutor nya LCD ini baca aja disini yak -> http://arduino.cc/en/Tutorial/LiquidCrystal
  30. #include <LiquidCrystal.h>
  31. LiquidCrystal lcd(10, 9, 8, 7, 6, 5);
  32. // tombol di rotary encoder
  33. const int buttonPin1 = 4;
  34. // variable
  35. int mode = 1;
  36. long freqdisplay = 7000000; // ini freq awal mau di brp
  37. long freqif = 9996660; // ini settingan IF silahkan edit sendiri
  38. long reffosc = 124998300;
  39. int band = 3;
  40. int currentvfo = 1;
  41. int txrx = 1;
  42. int signalstrength = 0;
  43. int polarityrit = 1;
  44. int currentrit = 1500;
  45. int buttonState1 = 0;
  46. int tombolrotary = 0;
  47. int lasttombolrotary =0;
  48. int step = 1;
  49. int naiksetengah = 0;
  50. int turunsetengah = 0;
  51. long currentstep = 1;
  52. int diatas10 = 0;
  53. int lastdiatas10 = 1;
  54. int sinyal = 0;
  55. int ssboffset = 1500;
  56. int cwoffset = 800;
  57. //lastfreqdds = freqdds
  58. long freqdds = 0;
  59. long lastfreqdds = 1;
  60.  
  61. void setup() {
  62.   // tombol di raotary
  63.   pinMode(buttonPin1, INPUT);
  64.   // Setup Serial untuk Logging
  65.   Serial.begin (9600);
  66.   // Setup Rotary Encoder
  67.   pinMode(encoderPin1, INPUT);
  68.   pinMode(encoderPin2, INPUT);
  69.   digitalWrite(encoderPin1, HIGH);
  70.   digitalWrite(encoderPin2, HIGH);
  71.   attachInterrupt(0, updateEncoder, CHANGE);
  72.   attachInterrupt(1, updateEncoder, CHANGE);
  73.   // Setup Tombol analog
  74.   pinMode(A3, INPUT_PULLUP);
  75.   // Setup LCD
  76.   // nah yg dibawah ini sesuaikan pake LCD nya yg berapa karakter.. kan ada 16x2, 16x1, 20x4 dan laen laen..
  77.   // oya ini LCD yg dimaksud LCD dengan chipset Hitachi HD44780 pokok nya yg paling populer deh hehehehey
  78.   lcd.begin(16, 2);
  79.   lcd.setCursor(0,0);
  80.   // Nah yg dibawah ini boleh diganti nama anda atau kolsen, biar terlihat lebih keren
  81.   lcd.print("ngoprekradio.com");
  82.   lcd.setCursor(0,1);
  83.   lcd.print("DDS Controller ");
  84.   // yg ini delay pas tampilin nama diatas...  set aja mao brp detik dah.. pura pura loading ceritanya... jangan di set 1 jam ya... cape deh tar nunggu lama
  85.   delay(2000); // 1 detik = 1000 ( satuan nya milli second )
  86.   lcd.clear(); // bersihin LCD
  87.   //freqdisplay = freqdisplay + 7000000;
  88.   lcd.setCursor(4,0);
  89.   lcd.print(freqdisplay);
  90.   // DDS
  91.   pinMode(FQ_UD, OUTPUT);
  92.   pinMode(W_CLK, OUTPUT);
  93.   pinMode(DATA, OUTPUT);
  94.   pinMode(RESET, OUTPUT);
  95.   pulseHigh(RESET);
  96.   pulseHigh(W_CLK);
  97.   pulseHigh(FQ_UD);  // this pulse enables serial mode - Datasheet page 12 figure 10
  98.   //send 1st freq
  99.   sendFrequency(7000000);
  100. }
  101.  
  102. void loop(){
  103.   // eksekusi baca tegangan tombol analog
  104.   a=readButtons(3);
  105.  
  106.   // cek tombol di rotary
  107.   button1();
  108.   // refreseh diplay
  109.   diplaymode();
  110.   displayfreq();
  111.  
  112.   // cek tombol rotary
  113.   if ( tombolrotary == 0 ) {
  114.     if ( lasttombolrotary == 1 ) {
  115.       lcd.clear();    
  116.      
  117.     }
  118.     displayband();
  119.       displayvfo();
  120.       displayrxtx();
  121.       displaysmeter();
  122.       //displayrit();
  123.       smeter();
  124.   }
  125.  
  126.   else if ( tombolrotary == 1 ) {
  127.     if ( lasttombolrotary == 0 ) {
  128.       lcd.clear();
  129.       displaystep();
  130.     }
  131.     displaystep();
  132.    
  133.   }
  134.  
  135.   // ENDING cek tombol rotary
  136.  
  137.   lasttombolrotary = tombolrotary;
  138.  
  139.   // detek switch band
  140.   switchband();
  141.   kirimfreq();
  142.   // kasih delay biar keren
  143.   delay(10);
  144.  
  145. }
  146. // ENDING Loop
  147.  
  148.  
  149. // yg dibawah ini utk kirim shift register ke DDS
  150.  void tfr_byte(byte data)
  151. {
  152.   for (int i=0; i<8; i++, data>>=1) {
  153.     digitalWrite(DATA, data & 0x01);
  154.     pulseHigh(W_CLK);   //after each bit sent, CLK is pulsed high
  155.   }
  156. }
  157.  
  158.  // frequency calc from datasheet page 8 = <sys clock> * <frequency tuning word>/2^32
  159. void sendFrequency(double frequency) {
  160.   int32_t freq = frequency * 4294967295/reffosc;  // note 125 MHz clock on 9850
  161.   for (int b=0; b<4; b++, freq>>=8) {
  162.     tfr_byte(freq & 0xFF);
  163.   }
  164.   tfr_byte(0x000);   // Final control byte, all 0 for 9850 chip
  165.   pulseHigh(FQ_UD);  // Done!  Should see output
  166. }
  167.  
  168.  
  169.  
  170. void kirimfreq() {
  171.   if ( freqdisplay < freqif ) {
  172.     freqdds = freqif - freqdisplay; // dibawah IF
  173.   }
  174.  else if ( freqdisplay > freqif ) {
  175.     freqdds = freqdisplay - freqif; // diatas IF
  176.  }
  177.  
  178. if ( freqdds == lastfreqdds ) {
  179.   // tidak ada action karena ga ada perubahan
  180. }
  181. else {
  182.  // nah skrg ada perubahan freq.. kirim freq baru ke chip dds  
  183.  if      ( mode ==  1 )  { sendFrequency( freqdds ); Serial.println(freqdds); }
  184.  else if ( mode ==  2 )  { sendFrequency( freqdds + ssboffset ); Serial.println(freqdds + ssboffset); }
  185.  else if ( mode ==  3 )  { sendFrequency( freqdds - ssboffset ); Serial.println(freqdds - ssboffset); }
  186.  else if ( mode ==  4 )  { sendFrequency( freqdds + cwoffset  ); Serial.println(freqdds - cwoffset ); }
  187. }
  188.  
  189. lastfreqdds = freqdds;
  190. } // END kirimfreq
  191.  
  192.  
  193.  
  194. void displaystep() {
  195.   //lcd.setCursor(12,0);
  196.   if      ( step == 1 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("      - 1hz "); currentstep = 1; }
  197.   else if ( step == 2 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("     -  10hz"); currentstep = 10; }
  198.   else if ( step == 3 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("    -  100hz"); currentstep = 100; }
  199.   else if ( step == 4 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("   -    1khz"); currentstep = 1000; }
  200.   else if ( step == 5 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("  -    10khz"); currentstep = 10000; }
  201.   else if ( step == 6 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" -    100khz"); currentstep = 100000; }
  202.   else if ( step == 7 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("-       1mhz"); currentstep = 1000000; }
  203. }
  204.  
  205.  
  206. void switchstepup() {
  207.   if      ( step == 1 ) { step = 2; }
  208.   else if ( step == 2 ) { step = 3; }
  209.   else if ( step == 3 ) { step = 4; }
  210.   else if ( step == 4 ) { step = 5; }
  211.   else if ( step == 5 ) { step = 6; }
  212.   else if ( step == 6 ) { step = 7; }
  213.   else if ( step == 7 ) { step = 1; }
  214. }
  215.  
  216. void switchstepdown() {
  217.   if      ( step == 7 ) { step = 6; }
  218.   else if ( step == 6 ) { step = 5; }
  219.   else if ( step == 5 ) { step = 4; }
  220.   else if ( step == 4 ) { step = 3; }
  221.   else if ( step == 3 ) { step = 2; }
  222.   else if ( step == 2 ) { step = 1; }
  223.   else if ( step == 1 ) { step = 7; }
  224. }
  225.  
  226.  
  227.  
  228. void button1(){
  229.   buttonState1 = digitalRead(buttonPin1);
  230.   if (buttonState1 == HIGH) {
  231.   tombolrotary = 1;    
  232.   }
  233.   else {
  234.   tombolrotary = 0;  
  235.   }
  236. }
  237.  
  238.  
  239.  
  240.  
  241. // baca tegangan tombol analog
  242. /* hasil tes pake resistor 2k2 5 tombol
  243. tombol 1 = 10 - 26  
  244. tombol 2 = 60 - 80
  245. tombol 3 = 110 - 130
  246. tombol 4 = 150 - 180
  247. tombol 5 = 200 - 220
  248. kalo mao baca tutor nya klik inih ajah -> http://tronixstuff.com/2011/01/11/tutorial-using-analog-input-for-multiple-buttons/
  249. */
  250.  
  251. int readButtons(int pin)
  252. {
  253.   int b,c = 0;
  254.   c=analogRead(pin); // baca adc  
  255.   if (c>1000)
  256.   {
  257.     // gak ada yg dipencet
  258.   }  
  259. else
  260.   //delay(100);
  261.   if (c>200 && c<220)
  262.   {
  263.     delay(200);
  264.     tombol1();
  265.   }    
  266.   else
  267.     if (c>150 && c<180)
  268.     {
  269.       delay(200);
  270.       tombol2();
  271.     }      
  272.     else
  273.       if (c>110 && c<130)
  274.       {
  275.         delay(200);
  276.         tombol3();
  277.       }        
  278.       else
  279.         if (c>60 && c<80)
  280.         {
  281.           delay(200);
  282.           tombol4();
  283.         }          
  284.         else
  285.           if (c<26)
  286.           {
  287.             delay(200);
  288.             tombol5();
  289.           }
  290. return b;
  291. //delay(100);
  292. }
  293. // ENDing baca tegangan tombol analog
  294.  
  295.  
  296.  
  297.  
  298.  
  299. // Rotary Encoder
  300. void updateEncoder(){
  301.   int MSB = digitalRead(encoderPin1);
  302.   int LSB = digitalRead(encoderPin2);
  303.   int encoded = (MSB << 1) |LSB;
  304.   int sum  = (lastEncoded << 2) | encoded;
  305.   if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
  306.     if ( turunsetengah == 0 ) {
  307.       turunsetengah = 1;
  308.     }
  309.     else {
  310.       if ( turunsetengah == 1 ) {
  311.         rotarydown();
  312.         turunsetengah = 0;
  313.       }
  314.     }
  315.    
  316.    }
  317.  
  318.   if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
  319.     if ( naiksetengah == 0 ) {
  320.       naiksetengah = 1;
  321.     }
  322.     else {
  323.       if ( naiksetengah == 1 ) {
  324.         rotaryup();
  325.         naiksetengah = 0;
  326.       }
  327.     }
  328.    }
  329.   lastEncoded = encoded;
  330. }
  331.  
  332. void rotaryup() {
  333.   if ( tombolrotary == 0 ) {
  334.   Serial.println("up");
  335.   freqdisplay = freqdisplay + currentstep;
  336.   }
  337.   else if ( tombolrotary == 1 ) {
  338.     switchstepdown();
  339.   }
  340. }
  341.  
  342. void rotarydown() {
  343.   if ( tombolrotary == 0 ) {
  344.   Serial.println("down");
  345.   freqdisplay = freqdisplay - currentstep;
  346.   }
  347.   else if ( tombolrotary == 1  ) {
  348.     switchstepup();
  349.   }
  350. }
  351.  
  352.  
  353. // action kalo tombol dipencet
  354.  
  355.  
  356. // Mode AM LSB USB
  357. void tombol1() {
  358.  Serial.println("tombol 1");
  359.  if      ( mode == 1 ) { mode = 2; }
  360.  else if ( mode == 2 ) { mode = 3; }
  361.  else if ( mode == 3 ) { mode = 4; }
  362.  else if ( mode == 4 ) { mode = 1; }
  363.  lastfreqdds = 0; // ini biar kedetek freq rubah dan update kirim lagi yg baru dengan mode baru
  364. }
  365.  
  366. //  VFO atau Memori
  367. void tombol2() {
  368.   Serial.println("tombol 2");
  369. }
  370.  
  371. // RIT
  372. void tombol3() {
  373.   Serial.println("tombol 3");
  374. }
  375.  
  376. // Display Mode
  377. void tombol4() {
  378.  Serial.println("tombol 4");
  379. }
  380.  
  381. // gatau untuk apa lagi belom kepikir
  382. void tombol5() {
  383.  Serial.println("tombol 5");
  384. }
  385.  
  386. // ENDING action kalo tombol dipencet
  387.  
  388.  
  389.  
  390. // sekarang per Display an
  391. /*
  392. kotretan nya kaya gini display nya kalo pake lcd 16x2 :
  393. LSB__7070000_40m
  394. Va_RX_s:60_+1500
  395. */
  396.  
  397. void diplaymode() {
  398.   lcd.setCursor(0,0);
  399.   if      ( mode == 1 ) { lcd.print("AM "); }
  400.   else if ( mode == 2 ) { lcd.print("LSB"); }
  401.   else if ( mode == 3 ) { lcd.print("USB"); }
  402.   else if ( mode == 4 ) { lcd.print("CW"); }
  403. }
  404.  
  405. void displayfreq() {
  406.   if ( freqdisplay > 0 && freqdisplay < 9999999 )   {
  407.     diatas10 = 0;
  408.   }
  409.   else {
  410.     diatas10 = 1;
  411.   }
  412.  
  413.  
  414.   if ( diatas10 == 0 ) {
  415.     if ( lastdiatas10 == 1 ) {
  416.       lcd.clear();
  417.       lcd.setCursor(4,0);
  418.     }
  419.   }
  420.  
  421.   if ( diatas10 == 1 ) {
  422.     if ( lastdiatas10 == 0 ) {
  423.       lcd.clear();
  424.       lcd.setCursor(3,0);
  425.     }
  426.   }
  427.  
  428.   if ( diatas10 == 0 ) {
  429.     lcd.setCursor(4,0);
  430.     lcd.print(freqdisplay);
  431.   }
  432.  
  433.   if ( diatas10 == 1 ) {
  434.     lcd.setCursor(3,0);
  435.     lcd.print(freqdisplay);
  436.   }
  437.  
  438. lastdiatas10 = diatas10;
  439.  
  440. }  // ending displayfreq
  441.  
  442.  
  443.  
  444.  
  445. void displayband() {
  446.   lcd.setCursor(12,0);
  447.   if ( band == 1 ) { lcd.print("160m"); }
  448.   if ( band == 2 ) { lcd.print(" 80m"); }
  449.   if ( band == 3 ) { lcd.print(" 40m"); }
  450.   if ( band == 4 ) { lcd.print(" 30m"); }
  451.   if ( band == 5 ) { lcd.print(" 20m"); }
  452.   if ( band == 6 ) { lcd.print(" 17m"); }
  453.   if ( band == 7 ) { lcd.print(" 15m"); }
  454.   if ( band == 8 ) { lcd.print(" 10m"); }
  455.   if ( band == 9 ) { lcd.print("11mc"); }
  456.   if ( band == 10 ) { lcd.print(" CB "); }
  457.   if ( band == 11 ) { lcd.print(" GC "); }
  458. }
  459.  
  460.  
  461. void displayvfo() {
  462.   lcd.setCursor(0,1);
  463.   if ( currentvfo == 1 ) { lcd.print("Va"); }
  464.   if ( currentvfo == 2 ) { lcd.print("Vb"); }
  465. }
  466.  
  467. void displaymemory() {
  468.   // tar dulu belom pake memory
  469. }
  470.  
  471. void displayrxtx() {
  472.   lcd.setCursor(3,1);
  473.   if ( txrx == 1 ) {lcd.print("RX"); }
  474.   if ( txrx == 2 ) {lcd.print("TX"); }
  475. }
  476.  
  477. void displaysmeter() {
  478.   lcd.setCursor(6,1);
  479.   lcd.print("S:");
  480.   lcd.setCursor(8,1);
  481.   lcd.print(signalstrength);
  482.   }
  483.  
  484. void displayrit() {
  485.   lcd.setCursor(11,1);
  486.   if      ( polarityrit == 0 ) { lcd.print(" "); }
  487.   else if ( polarityrit == 1 ) { lcd.print("+"); }
  488.   else if ( polarityrit == 1 ) { lcd.print("-"); }
  489.  
  490.   lcd.setCursor(12,1);
  491.   lcd.print(currentrit);
  492.   }
  493.  
  494. void smeter() {
  495.   lcd.setCursor(10,1);
  496.   if      ( sinyal == 0 )               {   lcd.print("______"); }
  497.   else if ( sinyal > 1 && sinyal < 10 )  {  lcd.print("*_____"); }
  498.   else if ( sinyal > 11 && sinyal < 20 ) {  lcd.print("**____"); }
  499.   else if ( sinyal > 21 && sinyal < 30 ) {  lcd.print("***___"); }
  500.   else if ( sinyal > 31 && sinyal < 40 ) {  lcd.print("****__"); }
  501.   else if ( sinyal > 41 && sinyal < 50 ) {  lcd.print("*****_"); }
  502.   else if ( sinyal > 51 && sinyal < 100 ) { lcd.print("******"); }
  503. }  
  504.  
  505. // ENDING per displayan...
  506.  
  507. // skrg Switchband..
  508. void switchband () {
  509.   if      ( freqdisplay > 1799999 && freqdisplay < 2000000 )   { band = 1; } // 160m
  510.   else if ( freqdisplay > 3499999 && freqdisplay < 3900000 )   { band = 2; } // 80m
  511.   else if ( freqdisplay > 6999999 && freqdisplay < 7200000 )   { band = 3; } // 40m
  512.   else if ( freqdisplay > 10999999 && freqdisplay < 10150000 ) { band = 4; } // 30m
  513.   else if ( freqdisplay > 13999999 && freqdisplay < 14500000 ) { band = 5; } // 20m
  514.   else if ( freqdisplay > 17999999 && freqdisplay < 18200000 ) { band = 6; } // 17m
  515.   else if ( freqdisplay > 23999999 && freqdisplay < 25000000 ) { band = 7; } // 15m
  516.   else if ( freqdisplay > 27999999 && freqdisplay < 29000000 ) { band = 8; } // 10m
  517.   else if ( freqdisplay > 11399999 && freqdisplay < 11450000 ) { band = 9; } // 11mc
  518.   else if ( freqdisplay > 26999999 && freqdisplay < 29000000 ) { band = 10; } // CB
  519.   else    { band = 11; } // general coverage / diluar band
  520. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement