-
Notifications
You must be signed in to change notification settings - Fork 0
/
instrument.ino
364 lines (294 loc) · 10.1 KB
/
instrument.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/***************************************************
* Multi-instrumento
*
* Author: Pavel Milanes Costa
* Email: pavelmc@gmail.com
****************************************************/
/********** DEBUG MODE *************************/
// uncomment this to get a few hits via serial
//~ #define DEBUG true
#include "SPI.h"
#include <EEPROM.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9340.h"
// These are the pins used for SPI comms in the UNO
#define _sclk 13
#define _miso 12
#define _mosi 11
#define _dc 10
#define _rst 9
#define _cs 8
// Use hardware SPI
Adafruit_ILI9340 tft = Adafruit_ILI9340(_cs, _dc, _rst);
// TFT dimensions
#define TFT_WIDTH 320
#define TFT_HEIGHT 240
// some needed for scale
#define TFTH_13 TFT_HEIGHT / 3
#define TFTH_12 TFT_HEIGHT / 2
#define TFTH_23 TFTH_13 * 2
/*******************************************************************************
* LCD coordinate model
*
* Y , X
*
* 0,0 **************************************************** 320,0
* * y y y y y y y y y y *
* * *
* * x *
* * x *
* * x *
* * x *
* * x *
* * x *
* * x *
* * x *
* * x *
* * *
* 0,240 ************************************************ 324,240
*
* - Coordinate system is always (y, x, +y, +x)
* - Text cursor is always referenced to the top left corner
*
* ****************************************************************************/
// Enable weak pullups in the rotary lib before inclusion
#define ENABLE_PULLUPS
// Flash memory
#include<SPIFlash.h> // http://github.com/???/spiflash
SPIFlash flash(7); // this is the PIN for the CHIP SELECT
// include the libs
#include <Rotary.h> // https://github.com/mathertel/RotaryEncoder/
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/
// define encoder pins
#define ENC_A 3 // Encoder pin A
#define ENC_B 2 // Encoder pin B
#define btnPush 4 // Encoder Button
// rotary encoder library setup
Rotary encoder = Rotary(ENC_A, ENC_B);
// the debounce instances
#define debounceInterval 30 // in milliseconds
Bounce dbBtnPush = Bounce();
//~ unsigned long btnDownTime = 0;
//#define SI_OVERCLOCK 950000000
// lib instantiation as "Si"
#include "si5351mcu.h"
Si5351mcu Si;
// analog buttons
#define BUTTONS_COUNT 4 // just 4 buttons
#include <BMux.h>
#define ANALOG_PIN A0
// Creating the AnalogButtons instance;
// defauult 5 msec of debounce and 20 units of tolerance
BMux abm;
/****** VERSION INFORMATION ***********************/
#define VERSION 6
// vars
// this is the IF with 27.000 Mhz XTALs
#define VFO_OFFSET 26994600 // default value
/****** Frequency control related ******************************************/
long vfoA = 100000000L; // VFO A
long vfoB = 7110000L; // VFO B
long *mainFreq; // main freq, the one it's used now
long *subFreq; // the one in reserve
int ppm = 3650; // this is the correction value for the si5351
long vfoOffset = VFO_OFFSET;
bool coff = false; // whether the carrier must be on/off in PC mode
// define the mixing xtal and jumping
// limits
#define LIMI_LOW 100000 // 100 kHz
#define LIMI_HIGH 225000000 - VFO_OFFSET //
/****** SWEEP related defines and vars **************************************/
byte sspan = 2;
const unsigned long sweep_spans[] = {
320, // 320
1000, // 1k
3000, // 3k
10000, // 10k
30000, // 30k
100000, // 100k
300000, // 300k
1000000, // 1M
3000000, // 3M
10000000, // 10MHz
30000000, // 30MHz
100000000, // 100MHz
300000000 // 300MHz
};
const char *sweep_spans_labels[] = {
" 320Hz",
"1.00kHz",
"3.00kHz",
"10.0kHz",
"30.0kHz",
" 100kHz",
" 300kHz",
"1.00MHz",
"3.00MHz",
"10.0MHz",
"30.0MHz",
" 100MHz",
" 300MHz"
};
#define SPAN_COUNT 12
/********************* SWEEP related vars *********************************/
// scan limits
long scan_low, scan_high, sstep;
// measure limits
long minf, maxf; // min/max feq values
long minv, maxv;
// the delay pause, in milli seconds after each freq set
// to avoid the click on the Si5351 and allow the voltage to settle
#define SCAN_PAUSE 1
/****************** FLASH related vars ***********************************/
// declare some vars related to the spi flash
byte flashDataPerPage; // how many data object we have per page of 256 bytes
byte flashPagesInAScan; // How many pages a scan covers.
word flashMaxScans; // the amount of scans we dispose of
word flashPosition; // the object space in the flash at we are now
// length of the data to write to the SPI flash
#define DATA_LEN 6 // bytes
/****** MODE related vars and defines ***************************************/
byte mode = 0;
byte smode = mode; // selected mode in the menu selection
#define MODE_MENU 0
#define MODE_SIGEN 1
#define MODE_SWEEP 2
#define MODE_SA 3
#define MODE_METER 4
#define MODE_LC 5
#define MODE_PC 6
#define MODE_CONFIG 7
#define MODE_COUNT 8
const char *modeLabels[] = {
"MODE SELECTOR",
"SIGNAL GENERATOR",
"SWEEP ANALYZER",
"SPECTRUM ANALYZER",
"POWER METER",
"INDUCTANCE METER",
"COMPUTER MODE",
"SETTINGS"
};
/***** Step related vars en defines *****************************************/
byte step = 2; // 100Hz
const char *stepLabels[] = {
" 1Hz",
" 10Hz",
" 100Hz",
" 1kHz",
" 10kHz",
"100kHz",
" 1MHz",
" 10MHz"
};
#define STEP_COUNT 7
/****** config / settings vars and define ***********************************/
byte config = 0;
const char *configLabels[] = {
"Si5351 PPM",
"VFO Offset {IF}"
};
boolean confSelected = false;
#define CONF_COUNT 2
/****** LCD print and formatting related ************************************/
char f[15]; // this is the frequency box like "145.170.670"
char t[15]; // this is a temp buffer
char empty[] = " "; // "empty" string to copy spaces from
/****** ADC related vars ******************************************************
* A0 = {Reserverd for the analog buttons}
* A1 = adcrR, dB: dB in the receiver side in dB *100
* A2 = adcrM, vm: voltages in the meter in raw adc unites / mv *10
*
******************************************************************************/
#define ADC_M (A1)
#define ADC_R (A3)
// raw vars in ADC units (0-1023)
word adcrR = 0; // Receptor
word adcrM = 0; // Meter
// final values in mv * 10
word mVr = 0; // mV read
word vm = 0; // Meter
long dB = 0; // dB in reference to the maximum of the scale
// in dB * 100; 1 = 0.01; 10 = 0.10, 100 = 1.00
// in PC mode it will default to * 100 instead of * 10
// ADC samples for oversampling
// how many EXTRA bits we want to get over the default 10 bits
#define ADC_OS 3 // max bits (13)
word max_samples = pow(2, (10 + ADC_OS)) - 1;
// This is the base level, if we are dealing with mV it's 0
// but if we handle it as dB it will be a real value
#define Base_level -1090 //
// define the 5 volts
#define V5V 49000 // 4.9 volts in the arduino
/***** meter mode vars and defines ******************************************/
#define MEASURE_INTERVAL 250 // msecs
unsigned long nextMeasure = millis() + MEASURE_INTERVAL;
/********************* LC vars **********************************************/
// feel free to add your values here if you need more, please increment the
// var below if you do so
const byte kcaps[19] = {
3, // 3p 30p 300p
5, // 5p 50p 500p
8, // 8p 80p 800p
10, // 10p 100p 1n0
12, // 12p 120p 1n2
15, // 15p 150p 1n5
18, // 18p 180p 1n8
20, // 20p 200p 2n0
22, // 22p 220p 2n2
24, // 24p 240p 2n4
33, // 33p 330p 3n3
39, // 39p 390p 3n9
47, // 47p 470p 4n7
52, // 52p 520p 5n2
56, // 56p 560p 5n6
68, // 68p 680p 6n8
75, // 75p 750p 7n5
82, // 82p 820p 8n2
91, // 91p 910p 9n2
};
#define CAP_MAX 18
// cap index
byte cindex = 0;
// cap multiplier
// final var is kcap[cindex] * 10^cmult
byte cmult = 0;
// the encoder need to move
void encoderMoved(char dir) {
// do what you need to do in any case (Mode)
// MENU
if (mode == MODE_MENU) {
// move in the menu selection
moveMenu(dir);
}
// SIGEN
if (mode == MODE_SIGEN) {
// move the VFO but in which dir
moveVFO(dir, true);
}
// SWEEP
if (mode == MODE_SWEEP || mode == MODE_SA) {
// move the scan span (sspan)
moveSpanUpdate(dir);
}
// METER
if (mode == MODE_METER) {
// select between measures for the main bar
moveVFO(dir, false);
// print main as sub
subFreqPrint(false);
}
// CONFIG
if (mode == MODE_CONFIG) {
// showing or modifiying
moveConfig(dir);
}
// LC
if (mode == MODE_LC) {
// move the cindex
cindex = moveWithLimits(cindex, dir, 0, CAP_MAX);
// update display
lcdUpdateC();
}
}
/*** Other function in k#-files and z-end ***/