VERSION 5.00
Begin VB.Form Form2 
   BackColor       =   &H00C0C0C0&
   Caption         =   "Triangle Frequency Output"
   ClientHeight    =   5145
   ClientLeft      =   165
   ClientTop       =   450
   ClientWidth     =   4875
   BeginProperty Font 
      Name            =   "MS Sans Serif"
      Size            =   9.75
      Charset         =   0
      Weight          =   400
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form2"
   ScaleHeight     =   5145
   ScaleWidth      =   4875
   StartUpPosition =   2  'CenterScreen
   Begin VB.CommandButton cmdOK 
      Caption         =   "OK"
      Height          =   375
      Left            =   2760
      TabIndex        =   3
      Top             =   1920
      Visible         =   0   'False
      Width           =   1095
   End
   Begin VB.CommandButton cmdCancel 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   1200
      TabIndex        =   4
      Top             =   1920
      Width           =   1095
   End
   Begin VB.TextBox txtRepeatTime 
      BackColor       =   &H00FFFFFF&
      Height          =   360
      Left            =   2520
      TabIndex        =   2
      Top             =   1200
      Width           =   1815
   End
   Begin VB.TextBox txtFrequencyTwo 
      Height          =   360
      Left            =   2520
      TabIndex        =   1
      Top             =   720
      Width           =   1815
   End
   Begin VB.CommandButton cmdEnter 
      Caption         =   "Enter"
      Height          =   375
      Left            =   2760
      TabIndex        =   6
      Top             =   1920
      Width           =   1095
   End
   Begin VB.TextBox txtCommandSequence 
      BackColor       =   &H00E0E0E0&
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   2055
      Left            =   360
      Locked          =   -1  'True
      MultiLine       =   -1  'True
      TabIndex        =   5
      TabStop         =   0   'False
      Top             =   2880
      Width           =   4215
   End
   Begin VB.TextBox txtFrequencyOne 
      Height          =   330
      Left            =   2520
      TabIndex        =   0
      Top             =   240
      Width           =   1815
   End
   Begin VB.CommandButton cmdForm1 
      Caption         =   "Form 1"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   615
      Left            =   840
      TabIndex        =   7
      Top             =   7800
      Width           =   1935
   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        =   11
      Top             =   2520
      Width           =   2535
   End
   Begin VB.Label Label3 
      Alignment       =   1  'Right Justify
      Caption         =   "Repeat Time in Milliseconds (RT)"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   0
      TabIndex        =   10
      Top             =   1320
      Width           =   2415
   End
   Begin VB.Label Label2 
      Alignment       =   1  'Right Justify
      Caption         =   "Frequency 2 in MHz (F2)"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   480
      TabIndex        =   9
      Top             =   840
      Width           =   1935
   End
   Begin VB.Label Label1 
      Alignment       =   1  'Right Justify
      Caption         =   "Frequency 1 in MHz (F1)"
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   8.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   360
      TabIndex        =   8
      Top             =   360
      Width           =   2055
   End
End
Attribute VB_Name = "Form2"
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 Form2
Form1.Show
End Sub

Private Sub cmdEnter_Click()
Dim N As Double 'number of steps
Dim a  As Boolean
Dim Fclk As Double 'clock frequency in hertz
Dim RT As Double 'Repeat Time
Dim FdOut As Double 'Desired output frequency
Dim b As Integer
Dim message As Boolean
Dim CommandSequence As Variant 'accumulates commands to be sent to DDS8m
a = False
'gAA = False
txtCommandSequence = ""
txtFrequencyOne.Locked = False
txtFrequencyTwo.Locked = False
txtRepeatTime.Locked = False
F1 = gComputeFcommand(txtFrequencyOne)
F2 = gComputeFcommand(txtFrequencyTwo)

'Check for valid frequency entries
If txtFrequencyOne = "" Or txtRepeatTime = "" Then message = True
If txtFrequencyTwo = "" Or F2 <= F1 Then message = True
If message = True Then
  MsgBox ("F1 must be smaller than F2 and all fields must have entries.  One or more of the frequency fields is empty or F1 is too big or F2 is too small.  Please correct the error and try again")
  Exit Sub
End If

'Compute Td
Td = 1

'Compute the number of steps and Tr and check to be sure they are within bounds
Fclk = gGetClockFreq() 'clock frequency in hertz
RT = txtRepeatTime * 0.001 'compute repeat time in seconds
N = (RT * Fclk * gClockMultiplier) / (2 * (Td + 1)) 'compute number of steps
N = Round(N)
RT = 1000 * 2 * N * (Td + 1) / (Fclk * gClockMultiplier) 'compute RT in milliseconds using the rounded value of steps N
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
Tr = N * (Td + 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 Fd
FdOut = (txtFrequencyTwo - 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 F2 and/or decrease F1 and try again")
  Exit Sub
End If

'Lock entry fields
txtFrequencyOne.Locked = True
txtFrequencyTwo.Locked = True
txtRepeatTime.Locked = True

'Accumlate DDS8m commands and display in txtCommandSequence field
'The cmdOK_click() sub actually causes the commands to be sent
CommandSequence = "The Number of frequency steps from F1 to F2 is " & N & ". The following Commands will be sent to the DDS8m:" & vbCrLf
CommandSequence = CommandSequence & "M 0" & vbCrLf
CommandSequence = CommandSequence & "F1 " & F1 & vbCrLf
CommandSequence = CommandSequence & "F2 " & F2 & vbCrLf
CommandSequence = CommandSequence & "Fd " & Fd & vbCrLf
CommandSequence = CommandSequence & "Td " & Td & vbCrLf
If Tr < 4294967296# Then CommandSequence = CommandSequence & "Tr " & Tr & vbCrLf
CommandSequence = CommandSequence & "M 2" & 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) = "F2 " & F2 & Chr$(13)
gOut(3) = "Fd " & Fd & Chr$(13)
gOut(4) = "Td " & Td & Chr$(13)
If Tr < 4294967296# Then
  gOut(5) = "Tr " & Tr & Chr$(13)
Else
  gOut(5) = "Tr to big"
End If
gOut(6) = "M 2" & Chr$(13)
Unload Form2
Form1.Show
End Sub

Private Sub Form_Load()
txtFrequencyOne = ""
txtFrequencyTwo = ""
txtRepeatTime = ""
cmdEnter.Visible = True
cmdOK.Visible = False
'gAA = False
End Sub

Public Function ComputeTd(DT As Double) As Integer
Dim Td As Double
Td = (DT * Form1.txtClockFrequency * gClockMultiplier) - 1
ComputeTd = Format(Td, "#######")
End Function

Private Sub txtRepeatTime_LostFocus()
txtRepeatTime = Format(txtRepeatTime, "####0.0#####")
End Sub


Private Sub txtFrequencyOne_LostFocus()
txtFrequencyOne = Format(txtFrequencyOne, "#0.0###########")
End Sub

Private Sub txtFrequencyTwo_LostFocus()
txtFrequencyTwo = Format(txtFrequencyTwo, "#0.0###########")
End Sub
