Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ###########################
- DDS Controller
- by Angga Sudrahanggara Arly
- www.ngoprekradio.com
- 2015
- ###########################
- */
- // DDS pin & setup
- #define W_CLK 11
- #define FQ_UD 12
- #define DATA 13
- #define RESET 0
- #define pulseHigh(pin) {digitalWrite(pin, HIGH); digitalWrite(pin, LOW); }
- // Inisialisasi rotary encoder
- int encoderPin1 = 2;
- int encoderPin2 = 3;
- volatile int lastEncoded = 0;
- volatile long encoderValue = 0;
- long lastencoderValue = 0;
- int lastMSB = 0;
- int lastLSB = 0;
- // analog button
- int a=0;
- // Inisialisasi LCD 16x2 ( sesuaikan angka pin dibawah )
- // oya tutor nya LCD ini baca aja disini yak -> http://arduino.cc/en/Tutorial/LiquidCrystal
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(10, 9, 8, 7, 6, 5);
- // tombol di rotary encoder
- const int buttonPin1 = 4;
- // variable
- int mode = 1;
- long freqdisplay = 7000000; // ini freq awal mau di brp
- long freqif = 9996660; // ini settingan IF silahkan edit sendiri
- long reffosc = 124998300;
- int band = 3;
- int currentvfo = 1;
- int txrx = 1;
- int signalstrength = 0;
- int polarityrit = 1;
- int currentrit = 1500;
- int buttonState1 = 0;
- int tombolrotary = 0;
- int lasttombolrotary =0;
- int step = 1;
- int naiksetengah = 0;
- int turunsetengah = 0;
- long currentstep = 1;
- int diatas10 = 0;
- int lastdiatas10 = 1;
- int sinyal = 0;
- int ssboffset = 1500;
- int cwoffset = 800;
- //lastfreqdds = freqdds
- long freqdds = 0;
- long lastfreqdds = 1;
- void setup() {
- // tombol di raotary
- pinMode(buttonPin1, INPUT);
- // Setup Serial untuk Logging
- Serial.begin (9600);
- // Setup Rotary Encoder
- pinMode(encoderPin1, INPUT);
- pinMode(encoderPin2, INPUT);
- digitalWrite(encoderPin1, HIGH);
- digitalWrite(encoderPin2, HIGH);
- attachInterrupt(0, updateEncoder, CHANGE);
- attachInterrupt(1, updateEncoder, CHANGE);
- // Setup Tombol analog
- pinMode(A3, INPUT_PULLUP);
- // Setup LCD
- // nah yg dibawah ini sesuaikan pake LCD nya yg berapa karakter.. kan ada 16x2, 16x1, 20x4 dan laen laen..
- // oya ini LCD yg dimaksud LCD dengan chipset Hitachi HD44780 pokok nya yg paling populer deh hehehehey
- lcd.begin(16, 2);
- lcd.setCursor(0,0);
- // Nah yg dibawah ini boleh diganti nama anda atau kolsen, biar terlihat lebih keren
- lcd.print("ngoprekradio.com");
- lcd.setCursor(0,1);
- lcd.print("DDS Controller ");
- // 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
- delay(2000); // 1 detik = 1000 ( satuan nya milli second )
- lcd.clear(); // bersihin LCD
- //freqdisplay = freqdisplay + 7000000;
- lcd.setCursor(4,0);
- lcd.print(freqdisplay);
- // DDS
- 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
- //send 1st freq
- sendFrequency(7000000);
- }
- void loop(){
- // eksekusi baca tegangan tombol analog
- a=readButtons(3);
- // cek tombol di rotary
- button1();
- // refreseh diplay
- diplaymode();
- displayfreq();
- // cek tombol rotary
- if ( tombolrotary == 0 ) {
- if ( lasttombolrotary == 1 ) {
- lcd.clear();
- }
- displayband();
- displayvfo();
- displayrxtx();
- displaysmeter();
- //displayrit();
- smeter();
- }
- else if ( tombolrotary == 1 ) {
- if ( lasttombolrotary == 0 ) {
- lcd.clear();
- displaystep();
- }
- displaystep();
- }
- // ENDING cek tombol rotary
- lasttombolrotary = tombolrotary;
- // detek switch band
- switchband();
- kirimfreq();
- // kasih delay biar keren
- delay(10);
- }
- // ENDING Loop
- // yg dibawah ini utk kirim shift register ke DDS
- 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 = <sys clock> * <frequency tuning word>/2^32
- void sendFrequency(double frequency) {
- int32_t freq = frequency * 4294967295/reffosc; // 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
- }
- void kirimfreq() {
- if ( freqdisplay < freqif ) {
- freqdds = freqif - freqdisplay; // dibawah IF
- }
- else if ( freqdisplay > freqif ) {
- freqdds = freqdisplay - freqif; // diatas IF
- }
- if ( freqdds == lastfreqdds ) {
- // tidak ada action karena ga ada perubahan
- }
- else {
- // nah skrg ada perubahan freq.. kirim freq baru ke chip dds
- if ( mode == 1 ) { sendFrequency( freqdds ); Serial.println(freqdds); }
- else if ( mode == 2 ) { sendFrequency( freqdds + ssboffset ); Serial.println(freqdds + ssboffset); }
- else if ( mode == 3 ) { sendFrequency( freqdds - ssboffset ); Serial.println(freqdds - ssboffset); }
- else if ( mode == 4 ) { sendFrequency( freqdds + cwoffset ); Serial.println(freqdds - cwoffset ); }
- }
- lastfreqdds = freqdds;
- } // END kirimfreq
- void displaystep() {
- //lcd.setCursor(12,0);
- if ( step == 1 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 1hz "); currentstep = 1; }
- else if ( step == 2 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 10hz"); currentstep = 10; }
- else if ( step == 3 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 100hz"); currentstep = 100; }
- else if ( step == 4 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 1khz"); currentstep = 1000; }
- else if ( step == 5 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 10khz"); currentstep = 10000; }
- else if ( step == 6 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print(" - 100khz"); currentstep = 100000; }
- else if ( step == 7 ) { lcd.setCursor(12,0); lcd.print("step"); lcd.setCursor(4,1); lcd.print("- 1mhz"); currentstep = 1000000; }
- }
- void switchstepup() {
- if ( step == 1 ) { step = 2; }
- else if ( step == 2 ) { step = 3; }
- else if ( step == 3 ) { step = 4; }
- else if ( step == 4 ) { step = 5; }
- else if ( step == 5 ) { step = 6; }
- else if ( step == 6 ) { step = 7; }
- else if ( step == 7 ) { step = 1; }
- }
- void switchstepdown() {
- if ( step == 7 ) { step = 6; }
- else if ( step == 6 ) { step = 5; }
- else if ( step == 5 ) { step = 4; }
- else if ( step == 4 ) { step = 3; }
- else if ( step == 3 ) { step = 2; }
- else if ( step == 2 ) { step = 1; }
- else if ( step == 1 ) { step = 7; }
- }
- void button1(){
- buttonState1 = digitalRead(buttonPin1);
- if (buttonState1 == HIGH) {
- tombolrotary = 1;
- }
- else {
- tombolrotary = 0;
- }
- }
- // baca tegangan tombol analog
- /* hasil tes pake resistor 2k2 5 tombol
- tombol 1 = 10 - 26
- tombol 2 = 60 - 80
- tombol 3 = 110 - 130
- tombol 4 = 150 - 180
- tombol 5 = 200 - 220
- kalo mao baca tutor nya klik inih ajah -> http://tronixstuff.com/2011/01/11/tutorial-using-analog-input-for-multiple-buttons/
- */
- int readButtons(int pin)
- {
- int b,c = 0;
- c=analogRead(pin); // baca adc
- if (c>1000)
- {
- // gak ada yg dipencet
- }
- else
- //delay(100);
- if (c>200 && c<220)
- {
- delay(200);
- tombol1();
- }
- else
- if (c>150 && c<180)
- {
- delay(200);
- tombol2();
- }
- else
- if (c>110 && c<130)
- {
- delay(200);
- tombol3();
- }
- else
- if (c>60 && c<80)
- {
- delay(200);
- tombol4();
- }
- else
- if (c<26)
- {
- delay(200);
- tombol5();
- }
- return b;
- //delay(100);
- }
- // ENDing baca tegangan tombol analog
- // Rotary Encoder
- void updateEncoder(){
- int MSB = digitalRead(encoderPin1);
- int LSB = digitalRead(encoderPin2);
- int encoded = (MSB << 1) |LSB;
- int sum = (lastEncoded << 2) | encoded;
- if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
- if ( turunsetengah == 0 ) {
- turunsetengah = 1;
- }
- else {
- if ( turunsetengah == 1 ) {
- rotarydown();
- turunsetengah = 0;
- }
- }
- }
- if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
- if ( naiksetengah == 0 ) {
- naiksetengah = 1;
- }
- else {
- if ( naiksetengah == 1 ) {
- rotaryup();
- naiksetengah = 0;
- }
- }
- }
- lastEncoded = encoded;
- }
- void rotaryup() {
- if ( tombolrotary == 0 ) {
- Serial.println("up");
- freqdisplay = freqdisplay + currentstep;
- }
- else if ( tombolrotary == 1 ) {
- switchstepdown();
- }
- }
- void rotarydown() {
- if ( tombolrotary == 0 ) {
- Serial.println("down");
- freqdisplay = freqdisplay - currentstep;
- }
- else if ( tombolrotary == 1 ) {
- switchstepup();
- }
- }
- // action kalo tombol dipencet
- // Mode AM LSB USB
- void tombol1() {
- Serial.println("tombol 1");
- if ( mode == 1 ) { mode = 2; }
- else if ( mode == 2 ) { mode = 3; }
- else if ( mode == 3 ) { mode = 4; }
- else if ( mode == 4 ) { mode = 1; }
- lastfreqdds = 0; // ini biar kedetek freq rubah dan update kirim lagi yg baru dengan mode baru
- }
- // VFO atau Memori
- void tombol2() {
- Serial.println("tombol 2");
- }
- // RIT
- void tombol3() {
- Serial.println("tombol 3");
- }
- // Display Mode
- void tombol4() {
- Serial.println("tombol 4");
- }
- // gatau untuk apa lagi belom kepikir
- void tombol5() {
- Serial.println("tombol 5");
- }
- // ENDING action kalo tombol dipencet
- // sekarang per Display an
- /*
- kotretan nya kaya gini display nya kalo pake lcd 16x2 :
- LSB__7070000_40m
- Va_RX_s:60_+1500
- */
- void diplaymode() {
- lcd.setCursor(0,0);
- if ( mode == 1 ) { lcd.print("AM "); }
- else if ( mode == 2 ) { lcd.print("LSB"); }
- else if ( mode == 3 ) { lcd.print("USB"); }
- else if ( mode == 4 ) { lcd.print("CW"); }
- }
- void displayfreq() {
- if ( freqdisplay > 0 && freqdisplay < 9999999 ) {
- diatas10 = 0;
- }
- else {
- diatas10 = 1;
- }
- if ( diatas10 == 0 ) {
- if ( lastdiatas10 == 1 ) {
- lcd.clear();
- lcd.setCursor(4,0);
- }
- }
- if ( diatas10 == 1 ) {
- if ( lastdiatas10 == 0 ) {
- lcd.clear();
- lcd.setCursor(3,0);
- }
- }
- if ( diatas10 == 0 ) {
- lcd.setCursor(4,0);
- lcd.print(freqdisplay);
- }
- if ( diatas10 == 1 ) {
- lcd.setCursor(3,0);
- lcd.print(freqdisplay);
- }
- lastdiatas10 = diatas10;
- } // ending displayfreq
- void displayband() {
- lcd.setCursor(12,0);
- if ( band == 1 ) { lcd.print("160m"); }
- if ( band == 2 ) { lcd.print(" 80m"); }
- if ( band == 3 ) { lcd.print(" 40m"); }
- if ( band == 4 ) { lcd.print(" 30m"); }
- if ( band == 5 ) { lcd.print(" 20m"); }
- if ( band == 6 ) { lcd.print(" 17m"); }
- if ( band == 7 ) { lcd.print(" 15m"); }
- if ( band == 8 ) { lcd.print(" 10m"); }
- if ( band == 9 ) { lcd.print("11mc"); }
- if ( band == 10 ) { lcd.print(" CB "); }
- if ( band == 11 ) { lcd.print(" GC "); }
- }
- void displayvfo() {
- lcd.setCursor(0,1);
- if ( currentvfo == 1 ) { lcd.print("Va"); }
- if ( currentvfo == 2 ) { lcd.print("Vb"); }
- }
- void displaymemory() {
- // tar dulu belom pake memory
- }
- void displayrxtx() {
- lcd.setCursor(3,1);
- if ( txrx == 1 ) {lcd.print("RX"); }
- if ( txrx == 2 ) {lcd.print("TX"); }
- }
- void displaysmeter() {
- lcd.setCursor(6,1);
- lcd.print("S:");
- lcd.setCursor(8,1);
- lcd.print(signalstrength);
- }
- void displayrit() {
- lcd.setCursor(11,1);
- if ( polarityrit == 0 ) { lcd.print(" "); }
- else if ( polarityrit == 1 ) { lcd.print("+"); }
- else if ( polarityrit == 1 ) { lcd.print("-"); }
- lcd.setCursor(12,1);
- lcd.print(currentrit);
- }
- void smeter() {
- lcd.setCursor(10,1);
- if ( sinyal == 0 ) { lcd.print("______"); }
- else if ( sinyal > 1 && sinyal < 10 ) { lcd.print("*_____"); }
- else if ( sinyal > 11 && sinyal < 20 ) { lcd.print("**____"); }
- else if ( sinyal > 21 && sinyal < 30 ) { lcd.print("***___"); }
- else if ( sinyal > 31 && sinyal < 40 ) { lcd.print("****__"); }
- else if ( sinyal > 41 && sinyal < 50 ) { lcd.print("*****_"); }
- else if ( sinyal > 51 && sinyal < 100 ) { lcd.print("******"); }
- }
- // ENDING per displayan...
- // skrg Switchband..
- void switchband () {
- if ( freqdisplay > 1799999 && freqdisplay < 2000000 ) { band = 1; } // 160m
- else if ( freqdisplay > 3499999 && freqdisplay < 3900000 ) { band = 2; } // 80m
- else if ( freqdisplay > 6999999 && freqdisplay < 7200000 ) { band = 3; } // 40m
- else if ( freqdisplay > 10999999 && freqdisplay < 10150000 ) { band = 4; } // 30m
- else if ( freqdisplay > 13999999 && freqdisplay < 14500000 ) { band = 5; } // 20m
- else if ( freqdisplay > 17999999 && freqdisplay < 18200000 ) { band = 6; } // 17m
- else if ( freqdisplay > 23999999 && freqdisplay < 25000000 ) { band = 7; } // 15m
- else if ( freqdisplay > 27999999 && freqdisplay < 29000000 ) { band = 8; } // 10m
- else if ( freqdisplay > 11399999 && freqdisplay < 11450000 ) { band = 9; } // 11mc
- else if ( freqdisplay > 26999999 && freqdisplay < 29000000 ) { band = 10; } // CB
- else { band = 11; } // general coverage / diluar band
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement