' arial_bold.bas
' Font type    : Full (95 characters)
' Font size    : 16x16 pixels
' Memory usage : 3044 bytes
' Font downloaded from: http://www.rinkydinkelectronics.com/r_fonts.php
DefineFont #2
5F201010 00000000 00000000 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00030003 00030003 00030003 00030003 00000003 00030003
00000000 00000000 60066006 60066006 00000000 00000000 00000000 00000000
00000000 00000000 60036003 F03FC006 C006F03F F03F800D 800DF03F 001B001B
00000000 00010000 C0078003 000D600D 8007000F E001C003 600D600D 8003C007
00000001 00000000 3066183C 60663066 C03CC066 B3019E01 33033303 1E0C3306
00000000 00000000 E00FC007 600C600C 8007C007 D819900D 7818F018 8807FC0F
00000000 00000000 80018001 80018001 00000000 00000000 00000000 00000000
00000000 80010000 00030003 00060006 00060006 00060006 00030006 80010003
00000000 00060000 00030003 80018001 80018001 80018001 00038001 00060003
00000000 00000000 00000000 80038003 F83F9833 C007C007 701CE00E 00004004
00000000 00000000 00000000 C0000000 C000C000 F807F807 C000C000 0000C000
00000000 00000000 00000000 00000000 00000000 00000000 00030003 00010001
00000002 00000000 00000000 00000000 00000000 E003E003 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00030000 00000003
00000000 00000000 60006000 C000C000 C000C000 80018001 80018001 00030003
00000000 00000000 E007C003 300C700E 300C300C 300C300C 700E300C C003E007
00000000 00000000 80038001 800D8007 80018009 80018001 80018001 80018001
00000000 00000000 E007C003 300C300E 60003000 C001E000 00068003 F00FF00F
00000000 00000000 F007E003 3000300E E001E001 30007000 700E300C C003E007
00000000 00000000 E0006000 E001E000 60036003 600C6006 F00FF00F 60006000
00000000 00000000 E007E007 000C0006 E00FC00F 3000700C 700E300C C003E007
00000000 00000000 F007E003 000C3006 E00FC00D 300C700E 3006300C C003E007
00000000 00000000 F00FF00F C0006000 8001C000 80018001 00038003 00030003
00000000 00000000 E007C003 300C300C E007300C 300CE007 300C300C C003E007
00000000 00000000 E007C003 300C600C 700E300C B003F007 600C3000 C007E00F
00000000 00000000 80010000 00008001 00000000 00000000 80018001 00000000
00000000 00000000 80010000 00008001 00000000 00000000 80018001 80008000
00000001 00000000 00000000 E0002000 000FC003 000F001C E000C003 00002000
00000000 00000000 00000000 F01FF01F 00000000 F01FF01F 00000000 00000000
00000000 00000000 00000000 000E0008 E0018007 E0017000 000E8007 00000008
00000000 00000000 E007C003 300C300E E0007000 8001C001 00008001 80018001
00000000 C0070000 7813300C C82CE817 C82CC82C E026D02F 10080810 0000E007
00000000 00000000 80038003 C006C006 600CC006 E00F600C 3018F01F 18303018
00000000 00000000 F01FE01F 30183018 E01F3018 1818F01F 18181818 E01FF01F
00000000 00000000 F00FE003 101C380C 00180018 00180018 380C101C E003F00F
00000000 00000000 F01FC01F 18183018 18181818 18181818 30181818 C01FF01F
00000000 00000000 F80FF80F 000C000C F80F000C 000CF80F 000C000C F80FF80F
00000000 00000000 F00FF00F 000C000C E00F000C 000CE00F 000C000C 000C000C
00000000 00000000 F01FE007 10383818 00300030 F830F830 38181838 E007F81F
00000000 00000000 18181818 18181818 F81F1818 1818F81F 18181818 18181818
00000000 00000000 80018001 80018001 80018001 80018001 80018001 80018001
00000000 00000000 30003000 30003000 30003000 30003000 700E300C C003E007
00000000 00000000 30181818 C0186018 C01B8019 601CC01E 30187018 18181818
00000000 00000000 000C000C 000C000C 000C000C 000C000C 000C000C F00FF00F
00000000 00000000 38383838 783C783C D8365834 D836D836 98339833 18319833
00000000 00000000 181C1818 181E181E 9819181B D8189819 78187818 18183818
00000000 00000000 F01FC007 18303018 18301830 18301830 30181830 C007F01F
00000000 00000000 F00FE00F 180C380C F00F380C 000CE00F 000C000C 000C000C
00000000 00000000 F01FC007 18303018 18301830 18301830 F018B831 B007F01F
00001800 00000000 F01FE01F 18183818 F01F3818 E018C01F 30187018 1C183818
00000000 00000000 F007E003 180C380E E007000F 3800F001 380E180C E003F007
00000000 00000000 F81FF81F 80018001 80018001 80018001 80018001 80018001
00000000 00000000 18181818 18181818 18181818 18181818 381C1818 E007F00F
00000000 00000000 30181830 30183018 600C600C C006E00E 8003C006 80038003
00000000 00000000 C631C631 6C1BC631 6C1B6C1B 6C1B6C1B 380E380E 380E380E
00000000 00000000 701C3018 C006600C 8003C007 C0078003 600CC006 3018701C
00000000 00000000 381C1818 6006300C C0036006 80018001 80018001 80018001
00000000 00000000 F807F807 60003000 C000E000 80038001 00060003 F80FF80F
00000000 C0030000 0003C003 00030003 00030003 00030003 00030003 C0030003
0000C003 00000000 00030003 80018001 80018001 C000C000 C000C000 60006000
00000000 C0030000 C000C003 C000C000 C000C000 C000C000 C000C000 C003C000
0000C003 80010000 C003C003 60066006 300C6006 00000000 00000000 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 FFFF0000
0000FFFF 00000000 C0008001 00000000 00000000 00000000 00000000 00000000
00000000 00000000 00000000 E00FC007 E001600C 600EE007 E00F600C 0000B007
00000000 000C0000 000C000C E00FC00D 300C700E 300C300C E00F700E 0000C00D
00000000 00000000 00000000 E007C003 000C600E 000C000C E007600E 0000C003
00000000 30000000 30003000 F007B003 300C700E 300C300C F007700E 0000B003
00000000 00000000 00000000 C0078003 E00F600C 000CE00F C007600E 00008003
00000000 C0030000 0006C007 800F800F 00060006 00060006 00060006 00000006
00000000 00000000 60070000 E01CE00F 60186018 E01C6018 6007E00F E01F6018
0000C00F 000C0000 000C000C F00FE00D 300C300E 300C300C 300C300C 0000300C
00000000 00000000 80018001 80010000 80018001 80018001 80018001 00008001
00000000 00000000 80018001 80010000 80018001 80018001 80018001 80018001
00078007 000C0000 000C000C C00C600C 800F800D C00EC00F 600CC00C 0000600C
00000000 80010000 80018001 80018001 80018001 80018001 80018001 00008001
00000000 00000000 00000000 FC3F3837 8C31CC39 8C318C31 8C318C31 00008C31
00000000 00000000 00000000 F00FE00D 300C300E 300C300C 300C300C 0000300C
00000000 00000000 00000000 E007C003 300C700E 300C300C E007700E 0000C003
00000000 00000000 00000000 E00FC00D 300C700E 300C300C E00F700E 000CC00D
000C000C 00000000 00000000 F007B003 300C700E 300C300C F007700E 3000B003
30003000 00000000 00000000 F0037003 00038003 00030003 00030003 00000003
00000000 00000000 00000000 E00FC007 000F600C E000C007 E00F600C 0000C007
00000000 00010000 00030003 C007C007 00030003 00030003 C0030003 0000C001
00000000 00000000 00000000 300C300C 300C300C 300C300C F00F700C 0000B007
00000000 00000000 00000000 30063006 60033006 60036003 C001C001 0000C001
00000000 00000000 00000000 C631C631 6C1BCC19 6C1B6C1B 380E380E 0000380E
00000000 00000000 00000000 70073006 C0016003 C001C001 70076003 00003006
00000000 00000000 00000000 180C180C 30063006 60036003 C001E003 8001C001
00078007 00000000 00000000 F803F803 70003000 C001E000 F8038001 0000F803
00000000 C003C001 00030003 00030003 000E000E 00030003 00030003 C0030003
0000C001 80018001 80018001 80018001 80018001 80018001 80018001 80018001
80018001 C0038003 C000C000 C000C000 70007000 C000C000 C000C000 C003C000
00008003 00000000 F80F8807 0000F008 00000000 00000000 00000000 00000000
00000000
End DefineFont
option autorun on
option explicit

