VERSION 5.00
Begin VB.Form Form3 
   Caption         =   "Sawtooth Frequency Sweep"
   ClientHeight    =   5280
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5040
   LinkTopic       =   "Form3"
   ScaleHeight     =   5280
   ScaleWidth      =   5040
   StartUpPosition =   2  'CenterScreen
   Begin VB.TextBox txtFrequencyOne 
      Height          =   330
      Left            =   2640
      TabIndex        =   0
      Top             =   360
      Width           =   1815
   End
   Begin VB.TextBox txtCommandSequence 
      BackColor       =   &H00E0E0E0&
      ForeColor       =   &H00000000&
      Height          =   2055
      Left            =   360
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      TabIndex        =   6
      TabStop         =   0   'False
      Top             =   3000
      Width           =   4215
   End
   Begin VB.CommandButton cmdEnter 
      Caption         =   "Enter"
      Height          =   375
      Left            =   2760
      TabIndex        =   3
      Top             =   2040
      Width           =   1095
   End
   Begin VB.TextBox txtRepeatTime 
      BackColor       =   &H00FFFFFF&
      Height          =   360
      Left            =   2640
      TabIndex        =   2
      Top             =   1320
      Width           =   1815
   End
   Begin VB.CommandButton cmdCancel 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   1200
      TabIndex        =   4
      Top             =   2040
      Width           =   1095
   End
   Begin VB.CommandButton cmdOK 
      Caption         =   "OK"
      Height          =   375
      Left            =   2760
      TabIndex        =   5
      Top             =   2040
      Visible         =   0   'False
      Width           =   1095
   End
   Begin VB.TextBox txtFinishFrequency 
      Height          =   360
      Left            =   2640
      TabIndex        =   1
      Top             =   840
      Width           =   1815
   End
   Begin VB.Label Label1 
      Alignment       =   1  'Right Justify
      Caption         =   "Frequency 1 in MHz (F1)"
      Height          =   255
      Left            =   480
      TabIndex        =   10
      Top             =   480
      Width           =   2055
   End
   Begin VB.Label Label3 
      Alignment       =   1  'Right Justify
      Caption         =   "Repeat Time in Milliseconds (RT)"
      Height          =   255
      Left            =   120
      TabIndex        =   9
      Top             =   1440
      Width           =   2415
   End
   Begin VB.Label Label6 
      Alignment       =   1  'Right Justify
      Caption         =   "Finish Frequency in MHz (Ffinish)"
      Height          =   255
      Left            =   120
      TabIndex        =   8
      Top             =   960
      Width           =   2415
   End
   Begin VB.Label Label4 
      Alignment       =   2  'Center
      Caption         =   "Computation Results"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   1200
      TabIndex        =   7
      Top             =   2640
      Width           =   2535
   End
End
Attribute VB_Name = "Form3"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Td As Double
Private Tr As Double
Private Fd As Double
Private F1 As Double
Private F2 As Double

Private Sub cmdCancel_Click()
Unload Form3
Form1.Show
End Sub

Private Sub cmdEnter_Click()
Dim N As Double 'number of frequency steps
Dim a  As Boolean
Dim b As Double
Dim FdOut As Double '
Dim RT As Double 'Repeat Time
Dim Ffinish As Double
Dim Fclk As Double
Dim message As Boolean
Dim CommandSequence As Variant
a = False
txtCommandSequence = ""
txtFrequencyOne.Locked = False
txtFinishFrequency.Locked = False
F1 = gComputeFcommand(txtFrequencyOne)
Ffinish = gComputeFcommand(txtFinishFrequency)

'Check for valid frequency entries
If txtFrequencyOne = "" Then message = True
If txtFinishFrequency = "" Then message = True
If message = True Then
  MsgBox ("One or more of the frequency fields is empty.  Please correct the error and try again")
  Exit Sub
End If
If F1 > 100 Then message = True 'Fcommand values above 100 exceed approx 40% of clock regardless of the actual clock frequency
If Ffinish > 100 Then message = True
If message = True Then
  MsgBox ("One or more of the frequency fields is too large.  Please correct the error and try again")
  Exit Sub
End If

