-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvfo_si5351.ino
More file actions
126 lines (108 loc) · 2.63 KB
/
Copy pathvfo_si5351.ino
File metadata and controls
126 lines (108 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Rotary.h>
#include <si5351.h>
const uint8_t encoderPinA = 8;
const uint8_t encoderPinB = 9;
const uint8_t encoderSwitchPin = 7;
const int32_t Si5351Cal = 127850;
const uint16_t minStep = 1;
const uint16_t maxStep = 10000;
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Rotary r = Rotary(encoderPinA, encoderPinB);
Si5351 si5351;
volatile uint32_t freq = 7200000;
uint32_t lastFreq = 0;
uint32_t freqStep = 1000;
void setup() {
Serial.begin(9600);
delay(10000);
setupEncoder();
setupSi5351();
setupDisplay();
}
void loop() {
int switchState = digitalRead(encoderSwitchPin);
if (switchState == LOW) {
nextStep();
delay(300);
}
if (lastFreq != freq) {
lastFreq = freq;
setSi5351();
displayFreq();
}
}
void setupEncoder() {
pinMode(encoderSwitchPin, INPUT_PULLUP);
r.begin();
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0) | (1 << PCINT1);
sei();
}
void setupSi5351() {
bool i2cFound = si5351.init(SI5351_CRYSTAL_LOAD_8PF, 0, 0);
if(i2cFound) {
si5351.output_enable(SI5351_CLK0, 1);
si5351.output_enable(SI5351_CLK1, 0);
si5351.output_enable(SI5351_CLK2, 0);
si5351.set_correction(Si5351Cal, SI5351_PLL_INPUT_XO);
si5351.drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA);
setSi5351();
si5351.update_status();
Serial.print("SYS_INIT: ");
Serial.print(si5351.dev_status.SYS_INIT);
Serial.print(" LOL_A: ");
Serial.print(si5351.dev_status.LOL_A);
Serial.print(" LOL_B: ");
Serial.print(si5351.dev_status.LOL_B);
Serial.print(" LOS: ");
Serial.print(si5351.dev_status.LOS);
Serial.print(" REVID: ");
Serial.println(si5351.dev_status.REVID);
} else {
Serial.println("Device not found on I2C bus!");
}
}
void setSi5351() {
si5351.set_freq(freq * SI5351_FREQ_MULT, SI5351_CLK0);
}
void setupDisplay() {
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("HELLO");
delay(2000);
lcd.clear();
}
ISR(PCINT0_vect) {
Serial.println("INTERUPT");
unsigned char result = r.process();
if (result == DIR_CW) {
setFreq(1);
}
else if (result == DIR_CCW) {
setFreq(-1);
}
}
void setFreq(int dir) {
freq = freq + (dir * freqStep);
}
void displayFreq() {
uint16_t mhz = freq / 1000000;
uint16_t khz = freq % 1000000 / 1000;
uint16_t hz = freq % 1000;
char buffer[11];
snprintf(buffer, sizeof(buffer), "%02d.%03d.%03d", mhz, khz, hz);
lcd.setCursor(0, 0);
lcd.print(buffer);
}
void nextStep() {
freqStep *= 10;
if (freqStep > maxStep) {
freqStep = minStep;
}
if (freqStep < 1000) {
freq -= freq % 1000;
}
}