Const cSI_CLK0_CONTROL=16
Const cSI_CLK1_CONTROL=17
Const cSI_CLK2_CONTROL=18
Const cSI_SYNTH_PLL_A =26
Const cSI_SYNTH_PLL_B =34
Const cSI_SYNTH_MS_0  =42
Const cSI_SYNTH_MS_1  =50
Const cSI_SYNTH_MS_2  =58
Const cSI_PLL_RESET   =177
Const cSI_R_DIV_1     =0       ' R-division ratio definitions
Const cSI_R_DIV_2     =16
Const cSI_R_DIV_4     =32
Const cSI_R_DIV_8     =48
Const cSI_R_DIV_16    =64
Const cSI_R_DIV_32    =80
Const cSI_R_DIV_64    =96
Const cSI_R_DIV_128   =112
Const cSI_CLK_SRC_PLL_A = 0
Const cSI_CLK_SRC_PLL_B =32
Const cXTAL_FREQ = 24999610     'Crystal frequency  was 24999610  24999480


Const cI2C_WRITE =192          ' I2C address for writing to the Si5351A
Const cI2C_READ  =193          ' I2C address for reading to the Si5351A


Dim integer dP1, dP2, dP3          'Synth config register P1, Synth config register P2, Synth config register P3
Dim integer bI2CRegister_Reg
Dim integer bI2CRegister_Data
Dim integer bOutputOff_Clk
Dim integer bSetupMultisynth_Synth
Dim integer dSetupMultisynth_Divider
Dim integer bSetupMultisynth_Div
Dim integer dSetupPLL_PLL
Dim integer bSetupPLL_Mult
Dim integer dSetupPLL_Num
Dim integer dSetupPLL_Denom
Dim integer fSetupPLL_Num
Dim integer fSetupPLL_Denom

