VERSION 5.00
Begin VB.Form mainFrm 
   Caption         =   "DDS-3X25 USB Demo"
   ClientHeight    =   4800
   ClientLeft      =   4200
   ClientTop       =   3405
   ClientWidth     =   6780
   LinkTopic       =   "Form1"
   ScaleHeight     =   4800
   ScaleWidth      =   6780
   Begin VB.Timer Timer1 
      Interval        =   50
      Left            =   5520
      Top             =   3960
   End
   Begin VB.OptionButton Option_Count 
      Caption         =   "Counter"
      Height          =   375
      Left            =   5280
      TabIndex        =   10
      Top             =   960
      Width           =   975
   End
   Begin VB.Frame Frame2 
      Caption         =   "Measure"
      Height          =   3375
      Left            =   3360
      TabIndex        =   8
      Top             =   240
      Width           =   3375
      Begin VB.CommandButton Command1 
         Caption         =   "Reset"
         Height          =   375
         Left            =   960
         TabIndex        =   12
         Top             =   2160
         Width           =   1215
      End
      Begin VB.TextBox textMeasure 
         Alignment       =   2  'Center
         BackColor       =   &H8000000B&
         ForeColor       =   &H0000C000&
         Height          =   375
         Left            =   840
         Locked          =   -1  'True
         TabIndex        =   11
         Top             =   1200
         Width           =   1575
      End
      Begin VB.OptionButton Option_Freq 
         Caption         =   "Frequency"
         Height          =   375
         Left            =   480
         TabIndex        =   9
         Top             =   720
         Value           =   -1  'True
         Width           =   1335
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "Download"
      Height          =   3375
      Left            =   120
      TabIndex        =   0
      Top             =   240
      Width           =   3135
      Begin VB.CommandButton download 
         Caption         =   "Download"
         Height          =   495
         Left            =   840
         TabIndex        =   14
         Top             =   2640
         Width           =   1575
      End
      Begin VB.TextBox m_offset 
         Height          =   375
         Left            =   1440
         TabIndex        =   7
         Top             =   2040
         Width           =   1095
      End
      Begin VB.TextBox m_amp 
         Height          =   375
         Left            =   1440
         TabIndex        =   5
         Top             =   1440
         Width           =   1095
      End
      Begin VB.TextBox m_frequency 
         Height          =   375
         Left            =   1440
         TabIndex        =   3
         Top             =   840
         Width           =   1095
      End
      Begin VB.Label label4 
         Alignment       =   1  'Right Justify
         Caption         =   "Offset"
         Height          =   255
         Left            =   360
         TabIndex        =   6
         Top             =   2040
         Width           =   855
      End
      Begin VB.Label Label3 
         Alignment       =   1  'Right Justify
         Caption         =   "Amplitude"
         Height          =   255
         Left            =   360
         TabIndex        =   4
         Top             =   1440
         Width           =   855
      End
      Begin VB.Label Label2 
         Alignment       =   1  'Right Justify
         Caption         =   "Frequency"
         Height          =   255
         Left            =   360
         TabIndex        =   2
         Top             =   840
         Width           =   855
      End
      Begin VB.Label Label1 
         Caption         =   "Waveform: sine"
         Height          =   255
         Left            =   240
         TabIndex        =   1
         Top             =   360
         Width           =   1575
      End
   End
   Begin VB.Label lableStatus 
      Alignment       =   2  'Center
      BorderStyle     =   1  'Fixed Single
      Caption         =   "sda"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   26.25
         Charset         =   0
         Weight          =   700
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H000000FF&
      Height          =   615
      Left            =   2040
      TabIndex        =   13
      Top             =   3960
      Width           =   2655
   End
End
Attribute VB_Name = "mainFrm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public g_frequency As Double   'frequency
Public g_amplitude As Double   'amplitude
Public g_offset As Double      'offset
Public g_bDownload As Boolean  'if equal 1, download waveform
Public g_bReset As Boolean     'whether reset counter or not
Public g_Measure As Double    'measure value
Public g_iDevice As Integer    'the serial index of device
Public g_nDevice As Integer    'the number of devices connected to PC
Public g_nWavePointNum As Long     'waveform  point number to download
Public g_nWavePeriodNum As Long     'waveform  period number to download
Public g_nFreCnt As Long
Const MAX_VOLT = 3.5                'max amplitude is 3.5V
Const PI = 3.1415926



Function myCreateBuf()
Dim i As Integer
Dim tmp As Double
Dim arBuffer(4095) As Integer     'waveform buffer
For i = 0 To g_nWavePointNum
    tmp = g_offset / MAX_VOLT + g_amplitude / MAX_VOLT * Sin((i / g_nWavePointNum) * 2 * PI * g_nWavePeriodNum)
    tmp = 2048 - 2047 * tmp
    If tmp > 4095 Then
    tmp = 4095
    ElseIf tmp < 0 Then
    tmp = 0
    End If
    arBuffer(i) = tmp
Next
result = DDSDownload(g_iDevice, arBuffer(0), g_nWavePointNum)
End Function
Function myDownload()
If g_bDownload = True Then
    result = DDSSetFrequency(g_iDevice, g_frequency, g_nWavePointNum, g_nWavePeriodNum)
    myCreateBuf
    g_bDownload = False
End If
End Function
Function myMeasure()
'Dim aaa As String

If g_bReset = True Then
    DDSResetCounter (g_iDevice)
    g_bReset = False
End If
If Option_Freq.value = True Then    'frequency
    g_nFreCnt = g_nFreCnt + 1
    If g_nFreCnt > 10 Then
        g_nFreCnt = 0
        result = DDSGetMeasure(g_iDevice, 0, g_Measure)
        If g_Measure < 1000 Then
            textMeasure.Text = Format(g_Measure, "###0.00KHz")
        ElseIf g_Measure < 100000 Then
            textMeasure.Text = Format(g_Measure / 1000, "###0.00KHz")
        Else
            textMeasure.Text = Format(g_Measure / 1000000, "###0.00MHz")
           
        End If
    End If
Else                                'counter
    result = DDSGetMeasure(g_iDevice, 1, g_Measure)
    textMeasure.Text = Format(g_Measure, "00000000000")
End If

End Function

Private Sub Command1_Click()
    g_bReset = True
End Sub

Private Sub download_Click()
g_frequency = m_frequency.Text
g_amplitude = m_amp.Text
g_offset = m_offset.Text
g_bDownload = True
End Sub

Private Sub Form_Load()

g_frequency = 1000
g_amplitude = 2#
g_offset = 0
g_bDownload = True
g_bReset = False
g_Measure = 0
g_iDevice = 0
g_nDevice = 0
g_nWavePointNum = 0
g_nWavePeriodNum = 0
g_nFreCnt = 0
m_frequency.Text = g_frequency
m_amp.Text = g_amplitude
m_offset.Text = g_offset
End Sub

Private Sub Option_Count_Click()
Option_Freq.value = False
End Sub

Private Sub Option_Freq_Click()
Option_Count.value = False
End Sub

Private Sub Timer1_Timer()
If g_nDevice = 0 Then
    g_nDevice = DDSSearch
    lableStatus.Caption = "No Item"
Else
    myDownload
    myMeasure
    If DDSCheck(g_iDevice) = 0 Then
        g_nDevice = 0
    End If
    If g_nDevice = 1 Then
        lableStatus.Caption = "1 Item"
    Else
        lableStatus.Caption = Str(g_nDevice) + " Items"
    End If
End If
End Sub
