-
Notifications
You must be signed in to change notification settings - Fork 0
/
dds-vfo.ino
227 lines (179 loc) · 4.82 KB
/
dds-vfo.ino
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* Copyright (c) Florian Thienel/DL3NEY.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
*/
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define PIN_ROT_A 2
#define PIN_ROT_B 3
#define PIN_ROT_BTN 14
#define PIN_DDS_W_CLK 8
#define PIN_DDS_FQ_UD 9
#define PIN_DDS_DATA 10
#define PIN_DDS_RESET 11
#define MIN_HERTZ 14000000
#define MAX_HERTZ 14350000
#define DDS_CLOCK 125000000
#define DDS_ADJUST -200
#define DDS_SHIFT 10700000
#define HERTZ_ADR 0
#define STEP_SIZE_ADR 4
const char DIR[] = {0, 0, -1, 1};
const int32_t STEP_SIZE[] = {10, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 100000, 1000000};
const String STEP_NAME[] = {",01", ",05", ",10", ",25", ",50", "1,0", "2,5", "5,0", " 10", "100", " 1M"};
const int STEPS = sizeof(STEP_SIZE) / sizeof(int32_t);
LiquidCrystal lcd(12, 13, 7, 6, 5, 4);
int32_t hertz = MIN_HERTZ;
int32_t visibleHertz = hertz;
byte stepSizeIndex = 0;
byte visibleStepSizeIndex = stepSizeIndex;
int32_t inputHertz = 0;
void setup() {
pinMode(PIN_ROT_A, INPUT);
pinMode(PIN_ROT_B, INPUT);
pinMode(PIN_ROT_BTN, INPUT);
digitalWrite(PIN_ROT_A, HIGH);
digitalWrite(PIN_ROT_B, HIGH);
digitalWrite(PIN_ROT_BTN, HIGH);
attachInterrupt(0, rotTurned, RISING);
lcd.begin(16, 2);
pinMode(PIN_DDS_FQ_UD, OUTPUT);
pinMode(PIN_DDS_W_CLK, OUTPUT);
pinMode(PIN_DDS_DATA, OUTPUT);
pinMode(PIN_DDS_RESET, OUTPUT);
resetDDS();
Serial.begin(9600);
delay(100);
setFrequency(loadFrequency());
setStepSize(loadStepSize());
sendFrequencyToDDS();
showFrequency();
showStepSize();
}
void loop() {
handleSerialInput();
handleFrequencyChange();
handleStepSizeChange();
}
void handleSerialInput() {
while (Serial.available() > 0) {
char c = (char) Serial.read();
if (c == 'F') {
changeFrequency(STEP_SIZE[stepSizeIndex]);
} else if (c == 'f') {
changeFrequency(-1 * STEP_SIZE[stepSizeIndex]);
} else if (c == 'S') {
changeStepSize(1);
} else if (c == 's') {
changeStepSize(-1);
} else if (c == '?') {
showFrequency();
} else if ((c == '\n' || c == '\r') && inputHertz > 0) {
setFrequency(inputHertz);
inputHertz = 0;
} else if (isDigit(c)) {
inputHertz = inputHertz * 10 + digit(c);
}
}
}
bool isDigit(char c) {
int value = digit(c);
return value >= 0 && value <= 9;
}
int digit(char c) {
return (int)c - '0';
}
void handleFrequencyChange() {
if (visibleHertz != hertz) {
visibleHertz = hertz;
sendFrequencyToDDS();
storeFrequency();
showFrequency();
}
}
void handleStepSizeChange() {
if (visibleStepSizeIndex != stepSizeIndex) {
visibleStepSizeIndex = stepSizeIndex;
storeStepSize();
showStepSize();
}
}
void rotTurned() {
byte turn = digitalRead(PIN_ROT_A) * 2 + digitalRead(PIN_ROT_B);
boolean pressed = !digitalRead(PIN_ROT_BTN);
if (pressed) {
changeStepSize(DIR[turn]);
} else {
changeFrequency(DIR[turn] * STEP_SIZE[stepSizeIndex]);
}
}
void changeFrequency(int32_t delta) {
setFrequency(hertz + delta);
}
void setFrequency(int32_t h) {
hertz = constrain(h, MIN_HERTZ, MAX_HERTZ);
}
int32_t loadFrequency() {
int32_t h;
EEPROM.get(HERTZ_ADR, h);
return h;
}
void storeFrequency() {
EEPROM.put(HERTZ_ADR, visibleHertz);
}
void showFrequency() {
String f = String(visibleHertz, DEC);
String t = f.substring(0, 2) + "." + f.substring(2, 5) + "," + f.substring(5, 7) + "kHz";
Serial.println(visibleHertz, DEC);
lcd.setCursor(0, 0);
lcd.print(t);
}
void sendFrequencyToDDS() {
uint32_t effectiveFrequency = visibleHertz + DDS_SHIFT;
uint32_t tuningWord = (effectiveFrequency * pow(2, 32)) / (DDS_CLOCK + DDS_ADJUST);
pulseHigh(PIN_DDS_W_CLK);
pulseHigh(PIN_DDS_FQ_UD);
for (int i = 0; i < 4; i += 1, tuningWord >>= 8) {
shiftOut(PIN_DDS_DATA, PIN_DDS_W_CLK, LSBFIRST, tuningWord);
}
shiftOut(PIN_DDS_DATA, PIN_DDS_W_CLK, LSBFIRST, 0x00);
digitalWrite(PIN_DDS_FQ_UD, HIGH);
}
void resetDDS() {
digitalWrite(PIN_DDS_W_CLK, LOW);
digitalWrite(PIN_DDS_FQ_UD, LOW);
pulseHigh(PIN_DDS_RESET);
digitalWrite(PIN_DDS_DATA, LOW);
pulseHigh(PIN_DDS_W_CLK);
pulseHigh(PIN_DDS_FQ_UD);
}
void pulseHigh(byte pin) {
digitalWrite(pin, HIGH);
delayMicroseconds(5);
digitalWrite(pin, LOW);
delayMicroseconds(5);
}
void changeStepSize(char delta) {
setStepSize(stepSizeIndex + delta);
}
void setStepSize(char i) {
stepSizeIndex = constrain(i, 0, STEPS - 1);
}
int loadStepSize() {
int s;
EEPROM.get(STEP_SIZE_ADR, s);
return s;
}
void storeStepSize() {
EEPROM.put(STEP_SIZE_ADR, visibleStepSizeIndex);
}
void showStepSize() {
String t = STEP_NAME[visibleStepSizeIndex];
lcd.setCursor(13, 0);
lcd.print(t);
}