-
Notifications
You must be signed in to change notification settings - Fork 5
/
arduino-arcs.ino
576 lines (475 loc) · 18.7 KB
/
arduino-arcs.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*******************************************************************************
*
* ARCS - Amateur Radio Control & Clock Solution
* -----------------------------------------------
* A full QRP/Hombrew transceiver control with RF generation, the Cuban way.
*
* Copyright (C) 2016...2017 Pavel Milanes (CO7WT) <[email protected]>
*
* This work is based on the previous work of these great people:
* * NT7S (http://nt7s.com)
* * SQ9NJE (http://sq9nje.pl)
* * AK2B (http://ak2b.blogspot.com)
* * QRL Labs team (http://qrp-labs.com)
* * WJ6C for the idea and hardware support.
* * Many other hams with code, ideas, critics and opinions
*
* Latest version is always found on the Github repository (URL below)
* https://www.github.com/pavelmc/arduino-arcs/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
/*******************************************************************************
* Important information you need to know about this code & Si5351
*
* We use a embed Si5351a control code that presume this:
* * We use CLK0 & CLK1 ONLY
* * CLK0 use PLLA & CLK1 use PLLB
* * CLK0 is employed as VFO (to mix with the RF from/to the antenna)
* * CLK1 is employed as BFO (to mix with the IF to mod/demodulate the audio)
* * We use the full power in all outputs (8mA)
*
* * Please have in mind that this IC has a SQUARE wave output and you MAY
* need to apply some kind of low-pass/band-pass filtering to smooth it
* and get rid of the nasty harmonics.
* * If you generate a BFO below 1Mhz you MUST filter the output of that CLK.
* The output of the Si5351 is harmonic rich below 1Mhz and you will NEED
* the filtering because those harmonics are close enough of the fundamental.
******************************************************************************/
/****************************** FEATURES SEGMENTATION *************************
* Here we activate/deactivate some of the sketch features, it's just a matter
* of comment out the feature and it will keep out of compilation (smaller code)
*
* For example: one user requested a "headless" mode: no lcd, no buttons, just
* cat control via serial/usb from the PC. for that we have the headless mode.
* Tip: uncomment th LCD define and you will have it.
*******************************************************************************/
// You like to have CAT control (PC control) of the sketch via Serial link?
#define CAT_CONTROL True
// Rotary and push control?
#define ROTARY True
// Analog button support?
// please note that if no rotary then abut is disabled
#define ABUT True
// Memories?
#define MEMORIES True // hard limited to 99 mems (~80 on the ATMEga168)
// because we have only two spaces to represent that
// Also you want to scan over enabled memories?
#define MEM_SCAN True
// Smeter on the LCD?
#define SMETER True
// You have two alternatives for the SMeter graphics
// The default is a simple bar with 24 points (high) resolution
// The alternative is a low resolution one with 12 points
// but numbers in the 1-3-5-7-9-+20
//
// If you want the alternative just uncomment this code below
// the alternative willl cost you about 10 bytes of firmware
//#define SMETER_ALT True
// do you have an LCD?
#define LCD True
// if you want a headless control unit just comment the line above to
// effectively disable the LCD, when you have no LCD then there is no
// meaning for buttons, memories, rotary encoder, etc.
// You will get just CAT control
#ifndef LCD
// no smeter
#ifdef SMETER
#undef SMETER
#endif
// no Analog Buttons
#ifdef ABUT
#undef ABUT
#endif // abut
// no rotary ot push buttons
#ifdef ROTARY
#undef ROTARY
#endif // rotary
// no memories
#ifdef MEMORIES
#undef MEMORIES
#endif // memories
// YES CAT_CONTROL
#ifndef CAT_CONTROL
#define CAT_CONTROL True
#endif // cat control
#endif // headless
// safety check for no rotary, if no rotary then abut has no use.
#ifndef ROTARY
// no need for Analog Buttons
#ifdef ABUT
#undef ABUT
#endif // abut
// no memories, beacause no abut
#ifdef MEMORIES
#undef MEMORIES
#endif // memories
#endif // rotary
// safety check for memories only when analog buttons are in use
#ifndef ABUT
#ifdef MEMORIES
#undef MEMORIES
#endif // memories
#endif // abut
// default (non optional) libraries loading
#include <EEPROM.h> // Internal EEPROM
#include <Wire.h> // I2C
// The eeprom & sketch version; if the eeprom version is lower than the one on
// the sketch we force an update (init) to make a consistent work on upgrades
const byte EEP_VER = 8;
const byte FMW_VER = 16;
// structured data: Main Configuration Parameters
struct userData {
byte fmwver = FMW_VER;
byte eepver = EEP_VER;
long ifreq; // first or unique IF
long if2; // second IF, usually higher than the ifreq
long a; // VFO a
byte aMode; // VFO a mode
long b; // VFO b
byte bMode; // VFO b mode
int usb;
int cw;
int ppm;
};
// declaring the main configuration variable for mem storage
// BEWARE: you can't use it outside a procedure or function
// the compiler is not happy with that.
struct userData u;
// The start byte in the eeprom where we put mem[0]
#define MEMSTART sizeof(u)
// the limits of the VFOs: full HF; you can tweak it with the
// limits of your particular hardware, again this are LCD diplay frequencies.
#define F_MIN 500000 // 500 kHz
#define F_MAX 30000000 // 30.0 MHz
// PTT IN/OUT pin
#define PTT 13 // PTT actuator, this will put the radio on TX
// this match the led on pin 13 with the PTT
#define inPTT 12 // PTT/CW KEY Line with pullup
#ifdef ROTARY
// Enable weak pullups in the rotary lib before inclusion
#define ENABLE_PULLUPS
// If you have a half step encoder (you need two clicks to make a step)
// uncomment this to get it working
//#define HALF_STEP
// 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 11 // Encoder Button
// rotary encoder library setup
Rotary encoder = Rotary(ENC_A, ENC_B);
// the debounce instances
#define debounceInterval 10 // in milliseconds
Bounce dbBtnPush = Bounce();
Bounce dbPTT = Bounce();
#endif // rotary
#ifdef ABUT
// define the max count for analog buttons in the BMux library
#define BUTTONS_COUNT 4
// define the sampling rate used
#define BMUX_SAMPLING 10 // 10 samples per second
// define the analog pin to handle the buttons
#define KEYS_PIN 2
// include the lib
#include <BMux.h> // https://github.com/pavelmc/BMux/
// instantiate it
BMux abm;
// Creating the analog buttons for the BMux lib; see the BMux doc for details
// you may have to tweak this values a little for your particular hardware
//
// Also on the examples on the lib there is a sketch that will allow to
// compute the exact values for your hardware.
//
// Define the adc levels of for the buttons values
// top resistor 4k7 2k2 10k
#define b1 207 // 1k2 470 2k2
#define b2 370 // 2k7 1k 4k7
#define b3 512 // 4k7 2k2 10k
#define b4 697 // 10k 4k7 22k
#ifdef MEMORIES
// Analog buttons has a second action related to memories
// but only if we have memories set
Button bvfoab = Button(b1, &btnVFOABClick, &btnVFOMEM);
Button bmode = Button(b2, &btnModeClick, &btnVFOsMEM);
Button brit = Button(b3, &btnRITClick, &btnEraseMEM);
Button bsplit = Button(b4, &btnSPLITClick, &btnEraseWholeMem);
// memory object definition
boolean vfoMode = true;
word mem = 0; // actual memory channel
word memCount; // how many mems this chip support
// (it's calculated in the setup process)
// memory type
struct mmem {
boolean configured;
long vfo;
byte vfoMode;
};
// declaring the main configuration variable for mem storage
struct mmem memo;
// some vars for the memory scan feature if defined
#ifdef MEM_SCAN
// general memscan flag
bool mscan = true;
#define MEM_SCAN_INTERVAL 5000; // in msecs, the time between scan jumps
unsigned long scanTime; // counter against millis
#endif
#else
// Analog buttons with single functions
Button bvfoab = Button(b1, &btnVFOABClick);
Button bmode = Button(b2, &btnModeClick);
Button brit = Button(b3, &btnRITClick);
Button bsplit = Button(b4, &btnSPLITClick);
#endif
#endif // abut
#ifdef CAT_CONTROL
// library include
#include <ft857d.h> // https://github.com/pavelmc/ft857d/
// instantiate it
ft857d cat = ft857d();
#endif // cat
#ifdef LCD
// lib include
#include <LiquidCrystal.h> // default
// lcd pins assuming a 1602 (16x2) at 4 bits
// COLAB shield + Arduino Mini/UNO Board
#define LCD_RS 5
#define LCD_E 6
#define LCD_D4 7
#define LCD_D5 8
#define LCD_D6 9
#define LCD_D7 10
// lcd library instantiate
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
#ifdef SMETER
// how many samples we take in the smeter, we use a 2/3 trick
// to get some inertia and improve the look & feel of the bar
#define BARGRAPH_SAMPLES 6
word pep[BARGRAPH_SAMPLES] = {}; // s-meter readings storage
boolean smeterOk = false; // it's ok to show the bar graph
word sMeter = 0; // the value of the Smeter readings
// in both RX and TX modes
boolean barReDraw = true; // Smeter bar needs to be redrawn
#endif // smeter
#endif // nolcd
// run mode constants
#define MODE_LSB 0
#define MODE_USB 1
#define MODE_CW 2
#define MODE_MAX 2 // the mode count to cycle (-1)
#define MAX_RIT 99900 // RIT 9.99 Khz * 10
// config constants
#define CONFIG_IF 0
#define CONFIG_IF2 1
#define CONFIG_VFO_A 2
#define CONFIG_MODE_A 3
#define CONFIG_VFO_B 4
#define CONFIG_MODE_B 5
#define CONFIG_USB 6
#define CONFIG_CW 7
#define CONFIG_PPM 8
// the amount of configure options
#define CONFIG_MAX 8
// Tick interval for the timed actions like the SMeter and the autosave
#define TICK_INTERVAL 250 // in milli seconds, 4 ticks per second
// EERPOM auto saving interval (if some parameter has changed) in TICK_INTERVAL
// var is word so max is 65535 in 1/4 secs is ~ 16383 sec ~ 273 min ~ 4h 33 min
#define SAVE_INTERVAL 2400 // 10 minutes = 60 sec * 4 ticks/sec * 10 min
// Si5351a Xtal
const long XTAL = 27000000; // default FREQ of the XTAL for the Si5351a
long CXTAL = XTAL + u.ppm; // corrected xtal with the ppm
/******************************************************************************
* The use of an XFO... some users are requesting the use of an XFO in the
* calculations.
*
* Some users with double conversion radios has 1st & 2nd IF, so this radios
* usually has a XTAL oscillator to mix 1st & 2nd IF back and forward, this is
* called an XFO.
*
* WARNING!: we are not generating that XFO frequency, just taking it into
* account in the calculations; so, put your soldering iron down and keep the
* XTAL oscillator running please.
*
* If you have this scenario just set the u.if2 value to the the 2nd IF value
* for example you have a high IF of 74.055 MHz and lower IF of 8.215 MHz, then
* your u.if2 = 74.055 MHz & u.ifreq = 8.215 MHz
*
* If you use just one IF set the u.if2 value to zero.
*
* This will trigger a few macros ahead and will calculate the correct VFO
* frequencies for you in normal and SETUP mode.
*****************************************************************************/
/****** hardware pre-configured values ******/
void setDefaultVals() {
// 1st (or High) IF value, if you have just one IF this is ZERO
u.if2 = 0; // Zero if no second IF
// 2nd of unique IF, this is the one you beat to get the audio
u.ifreq = 10700000; // 10.7 MHz
// USB shift
u.usb = 3.200; // typical value
// CW shift
u.cw = 600; // typical value
// VFO A default value
u.a = 7110000; // 7.110 kHz
// VFO A default mode
u.aMode = MODE_LSB; // LSB
// VFO B default value
u.b = 7125000; // 7.125 kHz
// VFO B default mode
u.aMode = MODE_LSB; // LSB
// This value is not the real PPM value is just the freq correction for
// yourparticular xtal from the 27.00000 Mhz one, if you can measure it
// put it here, this trick is computational simpler and yet accurate
u.ppm = 2256; // in Hz, mine is 2.256 Khz up
}
long tvfo = 0; // temporal VFO storage for RIT usage
long txSplitVfo = 0; // temporal VFO storage for SPPLIT
byte step = 3; // default steps position index:
// as 1*10E2 = 100 = 100hz; step position is
// calculated to avoid to use a big array
// see getStep() function
boolean update = true; // lcd update flag in normal mode
byte encoderState = 0; // encoder state (DIR_NONE)
byte config = 0; // holds the configuration item selected
boolean inSetup = false; // the setup mode
// false is just looking, true is modifying
#define STEP_SHOW_TIME 6 // the time the step must be shown in
// in 1/4 secs aka: aprox 1.5 secs
byte showStepCounter = 0; // the step timer counter
boolean runMode = true; // true: normal, false: setup
boolean activeVFO = true; // true: A, False: B
boolean ritActive = false; // true: rit active, false: rit disabled
boolean tx = false; // whether we are on TX mode or not
unsigned long lastMilis = 0; // to track the last sampled time
boolean split = false; // this holds th split state
boolean catTX = false; // CAT command to go to PTT
word qcounter = 0; // Timer to be incremented each 1/4 second
// approximately, to trigger a EEPROM update
// if needed
// temp boolean var (used in the loop function)
boolean tbool = false;
// pointers to the actual values
long *ptrVFO; // will hold the value of the selected VFO
byte *ptrMode; // idem but for the mode of the *ptrVFO
/******** MISCELLANEOUS FUNCTIONS RELATED TO SEVERAL PROCEDURES ***********/
// return the right step size to move
long getStep() {
// we get the step from the global step var
long ret = 1;
for (byte i=0; i < step; i++, ret *= 10);
return ret/10;
}
// split check
void splitCheck() {
if (split) {
// revert back the VFO
activeVFO = !activeVFO;
updateAllFreq();
}
}
// change the modes in a cycle
void changeMode() {
// normal increment
*ptrMode += 1;
// checking for overflow
if (*ptrMode > MODE_MAX) *ptrMode = 0;
// Apply the changes
updateAllFreq();
}
// hardware or software commands in to RX
void going2RX() {
// PTT released, going to RX
tx = false;
digitalWrite(PTT, 0);
// make changes if tx goes active when RIT is active
if (ritActive) {
// get the TX vfo and store it as the reference for the RIT
tvfo = *ptrVFO;
// restore the rit VFO to the actual VFO
*ptrVFO = txSplitVfo;
}
// make the split changes if needed
splitCheck();
}
// hardware or software commands in to TX
void going2TX() {
// PTT asserted, going into TX
tx = true;
digitalWrite(PTT, 1);
// make changes if tx goes active when RIT is active
if (ritActive) {
// save the actual rit VFO
txSplitVfo = *ptrVFO;
// set the TX freq to the active VFO
*ptrVFO = tvfo;
}
// make the split changes if needed
splitCheck();
}
// swaps the VFOs
void swapVFO(byte force = 2) {
// swap the VFOs if needed
if (force == 2) {
// just toggle it
activeVFO = !activeVFO;
} else {
// set it as commanded
activeVFO = bool(force);
}
// setting the VFO/mode pointers
if (activeVFO) {
ptrVFO = &u.a;
ptrMode = &u.aMode;
} else {
ptrVFO = &u.b;
ptrMode = &u.bMode;
}
}
// print the "A"/"B" letter to match the selected VFO, a trick to save space
void vfoLetter() {
if (activeVFO) lcd.print("A");
else lcd.print("B");
}
// beep function
#ifdef ROTARY
// beep function a 1.2Khz tone for 50 msecs
void beep() {
tone(4, 1200, 50);
delay(50);
}
// beep-boop
// a 1.2Khz tone for 50 msecs
// a 0.6Khz tone for 25 msecs
void beop() {
tone(4, 1200, 50);
delay(50);
tone(4, 600, 25);
delay(25);
}
#endif
/*****************************************************************************
* Where did the other functions go?
*
* This sketch use the "split in files" feature of the Arduino IDE, look for
* tabs in you editor.
*
* Tabs are organized by group of functions preceded with the letters "fx-"
* where "x" is a sequence to preserve order in the files upon load
*
* The "setup" and "loop" are placed in the file "z-end" to get placed at the
* end when the compiler does it's magic.
*
* ***************************************************************************/