Dim integer dSetFrequency
Dim integer dSetFrequency_PllFreq
Dim integer dSetFrequency_L
Dim integer fSetFrequency_F       'originally these were floats but operating in integer mode
Dim integer bSetFrequency_Mult
Dim integer dSetFrequency_Num
Dim integer dSetFrequency_Denom
Dim integer dSetFrequency_Divider
Dim integer ValueIn


'Dim integer SetPort   ' Used as Bool
dim CarrierON as integer
dim ToneON as integer
dim HoldWindow as integer
dim Band as integer
dim Frequency as integer
dim Freq as string length 10
dim ScreenType as integer
dim PointofTouch as integer
Dim Deviation as integer
dim PotData as integer

'Define the pins
'SetPin 3,ain           'A/D input to measure battery voltage
SetPin 21,din           'if low then 70cm, low = 2M
SetPin 24,dout          'tone out to 70cm 2B
SetPin 26,dout          'tone out to 2M   2A

'************************************************************

Sub Welcome
cls rgb(BLUE)
colour rgb(YELLOW), rgb(BLUE)
font #2,2
if Band = 0 then 
    text 40,100,"70 cms  "
    else     
    text 40,100,"2 metres"
endif
font #1,1
text 40,200,"VK5DJ sig gen"
text 40,220,"for repeater deviation"    
pause 2000
end Sub

'************************************************************

Sub FreqIn
cls rgb(blue)
colour rgb(YELLOW), rgb(BLUE)
RBox 10,200,140,40,4    'backspace box
RBox 170,200,140,40,4   'Cancel box
RBox 10,147,60,40,4     '9 box
RBOX 90,147,60,40,4     '0 box
Rbox 170,147,60,40,4    'dec point box
Rbox 250,147,60,40,4    'Enter box
RBox 10,94,60,40,4      '5 box
RBOX 90,94,60,40,4      '6 box
Rbox 170,94,60,40,4     '7 box
Rbox 250,94,60,40,4     '8 box
RBox 10,41,60,40,4      '1 box
RBOX 90,41,60,40,4      '2 box
Rbox 170,41,60,40,4     '3 box
Rbox 250,41,60,40,4     '4 box

font #2,2
text 5,1,Freq
font #2

text 20,210,"BackSp"
text 180,210,"Cancel"

text 35,158,"9"
text 115,158,"0"
text 193,158,"."
text 265,158,"Go"

text 35,105,"5"
text 115,105,"6"
text 190,105,"7"
text 270,105,"8"

text 35,55,"1"
text 115,55,"2"
text 190,55,"3"
text 270,55,"4"

end Sub

'************************************************

Sub Running
cls rgb(blue)
colour rgb(yellow), rgb(blue)
rBox 8,190,66,50,6
rBox 90,190,66,50,6
rBox 170,190,66,50,6
rBox 250,190,62,50,6
rBox 10,130,66,50,6
rBox 250,130,66,50,6
font #2
text 220,15,"VK5DJ"
font #1
text 18,200,"Change"
text 93,200,"Carrier"
text 177,200,"Moduln"
text 253,200,"Deviate"
text 15,140,"Deviate"
text 256,140,"Deviate"
font #2
text 12,155,"Down"
text 270,155,"Up"
PrintBottom
ChangeDeviation

end Sub

'*************************************************

sub PrintBottom
local PrintStr as string length 10
  Font #2
  text 10,220,"Freq"
  If CarrierON = 0 then 
    text 95,220,"OFF"
  else 
    text 95,220,"ON "
  endif
  If ToneON = 0 then 
    text 175,220,"OFF"
    else 
    text 175,220,"ON "
  endif
  Select Case Deviation
   Case 1
      text 255,220,"3.0"
   case 2
      text 255,220,"1.0"   
   case 3
      text 255,220,"U/D" 
  end select

  font #2,2
  if instr(Freq,".") = 0 then
     PrintStr = Freq +".0"
  else
     if instr(Freq,".") = len(Freq) then
       printStr = Freq + "0"
     else  
     PrintStr = Freq
     endif
  endif      
  if len(PrintStr)> 6 then
    font #2,2
    text 5,70,left$(PrintStr,10)
    font #2
    text 145,115,"MHz"
  else  
    font #2,2
    text 5,70,left$(PrintStr+" MHz",10)
    font #2
    text 145,115,"   "
  endif  
end sub

'*************************************************

sub TouchService
select case ScreenType
  case 0
      exit sub
  case 1      
      ScreenFreq
  case 2
      ScreenRunning 
 end select        
end sub   

'***************************************************

sub ScreenFreq
if touch(Y)>41 and TOUCH(Y)<81 Then          '1,2,3,4 
  if touch(X)>10 and TOUCH(X)<70 then        '1 
    Freq = Freq + "1"
  endif    
  if touch(X)>90 and TOUCH(X)<150 then       '2  
    Freq = Freq + "2"
  endif
  if TOUCH(X)>170 and TOUCH(X)<230 THEN      '3
    Freq = Freq + "3"    
  endif
  if TOUCH(X)>250 and TOUCH(X)<310 then      '4
    Freq = Freq + "4"
  endif   
  if len(freq)>10 then 
  freq = left$(freq,10)
  endif
  font #2,2
  text 5,1,Freq+space$(12-len(freq))
endif

if touch(Y)>94 and TOUCH(Y)<134 Then         '5,6,7,8  
  if touch(X)>10 and TOUCH(X)<70 then        '5
    Freq = Freq + "5"
  endif
  if touch(X)>90 and TOUCH(X)<150 then       '6
    Freq = Freq + "6"
  endif
  if TOUCH(X)>170 and TOUCH(X)<230 THEN      '7
    Freq = Freq + "7"    
  endif
  if TOUCH(X)>250 and TOUCH(X)<310 then      '8
    Freq = Freq + "8"           
  endif    
 ' font #2
  if len(  freq) >10 then 
     freq = left$(freq,10)
  endif
  font #2,2
  text 5,1,Freq+space$(12-len(freq))
