Vous êtes sur la page 1sur 124

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
------------------------------------------------------------------
' si5351a DDS Oscillator with Nokia 3310 LCD display
' Author: Andrew Woodfield ZL2PD
' Date: February 2016
' File Name: si5351VFOvxx.bas (where xx is version number)
'
' Released under the terms and conditions of the
' Creative Commons Public License ("CCPL")
' See creativecommons.org for details
'
' Commercial use is prohibited without written permission
' from the author
'
'------------------------------------------------------------------
' Version 11:
'
' V01: Initialised m328, startup code
' V02: Setup 3310 lcd
' V03: Send some text and graphics to the lcd
' V04: Added rotary encoder
' V05: Display routines
' V06: Modified ADC to improve encoder operation
' V07: Added si5351 code
' V08: Display routine updated to reflect step, RIT, Lock, PTT etc
' V09: Added vfo #2, a/b swap, rit, ifoffset, high/low injection
' V10: Added band memories, startup freqs, A=B, Band up/down, BCD out
' V11: Added S-meter
'
'------------------------------------------------------------------
' Credits:
'
' Nokia-3310 LCD code and test routines are from Mrshilov 2014
' si5351 code is adapted from OE1CGS and Jason Milldrum C code
'
' Rotary encoder software is adapted from:
' http://dl6gl.de/software/drehencoder-mit-bascom
'
'------------------------------------------------------------------
' Program Description:

' An ATmega328 is used to control a Silicon Labs si5351a 3-output DDS


' DDS oscillator and Nokia 3310 compatible 84x48 graphics LCD display
' with a rotary encoder and various pushbuttons. Four pins provide
' BCD-coded outputs for switching external lowpass and/or bandpass
' filters. RIT and S-meter analog inputs are also supported.
'
'------------------------------------------------------------------
' Fuse settings
'
' LOCK Byte: 0ffh (No locks)
' EXTd Byte: 0ffh (BOD disabled)
' HIGH Byte: 0d9h
' RSTDISBL 1 Not disabled
' DWEN 1 DW not enabled
' SPIEN 0 SPI programming enabled
' WDTON 1 Watchdog timer off
' EESAVE 1 EEPROM not preserved in erase
' BOOTSZ1 0 Boot ROM size
' BOOTSZ0 0
' BOOTRST 1 No boot vector at startup
'
' LOW Byte: 0e2h
' CKDIV8 1 NOT divided by 8
' CKOUT 1 Not enabled
' SUT1 1 Slow rising power
' SUT0 0
' CKSEL3 0 8 MHz internal RC clock
' CKSEL2 0
' CKSEL1 1
' CKSEL0 0
'
'------------------------------------------------------------------
' Compiler Directives (some fundamental hardware issues)

$regfile = "m328def.dat"
$crystal = 8000000 '8MHz internal RC clock

$hwstack = 128
$swstack = 64
$framesize = 128

'*******************************************************************************
$lib "glcd-Nokia3310.lib"

Config Graphlcd = 128x64sed , A0 = Portd.5 , Si = Portd.6 , Sclk = Portd.7

'-------------------------------------------------------------------------------
' Declare Variables
Dim Mhz As String * 2 'display subroutine variables
Dim Khz As String * 3
Dim Hz As String * 3
Dim Lastftext As String * 6 'for display of freqb khz & Hz digits
Dim Freqtxt As String * 8
Dim Fcnt As Byte

Dim Temp As Byte 'temporary variable used in sendfreq and sbarplot routines

Dim Frequency As Dword 'four byte unsigned variable


Dim Freqb As Dword 'second vfo frequency
Dim Fswap As Dword 'temp store for freq swap
Dim Gfreq As Dword 'calculates si5351 VFO output frequency
Dim Bfofreq As Dword 'bfo/cio frequency for output 2

Dim Stepsize As Byte

Dim Spin As Byte 'Encoder rotation (encoder incr = 2, decr = 1, no change = 0)


Dim Delta As Dword

Dim Encval As Byte 'current rotary encoder value


Dim Lastencoder As Byte 'previous rotary encoder value

Dim Update As Bit 'bit is set (1) if a switch or encoder has changed
Dim Upwards As Bit 'direction of band change

Dim Vrold As Word '10-bit adc result from RIT control


Dim Vrnew As Word

Dim Vadc As Word 'for adc read routine


Dim Vadcl As Byte At Vadc Overlay
Dim Vadch As Byte At Vadc + 1 Overlay

Dim Ritoffset As Word 'used to calculate required RIT offset

Dim Smval As Word 'saves a2d result from s-meter input

Dim Bandpointer As Byte 'identifies currently selected amateur band

Dim Rit As Bit 'preserve status of RIT Off=0, On=1


Dim Lock As Bit 'holds status of Lock switch Off=0, On=1
Dim Modetype As Byte 'holds mode (1=LSB, 2=USB, 3=CW)
Dim Oldptt As Bit 'preserves state of last ptt condition
Dim Adctype As Bit 'adc pointer 0=RIT and 1=s-meter

Dim Fvco As Dword 'si5351 vco frequency


Dim Outdivider As Dword '4,6,8-900
Dim Rdivider As Byte 'Normally 1 unless freqeuncy<1MHz then 1,2,4,8,16,32,64 or 128 only
Dim Rbyte As Byte 'used as temp store before final data load to si5351
Dim Afactor As Dword
Dim Bfactor As Dword
Dim Fvalue As Single 'used to determine accuracy of fractional calculation
Dim Finteger As Dword

Dim Ms0p1 As Dword '18-bit MS0_P1 si5351 parameters for Output 0


Dim M0p11 As Byte At Ms0p1 Overlay 'mirrored variables for dds calculation
Dim M0p12 As Byte At Ms0p1 + 1 Overlay
Dim M0p13 As Byte At Ms0p1 + 2 Overlay
Dim M0p14 As Byte At Ms0p1 + 3 Overlay 'most significant byte for ms0p1

'Note: MS0_P2 and MS0_P3 are hard-coded as 0 and 1 respectively so ignored during dimensioning

'si5351 parameters for PLLA


Dim Msnap1 As Dword '18-bit variable for MSNA_P1
Dim Ms11 As Byte At Msnap1 Overlay 'mirrored variables for dds calculation
Dim Ms12 As Byte At Msnap1 + 1 Overlay
Dim Ms13 As Byte At Msnap1 + 2 Overlay
Dim Ms14 As Byte At Msnap1 + 3 Overlay 'most significant byte

Dim Msnap2 As Dword '20-bit variable for MSNA_P2


Dim Ms21 As Byte At Msnap2 Overlay 'mirrored variables for dds calculation
Dim Ms22 As Byte At Msnap2 + 1 Overlay
Dim Ms23 As Byte At Msnap2 + 2 Overlay
Dim Ms24 As Byte At Msnap2 + 3 Overlay 'most significant byte

Dim Msnap3 As Dword '20-bit variable for MSNA_P3


Dim Ms31 As Byte At Msnap3 Overlay 'mirrored variables for dds calculation
Dim Ms32 As Byte At Msnap3 + 1 Overlay
Dim Ms33 As Byte At Msnap3 + 2 Overlay
Dim Ms34 As Byte At Msnap3 + 3 Overlay 'most significant byte

Dim Regaddr As Byte , Regdata As Byte 'for i2c calls

' declare band memories


Dim 160as As Dword 'memory for 160m vfo a
Dim 160bs As Dword 'and vfo b
Dim 80as As Dword
Dim 80bs As Dword
Dim 40as As Dword
Dim 40bs As Dword
Dim 30as As Dword
Dim 30bs As Dword
Dim 20as As Dword
Dim 20bs As Dword
Dim 17as As Dword
Dim 17bs As Dword
Dim 15as As Dword
Dim 15bs As Dword
Dim 12as As Dword
Dim 12bs As Dword
Dim 10as As Dword
Dim 10bs As Dword

' Declare Constants

Const Xtal = 25002900 '**NOTE: Value may be adjusted during alignment ***
Const Cfactor = 1048575
Const Ifoffset = 8867000 'ifoffset=0 if no offset e.g. VFO used in SDR or DC receiver
Const High2low = 10500000 'VFO=frequency+IFoffset below this limit and frequency-IFoffset at and above
Const Usbcio = 8870000 'bfo/cio frequencies but not currently supported in this version
Const Lsbcio = 8864000
Const Cwcio = 8867000

'initial frequency configuration ***Check values for your country***


Const 160ma = 1840000 'initialise main vfo (band limits not currently supported)
Const 160mb = 1800000
Const 80ma = 3650000 '80m
Const 80mb = 3500000
Const 40ma = 7100000 '40m
Const 40mb = 7000000
Const 30ma = 10140000 '30m
Const 30mb = 10100000
Const 20ma = 14100000 '20m
Const 20mb = 14000000
Const 17ma = 18110000 '60m
Const 17mb = 18068000
Const 15ma = 21125000 '15m
Const 15mb = 21000000
Const 12ma = 24930000 '12m
Const 12mb = 24890000
Const 10ma = 28300000 '10m
Const 10mb = 28000000

' Declare Subroutines


Declare Sub Si5351init 'initialises si5351
Declare Sub Memload 'loads frequency data to ram from pgm memory
Declare Sub Displaylcd 'displays current oscillator frequency
Declare Sub Calculate 'converts current VFO freq to data for si5351
Declare Sub Calcbfo 'calculates BFO/CIO data for si5351
Declare Sub Sendfreq 'sends VFO freq data to si5351 CLK0
Declare Sub Sendbfo 'sends BFO/CIO freq data to si5351 CLK1
Declare Sub Siout(sireg As Byte , Sidata As Byte) 'for i2c write to si5351
Declare Sub Lockicon 'displays lock icon on lcd
Declare Sub Txicon 'displays tx on icon on lcd
Declare Sub Txofficon 'clears tx on icon
Declare Sub Riticon 'displays rit icon
Declare Sub Ritclricon 'clears rit icon
Declare Sub Bandchange 'stores and recalls band memories and updates BPF select out
Declare Sub Update_smeter 'takes current s-meter value and displays as a bargraph

'------------------------------------------------------------------------------------
' Hardware Setup
'
' See schematic for details
'
' Hardware Aliases:

Ptt Alias Pind.0 'ptt input


Stepkey Alias Pind.1 'step key on encoder
Ritkey Alias Pind.4 'rit on/off button
Bandkey Alias Pinb.7 'band up select button
Downbandkey Alias Pinb.4
Swapkey Alias Pinb.6 'VFO A/B button
Equalkey Alias Pinb.5 'VFO A=B button
Enca Alias Pind.2 'rotary encoder input A
Encb Alias Pind.3 'rotary encoder input B

' Initialise I/O (LCD already done above)

Config Pind.0 = Input


Config Pind.1 = Input
Config Pind.2 = Input
Config Pind.3 = Input
Config Pind.4 = Input
Config Pinb.4 = Input
Config Pinb.5 = Input
Config Pinb.6 = Input
Config Pinb.7 = Input

Set Portd.0 'and add pullups to all inputs


Set Portd.1
Set Portd.2
Set Portd.3
Set Portd.4
Set Portb.4
Set Portb.5
Set Portb.6
Set Portb.7

Config Pinb.3 = Output 'msb for BCD output for BPF/LPF selection
Config Pinb.2 = Output
Config Pinb.1 = Output
Config Pinb.0 = Output 'lsb

Config Portc = Output 'used for filter selection and i2c

Config Sda = Portc.4


Config Scl = Portc.5

Config Pinc.0 = Input 'for RIT (adc0)


Config Pinc.3 = Input 'for s-meter (adc3)

'***************************** Program *****************************************

