Vfo Bfo Code
Vfo Bfo Code
#define LCD_RS 9
#define LCD_E 8
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
/**************************************/
/* Interrupt service routine for */
/* encoder frequency change */
/**************************************/
ISR(PCINT2_vect) {
unsigned char result = r.process();
if (result == DIR_CW)
enc_cnt = +1;
else if (result == DIR_CCW)
enc_cnt = -1;
if (setbfo)
set_bfo_freq();
else
set_frequency();
//enc_cnt=0;
}
/**************************************/
/* Change the frequencies */
/**************************************/
void set_frequency()
{
vfo += enc_cnt * radix;
enc_cnt =0;
changed_f = 1;
}
void set_bfo_freq()
{
bfo += enc_cnt * radix;
enc_cnt = 0;
changed_f = 1;
}
/**************************************/
/* Read the button with debouncing */
/**************************************/
boolean get_button()
{
if (!digitalRead(ENCODER_BTN))
{
delay(20);
if (!digitalRead(ENCODER_BTN))
{
long strttime=millis();
while (!digitalRead(ENCODER_BTN));
if((millis() - strttime) > 1000) // check if it was a
long press
{
setbfo = !setbfo; // flip-flop between set and not
set bfo on long press
changed_f = 1;
return 0;
}
else
return 1;
}
}
return 0;
}
/**************************************/
/* Displays the frequency */
/**************************************/
void display_frequency()
{
uint16_t f, g;
lcd.setCursor(3, 0);
f = vfo / 1000000; //variable is now vfo instead of
'frequency'
if (f < 10)
lcd.print(' ');
lcd.print(f);
lcd.print('.');
f = (vfo % 1000000) / 1000;
if (f < 100)
lcd.print('0');
if (f < 10)
lcd.print('0');
lcd.print(f);
lcd.print('.');
f = vfo % 1000;
if (f < 100)
lcd.print('0');
if (f < 10)
lcd.print('0');
lcd.print(f);
lcd.print("Hz");
/**************************************/
/* Displays the frequency change step */
/**************************************/
void display_radix()
{
lcd.setCursor(12, 1);
switch (radix)
{
case 1:
lcd.print(" 1");
break;
case 10:
lcd.print(" 10");
break;
case 100:
lcd.print(" 100");
break;
case 1000:
lcd.print(" 1k");
break;
case 10000:
lcd.print(" 10k");
break;
case 100000:
//lcd.setCursor(10, 1);
lcd.print("100k");
break;
case 1000000:
// lcd.setCursor(9, 1);
lcd.print(" 1M"); //1MHz increments needed when bands
need to be changed
break;
}
//lcd.print("Hz"); // no space on second line so skip
}
void setup()
{
//Serial.begin(19200); // for test
lcd.begin(16, 2); // Initialize and clear the LCD
lcd.clear();
Wire.begin();
si5351.set_pll(SI5351_PLL_FIXED, SI5351_PLLA);
// Set CLK0 to output the starting "vfo" frequency as set
above by vfo = ?
si5351.set_freq(opfreq, 0, SI5351_CLK0); //
SI5351_PLL_FIXED did not give any output so 0
volatile uint32_t vfoT = opfreq;
tbfo = "LSB";
// Now produce BFO
// Use CLK2 output to generate bfo frequency
si5351.set_freq( bfo, 0, SI5351_CLK2); // spb
#ifdef FreqX4
si5351.set_freq((vfo * SI5351_FREQ_MULT) * 4,
SI5351_PLL_FIXED, SI5351_CLK0);
#endif
pinMode(ENCODER_BTN, INPUT_PULLUP);
// In Mega PCINT 18/19 are A10, A11
PCMSK2 |= (1 << PCINT18)| (1 << PCINT19); // Change
interrupts 18 and 19 correspond to A10,A11 on Mega
// on ATMega386
these are D2 and D3
PCICR |= (1 << PCIE2); // Enable pin
change interrupt for the encoder
sei();
display_frequency(); // Update the display
display_radix();
}
void loop()
{
// Update the display if the frequency has been changed
if (changed_f)
{
display_frequency();
#ifdef IF_Offset
if (vfo > 10000000UL)
opfreq= vfo - bfo; // USB
else
opfreq= bfo - vfo; // LSB
// VFO
si5351.set_freq(opfreq, 0, SI5351_CLK0); //
SI5351_PLL_FIXED was not OK on 7MHz
// BFO
si5351.set_freq( bfo , 0, SI5351_CLK2); // spb
//OR you can also add the bfo to suit your needs
#ifdef FreqX4
si5351.set_freq((vfo * SI5351_FREQ_MULT) * 4,
SI5351_PLL_FIXED, SI5351_CLK0);
tbfo = "";
#endif
changed_f = 0;
}