endif  

if touch(Y)>147 and TOUCH(Y)<187 Then        '9,0,.,E  
  if touch(X)>10 and TOUCH(X)<70 then        '9
    Freq = Freq + "9"  
  endif
  if touch(X)>90 and TOUCH(X)<150 then       '0
    Freq = Freq + "0"
  endif
  if TOUCH(X)>170 and TOUCH(X)<230 THEN      '.  
    Freq = Freq + "."    
  endif
  if TOUCH(X)>250 and TOUCH(X)<310 then      'enter
    if len(freq)>10 then 
       freq = left$(freq,10)
    endif
    if band = 0 and (val(Freq)<430 or Val(Freq) > 450) then                      'ensures in band
      cls
      font #2
      text 50, 100, "Out of range"
      Freq = "432.0"
      Pause 2000
      HoldWindow = 0
      Screentype = 1
      exit sub
    endif
    if band = 1 and (val(Freq)<143.3 or Val(Freq) > 148) then                  
      cls
      font #2
      text 50, 100, "Out of range"
      Freq = "146.0"
      Pause 2000
      HoldWindow = 0
      Screentype = 1
      exit sub
    endif  

    Frequency = MakeInteger(Freq)            'convert freq string to integer
    
    If band = 0 then
        dsetfrequency = Frequency \ 24       'calculate 70 frequency
    else
        dsetfrequency = Frequency \ 8        'calculate 70cm frequency
    endif             
    screentype = 2
    HoldWindow = 0
    exit sub           
  endif
  font #2,2    
  text 5,1,Freq +space$(12-len(freq))
endif

 if touch(Y)>200 and TOUCH(Y)<240 Then      'Backspace and cancel 
  if touch(X)>10 and TOUCH(X)<150 then      'Backspace
    If len(Freq)>0 then 
       Freq = left$(freq, len(freq)-1)
       font #2,2     
       text 5,1,freq+ space$(12-len(freq))       
    endif   
  endif
  if touch(X)>170 and TOUCH(X)<310 then      'Cancel  
'    if len(freq)=0 then
'       Frequency = 27523437                  'this will need to measure the port later to give band default
'    endif
    HoldWindow = 0                           'allow running window to be called 
    screentype = 2
  endif                                      'end of frequency screen 
 endif    
end sub 

'************************************************************************************************************************

'now the running screen                      'working screen
sub ScreenRunning:                                         
 if touch(Y)>190 and touch(Y)<240 then
  if touch(X)> 9 and TOUCH(X)<85 then        'New freq
    cls
    Screentype = 1                           'set for the frequency entry screen
    HoldWindow = 0                           'allow the next window 
    exit sub
  endif
  if touch(X)>90 and TOUCH(X)<165 then      'Carrier OFF/ON
    if CarrierON = 1 then 
       CarrierOn=0

       SI5351A_OutputOff(17)                'turn off clk0
    else
       CarrierOn=1
       if band =0 then 
          dSetFrequency = Frequency \ 24    '70cm working from 3rd harmonic
       else 
          dSetFrequency = Frequency \ 8     'working from 1st harmonic 
       endif   
       SI5351A_SetFrequency(dSetFrequency)        
    endif 
  endif  
    
  if touch(X)>170 and TOUCH(X)<245 then      'Tone OFF/ON
       if ToneON = 1 then 
        ToneON = 0
       else       
        ToneON = 1
       endif    
       if ToneON = 1 then 
         pwm 2, 1000, 50, 50                    'Tone on
       else 
         pwm 2, stop
       endif
  endif   
    
  if touch(X)>250 and TOUCH(X)<320 then      'Deviation (Deviate button)   
      Deviation = Deviation+1
      if deviation>3 then Deviation = 1
      if Deviation<>3 then 
         Text 90,150, "        "
      endif   
  endif 
 endif 
if touch(y)>130 and touch(y)<180 then        'update potdata on screen
   if touch(X)>10 and touch(x)<75 then       'Deviate down
      if PotData = 0 then 
         PotData = 255
      else
         PotData = PotData -1
      endif
   endif
   if touch(X)>250 and touch(x)<316 then      'Deviate up
      if PotData = 255 then 
         PotData = 0
      else
         PotData = PotData +1
      endif   
   endif
   font #2
   Text 90,150, "Gain="+Str$(PotData)+"  "
   deviation = 3
endif 
 PrintBottom 
 Changedeviation      
end SUB                                      'end of touch screen routines

'************************************************************ create integer for frequency
Function  MakeInteger(StrToInt$) as integer

Local Sleft$ as string length 10
local Sright$ as string length 10
local Len1 as integer
local LenRight as integer
local BeforeDot as integer
local AfterDot as integer
local ResultInteger as integer
local dotSpot as integer

if len(StrToInt$)=0 then 
   BeforeDot = 0
   AfterDot = 0
   goto DoBeforeDot
endif   
DotSpot = instr(StrToInt$,".")
if DotSpot = 0 then 
   BeforeDot = val(StrToInt$)
   BeforeDot = BeforeDot*1000000
   AfterDot=0
   goto DoBeforeDot
endif     
Len1 = len(StrToInt$)
Sleft$ = left$(StrToInt$,DotSpot-1)
Sright$= right$(StrToInt$,Len1 - DotSpot)
Lenright = len(Sright$)
BeforeDot = Val(Sleft$)
BeforeDot = BeforeDot * 1000000
AfterDot = Val(Sright$)
select Case Lenright
case 1
     AfterDot = AfterDot * 100000
Case 2
     AfterDot = AfterDot * 10000
Case 3
    AfterDot = AfterDot * 1000  
Case 4
    AfterDot = AfterDot * 100     
Case 5
    AfterDot = AfterDot * 10 
end select    
DoBeforeDot:
MakeInteger = BeforeDot+AfterDot

end function

'-------------------------------------------------------------- Calculate level for digital pot

Sub ChangeDeviation 
local FreqD as float
local TempD as float
if Band=1 then                  'adjust to 1/3 of entry frequency for harmonic
   FreqD = val(Freq)
else
   FreqD = Val(freq)/3
endif      
font #1

'this allocates deviation for 1kHz
If Deviation = 2 or (band = 0 and Deviation = 1)  then 'or band = 0 then                       'use 1=3.0kHz, 2=1kHz, 3=Up/Dn
'first for 143 to 148 for 1kHz
  if FreqD>=143.0 and freqD<145.4 then PotData= 15
  If FreqD>=145.4 and FreqD<145.8 then PotData = 16
  if FreqD>=145.8 and freqD<146.2 then PotData= 17
  If FreqD>=146.2 and FreqD<146.6 then PotData = 18
  if FreqD>=146.6 and freqD<147.0 then PotData= 19
  If FreqD>=147.0 and FreqD<147.2 then PotData = 20
  if FreqD>=147.2 and freqD<147.4 then PotData= 21
  If FreqD>=147.4 and FreqD<147.6 then PotData = 22
  if FreqD>=147.6 and freqD<147.8 then PotData= 23
  If FreqD>=147.8 then PotData = 24
  goto JumpADD
endif
If Deviation = 1 then                     'this allocates for 3kHz on 143.5 to 145
  if FreqD <=145.0 then                   
     FreqD = 0.6814 * FreqD
     PotData = FreqD - 34.762
  endif   
  if FreqD >145 and FreqD<=145.5 then
     potdata=0.9684*FreqD + 5.0886      
  endif 
  if FreqD>145.5 and FreqD<=148 then
     potdata = 22*FreqD - 3137.4
  endif     
endif
JumpADD:
I2C open 100, 100
  'I2C write pReg,0, 1, pData
  'Corrected I2C Write format. MMBasic takes care of 7bit address for
  'I2C, the read/write bit is set by the command. 
  I2C WRITE 40,0,2,170,peek(var potdata,0)                '%01010000 (80),  %10101010 (170), %10000000 (128 = half power)
  I2C close
end sub

'------------------------------------------------------------ I2C routines