'initialise adc
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc 'not required - starts automatically but...

'initialise i2c here...


I2cinit
Waitms 50 'for i2c to settle and for lcd RC reset time

Initlcd 'initialise the nokia 3310/5110 lcd


Showpic 0 , 0 , Splash 'splash screen
Wait 1
Cls

'initialise some variables


Frequency = 20ma 'initialise frequency (20m for now)
Freqb = 20mb 'and second vfo on same band
Stepsize = 1
Delta = 5
Spin = 0
Rit = 0 'rit is off
Lock = 0 'tuning lock is off
Bandpointer = 5 'point to 20m
Portb.3 = 0 'select required BPF and LPF (Active Low)
Portb.2 = 1
Portb.1 = 0
Portb.0 = 1
Modetype = 2 'select USB mode (1=LSB, 2=USB, 3=CW)
Oldptt = 1 'no ptt pressed
Bfofreq = Usbcio 'set carrier oscillator for usb

Reset Update 'clear the flags


Reset Upwards

Si5351init 'initialises si5351


Memload 'load freq memory
Calculate 'calculate the startup VFO value
Sendfreq 'and send it to the si5351 CLK0
Calcbfo 'calculate startup bfo/cio
Sendbfo 'and send data to si5351 CLK1
Displaylcd 'then display frequency and status on lcd

'Configure the 8-bit Timer0 interrupt for encoder


'Overflow time = overflow counts * prescale / uP clock frequency
'= 128 * 256 / 8,000,000 = 4mS = 244 Hz (8 MHz RC clock)
'Here prescale = 256 (Possible prescale factors: 8, 64, 256, 1024)

Config Timer0 = Timer , Prescale = 256


Const Presettimer0 = 192 'Timer preset value to initialize Timer0 i.e. 2mS)
On Timer0 Timer0_isr 'Timer0 interrupt service routine on overflow
Enable Timer0 'interrupt encoder
Enable Interrupts 'enable all interrupts
Do 'Start of main program

'S-meter
Smval = Getadc(3)
Update_smeter 'display s-meter value

'STEP
If Stepkey = 0 Then
Incr Stepsize
If Stepsize = 5 Then Stepsize = 1
If Stepsize = 1 Then Delta = 5 'Delta value sets tuning rates of 5, 100, 1000 and 10000 Hz/step
If Stepsize = 2 Then Delta = 100 '24 steps/turn encoder gives about 120Hz, 2.4, 24 and 240kHz/turn
If Stepsize = 3 Then Delta = 1000
If Stepsize = 4 Then Delta = 10000
Set Update 'to flag this change
End If

Keyup1:
If Stepkey = 0 Then Goto Keyup1 'and wait for button to be released

'RIT
If Ritkey = 0 And Bandkey = 1 Then 'if RIT button has been pressed then...
Toggle Rit
Set Update 'flag for display routine
End If

Keyup2:
If Ritkey = 0 And Bandkey = 1 Then Goto Keyup2 'and wait for button to be released

If Rit = 1 Then 'if rit is turned on...


Vrnew = Getadc(0) 'read rit setting
If Vrnew <> Vrold Then
Vrold = Vrnew
Set Update 'but only flag an update if the value has changed
End If
End If

'LOCK
If Pinb.6 = 0 And Pinb.7 = 0 Then 'if TUNING LOCK button has been pressed then ...
Toggle Lock 'invert lock state
Set Update 'flag for display routine
End If

Keyup3:
If Pinb.6 = 0 And Pinb.7 = 0 Then Goto Keyup3 'and wait for button to be released

'PTT
If Ptt <> Oldptt Then 'if PTT condition has changed then ...
Oldptt = Ptt 'save state for next time
Set Update 'flag for display routine
End If

'BAND UP
If Bandkey = 0 And Swapkey = 1 And Ritkey = 1 Then
Incr Bandpointer
Set Upwards 'note direction for bandchange routine
If Bandpointer = 10 Then Bandpointer = 1 'assumes 9 bands
Bandchange 'do the detailed tasks to change the band
Set Update 'flag the update
End If

Keyup4:
If Bandkey = 0 And Swapkey = 1 And Ritkey = 1 Then Goto Keyup4 'and wait for button to be released

'BAND DOWN
If Downbandkey = 0 Then
Decr Bandpointer
Reset Upwards 'do it just in case (user presses both band up and band down)
If Bandpointer = 0 Then Bandpointer = 9 'assumes 9 bands
Bandchange 'do the detailed tasks to change the band
Set Update 'flag the update
End If

Keyup5:
If Downbandkey = 0 Then Goto Keyup5 'and wait for button to be released
'MODE
If Pinb.7 = 0 And Pind.4 = 0 Then 'if MODE button has been pressed then ...
Incr Modetype
If Modetype = 4 Then Modetype = 1
Set Update 'flag for display routine
End If

Keyup6:
If Pinb.7 = 0 And Pind.4 = 0 Then Goto Keyup6 'and wait for button to be released

'SWAP A/B
If Swapkey = 0 And Pinb.7 = 1 Then 'if VFO A/B button has been pressed then ...
Disable Timer0 'disable interrupts briefly to avoid freq increment until swap done
Fswap = Frequency 'swap the frequencies
Frequency = Freqb
Freqb = Fswap
Set Update 'flag for display routine
Enable Timer0 'enable interrupts again
End If

Keyup7:
If Swapkey = 0 And Pinb.7 = 1 Then Goto Keyup7 'and wait for button to be released

'A=B
If Equalkey = 0 Then 'if VFO A=B button has been pressed then ...
Disable Timer0 'disable interrupts briefly to avoid freq increment until swap done
Freqb = Frequency 'write VFO A frequency to VFO B
Set Update 'flag for display routine
Enable Timer0 'enable interrupts again
End If

Keyup8:
If Equalkey = 0 Then Goto Keyup8 'and wait for button to be released

'Processing of rotary encoder


Disable Timer0 'Stop Timer0 interrupts

If Lock = 0 Then 'no update if tuning lock is enabled


Select Case Spin
Case 1 : Frequency = Frequency - Delta
Case 2 : Frequency = Frequency + Delta
End Select
End If

If Spin > 0 Or Update = 1 Then 'encoder or buttons changed so update display and si5351
Displaylcd 'show new frequency and status
Calculate 'calculate new VFO data
Sendfreq 'and send it to the si5351
Spin = 0 'Reset spin after processing
Reset Update 'and update flag
End If

Enable Timer0 'Restart Timer0

Loop 'End of main program

'**********************************************************************
Subroutines...

'**********************************************************************
'Interrupt service routine for Timer0 (encoder)

'Encoder interrupt on timer0 overflow


'Timer0 should be stopped in main program while data is being displayed
'Output: spin = 2: increment or
' = 1: decrement if encoder has changed
'if no change then spin = 0
'
'**********************************************************************
Timer0_isr:
Timer0 = Presettimer0 'Initialize Timer0
Encval = 0 'clear variable

Encval.0 = Enca 'input current encoder levels


Encval.1 = Encb 'so encval now holds current encoder state

'Check if encoder encoder pattern is changed


'spin = 1: decremented (turned counter clockwise)
'spin = 2: incremented (turned clockwise)
If Encval <> Lastencoder Then 'encoder is changed

If Encval = &B00000011 And Lastencoder = &B00000010 Then Spin = 2


If Encval = &B00000011 And Lastencoder = &B00000001 Then Spin = 1

'the following lines may be required to be ADDED to those above with some encoders
'which give an additional step between mechanical detent positions
' If encval = &B00000000 And Lastencoder = &B00000001 Then spin = 2
' If encval = &B00000000 And Lastencoder = &B00000010 Then spin = 1

Lastencoder = Encval 'update for next time


End If
Return

'**********************************************************************

'**********************************************************************
' Displays frequency on the 16*2 LCD to 1 Hz resolution
' with slide-rule type scale on top line
'**********************************************************************

Sub Displaylcd

'parse Frequency for display


Freqtxt = Str(frequency) 'convert frequency to text
Hz = Right(freqtxt , 3) 'extract Hz text
Freqtxt = Str(frequency) 'do it again because for some reason !! freqtxt gets blown away by right()
Fcnt = Len(freqtxt) - 3 'nbr of remaining digits (assumes lowest freq is 1000 Hz)

While Fcnt < 5


Insertchar Freqtxt , 1 , " " 'add spaces to start of text for formatting
Fcnt = Fcnt + 1
Wend

Mhz = Left(freqtxt , 2) 'now grab first two chars


Khz = Mid(freqtxt , 3 , 3) 'and mid three chars

Setfont Font12x16dig
Lcdat 1 , 1 , Mhz
Lcdat 1 , 30 , Khz 'note: start shifted left by 1 pixel

Setfont Font6x8
Lcdat 1 , 67 , Hz
If Frequency > 999999 Then
Lcdat 2 , 25 , "."
Else
Lcdat 2 , 25 , " "
End If

'parse freqb for display


Freqtxt = Str(freqb)
Fcnt = Len(freqtxt)

While Fcnt < 8


Insertchar Freqtxt , 1 , " " 'add spaces to start of text for formatting
Fcnt = Fcnt + 1
Wend

Mhz = Left(freqtxt , 2) 'now grab first two chars


Lcdat 3 , 7 , Mhz 'print MHz digits
Lcdat 3 , 19 , "." 'then a decimal point
Lastftext = Right(freqtxt , 6) 'grab the remainder
Lcdat 3 , 25 , Lastftext
Lcdat 3 , 67 , "MHz"

Lcdat 4 , 1 , "--------------"

Select Case Stepsize


Case 1 : Lcdat 5 , 1 , " 5 " , 1
Case 2 : Lcdat 5 , 1 , "100" , 1
Case 3 : Lcdat 5 , 1 , " 1k" , 1
Case 4 : Lcdat 5 , 1 , "10k" , 1
End Select

Select Case Modetype


Case 1 : Lcdat 5 , 25 , "LSB"
Case 2 : Lcdat 5 , 25 , "USB"
Case 3 : Lcdat 5 , 25 , " CW"
End Select

If Rit = 1 Then
Riticon
Else
Ritclricon
End If

If Ptt = 0 Then
Txicon 'display tx on icon
Else
Txofficon 'blank tx icon
End If

If Lock = 1 Then
Lockicon 'display lock symbol
Else
Lcdat 5 , 79 , " "
End If

Lcdat 6 , 1 , "S" 's-meter text

End Sub

'**********************************************************************
' subroutine to initialise the si5351 chip via I2C
'**********************************************************************

Sub Si5351init

Regaddr = 183
Regdata = 192
Call Siout(regaddr , Regdata) 'set xtal load cap to 10pF
Regaddr = 3
Regdata = 0
Call Siout(regaddr , Regdata) 'enable all outputs
Regaddr = 15
Regdata = 0
Call Siout(regaddr , Regdata) 'select xtal as PLL input source
Regaddr = 16
Regdata = 15 'PLLA is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 0 control register
Regaddr = 17
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 1 control register
Regaddr = 18
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 2 control register
Regaddr = 177
Waitms 10 'wait 10ms just in case

End Sub

'************************************************************************
' calculate data bytes for current VFO frequency to send to si5351a CLK0
'************************************************************************
Sub Calculate

Gfreq = Frequency 'gfreq holds value used for si5351

'RIT routine here


If Ptt = 1 And Rit = 1 Then 'if transceiver is in receive mode and RIT is enabled......
'convert vrnew to freq offset (0 - 1023 = -2500 to +2500Hz RIT range)
Ritoffset = Vrnew * 5 '0 - 5115
If Ritoffset > 2560 Then 'if vrnew>511...
Ritoffset = Ritoffset - 2560
Gfreq = Gfreq + Ritoffset
Else
Ritoffset = 2555 - Ritoffset
Gfreq = Gfreq - Ritoffset
End If
End If

