Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <Adafruit_GFX.h>
#include <gfxfont.h>
#include <Adafruit_SPITFT_Macros.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <SimpleRotary.h>
#include "si5351.h"
#include "Wire.h"
#define keypin 9 //checks if the key is down
#define rxamp 6 //controls power to the rx amp
#define txamp 7 //controls power to the tx amp
#define steppin 10
#define OLED_RESET -1
Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);
int keystate = 0; //the current state of the key(on or off)
int lastkeystate = 0;//the previous state of the key
int tx = 0;
bool stepstate;
bool oldstepstate;
unsigned long freq = 700000000; //set the startup frequency to 7.000MHz
unsigned long oldfreq = 0; //will be set equal to freq for tx/rx switching problems
unsigned long freqstep = 1000;
unsigned long oldstep = 1000;
unsigned long currenttime = millis();
unsigned long marktime = 0;
Si5351 si5351;
SimpleRotary rotary(7, 8, 9);
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
void setup() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C));
pinMode (steppin, INPUT_PULLUP);
pinMode (keypin, INPUT_PULLUP);
pinMode (rxamp, OUTPUT);
pinMode (txamp, OUTPUT);
bool i2c_found;
freqstep = 1000;
i2c_found = si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
Serial.begin(9600);
Serial.println(freq / 100);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print(freq / 100);
display.println(" Hz");
display.print("Step: ");
display.println(freqstep / 100);
display.display();
si5351.set_freq(freq, SI5351_CLK0); //set the output to whatever the freq value is set to(by default 7.000MHz
si5351.set_freq(700050000, SI5351_CLK1); //to test the mixer
marktime = millis();
}
void loop() {
byte encoder;
encoder = rotary.rotate();
if(freq != oldfreq) {
Serial.println(freq / 100);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print(freq / 100);
display.println(" Hz");
display.print("Step: ");
display.println(freqstep / 100);
display.display();
}
if( freq >= 730000000) {
freq = 730000000; //stops it from going above 40m
}
if(freq <= 700000000) {
freq = 700000000; //stops it from going below 40m
}
bool keystate = digitalRead(keypin); //sets the value of txkey to either high or low to decide if a the key is down
oldfreq = freq;
if(keystate != lastkeystate && keystate == LOW) { //if the key is down and wasnt before
si5351.set_freq(freq + 80000, SI5351_CLK0);
Serial.println((freq + 80000) / 100);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print((freq / 100) + 800);
display.println(" Hz");
display.print("TX");
display.display();
tx = 1;
pinMode(rxamp, LOW); //switches the rx amp off
delay(3);
pinMode(txamp, HIGH); //switches the tx amp on
marktime = millis();
}
if(keystate != lastkeystate && keystate == HIGH) { //if the key is not down and was before
si5351.set_freq(freq, SI5351_CLK0);
Serial.println((freq / 100), (freqstep / 100));
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print(freq / 100);
display.println(" Hz");
display.print("Step: ");
display.println(freqstep / 100);
display.display();
pinMode(txamp, LOW); //switches the tx amp off
delay(3);
pinMode(rxamp, HIGH); //switches the rx amp on
tx = 0;
marktime = millis();
}
lastkeystate = keystate;
if(tx == 0) {
if(encoder == 1) { //if the encoder turns CW
freq += freqstep;
Serial.println(freq / 100);
marktime = millis();
}
if(encoder == 2) { //if the encoder turns CCW
freq -= freqstep;
Serial.println(freq / 100);
marktime = millis();
}
}
oldstep = freqstep;
bool stepstate = digitalRead(steppin);
if(tx == 0);
if(stepstate != oldstepstate && stepstate == HIGH) { //if the button changes from not being pressed to being pressed
freqstep = oldstep * 10;
if(freqstep > 100000) {
freqstep = 1000;
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print(freq / 100);
display.println(" Hz");
display.print("Step: ");
display.println(freqstep / 100);
display.display();
marktime = millis();
}
if(stepstate != oldstepstate && stepstate == LOW) { //if the button changes from being pressed to not being pressed
freqstep = freqstep;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 1);
display.print(freq / 100);
display.println(" Hz");
display.print("Step: ");
display.println(freqstep / 100);
display.display();
marktime = millis();
}
oldstepstate = stepstate;
currenttime = millis();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment