-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathusdx.ino
7120 lines (6425 loc) · 295 KB
/
usdx.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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// QCX-SSB.ino - https://github.com/threeme3/QCX-SSB
//
// Copyright 2019, 2020, 2021 Guido PE1NNZ <[email protected]>
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Copyright all additions 2022-2023 Rob Colclough GW8RDI, use of the additions and changes is permitted for all private, non-commecial use at user´s risk. No responsibility is accepted for any losses that may occur through the use of this modified code.
// THIS CODE SUPERCEEDS VERSIONS 1.02X AND 1.03. 1.03 HAS UPDATES FOR OLED CHPSETS BUT DOES NOT HAVE FUNCTIONAL OR DSP CHANGES COMPARED TO 1.02w.
// GW8RDI IMPORTANT NOTES: *** DO NOT RUSH - READ THE NOTES BELOW SEVERAL TIMES!
// *** THIS OPEN SOFTWARE IS FULLY SUPPORTED* FREE OF COST BY GW8RDI and others ***
/*
Comfigured for (tr)usdx clone:
Compile results 17 April 2023
"C:\\Users\\User\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7/bin/avr-size" -A "C:\\Users\\Usuario\\AppData\\Local\\Temp\\arduino\\sketches\\E2EA2B2706C6565E78832871CCAA7296/usdx.ino.elf"
Sketch uses 32244 bytes (99%) of program storage space. Maximum is 32256 bytes.
Global variables use 1499 bytes (73%) of dynamic memory, leaving 549 bytes for local variables. Maximum is 2048 bytes.
*/
// *** ISP DATA CORRUPTION WARNING1!! ALWAYS REMOVE C24 (C27?) (or as marked) it's a 10nF on the ISP HEADER'S MOSI line (PA ctrl out) (PB3) of the ISP header,
// and disconnect the internal mic by plugging in a disconnected Jack plug. FAILING TO DO THIS CAN CORRUPT YOUR MCU CHIP!!
// *** CHECK WITH ME FIRST PLEASE ***
// PLEASE DO NOT PUBLISH WHAT YOU THINK ARE FAULTS WITH THIS RELEASE *** BEFORE CHECKING WITH ME, GW8RDI ***.
// 99% OF REPORTED PROBLEMS ARE JUST CONFIGURATION RELATED, NOT BUGS OR LIMITATIONS.
// SO IF YOU USE THIS SOFTWARE, YOU AGREE NOT TO PUBLISH UNTIL YOU´VE CONTACTED ME, GW8RDI
// THIS AVOIDS CONFUSIONS.
// Supports up to 9 bands (using 8 filters, 15 and 17M share). To increase change N_BANDS, and add frequencies to array "band[N_BANDS] ="
// SSB TX quality tests, see below MORE_MIC_GAIN, QUAD
/* WELCOME TO THE OPEN SOURCE USDX PROJECT 2022 AND ONWARDS!
This version is for all uSDX/uSDR transceivers, release numbers are 4.XXx.
Compiled and programmed using Arudino IDE 2.0.4, available from www.arduinio.cc
To support the add-on module for uSDX with powerful DSP processor, version numbers will be 3.XXx.
*** READ ALL THE NOTES here and ask questions on the FB "USDX USDR" group before programming, it may save a lot of heartache!
Our intention is to make the uSDX a great option, with all the facilities and interfaces you can imagine!
The quadrature mixer (designed by Dan Tayloe N7VE) in the uSDX makes it as good a receiver as a professional rig, when built correctly.
The TX modulation has been improved and gets good reports, and with the add-on module it can be as good as the best out there.
Enjoy!
73 Rob, GW8RDI
*/
// G8RDI Modifications log:
#define VERSION "4.00c" // Fixed format "9.99z" : Additions and changes Copyright 2022-2023 GW8RDI - You can use and distribute if you maintain the copyright message, commercial use is prohibited.
// 2022/03/04 - Added delay to show serial number at start - G8RDI mod
// Added band change direction based on last freq step directions. See "case BE | DC:" - GW8RDI mod
// Set PB3/PB5 to output in init to drive LCD backlight
// 2022/03/05 - Release 1.02wA2 2022/03/20 : Added Cat 8.6 ad QUAD enable 8.7 menu items
// 2022/03/06 - With MORE_MIC_GAIN enabled & QUAD disabled, SSB TX voice is sounding much better!
// 2022/04/24 - Release 1.02wA3: Fixed band dir. bug
// 2022/05/08 - Added KEEP_BAND_DATA to maintain last freq and mode set on each band.
// 2022/05/09 - Release 1.02wA4 : Maintains last freq and mode set for each band, up to 9 bands set. Added error code display.
// 2022/05/11 - Changed menu to cycle end-start, start-end
// 2022/07/19 - Added full CW mode update on band change, minor fixes
// 2022/07/19 - Release 1.02wA5 : Maintains last freq and mode set for each band, up to 9 bands set. Added error code display.
// 2022/07/28 - Release 1.02wA6 : Minor changes. Reduced CW message duplication to release more memory.
// 2022/08/16 - Release 1.02wA7 : Directional band-change went in the wrong direction if SWAP_ROTARY was not enabled. This fix solves that issue.
// 2022/08/19 - Release 1.02wA8 : Directional band-change saved last band-mode to Flash, and not mode of band changed to, causing incorrect mode restore if shutdown without band change. Minor other changes, DEBUG modes changes callsign.
// 2022/08/22 - Release 1.02wA8a : Red Corners Unit: CW messages updated, Band menu fix as was showing 6m in the list. Added WHITE_CORNERS to config list.
// 2022/08/22 - Release 2.00a : Jumped version to avoid confusion with prev. releases. Added SWR, 115200 baud CAT, experimental changes to AM and FM, and minor tidy up
// 2022/10/12 - Release 2.00b : NR stays as last used.
// 2022/11/06 - Release 2.00c : #define KEYER CW keyer for Iambic -> NOTE: Auto CW msg sending aborts if not installed, and 700Hz filter selection gives reduced gain.
// : Added new post mag IQ filter, added BlackBrick config.
// 2023/02/09 - Release 2.00d : Added CAT freq. error handling, in RIT mode the TX freq. is now displayed, plus FIR noise filter, see #define NR_FIR below. NR 0-2 is 1.02x method, 3-8 is now FIR DSP filter method to be used with Full b/w and Att2 = 2
// 2023/02/09 - Release 2.00e : Ammended code to support +/- 99 KHz RIT receiver offset, and added CAT command to set the RIT offset.
// 2023/03/14 - Release 2.00f : Ammended code with new "TT" CAT command to set a TX offset frequency; see CAT_XO_CMD. Minor improvements for setting mode display, etc.
// 2023/03/14 - Update to 2.00f : Ammended code as missing #ifdef KEEP_BAND_DATA on line 5922
// 2023/04/01 - Release 4.00a : Changed to version 4.00 as DL2MAN jumped his version up to 2.00! CAT now enables directly from the menu without needing a reboot to activate! CAT MDx; now refreshes LCD fully after CW mode.
// Re-coded KEEP_BAND_DATA switch statements which freed 328 bytes!!
// 2023/04/11 - Release 4.00b : Fixed CAT mode change to allow it to select AM and FM, AM important to correctly track IQ flip. Also commented-out redundant BE | DC for mode change, as now used for directional band change.
// RIT mode now allows mode to be changed.
// 2023/04/17 - Release 4.00c : (UPDATE-2) Ammended configuration for TRUSDX clone so that latched-relay band switching and SWR selection is included.
// : Added new post mag IQ filter, added BlackBrick config.
// : todo see "// xyzzy Test with i_d"
// NOTE update #define VERSION "????" above!
// See "BACKLOG:" FOR PENDING WORK CARRIED FORWARD
// *** Use of this modified software is at the users risk *** PLEASE READ THE INSTRUCTIONS AVAILABLE IN THE FB GROUP "uSDX uSDR Radios" or uSDX Group IO online.
// Notes: To have CW tone in the menu, enable #define FILTER_700HZ. You´ll need to find 28 bytes depending on your config, i.e. disabling #define DIAG
// Configuration switches; remove/add a double-slash at line-start to enable/disable a feature; to save space disable e.g. CAT, DIAG, KEYER
// *** BEFORE ALL, READ THIS BLOCK !!!!!!!!
/* THIS CONFIG: / To change, ADD or DELETE the "//" in front of #define lines below
XXBLACK BRICK UNIT, FRONT PANEL ON LARGER SIDE: 25.0, SWR, SWAP-ROTARY, BACKLIGHT_PIN 0x08
RED BUTTONS UNIT 27001400 (CHANGE BELOW TO SET YOUR OFFSET OR SET 27000000), NO SWR, SWAP-ROTARY
XXWHITE BUTTONS UNIT 27000000 (CHANGE BELOW TO SET YOUR OFFSET OR SET 27000000), NO SWR, SWAP-ROTARY - NEEDS CHECKING TODO
XXRED CORNERS UNIT 25000000 SWR, SWAP-ROTARY
XXTRUESDX UNIT - PLEASE CONTACT GW8RDI TO CHECK YOUR CONFIGURATION BEFORE PROGAMMING AS WE ARE IN TESTING PHASE.
*/
// NOTE: ONLY ENABLE ONE OF THE MODELS BELOW BY ADDING OR REMOVING THE UNCOMMENT "//"
//________________________________________________________________________________________________________________________
#define BLACK_BRICK 1 // Backlight control PortD is PD3 0x08, SWR, NO ROTARY SWAP
//#define RED_CORNERS 1 // Backlight control PortD is PD3 0x08, or PD5 0x20 for Red Corners rig. Disable for Red and White buttons and most black brick uSDX.
// !!!!! ALWAYS DISABLE LINE BELOW !!!!!!
///#define MY_RED_CORNERS 1 // Only for my(GW8RDI) Red Corners with reversed rotary part!
// !!!!! ALWAYS DISABLE LINE ABOVE !!!!!!
//#define RED_BUTTONS 1 // Used for Small HF SDR TRANSCEIVER uSDX model, without SWR circuit. May have SMD inductors.
//#define WHITE_BUTTONS 1 // Small black unit with white or red buttons on front, without SWR circuit.
//#define TRUSDX 1 // Small USDX clone in 3D printed case marked "DL2MAN & PE1NNZ". CHECK WITH GW8RDI BEFORE USING THIS FOR UPDATES AND CONFIG DETAILS! SWR protection via PA sensing resistor can be added if needed.
// NOTE: DL2MAN claims (as of 17 April 2023) that his license blocks users from installing other software (Microsoft vs Linux et al), but it is understood that this violates consumer rights laws in the USA, UK and European Union.
// *** NOTE ***: If none of the above are enabled, configuration may match other units, but if tuning direction is reversed, backlight or frequency wrong, adjust as needed.
// IF IN DOUBT PLEASE ASK ME FIRST: GW8RDI
//________________________________________________________________________________________________________________________
#if defined(RED_CORNERS) || defined(BLACK_BRICK)
#define BACKLIGHT_PIN 0x20
#else
#define BACKLIGHT_PIN 0x08
#endif
#ifndef TRUSDX
#define KEEP_BAND_DATA 1 // Maintain last freq and mode set on each band - GW8RDI mod
#endif
#define SHOW_USB_LSB_CW_ONLY 1 // If defined, Menu will only cycle thro these 3 modes
// AM & FM Modulation changes
//#define FM_ARCTAN 1 // Enable FM differentiator TEST - GW8RDI mod
//#define AM_MOD_MAGN_SQRT 1 // Use more accurate SQRT method
//****************************************************************
//#define DEBUG_G8RDI 1 // Enables display of error codes on LCD
//****************************************************************
// Change Callsign from G8RDI to match your own, or set it to "uSDR+ " if you don't want it customised:-
// *** CALLSIGN NO MORE THAN 5 CHARACTERS!!! DON'T REMOVE THE 2 SPACES!!! IGNORE THIS WARNING IT WILL CRASH THE PROGRAM!!! ***
#ifdef DEBUG_G8RDI
#define MY_CALLSIGN_PADDED "DEBUG "
#else
// Put your callsigne below and remove the "///" in front to activate.
///#define MY_CALLSIGN "G8RDI" // <----- Add your callsign here or enable line below, replacing G8RDI! If you don´t want the LCD to show your callsign, enable the line "uSDR+" below.
///#define MY_CALLSIGN_PADDED "G8RDI " // <----- Also add your callsign here BUT keep the 2 spaces at the end!
/// Disable below line if using your own callsign by adding // in front.
#define MY_CALLSIGN_PADDED "uSDR+ " // Ensure two spaces at end of heading and that it is under 7 characters (including the 2 spaces), or this program may not work correctly.
//#define MY_PREFIX "" // No prefix, use this line by removing the //, add below to replica line.
#define MY_PREFIX "" // Add visiting country prefix here
#define MY_NAME "ROB" // <---- *** ADD YOUR NAME HERE FOR CW MESSAGES
#endif
#define CALLSIGN_LENGTH 5 // Change length to match your callsign but remember the LCD isn't very wide!
// *** MEMORY LIMITATION OF ATMEGA328 *** This means you may have to mix and match functions option defines. CAT requires considerable memory, so use only if needed.
// If your dial goes the wrong way, change SWAP_ROTARY
#if defined(RED_CORNERS) || defined(BLACK_BRICK)
// SWAP_ROTARY is isually required for Red Corners unless Rotary type changed, like mine!
#ifdef MY_RED_CORNERS
#define REVERSE_BAND_CHANGE 1 //If your freq. change is correct, but band jump goes backwards, define REVERSE_BAND_CHANGE
#else
#define SWAP_ROTARY 1 // Swap rotary direction (enable for WB2CBA-uSDX) NOTE: To enable SWAP without RED_CORNERS enabled, comment out the lines above and below with // character, i.e. //#ifdef RED_CORNERS and //#endif
//#define REVERSE_BAND_CHANGE 1 //If your freq. change is correct, but band jump goes backwards, define REVERSE_BAND_CHANGE
#endif
// :( No space for SWR with both CW Msgs and CAT
#define SWR_METER 1 // Supports SWR meter with bridge on A6/A7 (LQPF ATMEGA328P) by Alain, K1FM, see: https://groups.io/g/ucx/message/6262 and https://groups.io/g/ucx/message/6361
#endif
//***************** TRUSDX FEATURES
#if defined(TRUSDX)
//#define LCD_I2C 1 // LCD with I2C (PCF8574 module ), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
#define OLED_SSD1306 1 // OLED display (SSD1306 128x32 or 128x64), connect SDA (PD2), SCL (PD3)
//#define OLED_SH1106 1 // OLED display (SH1106 1.3" inch display), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
#define SWR_METER 1 // Supports SWR meter with bridge on A6/A7 (LQPF ATMEGA328P) by Alain, K1FM, see: https://groups.io/g/ucx/message/6262 and https://groups.io/g/ucx/message/6361
#endif
//*****************
#define LPF_SWITCHING_DL2MAN_USDX_REV3 1 // Default
//#define LPF_SWITCHING_DL2MAN_USDX_REV3_NOLATCH 1 // NOTE: CHANGE IF THIS VERSION LATCHES
#if defined(BLACK_BRICK)
#define SWR_METER 1 // Supports SWR meter with bridge on A6/A7 (LQPF ATMEGA328P) by Alain, K1FM, see: https://groups.io/g/ucx/message/6262 and https://groups.io/g/ucx/message/6361
#endif
//#define FAST_AGC 1 // Adds fast AGC option (good for CW) Slow mode not recommended. Remove for CAT if memory errors.
#define CAT 1 // CAT-interface - OTHER OPTIONS, SUCH AS CW_MESSAGES and KEEP_BAND_DATA MAY TO BE DISABLED TO MAKE SPACE FOR CAT
//#define CAT_EXT 1 // Extended CAT support: remote button and screen control commands over CAT
//#define CAT_STREAMING 1 // Streams audio and IQ, only 8KHz b/w, & needs faster 115200 baud RS232
#define CAT_FAST 1 // Uses faster 115200 baud (can be changed to 57600), else 38400, 8, 1, N.
// If short of memory on compile and not using Spectrum display, disable CAT_XO_CMD:- Like this:-> //#define CAT_XO_CMD
#ifdef CAT
#define CAT_TX_CMD 1 // GW8RDI mod - added - Send TX and RX status CAT cmds as PTT is pressed and released
#ifndef TRUSDX
#define CAT_XO_CMD 1 // GW8RDI mod - added - Set TX offset freq. for Quantum Spectrum module from QuantumSDR.com
// Note: to use CAT_XO_CMD, RIT_ENABLE must also be enabled.
#endif
#endif
// Lines below NEEDED FOR CW, removed to make space for CAT
//#define KEYER 1 // CW keyer for Iambic - NOTE: Auto CW msg sending aborts if not installed as changes dit timing. Can be removed to save memory for CAT
//#define KEY_CLICK 1 // G8RDI mod - may be removed to free memory for CAT - NEEDED FOR CW msg sending else CW TX sounds mushy & CW msg sending stops after one peep! // Reduce key clicks by envelope shaping
//#define FILTER_700HZ 1 // G8RDI mod - Moved here - Enabled shows in Menu
// CW Messages: Note: If CAT is enabled, CW messages may cause a program memory overflow. KEEP_BAND_DATA can be disabled to release memory for CW at cost of losing band frequency memory.
//#define CW_MESSAGE 1 // Transmits pre-defined CW messages on-demand (left-click menu item 4.2)
//#define CW_MESSAGE_EXT 1 // Additional CW messages
// NOTE: DO NOT CHANGE THE CW_MESSAGE LINES BELOW AS THEY ARE INCORPORATED OR NOT BASED ON CW_MESSAGE and CW_MESSAGE_EXT above.
// Note: !!!Do not exceed CW_MESSAGE_LENGTH when ammending messages!!!
#ifdef CW_MESSAGE_EXT
#define CW_MESSAGE_LENGTH 48 //48/32
#else
#define CW_MESSAGE_LENGTH 48 //48/32/16
#endif
// CHANGE THE CW MESSAGE TEXT BELOW AS YOU LIKE BUT LESS THAN CW_MESSAGE_LENGTH+1 CHARS! THE ## MEANS +, USED TO CONCATANTE STRINGS.
// DO NOT COMMENT OUT LINES BELOW, THESE TEXTS ARE NOT INCLUDED WHEN CW_MESSAGES/EXT ARE DISABLED
#define CW_STD_MSG "CQ CQ DE " MY_CALLSIGN " +" // 16 chars, change in code of size changes.
#define CW_MSG1 "CQ CQ DE " MY_CALLSIGN " +"
#define CW_MSG2 "CQ CQ DE " MY_PREFIX MY_CALLSIGN " +"
#define CW_MSG3 MY_PREFIX MY_CALLSIGN
#define CW_MSG4 "GE TKS 5NN 5NN NAME IS " MY_NAME " HW?"
#define CW_MSG5 "FB RPTR TX 5W ANT EFW 73 CUAGN"
#define CW_MSG6 "73 GL TU EE"
// Examples:
//"CQ" MY_CALLSIGN " +", "CQ CQ DE " MY_CALLSIGN + MY_CALLSIGN " +", "GE TKS 5NN 5NN NAME IS ROB ROB HW?", "FB RPTR TX 5W 5W ANT ENDFED 73 CUAGN", "73 TU E E", "G8RDI"
//"CQ CQ DE " MY_CALLSIGN " " MY_CALLSIGN " +", "GE TKS 5NN 5NN NAME IS " MY_NAME "" MY_NAME " HW?", "FB RPTR TX 5W 5W ANT ENDFED 73 CUAGN", "73 TU E E", MY_CALLSIGN
//#define CW_MSG2 '"CQ CQ DE " MY_CALLSIGN " +"' // Remove/add your Area prefit
//#define CW_MSG3 MY_CALLSIGN
//#define NR_FIR 1 // GW8RDI mod. Usually this won´t fit with CAT, but removing other options, such as CW messages, etc., can make enough space
///G8RDI comment out FAST_AGC & DIAG below to save mem space for CAT
//#define DIAG 1 // Hardware diagnostics on startup (use to debug problems)
#ifndef CAT_XO_CMD // Undefined CW_VOLUME to make space for CAT_XO_CMD
#define CW_VOLUME 1 // Enable separate CW tone volume in the menu
#endif
#define CW_DECODER 1 // CW decoder
//#define CW_INTERMEDIATE 1 // CW decoder shows intermediate characters (only available for LCD and F_MCU at 20M), sequences like: EIS[HV] EIUF EAW[JP] EARL TMO TMG[ZQ] TND[BX] TNK[YC], may be good to learn CW; a full list of possible sequences: EISH5 EISV3 EIUF EIUU2 EAWJ1 EAWP EARL TMOO0 TMOO9 TMOO8 TMGZ7 TMGQ TNDB6 TNDX TNKY TNKC
//#define CW_FREQS_QRP 1 // Defaults to CW QRP frequencies when changing bands
//#define CW_FREQS_FISTS 1 // Defaults to CW FISTS frequencies when changing bands
// NOTE: Make sure you have the correct xtal frequency enabled. This is the xtal near the SI5351/SI3253 chip, not the one near the Atmega MCU.
//#define F_XTAL 27005000 // 27MHz SI5351 crystal
//#define F_XTAL 25004000 // 25MHz SI5351 crystal (enable for WB2CBA-uSDX, SI5351 break-out board or uSDXDuO)
#if defined(RED_CORNERS) || defined(BLACK_BRICK)
//#ifdef RED_CORNERS
#define F_XTAL 25000000 // 25MHz SI5351 crystal (enable for 25MHz TCXO)
#else
#ifdef MY_RED_CORNERS
#define F_XTAL 27001400
#else
#define F_XTAL 27000000 // !!!! SET YOUR EXACT XTAL FREQ OR 27000000 !!!! 27MHz usually on black bricks, Red Buttons (27001400 is my calibration offset!!!) and White buttons versions
#endif
#endif
// GW8RDI NOTE: Enable to have battery voltage shown on the LCD.
// GW8RDI WARNING!!! The problem with the original code is that it switches the ADC VREF up to 5V to read the bat, V, this causes some noise on the IQ sampling which enters the audio,
// and can interfere with the reading of buttons which are sensed through an ADC.
// A better solution is to simply use a 2 resistor voltage divider and not change VREF, calculating the bar. voltage based on the res. divider ration. Alternatively, only show voltage in a menu function. todo - change code.
//#define VSS_METER 1 // Supports Vss measurement (as s-meter option), requires resistor of 1M between 12V and pin 26 (PC3)
//#define QCX 1 // Supports older (non-SDR) QCX HW modifications (QCX, QCX-SSB, QCX-DSP with I/Q alignment-feature)
//#define OLED_SSD1306 1 // OLED display (SSD1306 128x32 or 128x64), connect SDA (PD2), SCL (PD3)
//#define OLED_SH1106 1 // OLED display (SH1106 1.3" inch display), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
//#define LCD_I2C 1 // LCD with I2C (PCF8574 module ), connect SDA (PD2), SCL (PD3), NOTE that this display is pretty slow
//#define LPF_SWITCHING_DL2MAN_USDX_REV3 1 // Enable 8-band filter bank switching: latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.0 as common (ground), IO1.0..7 used by the individual latches K0-7 switching respectively LPFs for 10m, 15m, 17m, 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV3_NOLATCH 1 // Enable 8-band filter bank switching: non-latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.0 as common (ground), IO1.0..7 used by the individual latches K0-7 switching respectively LPFs for 10m, 15m, 17m, 20m, 30m, 40m, 60m, 80m. Enable this if you are using 8-band non-latching version for the relays, the radio will draw extra 15mA current but will work ity any relay (Tnx OH2UDS/TA7W Baris)
//#define LPF_SWITCHING_DL2MAN_USDX_REV2 1 // Enable 5-band filter bank switching: latching relays wired to a TCA/PCA9555 GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.1 as common (ground), IO0.3, IO0.5, IO0.7, IO1.1, IO1.3 used by the individual latches K1-5 switching respectively LPFs for 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV2_BETA 1 // Enable 5-band filter bank switching: latching relays wired to a PCA9539PW GPIO extender on the PC4/PC5 I2C bus; relays are using IO0.1 as common (ground), IO0.3, IO0.5, IO0.7, IO1.1, IO1.3 used by the individual latches K1-5 switching respectively LPFs for 20m, 30m, 40m, 60m, 80m
//#define LPF_SWITCHING_DL2MAN_USDX_REV1 1 // Enable 3-band filter bank switching: latching relays wired to a PCA9536D GPIO extender on the PC4/PC5 I2C bus; relays are using IO0 as common (ground), IO1-IO3 used by the individual latches K1-3 switching respectively LPFs for 20m, 40m, 80m
//#define LPF_SWITCHING_WB2CBA_USDX_OCTOBAND 1 // Enable 8-band filter bank switching: non-latching relays wired to a MCP23008 GPIO extender on the PC4/PC5 I2C bus; relays are using GND as common (ground), GP0..7 used by the individual latches K1-8 switching respectively LPFs for 80m, 60m, 40m, 30m, 20m, 17m, 15m, 10m
//#define LPF_SWITCHING_PE1DDA_USDXDUO 14 // Enable 2-band filter bank switching: non-latching relay wired to pin PD5 (pin 11); specify as value the frequency in MHz for which (and above) the relay should be altered (e.g. put 14 to enable the relay at 14MHz and above to use the 20m LPF).
#define SI5351_ADDR 0x60 // SI5351A I2C address: 0x60 for SI5351A-B-GT, Si5351A-B04771-GT, MS5351M; 0x62 for SI5351A-B-04486-GT; 0x6F for SI5351A-B02075-GT; see here for other variants: https://www.silabs.com/TimingUtility/timing-download-document.aspx?OPN=Si5351A-B02075-GT&OPNRevision=0&FileType=PublicAddendum
//#define F_MCU 16000000 // 16MHz ATMEGA328P crystal (enable for unmodified Arduino Uno/Nano boards with 16MHz crystal). You may change this value to any other crystal frequency (up to 28MHz may work)
// Advanced configuration switches
//#define CONDENSED 1 // Display in 4 line mode (for OLED and LCD2004 modules)
#define TX_ENABLE 1 // Disable this for RX only (no transmit), e.g. to support uSDX for kids idea: https://groups.io/g/ucx/topic/81030243#6276
#define SEMI_QSK 1 // Just after keying the transmitter, keeps the RX muted for a short amount of time in the anticipation for continued keying
#define RIT_ENABLE 1 // Receive-In-Transit alternates the receiving frequency with an user-defined offset to compensate for any necessary tuning needed on receive
#define VOX_ENABLE 1 // Voice-On-Xmit which is switching the transceiver into transmit as soon audio is detected (above noise gate level)
//#define MOX_ENABLE 1 // Monitor-On-Xmit which is audio monitoring on speaker during transmit
//#define ONEBUTTON 1 // Use single (encoder) button to control full the rig; optionally use L/R buttons to completely replace rotory encoder function
//#define DEBUG 1 // for development purposes only (adds debugging features such as CPU, sample-rate measurement, additional parameters)
//#define TESTBENCH 1 // Tests RX chain by injection of sine wave, measurements results are sent over serial
// G8RDI removed for memory due to CAT
//#define TX_DELAY 1 // Enables a delay in the actual transmission to allow relay-switching to be completed before the power is applied (see also NTX, PTX definitions below for GPIO that can switch relay/PA)
//#define NTX 11 // Enables LOW on TX, used as PTT out to enable external PAs (a value of 11 means PB3 is used)
#define PTX 11 // Enables HIGH on TX, used as PTT out to enable external PAs (a value of 11 means PB3 is used)
//#define CLOCK 1 // Enables clock
// G8RDI removed to save memory #define:
//#define F_XTAL 20000000 // Enable this for uSDXDuO, 20MHz SI5351 crystal
//#define TX_CLK0_CLK1 1 // Enable this for uSDXDuO, i.e. when PA is driven by CLK0, CLK1 (not CLK2); NTX pin may be used for enabling the TX path (this is like RX pin, except that RX may also be used as attenuator)
//#define F_CLK2 12000000 // Enables a fixed CLK2 clock output of choice (only applicable when TX_CLK0_CLK1 is enabled), e.g. for up-converter or to clock UART USB device
// QCX pin defintions
#define LCD_D4 0 //PD0 (pin 2)
#define LCD_D5 1 //PD1 (pin 3)
#define LCD_D6 2 //PD2 (pin 4)
#define LCD_D7 3 //PD3 (pin 5)
#define LCD_EN 4 //PD4 (pin 6)
#define FREQCNT 5 //PD5 (pin 11)
#define ROT_A 6 //PD6 (pin 12)
#define ROT_B 7 //PD7 (pin 13)
#define RX 8 //PB0 (pin 14)
#define SIDETONE 9 //PB1 (pin 15)
#define KEY_OUT 10 //PB2 (pin 16)
#define SIG_OUT 11 //PB3 (pin 17)
#define DAH 12 //PB4 (pin 18)
#define DIT 13 //PB5 (pin 19)
#define AUDIO1 14 //PC0/A0 (pin 23)
#define AUDIO2 15 //PC1/A1 (pin 24)
#define DVM 16 //PC2/A2 (pin 25)
#define BUTTONS 17 //PC3/A3 (pin 26)
#define LCD_RS 18 //PC4 (pin 27)
#define SDA 18 //PC4 (pin 27)
#define SCL 19 //PC5 (pin 28)
//#define NTX 11 //PB3 (pin 17)
//#define PTX 11 //PB3 (pin 17)
#ifdef SWAP_ROTARY
#undef ROT_A
#undef ROT_B
#define ROT_A 7 //PD7 (pin 13)
#define ROT_B 6 //PD6 (pin 12)
#endif
#if (defined(OLED_SSD1306) || defined(OLED_SH1106))
#define OLED 1
#endif
#if (defined(CAT) || defined(TESTBENCH)) && !(OLED)
#define _SERIAL 1 // Coexistence support for serial port and LCD on the same pins
#endif
#ifdef LPF_SWITCHING_DL2MAN_USDX_REV3_NOLATCH
#define LPF_SWITCHING_DL2MAN_USDX_REV3 1
#endif
#ifdef TX_CLK0_CLK1
#ifdef F_CLK2
#define TX1RX0 0b11111000
#define TX1RX1 0b11111000
#define TX0RX1 0b11111000
#define TX0RX0 0b11111011
#else //!F_CLK2
#define TX1RX0 0b11111100
#define TX1RX1 0b11111100
#define TX0RX1 0b11111100
#define TX0RX0 0b11111111
#endif //F_CLK2
#else //!TX_CLK0_CLK1
#define TX1RX0 0b11111011
#define TX1RX1 0b11111000
#define TX0RX1 0b11111100
#define TX0RX0 0b11111111
#endif //TX_CLK0_CLK1
#if defined(F_CLK2) && !defined(TX_CLK0_CLK1)
#error "TX_CLK0_CLK1 must be enabled in order to use F_CLK2."
#endif
#ifndef TX_ENABLE
#undef KEYER
#undef TX_DELAY
#undef SEMI_QSK
#undef RIT_ENABLE
#undef VOX_ENABLE
#undef MOX_ENABLE
#endif //!TX_ENABLE
#ifdef SWR_METER
float FWD;
float SWR;
float ref_V = 5 * 1.15;
static uint32_t stimer;
#define PIN_FWD A6
#define PIN_REF A7
#endif
/*
// UCX installation: On blank chip, use (standard Arduino Uno) fuse settings (E:FD, H:DE, L:FF), and use customized Optiboot bootloader for 20MHz clock, then upload via serial interface (with RX, TX and DTR lines connected to pin 1, 2, 3 respectively)
// UCX pin defintions
+#define SDA 3 //PD3 (pin 5)
+#define SCL 4 //PD4 (pin 6)
+#define ROT_A 6 //PD6 (pin 12)
+#define ROT_B 7 //PD7 (pin 13)
+#define RX 8 //PB0 (pin 14)
+#define SIDETONE 9 //PB1 (pin 15)
+#define KEY_OUT 10 //PB2 (pin 16)
+#define NTX 11 //PB3 (pin 17)
+#define DAH 12 //PB4 (pin 18)
+#define DIT 13 //PB5 (pin 19)
+#define AUDIO1 14 //PC0/A0 (pin 23)
+#define AUDIO2 15 //PC1/A1 (pin 24)
+#define DVM 16 //PC2/A2 (pin 25)
+#define BUTTONS 17 //PC3/A3 (pin 26)
// In addition set:
#define OLED 1
#define ONEBUTTON 1
#define ONEBUTTON_INV 1
#undef DEBUG
adjust I2C and I2C_ ports,
ssb_cap=1; dsp_cap=2;
#define _DELAY() for(uint8_t i = 0; i != 5; i++) asm("nop");
#define F_XTAL 20004000
#define F_CPU F_XTAL
*/
//FUSES = { .low = 0xFF, .high = 0xD6, .extended = 0xFD }; // Fuse settings should be set at programming (Arduino IDE > Tools > Burn bootloader)
#if(ARDUINO < 10810)
#if (ARDUINO != 10607) // G8RDI mod - IDE 2.0.1 has its version set to 10607, an IDE bug since fixed.
# error "Unsupported Arduino IDE version, use Arduino IDE 1.8.10 or later from https://www.arduino.cc/en/software"
#endif
#endif
#if !(defined(ARDUINO_ARCH_AVR))
#error "Unsupported architecture, select Arduino IDE > Tools > Board > Arduino AVR Boards > Arduino Uno."
#endif
#if(F_CPU != 16000000)
#error "Unsupported clock frequency, Arduino IDE must specify 16MHz clock; alternate crystal frequencies may be specified with F_MCU."
#endif
#undef F_CPU
#define F_CPU 20007000 // Actual crystal frequency of 20MHz XTAL1, note that this declaration is just informative and does not correct the timing in Arduino functions like delay(); hence a 1.25 factor needs to be added for correction.
#ifndef F_MCU
#define F_MCU 20000000 // 20MHz ATMEGA328P crystal
#endif
extern char __bss_end;
static int freeMemory() { char* sp = reinterpret_cast<char*>(SP); return sp - &__bss_end; } // see: http://www.nongnu.org/avr-libc/user-manual/malloc.html
#ifdef CAT_EXT
volatile uint8_t cat_key = 0;
uint8_t _digitalRead(uint8_t pin) { // reads pin or (via CAT) artificially overriden pins
serialEvent(); // allows CAT update
if (cat_key) { return (pin == BUTTONS) ? ((cat_key & 0x07) > 0) : (pin == DIT) ? ~cat_key & 0x10 : (pin == DAH) ? ~cat_key & 0x20 : 0; } // overrides digitalRead(DIT, DAH, BUTTONS);
return digitalRead(pin);
}
#else
#define _digitalRead(x) digitalRead(x)
#endif //CAT_EXT
//#define ONEBUTTON_INV 1 // Encoder button goes from PC3 to GND (instead PC3 to 5V, with 10k pull down)
#ifdef ONEBUTTON_INV
uint8_t inv = 1;
#else
uint8_t inv = 0;
#endif
//#ifdef KEYER
// Iambic Morse Code Keyer Sketch, Contribution by Uli, DL2DBG. Copyright (c) 2009 Steven T. Elliott Source: http://openqrp.org/?p=343, Trimmed by Bill Bishop - wrb[at]wrbishop.com. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library 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 Lesser General Public License for more details: Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
// keyerControl bit definitions
#define DIT_L 0x01 // Dit latch
#define DAH_L 0x02 // Dah latch
#define DIT_PROC 0x04 // Dit is being processed
#define PDLSWAP 0x08 // 0 for normal, 1 for swap
#define IAMBICB 0x10 // 0 for Iambic A, 1 for Iambic B
#define IAMBICA 0x00 // 0 for Iambic A, 1 for Iambic B
#define SINGLE 2 // Keyer Mode 0 1 -> Iambic2 2 ->SINGLE
int keyer_speed = 25;
static unsigned long ditTime; // No. milliseconds per dit
static uint8_t keyerControl;
static uint8_t keyerState;
static uint8_t keyer_mode = 2; //-> SINGLE
static uint8_t keyer_swap = 0; //-> DI/DAH
static uint32_t ktimer;
static int Key_state;
int debounce;
enum KSTYPE { IDLE, CHK_DIT, CHK_DAH, KEYED_PREP, KEYED, INTER_ELEMENT }; // State machine states
void update_PaddleLatch() // Latch dit and/or dah press, called by keyer routine
{
if (_digitalRead(DIT) == LOW) {
keyerControl |= keyer_swap ? DAH_L : DIT_L;
}
if (_digitalRead(DAH) == LOW) {
keyerControl |= keyer_swap ? DIT_L : DAH_L;
}
}
void loadWPM(int wpm) // Calculate new time constants based on wpm value
{
#if(F_MCU != 20000000)
ditTime = (1200ULL * F_MCU / 16000000) / wpm; //ditTime = 1200/wpm; compensated for F_CPU clock (running in a 16MHz Arduino environment)
#else
ditTime = (1200 * 5 / 4) / wpm; //ditTime = 1200/wpm; compensated for 20MHz clock (running in a 16MHz Arduino environment)
#endif
}
//#endif //KEYER
static uint8_t practice = false; // Practice mode
static int8_t prev_mode;
volatile uint8_t cat_active = 0; // Run-time set when serial data being processed to keep shared LCD pins controlled
volatile uint32_t rxend_event = 0;
volatile uint8_t vox = 0;
#include <avr/sleep.h>
#include <avr/wdt.h>
//#define _I2C_DIRECT_IO 1 // Enables communications that is not using the standard I/O pull-down approach with pull-up resistors, instead I/O is directly driven with 0V/5V
class I2C_ { // Secundairy I2C class used by I2C LCD/OLED, uses alternate pins: PD2 (SDA) and PD3 (SCL)
public:
#if(F_MCU > 20900000)
#ifdef OLED_SH1106
#define _DELAY() for(uint8_t i = 0; i != 9; i++) asm("nop");
#else
#ifdef OLED_SSD1306
#define _DELAY() for(uint8_t i = 0; i != 6; i++) asm("nop");
#else // other (I2C_LCD)
#define _DELAY() for(uint8_t i = 0; i != 7; i++) asm("nop");
#endif
#endif
#else // slow F_MCU
#ifdef OLED_SH1106
#define _DELAY() for(uint8_t i = 0; i != 8; i++) asm("nop");
#else
#ifdef OLED_SSD1306
#define _DELAY() for(uint8_t i = 0; i != 4; i++) asm("nop"); // 4=731kb/s
#else // other (I2C_LCD)
#define _DELAY() for(uint8_t i = 0; i != 5; i++) asm("nop");
#endif
#endif
#endif // F_MCU
#define _I2C_SDA (1<<2) // PD2
#define _I2C_SCL (1<<3) // PD3
#ifdef _I2C_DIRECT_IO
#define _I2C_INIT() _I2C_SDA_HI(); _I2C_SCL_HI(); DDRD |= (_I2C_SDA | _I2C_SCL); // direct I/O (no need for pull-ups)
#define _I2C_SDA_HI() PORTD |= _I2C_SDA;
#define _I2C_SDA_LO() PORTD &= ~_I2C_SDA;
#define _I2C_SCL_HI() PORTD |= _I2C_SCL; _DELAY();
#define _I2C_SCL_LO() PORTD &= ~_I2C_SCL; _DELAY();
#else // !_I2C_DIRECT_IO
#define _I2C_INIT() PORTD &= ~_I2C_SDA; PORTD &= ~_I2C_SCL; _I2C_SDA_HI(); _I2C_SCL_HI(); // open-drain
#define _I2C_SDA_HI() DDRD &= ~_I2C_SDA;
#define _I2C_SDA_LO() DDRD |= _I2C_SDA;
#define _I2C_SCL_HI() DDRD &= ~_I2C_SCL; _DELAY();
#define _I2C_SCL_LO() DDRD |= _I2C_SCL; _DELAY();
#endif // !_I2C_DIRECT_IO
#define _I2C_START() _I2C_SDA_LO(); _DELAY(); _I2C_SCL_LO(); // _I2C_SDA_HI();
#define _I2C_STOP() _I2C_SDA_LO(); _I2C_SCL_HI(); _I2C_SDA_HI();
#define _I2C_SUSPEND() //_I2C_SDA_LO(); // SDA_LO to allow re-use as output port
#define _SendBit(data, bit) \
if(data & 1 << bit){ \
_I2C_SDA_HI(); \
} else { \
_I2C_SDA_LO(); \
} \
_I2C_SCL_HI(); \
_I2C_SCL_LO();
inline void start() { _I2C_INIT(); _I2C_START(); };
inline void stop() { _I2C_STOP(); _I2C_SUSPEND(); };
inline void SendByte(uint8_t data) {
_SendBit(data, 7);
_SendBit(data, 6);
_SendBit(data, 5);
_SendBit(data, 4);
_SendBit(data, 3);
_SendBit(data, 2);
_SendBit(data, 1);
_SendBit(data, 0);
_I2C_SDA_HI(); // recv ACK
_DELAY(); //
_I2C_SCL_HI();
_I2C_SCL_LO();
}
void SendRegister(uint8_t addr, uint8_t* data, uint8_t n) {
start();
SendByte(addr << 1);
while (n--) SendByte(*data++);
stop();
}
//void SendRegister(uint8_t addr, uint8_t val){ SendRegister(addr, &val, 1); }
void begin() {};
void beginTransmission(uint8_t addr) { start(); SendByte(addr << 1); };
bool write(uint8_t byte) { SendByte(byte); return 1; };
uint8_t endTransmission() { stop(); return 0; };
};
I2C_ Wire;
//#include <Wire.h>
uint8_t backlight = 8;
//#define RS_HIGH_ON_IDLE 1 // Experimental LCD support where RS line is high on idle periods to comply with SDA I2C standard.
class LCD : public Print { // inspired by: http://www.technoblogy.com/show?2BET
public: // LCD1602 display in 4-bit mode, RS is pull-up and kept low when idle to prevent potential display RFI via RS line
#define _dn 0 // PD0 to PD3 connect to D4 to D7 on the display
#define _en 4 // PD4 - MUST have pull-up resistor
#define _rs 4 // PC4 - MUST have pull-up resistor
#define RS_PULLUP 1 // Use pullup on RS line, ensures compliancy to the absolute maximum ratings for the si5351 sda input that is shared with rs pin of lcd
#ifdef RS_PULLUP
#define LCD_RS_HI() DDRC &= ~(1 << _rs); asm("nop"); // RS high (pull-up)
#define LCD_RS_LO() DDRC |= 1 << _rs; // RS low (pull-down)
#else
#define LCD_RS_LO() PORTC &= ~(1 << _rs); // RS low
#define LCD_RS_HI() PORTC |= (1 << _rs); // RS high
#endif //RS_PULLUP
#define LCD_EN_LO() PORTD &= ~(1 << _en); // EN low
#define LCD_EN_HI() PORTD |= (1 << _en); // EN high
#define LCD_PREP_NIBBLE(b) (PORTD & ~(0xf << _dn)) | (b) << _dn | 1 << _en // Send data and enable high
uint8_t _cols;
void begin(uint8_t x = 0, uint8_t y = 0) { // Send command , make sure at least 40ms after power-up before sending commands
//bool reinit = (x == 0) && (y == 0);
#ifdef LCD_I2C
#define PCF_ADDR 0x27 // LCD I2C address where PCF8574 addess selection A0, A1, A2 are all open
#define PCF_RS 0x01
#define PCF_RW 0x02 // the 0xF0 bits are used for 4-bit data to the display.
#define PCF_EN 0x04
#define PCF_BACKLIGHT 0x08
Wire.beginTransmission(PCF_ADDR);
Wire.write(0);
Wire.endTransmission();
delayMicroseconds(50000);
#else //!LCD_I2C
DDRD |= 0xf << _dn | 1 << _en; // Make data, EN outputs
DDRC |= 1 << _rs;
//PORTC &= ~(1 << _rs); // Set RS low in case to support pull-down when DDRC is output
delayMicroseconds(50000); // *
LCD_RS_LO(); LCD_EN_LO();
#endif //!LCD_I2C
cmd(0x33); // Ensures display is in 8-bit mode
delayMicroseconds(4500); cmd(0x33); delayMicroseconds(4500); cmd(0x33); delayMicroseconds(150); // * Ensures display is in 8-bit mode
cmd(0x32); // Puts display in 4-bit mode
cmd(0x28); // * Function set: 2-line, 5x8
cmd(0x0c); // Display on
//if(reinit) return;
cmd(0x01); // Clear display
delay(3); // Allow to execute Clear on display [https://www.sparkfun.com/datasheets/LCD/HD44780.pdf, p.49, p58]
cmd(0x06); // * Entrymode: left, shift-dec
}
#ifdef LCD_I2C
void nib(uint8_t b, bool isData) { // Send four bit nibble to display
b = (b << 4) | ((backlight) ? PCF_BACKLIGHT : 0) | ((isData) ? PCF_RS : 0);
Wire.write(b | PCF_EN); // write command EN HI
delayMicroseconds(4); // enable pulse must be >450ns
Wire.write(b); // write command EN LO
delayMicroseconds(60); // commands need > 37us to settle
Wire.write(b); // must write for some unknown reason
}
void cmd(uint8_t b) {
Wire.beginTransmission(PCF_ADDR);
nib(b >> 4, false); nib(b, false);
Wire.endTransmission();
}
size_t write(uint8_t b) {
Wire.beginTransmission(PCF_ADDR);
nib((b >> 4), true); nib((b), true);
Wire.endTransmission();
}
#else //!LCD_I2C
// Since LCD is using PD0(RXD), PD1(TXD) pins in the data-path, some co-existence feature is required when using the serial port.
// The following functions are temporarily disabling the serial port when LCD writes happen, and make sure that serial transmission is ended.
// To prevent that LCD writes are received by the serial receiver, PC2 is made HIGH during writes to pull-up TXD via a diode.
// The RXD, TXD lines are connected to the host via 1k resistors, a 1N4148 is placed between PC2 (anode) and the TXD resistor.
// NOISE LEAK INTO RX!!! G8RDI NOTE
// There are two drawbacks when continuous LCD writes happen:
// 1. noise is leaking via the AREF pull-ups into the receiver
// 2. serial data cannot be received
void pre() {
#ifdef _SERIAL
if (!vox) if (cat_active) { Serial.flush(); for (; millis() < rxend_event;)wdt_reset(); PORTC |= 1 << 2; DDRC |= 1 << 2; } UCSR0B &= ~((1 << RXEN0) | (1 << TXEN0)); // Complete serial TX and RX; mask PD1 LCD data-exchange by pulling-up TXD via PC2 HIGH; enable PD0/PD1, disable serial port
#endif
noInterrupts(); // ***!!!*** do not allow LCD tranfer to be interrupted, to prevent backlight to lighten-up
}
void post() {
///if(backlight) PORTD |= 0x08; else PORTD &= ~0x08; // Backlight control
if (backlight)
PORTD |= BACKLIGHT_PIN;
else
PORTD &= ~BACKLIGHT_PIN; // Backlight control - G8RDI MOD
#ifdef _SERIAL
//UCSR0B |= (1<<RXEN0)|(1<<TXEN0); if(!vox) if(cat_active){ DDRC &= ~(1<<2); } // Enable serial port, disable PD0, PD1; disable PC2
UCSR0B |= (1 << RXEN0) | (1 << TXEN0); if (!vox) if (cat_active) { PORTC &= ~(1 << 2); } // Enable serial port, disable PD0, PD1; PC2 LOW to prevent CAT TX disruption via MIC input
#endif
interrupts();
}
#ifdef RS_HIGH_ON_IDLE
void cmd(uint8_t b) {
pre();
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
LCD_RS_LO();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
asm("nop"); asm("nop"); // Keep RS low, but complete enable cycle (should be 500ns)
LCD_EN_LO();
LCD_RS_HI();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
}
size_t write(uint8_t b) { // Write data: send nibbles while RS high
pre();
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
asm("nop"); asm("nop"); // Keep RS high, but complete enable cycle (should be 500ns)
LCD_EN_LO();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
return 1;
}
#else //!RS_HIGH_ON_IDLE
void nib(uint8_t b) { // Send four bit nibble to display
pre();
PORTD = LCD_PREP_NIBBLE(b); // Send data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns
delayMicroseconds(4);
LCD_EN_LO();
post();
//delayMicroseconds(52); // Execution time
delayMicroseconds(60); // Execution time
}
void cmd(uint8_t b) { nib(b >> 4); nib(b & 0xf); } // Write command: send nibbles while RS low
size_t write(uint8_t b) { // Write data: send nibbles while RS high
pre();
//LCD_EN_HI(); // Complete Enable cycle must be at least 500ns (so start early)
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns; ATMEGA clock-cycle is 50ns (so at least 5 cycles)
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
LCD_RS_LO();
//asm("nop"); asm("nop"); // Complete Enable cycle must be at least 500ns
//PORTD = nibl; // Send low nibble data and enable high
//asm("nop"); // Enable high pulse width must be at least 230ns high, data-setup time 80ns; ATMEGA clock-cycle is 50ns (so at least 5 cycles)
LCD_RS_HI();
LCD_EN_LO();
LCD_RS_LO();
post();
delayMicroseconds(60); // Execution time (37+4)*1.25 us
return 1;
}
#endif // RS_HIGH_ON_IDLE
#endif //!LCD_I2C
#ifdef CONDENSED
void setCursor(uint8_t x, uint8_t y) { cmd(0x80 | (x + (uint8_t[]) { 0x00, 0x40, 0x00 + 20, 0x40 + 20 } [y])); } // ONLY for LCD2004 display
#else
void setCursor(uint8_t x, uint8_t y) { cmd(0x80 | (x + y * 0x40)); }
#endif
void cursor() { cmd(0x0e); }
void noCursor() { cmd(0x0c); }
void noDisplay() { cmd(0x08); }
void createChar(uint8_t l, uint8_t glyph[]) { cmd(0x40 | ((l & 0x7) << 3)); for (int i = 0; i != 8; i++) write(glyph[i]); }
};
/*
class LCD : public Print { // inspired by: http://www.technoblogy.com/show?2BET
public: // LCD1602 display in 4-bit mode, RS is pull-up and kept low when idle to prevent potential display RFI via RS line
#define _dn 0 // PD0 to PD3 connect to D4 to D7 on the display
#define _en 4 // PC4 - MUST have pull-up resistor
#define _rs 4 // PC4 - MUST have pull-up resistor
#define LCD_RS_HI() DDRC &= ~(1 << _rs); // RS high (pull-up)
#define LCD_RS_LO() DDRC |= 1 << _rs; // RS low (pull-down)
#define LCD_EN_LO() PORTD &= ~(1 << _en); // EN low
#define LCD_PREP_NIBBLE(b) (PORTD & ~(0xf << _dn)) | (b) << _dn | 1 << _en // Send data and enable high
void begin(uint8_t x, uint8_t y){ // Send command
DDRD |= 0xf << _dn | 1 << _en; // Make data, EN and RS pins outputs
PORTC &= ~(1 << _rs); // Set RS low in case to support pull-down when DDRC is output
delayMicroseconds(50000); // * At least 40ms after power rises above 2.7V before sending commands
LCD_RS_LO();
cmd(0x33); // Ensures display is in 8-bit mode
cmd(0x32); // Puts display in 4-bit mode
cmd(0x0e); // Display and cursor on
cmd(0x01); // Clear display
delay(3); // Allow to execute on display [https://www.sparkfun.com/datasheets/LCD/HD44780.pdf, p.49, p58]
}
void nib(uint8_t b){ // Send four bit nibble to display
PORTD = LCD_PREP_NIBBLE(b); // Send data and enable high
delayMicroseconds(4);
LCD_EN_LO();
delayMicroseconds(60); // Execution time (was: 37)
}
void cmd(uint8_t b){ nib(b >> 4); nib(b & 0xf); }// Write command: send nibbles while RS low
size_t write(uint8_t b){ // Write data: send nibbles while RS high
uint8_t nibh = LCD_PREP_NIBBLE(b >> 4); // Prepare high nibble data and enable high
uint8_t nibl = LCD_PREP_NIBBLE(b & 0xf); // Prepare low nibble data and enable high
PORTD = nibh; // Send high nibble data and enable high
LCD_RS_HI();
LCD_EN_LO();
PORTD = nibl; // Send low nibble data and enable high
LCD_RS_LO();
LCD_RS_HI();
LCD_EN_LO();
LCD_RS_LO();
delayMicroseconds(41); // Execution time
PORTD |= 0x02; // To support serial-interface keep LCD_D5 high, so that DVM is not pulled-down via D
return 1;
}
void setCursor(uint8_t x, uint8_t y){ cmd(0x80 | (x + y * 0x40)); }
void cursor(){ cmd(0x0e); }
void noCursor(){ cmd(0x0c); }
void noDisplay(){ cmd(0x08); }
void createChar(uint8_t l, uint8_t glyph[]){ cmd(0x40 | ((l & 0x7) << 3)); for(int i = 0; i != 8; i++) write(glyph[i]); }
};
*/
/*
#include <LiquidCrystal.h>
class LCD_ : public LiquidCrystal {
public: // QCXLiquidCrystal extends LiquidCrystal library for pull-up driven LCD_RS, as done on QCX. LCD_RS needs to be set to LOW in advance of calling any operation.
//LCD_(uint8_t rs = LCD_RS, uint8_t en = LCD_EN, uint8_t d4 = LCD_D4, uint8_t d5, = LCD_D5 uint8_t d6 = LCD_D6, uint8_t d7 = LCD_D7) : LiquidCrystal(rs, en, d4, d5, d6, d7){ };
LCD_() : LiquidCrystal(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7){ };
virtual size_t write(uint8_t value){ // overwrites LiquidCrystal::write() and re-implements LCD data writes
pinMode(LCD_RS, INPUT); // pull-up LCD_RS
write4bits(value >> 4);
write4bits(value);
pinMode(LCD_RS, OUTPUT); // pull-down LCD_RS
return 1;
};
void write4bits(uint8_t value){
digitalWrite(LCD_D4, (value >> 0) & 0x01);
digitalWrite(LCD_D5, (value >> 1) & 0x01);
digitalWrite(LCD_D6, (value >> 2) & 0x01);
digitalWrite(LCD_D7, (value >> 3) & 0x01);
digitalWrite(LCD_EN, LOW); // pulseEnable
delayMicroseconds(1);
digitalWrite(LCD_EN, HIGH);
delayMicroseconds(1); // enable pulse must be >450ns
digitalWrite(LCD_EN, LOW);
delayMicroseconds(100); // commands need > 37us to settle
};
};
*/
/* // 6x8 technoblogy font
const uint8_t font[]PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x5F, 0x00, 0x00, 0x00,
0x00, 0x07, 0x00, 0x07, 0x00, 0x00,
0x14, 0x7F, 0x14, 0x7F, 0x14, 0x00,
0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x00,
0x23, 0x13, 0x08, 0x64, 0x62, 0x00,
0x36, 0x49, 0x56, 0x20, 0x50, 0x00,
0x00, 0x08, 0x07, 0x03, 0x00, 0x00,
0x00, 0x1C, 0x22, 0x41, 0x00, 0x00,
0x00, 0x41, 0x22, 0x1C, 0x00, 0x00,
0x2A, 0x1C, 0x7F, 0x1C, 0x2A, 0x00,
0x08, 0x08, 0x3E, 0x08, 0x08, 0x00,
0x00, 0x80, 0x70, 0x30, 0x00, 0x00,
0x08, 0x08, 0x08, 0x08, 0x08, 0x00,
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,
0x20, 0x10, 0x08, 0x04, 0x02, 0x00,
0x3E, 0x51, 0x49, 0x45, 0x3E, 0x00,
0x00, 0x42, 0x7F, 0x40, 0x00, 0x00,
0x72, 0x49, 0x49, 0x49, 0x46, 0x00,
0x21, 0x41, 0x49, 0x4D, 0x33, 0x00,
0x18, 0x14, 0x12, 0x7F, 0x10, 0x00,
0x27, 0x45, 0x45, 0x45, 0x39, 0x00,
0x3C, 0x4A, 0x49, 0x49, 0x31, 0x00,
0x41, 0x21, 0x11, 0x09, 0x07, 0x00,
0x36, 0x49, 0x49, 0x49, 0x36, 0x00,
0x46, 0x49, 0x49, 0x29, 0x1E, 0x00,
0x00, 0x00, 0x14, 0x00, 0x00, 0x00,
0x00, 0x40, 0x34, 0x00, 0x00, 0x00,
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,
0x14, 0x14, 0x14, 0x14, 0x14, 0x00,
0x00, 0x41, 0x22, 0x14, 0x08, 0x00,
0x02, 0x01, 0x59, 0x09, 0x06, 0x00,
0x3E, 0x41, 0x5D, 0x59, 0x4E, 0x00,
0x7C, 0x12, 0x11, 0x12, 0x7C, 0x00,
0x7F, 0x49, 0x49, 0x49, 0x36, 0x00,
0x3E, 0x41, 0x41, 0x41, 0x22, 0x00,
0x7F, 0x41, 0x41, 0x41, 0x3E, 0x00,
0x7F, 0x49, 0x49, 0x49, 0x41, 0x00,
0x7F, 0x09, 0x09, 0x09, 0x01, 0x00,
0x3E, 0x41, 0x41, 0x51, 0x73, 0x00,
0x7F, 0x08, 0x08, 0x08, 0x7F, 0x00,
0x00, 0x41, 0x7F, 0x41, 0x00, 0x00,
0x20, 0x40, 0x41, 0x3F, 0x01, 0x00,
0x7F, 0x08, 0x14, 0x22, 0x41, 0x00,
0x7F, 0x40, 0x40, 0x40, 0x40, 0x00,
0x7F, 0x02, 0x1C, 0x02, 0x7F, 0x00,
0x7F, 0x04, 0x08, 0x10, 0x7F, 0x00,
0x3E, 0x41, 0x41, 0x41, 0x3E, 0x00,
0x7F, 0x09, 0x09, 0x09, 0x06, 0x00,
0x3E, 0x41, 0x51, 0x21, 0x5E, 0x00,
0x7F, 0x09, 0x19, 0x29, 0x46, 0x00,
0x26, 0x49, 0x49, 0x49, 0x32, 0x00,
0x03, 0x01, 0x7F, 0x01, 0x03, 0x00,
0x3F, 0x40, 0x40, 0x40, 0x3F, 0x00,
0x1F, 0x20, 0x40, 0x20, 0x1F, 0x00,
0x3F, 0x40, 0x38, 0x40, 0x3F, 0x00,
0x63, 0x14, 0x08, 0x14, 0x63, 0x00,
0x03, 0x04, 0x78, 0x04, 0x03, 0x00,
0x61, 0x59, 0x49, 0x4D, 0x43, 0x00,
0x00, 0x7F, 0x41, 0x41, 0x41, 0x00,
0x02, 0x04, 0x08, 0x10, 0x20, 0x00,
0x00, 0x41, 0x41, 0x41, 0x7F, 0x00,
0x04, 0x02, 0x01, 0x02, 0x04, 0x00,
0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
0x00, 0x03, 0x07, 0x08, 0x00, 0x00,
0x20, 0x54, 0x54, 0x78, 0x40, 0x00,
0x7F, 0x28, 0x44, 0x44, 0x38, 0x00,
0x38, 0x44, 0x44, 0x44, 0x28, 0x00,
0x38, 0x44, 0x44, 0x28, 0x7F, 0x00,
0x38, 0x54, 0x54, 0x54, 0x18, 0x00,
0x00, 0x08, 0x7E, 0x09, 0x02, 0x00,
0x18, 0xA4, 0xA4, 0x9C, 0x78, 0x00,
0x7F, 0x08, 0x04, 0x04, 0x78, 0x00,
0x00, 0x44, 0x7D, 0x40, 0x00, 0x00,
0x20, 0x40, 0x40, 0x3D, 0x00, 0x00,
0x7F, 0x10, 0x28, 0x44, 0x00, 0x00,
0x00, 0x41, 0x7F, 0x40, 0x00, 0x00,
0x7C, 0x04, 0x78, 0x04, 0x78, 0x00,
0x7C, 0x08, 0x04, 0x04, 0x78, 0x00,
0x38, 0x44, 0x44, 0x44, 0x38, 0x00,
0xFC, 0x18, 0x24, 0x24, 0x18, 0x00,
0x18, 0x24, 0x24, 0x18, 0xFC, 0x00,
0x7C, 0x08, 0x04, 0x04, 0x08, 0x00,
0x48, 0x54, 0x54, 0x54, 0x24, 0x00,
0x04, 0x04, 0x3F, 0x44, 0x24, 0x00,
0x3C, 0x40, 0x40, 0x20, 0x7C, 0x00,
0x1C, 0x20, 0x40, 0x20, 0x1C, 0x00,
0x3C, 0x40, 0x30, 0x40, 0x3C, 0x00,
0x44, 0x28, 0x10, 0x28, 0x44, 0x00,
0x4C, 0x90, 0x90, 0x90, 0x7C, 0x00,
0x44, 0x64, 0x54, 0x4C, 0x44, 0x00,
0x00, 0x08, 0x36, 0x41, 0x00, 0x00,
0x00, 0x00, 0x77, 0x00, 0x00, 0x00,
0x00, 0x41, 0x36, 0x08, 0x00, 0x00,
0x02, 0x01, 0x02, 0x04, 0x02, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 };
#define FONT_W 12//6
#define FONT_H 2
#define FONT_STRETCHV 1
#define FONT_STRETCHH 1//0
*/
#ifndef REMOVEFONT // G8RDI mod - FONT NOT COMPILED IF NOT USED
// C64 real
//G8RDI mod to free more prog memory (FONT NOT COMPILED IF NOT USED) //const uint8_t font[]PROGMEM = {
const uint8_t font[]PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // ' '
0x00, 0x00, 0x00, 0x4f, 0x4f, 0x00, 0x00, 0x00, // !
0x00, 0x07, 0x07, 0x00, 0x00, 0x07, 0x07, 0x00, // "
0x14, 0x7f, 0x7f, 0x14, 0x14, 0x7f, 0x7f, 0x14, // #
0x00, 0x24, 0x2e, 0x6b, 0x6b, 0x3a, 0x12, 0x00, // $
0x00, 0x63, 0x33, 0x18, 0x0c, 0x66, 0x63, 0x00, // %
0x00, 0x32, 0x7f, 0x4d, 0x4d, 0x77, 0x72, 0x50, // &
0x00, 0x00, 0x00, 0x04, 0x06, 0x03, 0x01, 0x00, // '
0x00, 0x00, 0x1c, 0x3e, 0x63, 0x41, 0x00, 0x00, // (
0x00, 0x00, 0x41, 0x63, 0x3e, 0x1c, 0x00, 0x00, // )
0x08, 0x2a, 0x3e, 0x1c, 0x1c, 0x3e, 0x2a, 0x08, // *
0x00, 0x08, 0x08, 0x3e, 0x3e, 0x08, 0x08, 0x00, // +
0x00, 0x00, 0x80, 0xe0, 0x60, 0x00, 0x00, 0x00, // ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, // -
0x00, 0x00, 0x00, 0x60, 0x60, 0x00, 0x00, 0x00, // .
0x00, 0x40, 0x60, 0x30, 0x18, 0x0c, 0x06, 0x02, // /
0x00, 0x3e, 0x7f, 0x49, 0x45, 0x7f, 0x3e, 0x00, // 0
0x00, 0x40, 0x44, 0x7f, 0x7f, 0x40, 0x40, 0x00, // 1
0x00, 0x62, 0x73, 0x51, 0x49, 0x4f, 0x46, 0x00, // 2
0x00, 0x22, 0x63, 0x49, 0x49, 0x7f, 0x36, 0x00, // 3
0x00, 0x18, 0x18, 0x14, 0x16, 0x7f, 0x7f, 0x10, // 4
0x00, 0x27, 0x67, 0x45, 0x45, 0x7d, 0x39, 0x00, // 5
0x00, 0x3e, 0x7f, 0x49, 0x49, 0x7b, 0x32, 0x00, // 6
0x00, 0x03, 0x03, 0x79, 0x7d, 0x07, 0x03, 0x00, // 7
0x00, 0x36, 0x7f, 0x49, 0x49, 0x7f, 0x36, 0x00, // 8
0x00, 0x26, 0x6f, 0x49, 0x49, 0x7f, 0x3e, 0x00, // 9
0x00, 0x00, 0x00, 0x24, 0x24, 0x00, 0x00, 0x00, // :
0x00, 0x00, 0x80, 0xe4, 0x64, 0x00, 0x00, 0x00, // ;