'frequency offset routine


If Frequency < High2low Then 'high2low is operating frequency where high side changes to low side injection
Gfreq = Gfreq + Ifoffset
Else
Gfreq = Gfreq - Ifoffset
End If

Rdivider = 1 'initialise r

Outdivider = 900000000 \ Gfreq 'and outdivider

While Outdivider > 900


Rdivider = 2 * Rdivider
Outdivider = Outdivider \ 2
Wend

Fvco = Outdivider Mod 2 'check outdivider is an even number


If Fvco <> 0 Then Outdivider = Outdivider - 1 'using fvco as a temporary register

Fvco = Outdivider * Rdivider


Fvco = Fvco * Gfreq 'determine PLL frequency

Select Case Rdivider 'determine value of R44 bits [6:4]


Case 1 : Rbyte = 0 '000 0000
Case 2 : Rbyte = 16 '001 0000
Case 4 : Rbyte = 32 '010 0000
Case 8 : Rbyte = 48 '011 0000
Case 16 : Rbyte = 64 '100 0000
Case 32 : Rbyte = 80 '101 0000
Case 64 : Rbyte = 96 '110 0000
Case 128 : Rbyte = 112 '111 0000
End Select

Afactor = Fvco \ Xtal


Fvalue = Afactor * Xtal 'fvalue = fvco -afactor * xtal
Fvalue = Fvco - Fvalue 'a+b/c
Fvalue = Fvalue * Cfactor
Fvalue = Fvalue \ Xtal
Bfactor = Fvalue

Ms0p1 = 128 * Outdivider


Ms0p1 = Ms0p1 - 512
Fvalue = 128 * Bfactor
Fvalue = Fvalue \ Cfactor
Finteger = Fvalue
Msnap1 = 128 * Afactor
Msnap1 = Msnap1 + Finteger 'finteger used instead of fvalue for variable matching
Msnap1 = Msnap1 - 512
Msnap3 = Finteger * Cfactor 'using msnap3 as temp result buffer
Msnap2 = 128 * Bfactor
Msnap2 = Msnap2 - Msnap3
Msnap3 = Cfactor

'calculations are complete so now build and send the required output data bytes by calling sendfreq (About 20 bytes sent per

End Sub

'****************************************************************************
' calculate data bytes for current BFO/CIO frequency to send to si5351a CLK1
'****************************************************************************
Sub Calcbfo
Rdivider = 1 'initialise r

Outdivider = 900000000 \ Bfofreq 'and outdivider

While Outdivider > 900


Rdivider = 2 * Rdivider
Outdivider = Outdivider \ 2
Wend

Fvco = Outdivider Mod 2 'check outdivider is an even number


If Fvco <> 0 Then Outdivider = Outdivider - 1 'using fvco as a temporary register

Fvco = Outdivider * Rdivider


Fvco = Fvco * Bfofreq 'determine PLL frequency

Select Case Rdivider 'determine value of R52 bits [6:4]


Case 1 : Rbyte = 0 '000 0000
Case 2 : Rbyte = 16 '001 0000
Case 4 : Rbyte = 32 '010 0000
Case 8 : Rbyte = 48 '011 0000
Case 16 : Rbyte = 64 '100 0000
Case 32 : Rbyte = 80 '101 0000
Case 64 : Rbyte = 96 '110 0000
Case 128 : Rbyte = 112 '111 0000
End Select

Afactor = Fvco \ Xtal


Fvalue = Afactor * Xtal 'fvalue = fvco -afactor * xtal
Fvalue = Fvco - Fvalue 'a+b/c
Fvalue = Fvalue * Cfactor
Fvalue = Fvalue \ Xtal
Bfactor = Fvalue

Ms0p1 = 128 * Outdivider


Ms0p1 = Ms0p1 - 512
Fvalue = 128 * Bfactor
Fvalue = Fvalue \ Cfactor
Finteger = Fvalue
Msnap1 = 128 * Afactor
Msnap1 = Msnap1 + Finteger 'finteger used instead of fvalue for variable matching
Msnap1 = Msnap1 - 512
Msnap3 = Finteger * Cfactor 'using msnap3 as temp result buffer
Msnap2 = 128 * Bfactor
Msnap2 = Msnap2 - Msnap3
Msnap3 = Cfactor
'calculations are complete so now build and send the required output data bytes by calling sendbfo (About 20 bytes sent per

End Sub

'**********************************************************************
' send converted freq data to si5351 for CLK0 output
'**********************************************************************
Sub Sendfreq

Regaddr = 26
Call Siout(regaddr , Ms32) 'R26=msnap3[15:8]
Incr Regaddr
Call Siout(regaddr , Ms31) 'R27=msnap3[7:0]
Incr Regaddr
Call Siout(regaddr , Ms13) 'R28=msnap1[17:16] in R28[1:0] with R28[7:2]=0
Incr Regaddr
Call Siout(regaddr , Ms12) 'R29=msnap1[15:8]
Incr Regaddr
Call Siout(regaddr , Ms11) 'R30=msnap1[7:0]
Temp = Ms33 'temp[3:0]=msnap3[19:16]
Swap Temp 'temp[7:4]=msnap3[19:16]
Temp = Temp Or Ms23 'temp[7:4]=msnap3[19:16] and temp[3:0]=msnap2[19:16]
Incr Regaddr
Call Siout(regaddr , Temp) 'R31=(msnap3[19:16],msnap2[19:16])
Incr Regaddr
Call Siout(regaddr , Ms22) 'R32=msnap2[15:8]
Incr Regaddr
Call Siout(regaddr , Ms21) 'R33=msnap2[7:0]
Regaddr = 42
Regdata = 0
Call Siout(regaddr , Regdata) 'R42=MS0_P3[15:8]
Incr Regaddr
Regdata = 1
Call Siout(regaddr , Regdata) 'R43=MS0_P3[7:0]
Temp = Rbyte
Temp = Temp Or M0p13
Incr Regaddr
Call Siout(regaddr , Temp) 'R44=(0,R[7:4],MS0_P1[17:16])
Incr Regaddr
Call Siout(regaddr , M0p12) 'R45=MS0_P1[15:8]
Incr Regaddr
Call Siout(regaddr , M0p11) 'R46=MS0_P1[7:0]
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R47=(MS0_P3[19:16],MS0_P2[19:16]) and both are always 0
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R48=MS0_P2[15:8]=0 always
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R49=MS0_P2[7:0]=0 always
If Outdivider = 4 Then 'for output frequencies greater than 150MHz and less than 200MHz
Regaddr = 44
Regdata = 12
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
End If
Regaddr = 3
Regdata = 4
Call Siout(regaddr , Regdata) 'enable all outputs except CLK2
Regaddr = 16
Regdata = 79
Call Siout(regaddr , Regdata) 'enable CLK0, source=crystal, PLLA=integer mode, power level=8mA i.e. 0dB

End Sub

'**********************************************************************
' send converted freq data to si5351 for CLK1 output
'**********************************************************************
Sub Sendbfo

Regaddr = 34
Call Siout(regaddr , Ms32) 'R34=msnbp3[15:8]
Incr Regaddr
Call Siout(regaddr , Ms31) 'R35=msnbp3[7:0]
Incr Regaddr
Call Siout(regaddr , Ms13) 'R36=msnbp1[17:16] in R36[1:0] with R36[7:2]=0
Incr Regaddr
Call Siout(regaddr , Ms12) 'R37=msnbp1[15:8]
Incr Regaddr
Call Siout(regaddr , Ms11) 'R38=msnbp1[7:0]
Temp = Ms33 'temp[3:0]=msnbp3[19:16]
Swap Temp 'temp[7:4]=msnbp3[19:16]
Temp = Temp Or Ms23 'temp[7:4]=msnbp3[19:16] and temp[3:0]=msnbp2[19:16]
Incr Regaddr
Call Siout(regaddr , Temp) 'R39=(msnbp3[19:16],msnbp2[19:16])
Incr Regaddr
Call Siout(regaddr , Ms22) 'R40=msnbp2[15:8]
Incr Regaddr
Call Siout(regaddr , Ms21) 'R41=msnbp2[7:0]
Regaddr = 50
Regdata = 0
Call Siout(regaddr , Regdata) 'R50=MS1_P3[15:8]
Incr Regaddr
Regdata = 1
Call Siout(regaddr , Regdata) 'R51=MS1_P3[7:0]
Temp = Rbyte
Temp = Temp Or M0p13
Incr Regaddr
Call Siout(regaddr , Temp) 'R52=(0,R[7:4],MS1_P1[17:16])
Incr Regaddr
Call Siout(regaddr , M0p12) 'R53=MS1_P1[15:8]
Incr Regaddr
Call Siout(regaddr , M0p11) 'R54=MS1_P1[7:0]
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R55=(MS1_P3[19:16],MS1_P2[19:16]) and both are always 0
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R56=MS1_P2[15:8]=0 always
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R57=MS1_P2[7:0]=0 always
If Outdivider = 4 Then 'for output frequencies greater than 150MHz and less than 200MHz
Regaddr = 52
Regdata = 12
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
End If
Regaddr = 3
Regdata = 4
Call Siout(regaddr , Regdata) 'enable all outputs except CLK2
Regaddr = 17
Regdata = 111
Call Siout(regaddr , Regdata) 'enable CLK1, source=crystal, PLLB=integer mode, power level=8mA i.e. 0dB

End Sub

'**********************************************************************
' i2c transmission to si5351
'**********************************************************************
Sub Siout(sireg As Byte , Sidata As Byte)
'subroutine sends databyte to register_nbr at i2c address 192 (si5351 register read is from address 194)

'Write a single byte (slave address 192, register sireg, value sibyte)
I2cstart
I2cwbyte 192 'i2c address of si5351a
Waitus 1 'suggested delay from forum
I2cwbyte Sireg
Waitus 1
I2cwbyte Sidata
Waitus 1
I2cstop

End Sub

'**********************************************************************
' initialise band by band frequency storage in ram from initial freq
' data tables in program memory
'**********************************************************************
Sub Memload

160as = 160ma 'load ram memory for 160m vfo a


160bs = 160mb 'and then repeat for vfo b
80as = 80ma
80bs = 80mb
40as = 40ma
40bs = 40mb
30as = 30ma
30bs = 30mb
20as = 20ma
20bs = 20mb
17as = 17ma
17bs = 17mb
15as = 15ma
15bs = 15mb
12as = 12ma
12bs = 12mb
10as = 10ma
10bs = 10mb 'crude but effective!

End Sub

'**********************************************************************
' display the dial locked icon on lcd
'**********************************************************************
Sub Lockicon

Glcdcmd 32 'normal command


Glcdcmd 207 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 126 '8-bit vertical column symbol data
Glcddata 121
Glcddata 73
Glcddata 121
Glcddata 126
Glcddata 0

End Sub

'**********************************************************************
' display the Inverted Tx icon on lcd
'**********************************************************************
Sub Txicon

Glcdcmd 32 'normal command


Glcdcmd 197 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 255 '8-bit vertical column symbol data
Glcddata 253
Glcddata 129
Glcddata 253
Glcddata 175
Glcddata 223
Glcddata 175
Glcddata 255

End Sub

'**********************************************************************
' clear the Tx icon on lcd
'**********************************************************************
Sub Txofficon
Glcdcmd 32 'normal command
Glcdcmd 197 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 0 '8-bit vertical column symbol data
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0

End Sub