Sub SI5351A_I2CSendRegister(pReg,pData)
  I2C open 100, 100
  'I2C write pReg,0, 1, pData
  'Corrected I2C Write format. MMBasic takes care of 7bit address for
  'I2C, the read/write bit is set by the command.
  'This setup assumes SI5351A is at default address &H60
  
  I2C WRITE &H60,0,2,pReg,pData
  I2C close

End Sub

Sub SI5351A_I2CReadRegister(pReg, pData)
  I2C open 100, 100
  'I2C read pReg, 0, 1, bI2CRegister_Data  'data now in bI2CRegister_Data but may need transfer to pData
  'Corected I2C Read format. MMBasic takes care of 7bit address for
  'I2C, the read/write bit is set by the command.
  'To read an address we first have to write the register address
  'and then read the number of bytes we want.
  I2C Write &H60,0,1,pReg
  I2C Read &H60,0,1,BI2CRegister_Data
  I2C close
End Sub

'------------------------------------------------------------- end of I2C routines

' Set up specified PLL with mult, num and denom
' pMult is 15..90
' pNum is 0..1,048,575 ($FFFFF)
' pDenom is 0..1,048,575 ($FFFFF)
'

Sub SI5351A_SetupPLL(pPll, pMult, pNum, pDenom)
Local TempInt as integer
  dP1 = (128 * pNum) \ pDenom
  dP1 = (128 * pMult) + dP1 - 512
  dP2 = (128 * pNum) \ pDenom
  dP2 = (128 * pNum) - (pDenom * dP2)
  dP3 = pDenom  

  SI5351A_I2CSendRegister(pPll, peek(var dP3,1))
  SI5351A_I2CSendRegister(PPll + 1, peek(var dP3,0))
  TempInt = peek(var dP1,2) And &H03  '(dP1 & $00030000) >> 16)
  SI5351A_I2CSendRegister(pPll + 2, peek(var TempInt,0))
  SI5351A_I2CSendRegister(pPll + 3, peek(var dP1,1))
  SI5351A_I2CSendRegister(pPll + 4, peek(var dP1,0))
  TempInt = ((dP3 and &H000F0000) >> 12) or (peek(Var dP2,2) and &H0F)
  SI5351A_I2CSendRegister(pPll + 5, peek(var TempInt,0))
  SI5351A_I2CSendRegister(pPll + 6, peek(var dP2,1))
  SI5351A_I2CSendRegister(pPll + 7, peek(var dP2,0))

End Sub

'---------------------------------------------------------------------------
' Set up MultiSynth with integer divider and R divider
' R divider is the bit value which is OR'ed onto the appropriate register, it is a $define
'

Sub  SI5351A_SetupMultisynth(pSynth, pDivider, pDiv)
local TempInt as integer
  dP1 = (128 * pDivider) - 512
  dP2 = 0                                         ' dP2 = 0, dP3 = 1 forces an integer value for the divider
  dP3 = 1

  SI5351A_I2CSendRegister(pSynth,    peek(var dP3,1))
  SI5351A_I2CSendRegister(pSynth + 1, peek(var dP3,0))
  TempInt = (peek(var dP1,2) And &H03) Or pDiv
  SI5351A_I2CSendRegister(pSynth + 2, peek(var TempInt,0))
  SI5351A_I2CSendRegister(pSynth + 3, peek(var dP1,1))
  SI5351A_I2CSendRegister(pSynth + 4, peek(var dP1,0))
  TempInt = ((dP3 And 983040)>>12) Or ((dP2 And &HF0000)>>16)
  SI5351A_I2CSendRegister(pSynth + 5, peek(var TempInt,0))
  SI5351A_I2CSendRegister(pSynth + 6, peek(var dP2,1))
  SI5351A_I2CSendRegister(pSynth + 7, peek(var dP2,0))

End Sub

'The following controls outputs
'---------------------------------------------------------------------------
' Switches off Si5351a output
' Example: SI5351A_OutputOff(cSI_CLK0_CONTROL) will switch off output CLK0
Sub SI5351A_OutputOff (pClk)
  SI5351A_I2CSendRegister(pClk, &H80)  
  'SI5351A_I2CSendRegister(pClk, &H80)        ' Refer to SiLabs AN619 to see bit values - $80 turns off the output stage