'Set Td
Td = 1

'Compute Tr and check to be sure it is within bounds
Fclk = gGetClockFreq() 'Fclk is in hertz
Tr = (txtRepeatTime * 0.001 * Fclk * gClockMultiplier) / 2 - 1
If Tr < 5 Then
  MsgBox ("The Repeat Time (RT) is too small.  Please increase the Repeat Time and try again")
  Exit Sub
End If
Tr = Format(Tr, "##########")
If Tr > 4294967295# Then
   MsgBox "The Repeat Time is too large. Please reduce the Repeat Time and try again"
  Exit Sub
End If

'Compute RT
RT = 10 ^ 3 * (2 * (Tr + 1)) / (Fclk * gClockMultiplier)
RT = Format(RT, "##########.0#######")
If (RT - txtRepeatTime) > 0.000000001 Then
  txtRepeatTime = RT
  MsgBox ("The Repeat Time has been changed to provide an even number of steps")
  Exit Sub
ElseIf (RT - txtRepeatTime) < -0.000000001 Then
  txtRepeatTime = RT
  MsgBox ("The Repeat Time has been changed to provide an even number of steps")
  Exit Sub
End If

'Compute N and Fd
N = 2 * (Tr + 1) / (Td + 1) - 1  'Td is fixed at 1 so this reduces to N = Tr
FdOut = (txtFinishFrequency - txtFrequencyOne) / N  'FdOut is Fd output value in MHz
Fd = gComputeFcommand(FdOut)  'Fd is the Fd command value

'Check that Fd is larger than 10^-6 hertz
If (Fd < 10 ^ -12) Then
  MsgBox ("The input values are out of range.  Either decrease the Repeat Time (RT) and/or increase the Finish Frequency and try again")
  Exit Sub
End If

'Lock entry fields
txtFrequencyOne.Locked = True
txtFinishFrequency.Locked = True
txtRepeatTime.Locked = True

'Accumlate DDS8m commands and display in txtCommandSequence field
'The cmdOK_click() sub causes the commands to be sent
CommandSequence = "There will be " & N & " frequency steps." & vbCrLf
CommandSequence = CommandSequence & "The following Commands will be sent to the DDS8m:" & vbCrLf
CommandSequence = CommandSequence & "M 0" & vbCrLf
CommandSequence = CommandSequence & "F1 " & F1 & vbCrLf
CommandSequence = CommandSequence & "Fd " & Fd & vbCrLf
CommandSequence = CommandSequence & "Td " & Td & vbCrLf
If Tr < 4294967296# Then CommandSequence = CommandSequence & "Tr " & Tr & vbCrLf
CommandSequence = CommandSequence & "M 3" & vbCrLf
txtCommandSequence = CommandSequence
cmdEnter.Visible = False
cmdOK.Visible = True
End Sub

Private Sub cmdForm1_Click()
Form1.Show
End Sub

Private Sub cmdOK_Click()
gOut(0) = "M 0" & Chr$(13)
gOut(1) = "F1 " & F1 & Chr$(13)
gOut(2) = "Fd " & Fd & Chr$(13)
gOut(3) = "Td " & Td & Chr$(13)
If Tr < 4294967296# Then
  gOut(4) = "Tr " & Tr & Chr$(13)
Else
  gOut(4) = "Tr to big"
End If
gOut(5) = "M 3" & Chr$(13)
Unload Form3
Form1.Show
End Sub

Private Sub Form_Load()
txtFrequencyOne = ""
txtRepeatTime = ""
txtFinishFrequency = ""
'txtDwellTime = ""
cmdEnter.Visible = True
cmdOK.Visible = False
'gAA = False
'txtStepTime = Format(txtStepTime, "###0.####")
End Sub

Private Sub txtFinishFrequency_LostFocus()
txtFinishFrequency = Format(txtFinishFrequency, "#0.0###########")
End Sub


Private Sub txtFrequencyOne_LostFocus()
txtFrequencyOne = Format(txtFrequencyOne, "#0.0###########")
End Sub

Private Sub txtRepeatTime_LostFocus()
txtRepeatTime = Format(txtRepeatTime, "#########0.0#######")
End Sub