'**********************************************************************
' display the RIT icon on lcd
'**********************************************************************
Sub Riticon

Glcdcmd 32 'normal command


Glcdcmd 183 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 64 '8-bit vertical column symbol data
Glcddata 112
Glcddata 124
Glcddata 127
Glcddata 124
Glcddata 112
Glcddata 64
Glcddata 8
Glcddata 126
Glcddata 9
Glcddata 2
Glcddata 0

End Sub

'**********************************************************************
' clear the RIT icon on lcd
'**********************************************************************
Sub Ritclricon

Glcdcmd 32 'normal command


Glcdcmd 183 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 0 '8-bit vertical column symbol data
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0

End Sub

'**********************************************************************
' do the detailed work required to change the selected band
'**********************************************************************
Sub Bandchange

Select Case Bandpointer 'determine value of R44 bits [6:4]


Case 1 : '160m
If Upwards = 1 Then 'save previous vfo frequencies
10as = Frequency 'these if the BAND UP key was pressed
10bs = Freqb
Else
80as = Frequency 'or these if the BAND DOWN key was pressed
80bs = Freqb
End If
Frequency = 160as 'load freq for main vfo (and add band limits later?)
Freqb = 160bs 'and for second vfo
Portb.3 = 0 'configure BPF select lines
Portb.2 = 0
Portb.1 = 0
Portb.0 = 1
Modetype = 1 'LSB
Case 2 :
If Upwards = 1 Then
160as = Frequency
160bs = Freqb
Else
40as = Frequency
40bs = Freqb
End If
Frequency = 80as '80m
Freqb = 80bs
Portb.3 = 0
Portb.2 = 0
Portb.1 = 1
Portb.0 = 0
Modetype = 1
Case 3 :
If Upwards = 1 Then
80as = Frequency
80bs = Freqb
Else
30as = Frequency
30bs = Freqb
End If
Frequency = 40as '40m
Freqb = 40bs
Portb.3 = 0
Portb.2 = 0
Portb.1 = 1
Portb.0 = 1
Modetype = 1
Case 4 :
If Upwards = 1 Then
40as = Frequency
40bs = Freqb
Else
20as = Frequency
20bs = Freqb
End If
Frequency = 30as '30m
Freqb = 30bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 0
Portb.0 = 0
Modetype = 1
Case 5 :
If Upwards = 1 Then
30as = Frequency
30bs = Freqb
Else
17as = Frequency
17bs = Freqb
End If
Frequency = 20as '20m
Freqb = 20bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 0
Portb.0 = 1
Modetype = 1
Case 6 :
If Upwards = 1 Then
20as = Frequency
20bs = Freqb
Else
15as = Frequency
15bs = Freqb
End If
Frequency = 17as '17m
Freqb = 17bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 1
Portb.0 = 0
Modetype = 2 'USB
Case 7 :
If Upwards = 1 Then
17as = Frequency
17bs = Freqb
Else
12as = Frequency
12bs = Freqb
End If
Frequency = 15as '15m
Freqb = 15bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 1
Portb.0 = 1
Modetype = 2
Case 8 :
If Upwards = 1 Then
15as = Frequency
15bs = Freqb
Else
10as = Frequency
10bs = Freqb
End If
Frequency = 12as '12m
Freqb = 12bs
Portb.3 = 1
Portb.2 = 0
Portb.1 = 0
Portb.0 = 0
Modetype = 2
Case 9 :
If Upwards = 1 Then
12as = Frequency
12bs = Freqb
Else
160as = Frequency
160bs = Freqb
End If
Frequency = 10as '10m
Freqb = 10bs
Portb.3 = 1
Portb.2 = 0
Portb.1 = 0
Portb.0 = 1
Modetype = 2
End Select
Reset Upwards 'reset flag for next time
End Sub

'**********************************************************************
' displays value of s-meter voltage using a bar graph on last line
' of the LCD scaling input value of 0-1023 as a bar-graph plotted
' from columns 6 to 80 on the last line of the Nokia LCD
' 10-bit adc value is scaled via (smval/16+smval/128+smval/256)
'**********************************************************************
Sub Update_smeter

Shift Smval , Right , 4 'div by 16


Temp = Smval 'and save result
Shift Smval , Right , 3 'total of 7 bit shift = div by 128
Temp = Temp + Smval
Shift Smval , Right , 1 'total of 8 bit shift = div by 256
Smval = Temp + Smval 'save result (in smval!!) and plot result on LCD

Glcdcmd 32 'normal command


Glcdcmd 134 'x pointer (128+column) where column=0-83
Glcdcmd 69 'ypointer (64+row) where row=0-5
For Temp = 1 To Smval 'plot value
Glcddata 120 '8-bit vertical column symbol data
Next Temp

Incr Smval
If Smval < 80 Then
For Temp = Smval To 80
Glcddata 0 'and erase any previous value
Next Temp
End If

End Sub

'**********************************************************************

End

'===============================================================================
Splash:
$bgf "DDSsplash.bgf"
$include "font6x8.font"
$include "font12x16dig.font"
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
si5351a DDS Oscillator with Nokia 3310 LCD display
' Author: Andrew Woodfield ZL2PD
' Date: February 2016
' File Name: si5351VFOvxx.bas (where xx is version number)
'
' Released under the terms and conditions of the
' Creative Commons Public License ("CCPL")
' See creativecommons.org for details
'
' Commercial use is prohibited without written permission
' from the author
'
'------------------------------------------------------------------
' Version 11:
'
' V01: Initialised m328, startup code
' V02: Setup 3310 lcd
' V03: Send some text and graphics to the lcd
' V04: Added rotary encoder
' V05: Display routines
' V06: Modified ADC to improve encoder operation
' V07: Added si5351 code
' V08: Display routine updated to reflect step, RIT, Lock, PTT etc
' V09: Added vfo #2, a/b swap, rit, ifoffset, high/low injection
' V10: Added band memories, startup freqs, A=B, Band up/down, BCD out
' V11: Added S-meter
'
'------------------------------------------------------------------
' Credits:
'
' Nokia-3310 LCD code and test routines are from Mrshilov 2014
' si5351 code is adapted from OE1CGS and Jason Milldrum C code
'
' Rotary encoder software is adapted from:
' http://dl6gl.de/software/drehencoder-mit-bascom
'
'------------------------------------------------------------------
' Program Description:

' An ATmega328 is used to control a Silicon Labs si5351a 3-output DDS


' DDS oscillator and Nokia 3310 compatible 84x48 graphics LCD display
' with a rotary encoder and various pushbuttons. Four pins provide
' BCD-coded outputs for switching external lowpass and/or bandpass
' filters. RIT and S-meter analog inputs are also supported.
'
'------------------------------------------------------------------
' Fuse settings
'
' LOCK Byte: 0ffh (No locks)
' EXTd Byte: 0ffh (BOD disabled)
' HIGH Byte: 0d9h
' RSTDISBL 1 Not disabled
' DWEN 1 DW not enabled
' SPIEN 0 SPI programming enabled
' WDTON 1 Watchdog timer off
' EESAVE 1 EEPROM not preserved in erase
' BOOTSZ1 0 Boot ROM size
' BOOTSZ0 0
' BOOTRST 1 No boot vector at startup
'
' LOW Byte: 0e2h
' CKDIV8 1 NOT divided by 8
' CKOUT 1 Not enabled
' SUT1 1 Slow rising power
' SUT0 0
' CKSEL3 0 8 MHz internal RC clock
' CKSEL2 0
' CKSEL1 1
' CKSEL0 0
'
'------------------------------------------------------------------
' Compiler Directives (some fundamental hardware issues)

$regfile = "m328def.dat"
$crystal = 8000000 '8MHz internal RC clock

$hwstack = 128
$swstack = 64
$framesize = 128

'*******************************************************************************
$lib "glcd-Nokia3310.lib"

Config Graphlcd = 128x64sed , A0 = Portd.5 , Si = Portd.6 , Sclk = Portd.7

'-------------------------------------------------------------------------------
' Declare Variables

Dim Mhz As String * 2 'display subroutine variables


Dim Khz As String * 3
Dim Hz As String * 3
Dim Lastftext As String * 6 'for display of freqb khz & Hz digits
Dim Freqtxt As String * 8
Dim Fcnt As Byte

Dim Temp As Byte 'temporary variable used in sendfreq and sbarplot routines

Dim Frequency As Dword 'four byte unsigned variable


Dim Freqb As Dword 'second vfo frequency
Dim Fswap As Dword 'temp store for freq swap
Dim Gfreq As Dword 'calculates si5351 VFO output frequency
Dim Bfofreq As Dword 'bfo/cio frequency for output 2

Dim Stepsize As Byte

Dim Spin As Byte 'Encoder rotation (encoder incr = 2, decr = 1, no change = 0)


Dim Delta As Dword

Dim Encval As Byte 'current rotary encoder value


Dim Lastencoder As Byte 'previous rotary encoder value

Dim Update As Bit 'bit is set (1) if a switch or encoder has changed
Dim Upwards As Bit 'direction of band change

Dim Vrold As Word '10-bit adc result from RIT control


Dim Vrnew As Word

Dim Vadc As Word 'for adc read routine


Dim Vadcl As Byte At Vadc Overlay
Dim Vadch As Byte At Vadc + 1 Overlay

Dim Ritoffset As Word 'used to calculate required RIT offset

Dim Smval As Word 'saves a2d result from s-meter input

Dim Bandpointer As Byte 'identifies currently selected amateur band

Dim Rit As Bit 'preserve status of RIT Off=0, On=1


Dim Lock As Bit 'holds status of Lock switch Off=0, On=1
Dim Modetype As Byte 'holds mode (1=LSB, 2=USB, 3=CW)
Dim Oldptt As Bit 'preserves state of last ptt condition
Dim Adctype As Bit 'adc pointer 0=RIT and 1=s-meter
Dim Fvco As Dword 'si5351 vco frequency
Dim Outdivider As Dword '4,6,8-900
Dim Rdivider As Byte 'Normally 1 unless freqeuncy<1MHz then 1,2,4,8,16,32,64 or 128 only
Dim Rbyte As Byte 'used as temp store before final data load to si5351
Dim Afactor As Dword
Dim Bfactor As Dword
Dim Fvalue As Single 'used to determine accuracy of fractional calculation
Dim Finteger As Dword

Dim Ms0p1 As Dword '18-bit MS0_P1 si5351 parameters for Output 0


Dim M0p11 As Byte At Ms0p1 Overlay 'mirrored variables for dds calculation
Dim M0p12 As Byte At Ms0p1 + 1 Overlay
Dim M0p13 As Byte At Ms0p1 + 2 Overlay
Dim M0p14 As Byte At Ms0p1 + 3 Overlay 'most significant byte for ms0p1

'Note: MS0_P2 and MS0_P3 are hard-coded as 0 and 1 respectively so ignored during dimensioning

'si5351 parameters for PLLA


Dim Msnap1 As Dword '18-bit variable for MSNA_P1
Dim Ms11 As Byte At Msnap1 Overlay 'mirrored variables for dds calculation
Dim Ms12 As Byte At Msnap1 + 1 Overlay
Dim Ms13 As Byte At Msnap1 + 2 Overlay
Dim Ms14 As Byte At Msnap1 + 3 Overlay 'most significant byte

Dim Msnap2 As Dword '20-bit variable for MSNA_P2


Dim Ms21 As Byte At Msnap2 Overlay 'mirrored variables for dds calculation
Dim Ms22 As Byte At Msnap2 + 1 Overlay
Dim Ms23 As Byte At Msnap2 + 2 Overlay
Dim Ms24 As Byte At Msnap2 + 3 Overlay 'most significant byte