End Sub


'---------------------------------------------------------------------------
' Set CLK0 or CLK1 output to ON, turn off the other and set to the specified frequency
' Frequency is in the range 1MHz to 150MHz
' Example: SI5351A_SetFrequency(10000000) will set output CLK0 to 10MHz

Sub SI5351A_SetFrequency(pFrequency)
  dSetFrequency_Divider = 900000000 \ pFrequency                      ' Calculate the division ratio. 900,000,000 is the maximum internal
  If (dSetFrequency_Divider Mod 2) > 0 Then
    dSetFrequency_Divider=dSetFrequency_Divider-1
  End If ' Ensure an even integer division ratio
  dSetFrequency_PllFreq = dSetFrequency_Divider * pFrequency          ' Calculate the pllFrequency: the dSetFrequency_Divider * desired output frequency
  bSetFrequency_Mult = dSetFrequency_PllFreq \ cXTAL_FREQ             ' Determine the multiplier to get to the required pllFrequency
  dSetFrequency_L = dSetFrequency_PllFreq Mod cXTAL_FREQ              ' It has three parts:
  fSetFrequency_F = dSetFrequency_L                                   ' bSetFrequency_Mult is an integer that must be in the range 15..90
  fSetFrequency_F = fSetFrequency_F * 1048575                         ' dSetFrequency_Num and dSetFrequency_Denom are the fractional parts, the numerator and denominator
  fSetFrequency_F = fSetFrequency_F \ cXTAL_FREQ                      ' each is 20 bits (range 0..1048575)
  dSetFrequency_Num = fSetFrequency_F                                 ' the actual multiplier is  bSetFrequency_Mult + dSetFrequency_Num / dSetFrequency_Denom
  dSetFrequency_Denom = 1048575                                       ' For simplicity we set the denominator to the maximum 1048575


' Set up PLL B with the calculated multiplication ratio               'setup PLLB to output on CLK1
  SI5351A_OutputOff(cSI_CLK0_CONTROL)
  SI5351A_SetupPLL(cSI_SYNTH_PLL_B, bSetFrequency_Mult, dSetFrequency_Num, dSetFrequency_Denom)
  SI5351A_SetupMultisynth(cSI_SYNTH_MS_1, dSetFrequency_Divider, cSI_R_DIV_1)
  SI5351A_I2CSendRegister(cSI_PLL_RESET, &HA0)
  SI5351A_I2CSendRegister(cSI_CLK1_CONTROL, &H4C Or cSI_CLK_SRC_PLL_B)          '2mA or 0dB power %01001100
End Sub

Sub GetVolts
  local Volt as Float
  Local VoltStr as string length 10
  setpin 4,ain
  Volt = pin(4) * 4.49
  voltstr = str$(volt)
  font #2
  text 10,15, left$(voltstr,4) +" V  "
End Sub

'----------------------------------------------------------------------------------------------------------
'Now the Main program
'----------------------------------------------------------------------------------------------------------

'now to set things up
'valuee = 0
Band = pin(21)                              '1 = 2M and 0 = 70cm
ToneOn = 0                                  'tone on
CarrierON = 1                               'carrier on
Deviation = 1                               '1=3.0, 2=buttons
PotData = 128                               'set pot to middle of range
ChangeDeviation                             'set for 3kHz deviation
ScreenType = 0                              'opening screen 1=enter freq, 2 = resting screen
Frequency = 0
Freq= ""
SETpin 15,intl, TouchService
Welcome                                     'opening screen tells band
HoldWindow = 0                              'stops window from being rewritten 0=allow
screentype = 1                              'begin with FreqIn

do
if screentype = 2 and HoldWindow = 0 then
   holdwindow = 1
   running
   settick 1000, getvolts, 1
   SI5351A_SetFrequency(dSetFrequency)
endif

if screentype = 1 and HoldWindow = 0 then
   settick 0,0,1                            'turn off volts update
   HoldWindow = 1
   FreqIn   
endif

loop                                        'waits for the interrupt from the display