Dim Msnap3 As Dword '20-bit variable for MSNA_P3


Dim Ms31 As Byte At Msnap3 Overlay 'mirrored variables for dds calculation
Dim Ms32 As Byte At Msnap3 + 1 Overlay
Dim Ms33 As Byte At Msnap3 + 2 Overlay
Dim Ms34 As Byte At Msnap3 + 3 Overlay 'most significant byte

Dim Regaddr As Byte , Regdata As Byte 'for i2c calls

' declare band memories


Dim 160as As Dword 'memory for 160m vfo a
Dim 160bs As Dword 'and vfo b
Dim 80as As Dword
Dim 80bs As Dword
Dim 40as As Dword
Dim 40bs As Dword
Dim 30as As Dword
Dim 30bs As Dword
Dim 20as As Dword
Dim 20bs As Dword
Dim 17as As Dword
Dim 17bs As Dword
Dim 15as As Dword
Dim 15bs As Dword
Dim 12as As Dword
Dim 12bs As Dword
Dim 10as As Dword
Dim 10bs As Dword

' Declare Constants

Const Xtal = 25002900 '**NOTE: Value may be adjusted during alignment ***
Const Cfactor = 1048575
Const Ifoffset = 8867000 'ifoffset=0 if no offset e.g. VFO used in SDR or DC receiver
Const High2low = 10500000 'VFO=frequency+IFoffset below this limit and frequency-IFoffset at and above
Const Usbcio = 8870000 'bfo/cio frequencies but not currently supported in this version
Const Lsbcio = 8864000
Const Cwcio = 8867000

'initial frequency configuration ***Check values for your country***


Const 160ma = 1840000 'initialise main vfo (band limits not currently supported)
Const 160mb = 1800000
Const 80ma = 3650000 '80m
Const 80mb = 3500000
Const 40ma = 7100000 '40m
Const 40mb = 7000000
Const 30ma = 10140000 '30m
Const 30mb = 10100000
Const 20ma = 14100000 '20m
Const 20mb = 14000000
Const 17ma = 18110000 '60m
Const 17mb = 18068000
Const 15ma = 21125000 '15m
Const 15mb = 21000000
Const 12ma = 24930000 '12m
Const 12mb = 24890000
Const 10ma = 28300000 '10m
Const 10mb = 28000000

' Declare Subroutines


Declare Sub Si5351init 'initialises si5351
Declare Sub Memload 'loads frequency data to ram from pgm memory
Declare Sub Displaylcd 'displays current oscillator frequency
Declare Sub Calculate 'converts current VFO freq to data for si5351
Declare Sub Calcbfo 'calculates BFO/CIO data for si5351
Declare Sub Sendfreq 'sends VFO freq data to si5351 CLK0
Declare Sub Sendbfo 'sends BFO/CIO freq data to si5351 CLK1
Declare Sub Siout(sireg As Byte , Sidata As Byte) 'for i2c write to si5351
Declare Sub Lockicon 'displays lock icon on lcd
Declare Sub Txicon 'displays tx on icon on lcd
Declare Sub Txofficon 'clears tx on icon
Declare Sub Riticon 'displays rit icon
Declare Sub Ritclricon 'clears rit icon
Declare Sub Bandchange 'stores and recalls band memories and updates BPF select out
Declare Sub Update_smeter 'takes current s-meter value and displays as a bargraph

'------------------------------------------------------------------------------------
' Hardware Setup
'
' See schematic for details
'
' Hardware Aliases:

Ptt Alias Pind.0 'ptt input


Stepkey Alias Pind.1 'step key on encoder
Ritkey Alias Pind.4 'rit on/off button
Bandkey Alias Pinb.7 'band up select button
Downbandkey Alias Pinb.4
Swapkey Alias Pinb.6 'VFO A/B button
Equalkey Alias Pinb.5 'VFO A=B button
Enca Alias Pind.2 'rotary encoder input A
Encb Alias Pind.3 'rotary encoder input B

' Initialise I/O (LCD already done above)

Config Pind.0 = Input


Config Pind.1 = Input
Config Pind.2 = Input
Config Pind.3 = Input
Config Pind.4 = Input
Config Pinb.4 = Input
Config Pinb.5 = Input
Config Pinb.6 = Input
Config Pinb.7 = Input

Set Portd.0 'and add pullups to all inputs


Set Portd.1
Set Portd.2
Set Portd.3
Set Portd.4
Set Portb.4
Set Portb.5
Set Portb.6
Set Portb.7

Config Pinb.3 = Output 'msb for BCD output for BPF/LPF selection
Config Pinb.2 = Output
Config Pinb.1 = Output
Config Pinb.0 = Output 'lsb

Config Portc = Output 'used for filter selection and i2c

Config Sda = Portc.4


Config Scl = Portc.5

Config Pinc.0 = Input 'for RIT (adc0)


Config Pinc.3 = Input 'for s-meter (adc3)

'***************************** Program *****************************************

'initialise adc
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc 'not required - starts automatically but...

'initialise i2c here...


I2cinit
Waitms 50 'for i2c to settle and for lcd RC reset time

Initlcd 'initialise the nokia 3310/5110 lcd


Showpic 0 , 0 , Splash 'splash screen
Wait 1
Cls

'initialise some variables


Frequency = 20ma 'initialise frequency (20m for now)
Freqb = 20mb 'and second vfo on same band
Stepsize = 1
Delta = 5
Spin = 0
Rit = 0 'rit is off
Lock = 0 'tuning lock is off
Bandpointer = 5 'point to 20m
Portb.3 = 0 'select required BPF and LPF (Active Low)
Portb.2 = 1
Portb.1 = 0
Portb.0 = 1
Modetype = 2 'select USB mode (1=LSB, 2=USB, 3=CW)
Oldptt = 1 'no ptt pressed
Bfofreq = Usbcio 'set carrier oscillator for usb

Reset Update 'clear the flags


Reset Upwards

Si5351init 'initialises si5351


Memload 'load freq memory
Calculate 'calculate the startup VFO value
Sendfreq 'and send it to the si5351 CLK0
Calcbfo 'calculate startup bfo/cio
Sendbfo 'and send data to si5351 CLK1
Displaylcd 'then display frequency and status on lcd

'Configure the 8-bit Timer0 interrupt for encoder


'Overflow time = overflow counts * prescale / uP clock frequency
'= 128 * 256 / 8,000,000 = 4mS = 244 Hz (8 MHz RC clock)
'Here prescale = 256 (Possible prescale factors: 8, 64, 256, 1024)

Config Timer0 = Timer , Prescale = 256


Const Presettimer0 = 192 'Timer preset value to initialize Timer0 i.e. 2mS)
On Timer0 Timer0_isr 'Timer0 interrupt service routine on overflow
Enable Timer0 'interrupt encoder
Enable Interrupts 'enable all interrupts
Do 'Start of main program

'S-meter
Smval = Getadc(3)
Update_smeter 'display s-meter value

'STEP
If Stepkey = 0 Then
Incr Stepsize
If Stepsize = 5 Then Stepsize = 1
If Stepsize = 1 Then Delta = 5 'Delta value sets tuning rates of 5, 100, 1000 and 10000 Hz/step
If Stepsize = 2 Then Delta = 100 '24 steps/turn encoder gives about 120Hz, 2.4, 24 and 240kHz/turn
If Stepsize = 3 Then Delta = 1000
If Stepsize = 4 Then Delta = 10000
Set Update 'to flag this change
End If

Keyup1:
If Stepkey = 0 Then Goto Keyup1 'and wait for button to be released

'RIT
If Ritkey = 0 And Bandkey = 1 Then 'if RIT button has been pressed then...
Toggle Rit
Set Update 'flag for display routine
End If

Keyup2:
If Ritkey = 0 And Bandkey = 1 Then Goto Keyup2 'and wait for button to be released

If Rit = 1 Then 'if rit is turned on...


Vrnew = Getadc(0) 'read rit setting
If Vrnew <> Vrold Then
Vrold = Vrnew
Set Update 'but only flag an update if the value has changed
End If
End If

'LOCK
If Pinb.6 = 0 And Pinb.7 = 0 Then 'if TUNING LOCK button has been pressed then ...
Toggle Lock 'invert lock state
Set Update 'flag for display routine
End If

Keyup3:
If Pinb.6 = 0 And Pinb.7 = 0 Then Goto Keyup3 'and wait for button to be released

'PTT
If Ptt <> Oldptt Then 'if PTT condition has changed then ...
Oldptt = Ptt 'save state for next time
Set Update 'flag for display routine
End If

'BAND UP
If Bandkey = 0 And Swapkey = 1 And Ritkey = 1 Then
Incr Bandpointer
Set Upwards 'note direction for bandchange routine
If Bandpointer = 10 Then Bandpointer = 1 'assumes 9 bands
Bandchange 'do the detailed tasks to change the band
Set Update 'flag the update
End If

Keyup4:
If Bandkey = 0 And Swapkey = 1 And Ritkey = 1 Then Goto Keyup4 'and wait for button to be released

'BAND DOWN
If Downbandkey = 0 Then
Decr Bandpointer
Reset Upwards 'do it just in case (user presses both band up and band down)
If Bandpointer = 0 Then Bandpointer = 9 'assumes 9 bands
Bandchange 'do the detailed tasks to change the band
Set Update 'flag the update
End If

Keyup5:
If Downbandkey = 0 Then Goto Keyup5 'and wait for button to be released

'MODE
If Pinb.7 = 0 And Pind.4 = 0 Then 'if MODE button has been pressed then ...
Incr Modetype
If Modetype = 4 Then Modetype = 1
Set Update 'flag for display routine
End If

Keyup6:
If Pinb.7 = 0 And Pind.4 = 0 Then Goto Keyup6 'and wait for button to be released

'SWAP A/B
If Swapkey = 0 And Pinb.7 = 1 Then 'if VFO A/B button has been pressed then ...
Disable Timer0 'disable interrupts briefly to avoid freq increment until swap done
Fswap = Frequency 'swap the frequencies
Frequency = Freqb
Freqb = Fswap
Set Update 'flag for display routine
Enable Timer0 'enable interrupts again
End If

Keyup7:
If Swapkey = 0 And Pinb.7 = 1 Then Goto Keyup7 'and wait for button to be released

'A=B
If Equalkey = 0 Then 'if VFO A=B button has been pressed then ...
Disable Timer0 'disable interrupts briefly to avoid freq increment until swap done
Freqb = Frequency 'write VFO A frequency to VFO B
Set Update 'flag for display routine
Enable Timer0 'enable interrupts again
End If

Keyup8:
If Equalkey = 0 Then Goto Keyup8 'and wait for button to be released

'Processing of rotary encoder


Disable Timer0 'Stop Timer0 interrupts

If Lock = 0 Then 'no update if tuning lock is enabled


Select Case Spin
Case 1 : Frequency = Frequency - Delta
Case 2 : Frequency = Frequency + Delta
End Select
End If

If Spin > 0 Or Update = 1 Then 'encoder or buttons changed so update display and si5351
Displaylcd 'show new frequency and status
Calculate 'calculate new VFO data
Sendfreq 'and send it to the si5351
Spin = 0 'Reset spin after processing
Reset Update 'and update flag
End If

Enable Timer0 'Restart Timer0

Loop 'End of main program

'**********************************************************************
'Subroutines...

'**********************************************************************
'Interrupt service routine for Timer0 (encoder)

'Encoder interrupt on timer0 overflow


'Timer0 should be stopped in main program while data is being displayed
'Output: spin = 2: increment or
' = 1: decrement if encoder has changed
'if no change then spin = 0
'
'**********************************************************************
Timer0_isr:
Timer0 = Presettimer0 'Initialize Timer0
Encval = 0 'clear variable

Encval.0 = Enca 'input current encoder levels


Encval.1 = Encb 'so encval now holds current encoder state

'Check if encoder encoder pattern is changed


'spin = 1: decremented (turned counter clockwise)
'spin = 2: incremented (turned clockwise)
If Encval <> Lastencoder Then 'encoder is changed

If Encval = &B00000011 And Lastencoder = &B00000010 Then Spin = 2


If Encval = &B00000011 And Lastencoder = &B00000001 Then Spin = 1

'the following lines may be required to be ADDED to those above with some encoders
'which give an additional step between mechanical detent positions
' If encval = &B00000000 And Lastencoder = &B00000001 Then spin = 2
' If encval = &B00000000 And Lastencoder = &B00000010 Then spin = 1

Lastencoder = Encval 'update for next time


End If
Return

'**********************************************************************

'**********************************************************************
' Displays frequency on the 16*2 LCD to 1 Hz resolution
' with slide-rule type scale on top line
'**********************************************************************

Sub Displaylcd

'parse Frequency for display


Freqtxt = Str(frequency) 'convert frequency to text
Hz = Right(freqtxt , 3) 'extract Hz text
Freqtxt = Str(frequency) 'do it again because for some reason !! freqtxt gets blown away by right()
Fcnt = Len(freqtxt) - 3 'nbr of remaining digits (assumes lowest freq is 1000 Hz)

While Fcnt < 5


Insertchar Freqtxt , 1 , " " 'add spaces to start of text for formatting
Fcnt = Fcnt + 1
Wend

Mhz = Left(freqtxt , 2) 'now grab first two chars


Khz = Mid(freqtxt , 3 , 3) 'and mid three chars

Setfont Font12x16dig
Lcdat 1 , 1 , Mhz
Lcdat 1 , 30 , Khz 'note: start shifted left by 1 pixel

Setfont Font6x8
Lcdat 1 , 67 , Hz
If Frequency > 999999 Then
Lcdat 2 , 25 , "."
Else
Lcdat 2 , 25 , " "
End If

'parse freqb for display


Freqtxt = Str(freqb)
Fcnt = Len(freqtxt)

While Fcnt < 8


Insertchar Freqtxt , 1 , " " 'add spaces to start of text for formatting
Fcnt = Fcnt + 1
Wend

Mhz = Left(freqtxt , 2) 'now grab first two chars


Lcdat 3 , 7 , Mhz 'print MHz digits
Lcdat 3 , 19 , "." 'then a decimal point
Lastftext = Right(freqtxt , 6) 'grab the remainder
Lcdat 3 , 25 , Lastftext
Lcdat 3 , 67 , "MHz"

Lcdat 4 , 1 , "--------------"

Select Case Stepsize


Case 1 : Lcdat 5 , 1 , " 5 " , 1
Case 2 : Lcdat 5 , 1 , "100" , 1
Case 3 : Lcdat 5 , 1 , " 1k" , 1
Case 4 : Lcdat 5 , 1 , "10k" , 1
End Select

Select Case Modetype


Case 1 : Lcdat 5 , 25 , "LSB"
Case 2 : Lcdat 5 , 25 , "USB"
Case 3 : Lcdat 5 , 25 , " CW"
End Select

If Rit = 1 Then
Riticon
Else
Ritclricon
End If
If Ptt = 0 Then
Txicon 'display tx on icon
Else
Txofficon 'blank tx icon
End If

If Lock = 1 Then
Lockicon 'display lock symbol
Else
Lcdat 5 , 79 , " "
End If

Lcdat 6 , 1 , "S" 's-meter text

End Sub

'**********************************************************************
' subroutine to initialise the si5351 chip via I2C
'**********************************************************************

Sub Si5351init

Regaddr = 183
Regdata = 192
Call Siout(regaddr , Regdata) 'set xtal load cap to 10pF
Regaddr = 3
Regdata = 0
Call Siout(regaddr , Regdata) 'enable all outputs
Regaddr = 15
Regdata = 0
Call Siout(regaddr , Regdata) 'select xtal as PLL input source
Regaddr = 16
Regdata = 15 'PLLA is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 0 control register
Regaddr = 17
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 1 control register
Regaddr = 18
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 2 control register
Regaddr = 177
Waitms 10 'wait 10ms just in case

End Sub

'************************************************************************
' calculate data bytes for current VFO frequency to send to si5351a CLK0
'************************************************************************
Sub Calculate

Gfreq = Frequency 'gfreq holds value used for si5351

'RIT routine here


If Ptt = 1 And Rit = 1 Then 'if transceiver is in receive mode and RIT is enabled......
'convert vrnew to freq offset (0 - 1023 = -2500 to +2500Hz RIT range)
Ritoffset = Vrnew * 5 '0 - 5115
If Ritoffset > 2560 Then 'if vrnew>511...
Ritoffset = Ritoffset - 2560
Gfreq = Gfreq + Ritoffset
Else
Ritoffset = 2555 - Ritoffset
Gfreq = Gfreq - Ritoffset
End If
End If

'frequency offset routine


If Frequency < High2low Then 'high2low is operating frequency where high side changes to low side injection
Gfreq = Gfreq + Ifoffset
Else
Gfreq = Gfreq - Ifoffset
End If

Rdivider = 1 'initialise r

Outdivider = 900000000 \ Gfreq 'and outdivider

While Outdivider > 900


Rdivider = 2 * Rdivider
Outdivider = Outdivider \ 2
Wend

Fvco = Outdivider Mod 2 'check outdivider is an even number


If Fvco <> 0 Then Outdivider = Outdivider - 1 'using fvco as a temporary register
Fvco = Outdivider * Rdivider
Fvco = Fvco * Gfreq 'determine PLL frequency

Select Case Rdivider 'determine value of R44 bits [6:4]


Case 1 : Rbyte = 0 '000 0000
Case 2 : Rbyte = 16 '001 0000
Case 4 : Rbyte = 32 '010 0000
Case 8 : Rbyte = 48 '011 0000
Case 16 : Rbyte = 64 '100 0000
Case 32 : Rbyte = 80 '101 0000
Case 64 : Rbyte = 96 '110 0000
Case 128 : Rbyte = 112 '111 0000
End Select

Afactor = Fvco \ Xtal


Fvalue = Afactor * Xtal 'fvalue = fvco -afactor * xtal
Fvalue = Fvco - Fvalue 'a+b/c
Fvalue = Fvalue * Cfactor
Fvalue = Fvalue \ Xtal
Bfactor = Fvalue

Ms0p1 = 128 * Outdivider


Ms0p1 = Ms0p1 - 512
Fvalue = 128 * Bfactor
Fvalue = Fvalue \ Cfactor
Finteger = Fvalue
Msnap1 = 128 * Afactor
Msnap1 = Msnap1 + Finteger 'finteger used instead of fvalue for variable matching
Msnap1 = Msnap1 - 512
Msnap3 = Finteger * Cfactor 'using msnap3 as temp result buffer
Msnap2 = 128 * Bfactor
Msnap2 = Msnap2 - Msnap3
Msnap3 = Cfactor

'calculations are complete so now build and send the required output data bytes by calling sendfreq (About 20 bytes sent per

End Sub

'****************************************************************************
' calculate data bytes for current BFO/CIO frequency to send to si5351a CLK1
'****************************************************************************
Sub Calcbfo

Rdivider = 1 'initialise r
Outdivider = 900000000 \ Bfofreq 'and outdivider

While Outdivider > 900


Rdivider = 2 * Rdivider
Outdivider = Outdivider \ 2
Wend

Fvco = Outdivider Mod 2 'check outdivider is an even number


If Fvco <> 0 Then Outdivider = Outdivider - 1 'using fvco as a temporary register

Fvco = Outdivider * Rdivider


Fvco = Fvco * Bfofreq 'determine PLL frequency

Select Case Rdivider 'determine value of R52 bits [6:4]


Case 1 : Rbyte = 0 '000 0000
Case 2 : Rbyte = 16 '001 0000
Case 4 : Rbyte = 32 '010 0000
Case 8 : Rbyte = 48 '011 0000
Case 16 : Rbyte = 64 '100 0000
Case 32 : Rbyte = 80 '101 0000
Case 64 : Rbyte = 96 '110 0000
Case 128 : Rbyte = 112 '111 0000
End Select

Afactor = Fvco \ Xtal


Fvalue = Afactor * Xtal 'fvalue = fvco -afactor * xtal
Fvalue = Fvco - Fvalue 'a+b/c
Fvalue = Fvalue * Cfactor
Fvalue = Fvalue \ Xtal
Bfactor = Fvalue

Ms0p1 = 128 * Outdivider


Ms0p1 = Ms0p1 - 512
Fvalue = 128 * Bfactor
Fvalue = Fvalue \ Cfactor
Finteger = Fvalue
Msnap1 = 128 * Afactor
Msnap1 = Msnap1 + Finteger 'finteger used instead of fvalue for variable matching
Msnap1 = Msnap1 - 512
Msnap3 = Finteger * Cfactor 'using msnap3 as temp result buffer
Msnap2 = 128 * Bfactor
Msnap2 = Msnap2 - Msnap3
Msnap3 = Cfactor
'calculations are complete so now build and send the required output data bytes by calling sendbfo (About 20 bytes sent per

End Sub

'**********************************************************************
' send converted freq data to si5351 for CLK0 output
'**********************************************************************
Sub Sendfreq

Regaddr = 26
Call Siout(regaddr , Ms32) 'R26=msnap3[15:8]
Incr Regaddr
Call Siout(regaddr , Ms31) 'R27=msnap3[7:0]
Incr Regaddr
Call Siout(regaddr , Ms13) 'R28=msnap1[17:16] in R28[1:0] with R28[7:2]=0
Incr Regaddr
Call Siout(regaddr , Ms12) 'R29=msnap1[15:8]
Incr Regaddr
Call Siout(regaddr , Ms11) 'R30=msnap1[7:0]
Temp = Ms33 'temp[3:0]=msnap3[19:16]
Swap Temp 'temp[7:4]=msnap3[19:16]
Temp = Temp Or Ms23 'temp[7:4]=msnap3[19:16] and temp[3:0]=msnap2[19:16]
Incr Regaddr
Call Siout(regaddr , Temp) 'R31=(msnap3[19:16],msnap2[19:16])
Incr Regaddr
Call Siout(regaddr , Ms22) 'R32=msnap2[15:8]
Incr Regaddr
Call Siout(regaddr , Ms21) 'R33=msnap2[7:0]
Regaddr = 42
Regdata = 0
Call Siout(regaddr , Regdata) 'R42=MS0_P3[15:8]
Incr Regaddr
Regdata = 1
Call Siout(regaddr , Regdata) 'R43=MS0_P3[7:0]
Temp = Rbyte
Temp = Temp Or M0p13
Incr Regaddr
Call Siout(regaddr , Temp) 'R44=(0,R[7:4],MS0_P1[17:16])
Incr Regaddr
Call Siout(regaddr , M0p12) 'R45=MS0_P1[15:8]
Incr Regaddr
Call Siout(regaddr , M0p11) 'R46=MS0_P1[7:0]
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R47=(MS0_P3[19:16],MS0_P2[19:16]) and both are always 0
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R48=MS0_P2[15:8]=0 always
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R49=MS0_P2[7:0]=0 always
If Outdivider = 4 Then 'for output frequencies greater than 150MHz and less than 200MHz
Regaddr = 44
Regdata = 12
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
End If
Regaddr = 3
Regdata = 4
Call Siout(regaddr , Regdata) 'enable all outputs except CLK2
Regaddr = 16
Regdata = 79
Call Siout(regaddr , Regdata) 'enable CLK0, source=crystal, PLLA=integer mode, power level=8mA i.e. 0dB

End Sub

'**********************************************************************
' send converted freq data to si5351 for CLK1 output
'**********************************************************************
Sub Sendbfo

Regaddr = 34
Call Siout(regaddr , Ms32) 'R34=msnbp3[15:8]
Incr Regaddr
Call Siout(regaddr , Ms31) 'R35=msnbp3[7:0]
Incr Regaddr
Call Siout(regaddr , Ms13) 'R36=msnbp1[17:16] in R36[1:0] with R36[7:2]=0
Incr Regaddr
Call Siout(regaddr , Ms12) 'R37=msnbp1[15:8]
Incr Regaddr
Call Siout(regaddr , Ms11) 'R38=msnbp1[7:0]
Temp = Ms33 'temp[3:0]=msnbp3[19:16]
Swap Temp 'temp[7:4]=msnbp3[19:16]
Temp = Temp Or Ms23 'temp[7:4]=msnbp3[19:16] and temp[3:0]=msnbp2[19:16]
Incr Regaddr
Call Siout(regaddr , Temp) 'R39=(msnbp3[19:16],msnbp2[19:16])
Incr Regaddr
Call Siout(regaddr , Ms22) 'R40=msnbp2[15:8]
Incr Regaddr
Call Siout(regaddr , Ms21) 'R41=msnbp2[7:0]
Regaddr = 50
Regdata = 0
Call Siout(regaddr , Regdata) 'R50=MS1_P3[15:8]
Incr Regaddr
Regdata = 1
Call Siout(regaddr , Regdata) 'R51=MS1_P3[7:0]
Temp = Rbyte
Temp = Temp Or M0p13
Incr Regaddr
Call Siout(regaddr , Temp) 'R52=(0,R[7:4],MS1_P1[17:16])
Incr Regaddr
Call Siout(regaddr , M0p12) 'R53=MS1_P1[15:8]
Incr Regaddr
Call Siout(regaddr , M0p11) 'R54=MS1_P1[7:0]
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R55=(MS1_P3[19:16],MS1_P2[19:16]) and both are always 0
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R56=MS1_P2[15:8]=0 always
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R57=MS1_P2[7:0]=0 always
If Outdivider = 4 Then 'for output frequencies greater than 150MHz and less than 200MHz
Regaddr = 52
Regdata = 12
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
End If
Regaddr = 3
Regdata = 4
Call Siout(regaddr , Regdata) 'enable all outputs except CLK2
Regaddr = 17
Regdata = 111
Call Siout(regaddr , Regdata) 'enable CLK1, source=crystal, PLLB=integer mode, power level=8mA i.e. 0dB
End Sub

'**********************************************************************
' i2c transmission to si5351
'**********************************************************************
Sub Siout(sireg As Byte , Sidata As Byte)
'subroutine sends databyte to register_nbr at i2c address 192 (si5351 register read is from address 194)

'Write a single byte (slave address 192, register sireg, value sibyte)
I2cstart
I2cwbyte 192 'i2c address of si5351a
Waitus 1 'suggested delay from forum
I2cwbyte Sireg
Waitus 1
I2cwbyte Sidata
Waitus 1
I2cstop

End Sub

'**********************************************************************
' initialise band by band frequency storage in ram from initial freq
' data tables in program memory
'**********************************************************************
Sub Memload

160as = 160ma 'load ram memory for 160m vfo a


160bs = 160mb 'and then repeat for vfo b
80as = 80ma
80bs = 80mb
40as = 40ma
40bs = 40mb
30as = 30ma
30bs = 30mb
20as = 20ma
20bs = 20mb
17as = 17ma
17bs = 17mb
15as = 15ma
15bs = 15mb
12as = 12ma
12bs = 12mb
10as = 10ma
10bs = 10mb 'crude but effective!

End Sub

'**********************************************************************
' display the dial locked icon on lcd
'**********************************************************************
Sub Lockicon

Glcdcmd 32 'normal command


Glcdcmd 207 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 126 '8-bit vertical column symbol data
Glcddata 121
Glcddata 73
Glcddata 121
Glcddata 126
Glcddata 0

End Sub

'**********************************************************************
' display the Inverted Tx icon on lcd
'**********************************************************************
Sub Txicon

Glcdcmd 32 'normal command


Glcdcmd 197 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 255 '8-bit vertical column symbol data
Glcddata 253
Glcddata 129
Glcddata 253
Glcddata 175
Glcddata 223
Glcddata 175
Glcddata 255

End Sub

'**********************************************************************
' clear the Tx icon on lcd
'**********************************************************************
Sub Txofficon
Glcdcmd 32 'normal command
Glcdcmd 197 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 0 '8-bit vertical column symbol data
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0

End Sub

'**********************************************************************
' display the RIT icon on lcd
'**********************************************************************
Sub Riticon

Glcdcmd 32 'normal command


Glcdcmd 183 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 64 '8-bit vertical column symbol data
Glcddata 112
Glcddata 124
Glcddata 127
Glcddata 124
Glcddata 112
Glcddata 64
Glcddata 8
Glcddata 126
Glcddata 9
Glcddata 2
Glcddata 0

End Sub

'**********************************************************************
' clear the RIT icon on lcd
'**********************************************************************
Sub Ritclricon

Glcdcmd 32 'normal command


Glcdcmd 183 'x pointer (128+column) where column=0-83
Glcdcmd 68 'ypointer (64+row) where row=0-5
Glcddata 0 '8-bit vertical column symbol data
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0
Glcddata 0

End Sub

'**********************************************************************
' do the detailed work required to change the selected band
'**********************************************************************
Sub Bandchange

Select Case Bandpointer 'determine value of R44 bits [6:4]


Case 1 : '160m
If Upwards = 1 Then 'save previous vfo frequencies
10as = Frequency 'these if the BAND UP key was pressed
10bs = Freqb
Else
80as = Frequency 'or these if the BAND DOWN key was pressed
80bs = Freqb
End If
Frequency = 160as 'load freq for main vfo (and add band limits later?)
Freqb = 160bs 'and for second vfo
Portb.3 = 0 'configure BPF select lines
Portb.2 = 0
Portb.1 = 0
Portb.0 = 1
Modetype = 1 'LSB
Case 2 :
If Upwards = 1 Then
160as = Frequency
160bs = Freqb
Else
40as = Frequency
40bs = Freqb
End If
Frequency = 80as '80m
Freqb = 80bs
Portb.3 = 0
Portb.2 = 0
Portb.1 = 1
Portb.0 = 0
Modetype = 1
Case 3 :
If Upwards = 1 Then
80as = Frequency
80bs = Freqb
Else
30as = Frequency
30bs = Freqb
End If
Frequency = 40as '40m
Freqb = 40bs
Portb.3 = 0
Portb.2 = 0
Portb.1 = 1
Portb.0 = 1
Modetype = 1
Case 4 :
If Upwards = 1 Then
40as = Frequency
40bs = Freqb
Else
20as = Frequency
20bs = Freqb
End If
Frequency = 30as '30m
Freqb = 30bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 0
Portb.0 = 0
Modetype = 1
Case 5 :
If Upwards = 1 Then
30as = Frequency
30bs = Freqb
Else
17as = Frequency
17bs = Freqb
End If
Frequency = 20as '20m
Freqb = 20bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 0
Portb.0 = 1
Modetype = 1
Case 6 :
If Upwards = 1 Then
20as = Frequency
20bs = Freqb
Else
15as = Frequency
15bs = Freqb
End If
Frequency = 17as '17m
Freqb = 17bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 1
Portb.0 = 0
Modetype = 2 'USB
Case 7 :
If Upwards = 1 Then
17as = Frequency
17bs = Freqb
Else
12as = Frequency
12bs = Freqb
End If
Frequency = 15as '15m
Freqb = 15bs
Portb.3 = 0
Portb.2 = 1
Portb.1 = 1
Portb.0 = 1
Modetype = 2
Case 8 :
If Upwards = 1 Then
15as = Frequency
15bs = Freqb
Else
10as = Frequency
10bs = Freqb
End If
Frequency = 12as '12m
Freqb = 12bs
Portb.3 = 1
Portb.2 = 0
Portb.1 = 0
Portb.0 = 0
Modetype = 2
Case 9 :
If Upwards = 1 Then
12as = Frequency
12bs = Freqb
Else
160as = Frequency
160bs = Freqb
End If
Frequency = 10as '10m
Freqb = 10bs
Portb.3 = 1
Portb.2 = 0
Portb.1 = 0
Portb.0 = 1
Modetype = 2
End Select
Reset Upwards 'reset flag for next time
End Sub

'**********************************************************************
' displays value of s-meter voltage using a bar graph on last line
' of the LCD scaling input value of 0-1023 as a bar-graph plotted
' from columns 6 to 80 on the last line of the Nokia LCD
' 10-bit adc value is scaled via (smval/16+smval/128+smval/256)
'**********************************************************************
Sub Update_smeter

Shift Smval , Right , 4 'div by 16


Temp = Smval 'and save result
Shift Smval , Right , 3 'total of 7 bit shift = div by 128
Temp = Temp + Smval
Shift Smval , Right , 1 'total of 8 bit shift = div by 256
Smval = Temp + Smval 'save result (in smval!!) and plot result on LCD

Glcdcmd 32 'normal command


Glcdcmd 134 'x pointer (128+column) where column=0-83
Glcdcmd 69 'ypointer (64+row) where row=0-5

For Temp = 1 To Smval 'plot value


Glcddata 120 '8-bit vertical column symbol data
Next Temp

Incr Smval
If Smval < 80 Then
For Temp = Smval To 80
Glcddata 0 'and erase any previous value
Next Temp
End If

End Sub

'**********************************************************************

End

'===============================================================================
Splash:
$bgf "DDSsplash.bgf"
$include "font6x8.font"
$include "font12x16dig.font"
si5351a DDS Oscillator with I2C LCD display
' Author: Andrew Woodfield ZL2PD
' Date: March 2016
' File Name: si85vfoxx.bas (where xx is version number)
'
' Released under the terms and conditions of the
' Creative Commons Public License ("CCPL")
' See creativecommons.org for details
'
' Commercial use is prohibited without written permission
' from the author
'
'------------------------------------------------------------------
' Adapted from boardtest11.bas:
'
' V01: initial code
'
'------------------------------------------------------------------
' Credits:
'
' I2C LCD code adapted from www.ne.jp/asahi/shared/o-family/ElecRoom/ElecMAIN.htm
' si5351 code is adapted from OE1CGS and Jason Milldrum C code
'

'
'------------------------------------------------------------------
' Strpped down for basic operation and control over RS232
' by Burkhard Kainka, DK7JD, March 2019

'------------------------------------------------------------------

'------------------------------------------------------------------
' Compiler Directives (some fundamental hardware issues)

$regfile = "attiny85.dat"
$crystal = 8000000
$hwstack = 128
$swstack = 64
$framesize = 128
'-------------------------------------------------------------------------------
' Declare Variables

Dim Temp As Byte 'temporary variable used in sendfreq and sbarplot routines
Dim Frequency As Dword 'four byte unsigned variable
Dim Gfreq As Dword 'calculates si5351 VFO output frequency
Dim Clk As Byte 'output 0/1/2

Dim Fvco As Dword 'si5351 vco frequency


Dim Outdivider As Dword '4,6,8-900
Dim Rdivider As Byte 'Normally 1 unless freqeuncy<1MHz then 1,2,4,8,16,32,64 or 128 only
Dim Rbyte As Byte 'used as temp store before final data load to si5351
Dim Afactor As Dword
Dim Bfactor As Dword
Dim Fvalue As Single 'used to determine accuracy of fractional calculation
Dim Finteger As Dword

Dim Ms0p1 As Dword '18-bit MS0_P1 si5351 parameters for Output 0


Dim M0p11 As Byte At Ms0p1 Overlay 'mirrored variables for dds calculation
Dim M0p12 As Byte At Ms0p1 + 1 Overlay
Dim M0p13 As Byte At Ms0p1 + 2 Overlay
Dim M0p14 As Byte At Ms0p1 + 3 Overlay 'most significant byte for ms0p1

'Note: MS0_P2 and MS0_P3 are hard-coded as 0 and 1 respectively so ignored during dimensioning
'si5351 parameters for PLLA
Dim Msnap1 As Dword '18-bit variable for MSNA_P1
Dim Ms11 As Byte At Msnap1 Overlay 'mirrored variables for dds calculation
Dim Ms12 As Byte At Msnap1 + 1 Overlay
Dim Ms13 As Byte At Msnap1 + 2 Overlay
Dim Ms14 As Byte At Msnap1 + 3 Overlay 'most significant byte

Dim Msnap2 As Dword '20-bit variable for MSNA_P2


Dim Ms21 As Byte At Msnap2 Overlay 'mirrored variables for dds calculation
Dim Ms22 As Byte At Msnap2 + 1 Overlay
Dim Ms23 As Byte At Msnap2 + 2 Overlay
Dim Ms24 As Byte At Msnap2 + 3 Overlay 'most significant byte

Dim Msnap3 As Dword '20-bit variable for MSNA_P3


Dim Ms31 As Byte At Msnap3 Overlay 'mirrored variables for dds calculation
Dim Ms32 As Byte At Msnap3 + 1 Overlay
Dim Ms33 As Byte At Msnap3 + 2 Overlay
Dim Ms34 As Byte At Msnap3 + 3 Overlay 'most significant byte

Dim Regaddr As Byte , Regdata As Byte 'for i2c calls


Dim Siaddr1 As Byte , Siaddr2 As Byte , Siaddr3 As Byte 'for writing to si5351 CLK0 and CLK1

Dim Zeile As String * 20


Dim Komm As String * 1
Dim L As Byte
Dim Freq As Word
Dim Freq2 As Word
Dim Ampl As Word
Dim Ampl2 As Word
Dim Ampl3 As Word
Dim Modus As Byte
Dim U As Word
Dim S As String * 10
Dim Dat As Word
Dim D As Word

' Declare Constants


Const Xtal = 25000000 '**NOTE: Value may be adjusted during alignment ***
Const Cfactor = 1048575
'Const Ifoffset = 8867000 'ifoffset=0 if no offset e.g. VFO used in SDR or DC receiver
'Const Bfofreq = 8870000 'bfo/cio frequencies but not currently supported in this version

'Const Midas_vdd = 3 'LCD display is powered from 3V rail


Open "comb.2:9600,8,n,1" For Input As #1

' Declare Subroutines


Declare Sub Si5351init 'initialises si5351
Declare Sub Setfreq 'output to CLK0/1/2 in Hz
Declare Sub Calculate 'converts current VFO or BFO freq to data for si5351
Declare Sub Sendfreq 'sends VFO or BFO/CIO freq data to si5351 CLK0 or CLK1 respectively
Declare Sub Siout(sireg As Byte , Sidata As Byte) 'for i2c write to si5351
'plots current s-meter input voltage

'------------------------------------------------------------------------------------

Config Portb = Output '

Config Sda = Portb.4


Config Scl = Portb.3
I2cinit
Waitms 50 'for i2c to settle and for lcd RC reset time
Si5351init 'initialises si5351
Waitms 500
Gfreq = 14296000
Clk = 1
Setfreq
Waitms 500

Do
Dat = Ischarwaiting(#1)
If Dat > 0 Then
Input #1 , Zeile
Komm = Left(zeile , 1)
L = Len(zeile)
L=L-1
Zeile = Right(zeile , L)

If Komm = "x" Then 'Frequenz 0


Freq = Val(zeile)
Gfreq = Freq * 1000
Clk = 0
Setfreq
End If

If Komm = "y" Then 'Frequenz 1


Freq = Val(zeile)
Gfreq = Freq * 1000 '
Clk = 1
Setfreq
End If
End If
Loop

'**********************************************************************

Sub Setfreq
Calculate 'Gfreq to chip registers
If Clk = 0 Then
Siaddr1 = 26 'point to CLK0 base register addresses
Siaddr2 = 42
Siaddr3 = 44
Sendfreq 'and send it to the si5351 CLK0
Regaddr = 16
Regdata = 79
Call Siout(regaddr , Regdata) 'enable CLK0, source=crystal, PLLA=integer mode, power level=8mA i.e. 0dB
End If
If Clk = 1 Then
Siaddr1 = 34 'point to CLK1 base register addresses
Siaddr2 = 50
Siaddr3 = 52
Sendfreq 'and send data to si5351 CLK1
Regaddr = 17
Regdata = 111
Call Siout(regaddr , Regdata) 'enable CLK1, source=crystal, PLLB=integer mode, power level=8mA i.e. 0dB
End If
If Clk = 2 Then
Siaddr1 = 42 'point to CLK1 base register addresses
Siaddr2 = 58
Siaddr3 = 60
Sendfreq 'and send data to si5351 CLK1
Regaddr = 18
Regdata = 111
Call Siout(regaddr , Regdata) 'enable CLK2, source=crystal, PLLB=integer mode, power level=8mA i.e. 0dB
End If
End Sub

'**********************************************************************
' subroutine to initialise the si5351 chip via I2C
'**********************************************************************

Sub Si5351init

Regaddr = 183
Regdata = 192
Call Siout(regaddr , Regdata) 'set xtal load cap to 10pF
Regaddr = 3
Regdata = 0
Call Siout(regaddr , Regdata) 'enable all outputs
Regaddr = 15
Regdata = 0
Call Siout(regaddr , Regdata) 'select xtal as PLL input source
Regaddr = 16
Regdata = 15 'PLLA is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 0 control register
Regaddr = 17
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 1 control register
Regaddr = 18
Regdata = 47 'PLLB is source, 8mA output
Call Siout(regaddr , Regdata) 'configure clock 2 control register
Regaddr = 177
Waitms 10 'wait 10ms just in case

End Sub

'********************************************************************************
' calculate data bytes for current VFO or BFO frequency to send to si5351a CLK0/1
'********************************************************************************
Sub Calculate

Rdivider = 1 'initialise r

Outdivider = 900000000 \ Gfreq 'and outdivider

While Outdivider > 900


Rdivider = 2 * Rdivider
Outdivider = Outdivider \ 2
Wend

Fvco = Outdivider Mod 2 'check outdivider is an even number


If Fvco <> 0 Then Outdivider = Outdivider - 1 'using fvco as a temporary register

Fvco = Outdivider * Rdivider


Fvco = Fvco * Gfreq 'determine PLL frequency

Select Case Rdivider 'determine value of R44 bits [6:4]


Case 1 : Rbyte = 0 '000 0000
Case 2 : Rbyte = 16 '001 0000
Case 4 : Rbyte = 32 '010 0000
Case 8 : Rbyte = 48 '011 0000
Case 16 : Rbyte = 64 '100 0000
Case 32 : Rbyte = 80 '101 0000
Case 64 : Rbyte = 96 '110 0000
Case 128 : Rbyte = 112 '111 0000
End Select

Afactor = Fvco \ Xtal


Fvalue = Afactor * Xtal 'fvalue = fvco -afactor * xtal
Fvalue = Fvco - Fvalue 'a+b/c
Fvalue = Fvalue * Cfactor
Fvalue = Fvalue \ Xtal
Bfactor = Fvalue

Ms0p1 = 128 * Outdivider


Ms0p1 = Ms0p1 - 512
Fvalue = 128 * Bfactor
Fvalue = Fvalue \ Cfactor
Finteger = Fvalue
Msnap1 = 128 * Afactor
Msnap1 = Msnap1 + Finteger 'finteger used instead of fvalue for variable matching
Msnap1 = Msnap1 - 512
Msnap3 = Finteger * Cfactor 'using msnap3 as temp result buffer
Msnap2 = 128 * Bfactor
Msnap2 = Msnap2 - Msnap3
Msnap3 = Cfactor

'calculations are complete so now build and send the required output data bytes by calling sendfreq (About 20 bytes sent per

End Sub

'**********************************************************************
' send converted freq data to si5351 for CLK0 output
'**********************************************************************
Sub Sendfreq

Regaddr = Siaddr1
Call Siout(regaddr , Ms32) 'R26=msnap3[15:8]
Incr Regaddr
Call Siout(regaddr , Ms31) 'R27=msnap3[7:0]
Incr Regaddr
Call Siout(regaddr , Ms13) 'R28=msnap1[17:16] in R28[1:0] with R28[7:2]=0
Incr Regaddr
Call Siout(regaddr , Ms12) 'R29=msnap1[15:8]
Incr Regaddr
Call Siout(regaddr , Ms11) 'R30=msnap1[7:0]
Temp = Ms33 'temp[3:0]=msnap3[19:16]
Swap Temp 'temp[7:4]=msnap3[19:16]
Temp = Temp Or Ms23 'temp[7:4]=msnap3[19:16] and temp[3:0]=msnap2[19:16]
Incr Regaddr
Call Siout(regaddr , Temp) 'R31=(msnap3[19:16],msnap2[19:16])
Incr Regaddr
Call Siout(regaddr , Ms22) 'R32=msnap2[15:8]
Incr Regaddr
Call Siout(regaddr , Ms21) 'R33=msnap2[7:0]
Regaddr = Siaddr2
Regdata = 0
Call Siout(regaddr , Regdata) 'R42=MS0_P3[15:8]
Incr Regaddr
Regdata = 1
Call Siout(regaddr , Regdata) 'R43=MS0_P3[7:0]
Temp = Rbyte
Temp = Temp Or M0p13
Incr Regaddr
Call Siout(regaddr , Temp) 'R44=(0,R[7:4],MS0_P1[17:16])
Incr Regaddr
Call Siout(regaddr , M0p12) 'R45=MS0_P1[15:8]
Incr Regaddr
Call Siout(regaddr , M0p11) 'R46=MS0_P1[7:0]
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R47=(MS0_P3[19:16],MS0_P2[19:16]) and both are always 0
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R48=MS0_P2[15:8]=0 always
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata) 'R49=MS0_P2[7:0]=0 always
If Outdivider = 4 Then 'for output frequencies greater than 150MHz and less than 200MHz
Regaddr = Siaddr3
Regdata = 12
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
Incr Regaddr
Regdata = 0
Call Siout(regaddr , Regdata)
End If
Regaddr = 3
Regdata = 0 '4
Call Siout(regaddr , Regdata) 'enable all outputs except CLK2

End Sub

'**********************************************************************
' i2c transmission to si5351
'**********************************************************************
Sub Siout(sireg As Byte , Sidata As Byte)
'subroutine sends databyte to register_nbr at i2c address 192 (si5351 register read is from address 194)
'Write a single byte (slave address 192, register sireg, value sibyte)
I2cstart
I2cwbyte 192 'i2c address of si5351a
Waitus 1 'suggested delay from forum
I2cwbyte Sireg
Waitus 1
I2cwbyte Sidata
Waitus 1
I2cstop

End Sub

End

Vous aimerez peut-être aussi