⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vb2d9.tmp

📁 AD5933评估板下位机程序(keil C)和上位机程序(VB)
💻 TMP
📖 第 1 页 / 共 5 页
字号:
Dim MinMagnitude As Double
Dim sweep_phase As Double
Dim Frequency As Double
Dim Increment As Long ' number of increments
Dim i As Integer
Dim xy As Variant

Dim varray As Variant

                IndexArray = 0
                Increment = NumberIncrements + 1 ' number of increments in the sweep
                Frequency = StartFrequency   ' the sweep starts from here
                PBar.Min = 0
                PBar.Max = NumberIncrements
                ' Enter normal mode
                EnterStandbyMode
                UpdateStartFrequency
                UpdateNumberIncrements
                UpdateFrequencyIncrement
                UpdateSettlingTime
                '----------------------------- systemn clock ---------------------
                If (ExternalClock.Value = True) Then
                ' Enable external Oscillator
                WritetToControlRegister2 &H81, &H8
                Else
                ' Enable internal Oscillator
                WritetToControlRegister2 &H81, &H0
                End If
                '----------------------------initialise sensor----------------------
                ' Initialise Sensor with Start Frequency
                WritetToControlRegister &H80, &H10
                msDelay 2  'this is user determined
                '------------------------start the frequency sweep-------------------
                ' Start Frequency Sweep
                WritetToControlRegister &H80, &H20
                
                '--------------------------------------------------------------------
                ' Enter Frequency Sweep Loop
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H4 ' mask off bit D2
                Do While ((ReadbackStatusRegister <> 4) And (Increment <> 0))
                ' check to see if current sweep point complete
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H2 ' mask off bit D1
                '------------------------------------------------------------------------
                If (ReadbackStatusRegister = 2) Then
                ' this sweep point has returned valid data so we can proceed with sweep
                Else
                Do
                'if valid data has not been returned then we need to pole stat reg until such time as valid data
                'has been returned
                'i.e. if point is not complete then Repeat sweep point and pole staus reg until valid data returned
                WritetToControlRegister &H80, &H40 'repeat sweep point
                       Do
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H2 ' mask off bit D1- Wait until dft complete
                       Loop While (ReadbackStatusRegister <> 2)
                
                Loop Until (ReadbackStatusRegister = 2)
                End If
                '-------------------------------------------------------------------------
                RealDataUpper = PortRead(&HD, &H94)
                RealDataLower = PortRead(&HD, &H95)
                RealData = RealDataLower + (RealDataUpper * 256)
                
                If RealData < &H7FFF Then  ' h7fff 32767
                ' Positive
                Else
                ' Negative
                RealData = RealData And &H7FFF
                RealData = RealData - &H7FFF
                End If
                ImagineryDataUpper = PortRead(&HD, &H96)
                ImagineryDataLower = PortRead(&HD, &H97)
                ImagineryData = ImagineryDataLower + (ImagineryDataUpper * 256)
                
                If ImagineryData < &H7FFF Then
                ' Positive Data.
                Else
                ' Negative
                ImagineryData = ImagineryData And &H7FFF
                ImagineryData = ImagineryData - &H7FFF
                End If
                Magnitude = ((RealData ^ 2) + (ImagineryData ^ 2)) ^ 0.5
                
                   If (Form1.Option7.Value = True And Form1.Option8.Value = False) Then
                ' mid point cal
                sweep_phase = -(calibration_phase_mid_point - phase2(ImagineryData, RealData))
                GainFactor = Val(Text5.Text)
'                Impedance = (1 / (Magnitude * GainFactor)) - Val(Form10.Text1.Text)
                Impedance = 1 / (Magnitude * GainFactor)
                End If
               If (Form1.Option7.Value = False And Form1.Option8.Value = True) Then
                ' multipoint cal
                sweep_phase = -(calibration_phase_multi_point(IndexArray) - phase2(ImagineryData, RealData))
                GainFactor = calibration_GainFactors(IndexArray)
'                Impedance = (1 / (Magnitude * GainFactor)) - Val(Form10.Text1.Text)
                Impedance = 1 / (Magnitude * GainFactor)
                End If
                         
                ' Write Data to each array.
                MagnitudeArray(IndexArray) = Impedance
                PhaseArray(IndexArray) = sweep_phase
                ImagineryDataArray(IndexArray) = ImagineryData
                code(IndexArray) = Magnitude
                RealDataArray(IndexArray) = RealData
                Increment = Increment - 1 ' incremet was set to number of increments of sweep at the start
                FrequencyPoints(IndexArray) = Frequency
                Frequency = Frequency + FrequencyIncrements ' holds the current value of the sweep freq
                IndexArray = IndexArray + 1
                
                '------------- Check to see if sweep complete ----------------------
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H4 ' mask off bit D2
                ' Increment to next frequency point Frequency
                WritetToControlRegister &H80, &H30
                Loop
                
                
                
                
                
                
                
                '--- the sweep is complete at this point now we plot the data --------------
                ' Calculate minimum and maximum values for magnitude plot
                MinMagnitude = MagnitudeArray(0)
                MaxMagnitude = MagnitudeArray(0)
                For i = 0 To NumberIncrements - 1
                ' Setting the max value for the plot.
                If MagnitudeArray(i) > MaxMagnitude Then
                  MaxMagnitude = MagnitudeArray(i)
                End If
                ' Setting the min value for the plot.
                If MagnitudeArray(i) < MinMagnitude Then
                   MinMagnitude = MagnitudeArray(i)
                End If
                Next i
                '----------------------------------------------------------------------------
                ' Calculate minimum and maximum values for phase plot
                MinPhase = PhaseArray(0)
                MaxPhase = PhaseArray(0)
                For i = 0 To NumberIncrements - 1
                ' Setting the max value for the plot.
                If PhaseArray(i) > MaxPhase Then
                   MaxPhase = PhaseArray(i)
                End If
                
                ' Setting the min value for the plot.
                If PhaseArray(i) < MinPhase Then
                   MinPhase = PhaseArray(i)
                End If
                Next i
                '-----------------------------------------------------------------------------
                 ReDim xy(0 To 1, 0 To IndexArray - 1) As Double
                'strip1 = impedance vs frequency plot
                Form1.Strip1.TrackID = (0)
                Form1.Strip1.ClearAll
                ' clear the buffer for the graph
                 Form1.Strip1.XSpan = (NumberIncrements * FrequencyIncrements)
                 
                 'set the amount of x axis you view
                 Form1.Strip1.XDisplayMin = FrequencyPoints(0) 'start frequency
                 ' sets the x axis to start at the "start frequency"
                 Form1.Strip1.XDisplayMax = FrequencyPoints(IndexArray - 1) 'stop frequency
                 ' set the x axis to finish at the " final programmed frequency"
                 'set the amount of x axis you view
                                  
                 Form1.Strip1.TrackMax = (MaxMagnitude * 1.1)
                 Form1.Strip1.TrackMin = (MinMagnitude * 0.9)
                 
                 Form1.Strip1.TrackDisplayMax = (MaxMagnitude * 1.1)  'determines the max value of the top handle
                 Form1.Strip1.TrackDisplayMin = (MinMagnitude * 0.9)  'determines the min value of te bottom handle
                 Form1.Strip1.MaxBufferSize = NumberIncrements + 1
               
                
              
                For i = 0 To IndexArray - 1
                    xy(0, i) = FrequencyPoints(i)
                    xy(1, i) = MagnitudeArray(i)
                    PBar.Value = i
                Next
                varray = xy
                Form1.Strip1.AddXYArray 0, varray ' Add Graph X,Y Data
                DoEvents
                    If (Check1.Value = 1) Then
                        theoryplot                  ' Add Graph X,Y Data
                        Form1.Strip1.TrackID = (1)
                        Form1.Strip1.XDisplayMin = FrequencyPoints(0) 'start frequency
                        Form1.Strip1.XDisplayMax = FrequencyPoints(IndexArray - 1) 'stop frequency
                        DoEvents
                    Else
                    End If
                
                 
                '-----------------------------------------------------------------------------
                ' plot graph of Phase vs Frequency
                ReDim xy(0 To 1, 0 To IndexArray - 1) As Double
                Form1.Strip2.CaptionID = 0
                Form1.Strip2.ClearAll
                Form1.Strip2.XDisplayMin = FrequencyPoints(0)
                Form1.Strip2.XDisplayMax = FrequencyPoints(IndexArray - 1)
                Form1.Strip2.XSpan = (NumberIncrements * FrequencyIncrements)  ' set the amount of x axis you view
                Form1.Strip2.TrackDisplayMax = 360
                Form1.Strip2.TrackMax = 360
                Form1.Strip2.TrackDisplayMin = -360
                Form1.Strip2.TrackMin = -360
                Form1.Strip2.MaxBufferSize = NumberIncrements + 1
                
                    
                    For i = 0 To IndexArray - 1
                        xy(0, i) = FrequencyPoints(i)
                        xy(1, i) = PhaseArray(i)
                    Next
                    varray = xy
                    Form1.Strip2.AddXYArray 0, varray
                    Form1.Strip2.XDisplayMin = FrequencyPoints(0)
                    Form1.Strip2.XDisplayMax = FrequencyPoints(IndexArray - 1)
                    DoEvents

                 
                 
                 
                 
                 MsgBox " sweep complete "
                 PBar = 0
                 DoEvents
'                 'take a temperature measurement
'                  MeasureTemperature
                Exit Sub
                
sweepErrorMsg:
                    MsgBox "Error completing sweep check values"
End Sub
Private Sub sweep2()
On Error GoTo sweepErrorMsg2
Dim ReadbackStatusRegister As Long
Dim RealData As Double
Dim RealDataUpper As Long
Dim RealDataLower As Long
Dim ImagineryData As Double
Dim ImagineryDataLower As Long
Dim ImagineryDataUpper As Long
Dim Magnitude As Double
Dim Impedance As Double
Dim MaxMagnitude As Double
Dim MinMagnitude As Double
Dim phase As Long
Dim Frequency As Double
Dim Increment As Long ' number of increments
Dim i As Integer
Dim xy As Variant

Dim varray As Variant
DoEvents
                IndexArray = 0
                Increment = NumberIncrements + 1 ' number of increments in the sweep
                Frequency = StartFrequency   ' the sweep starts from here
                PBar.Min = 0
                PBar.Max = NumberIncrements
                ' Enter normal mode
                EnterStandbyMode
                UpdateStartFrequency
                UpdateNumberIncrements
                UpdateFrequencyIncrement
                UpdateSettlingTime
                '----------------------------- systemn clock ---------------------
                If (ExternalClock.Value = True) Then
                ' Enable external Oscillator
                WritetToControlRegister2 &H81, &H8
                Else
                ' Enable internal Oscillator
                WritetToControlRegister2 &H81, &H0
                End If
                '----------------------------initialise sensor----------------------
                ' Initialise Sensor with Start Frequency
                WritetToControlRegister &H80, &H10
                msDelay 2  'this is user determined
                '------------------------start the frequency sweep-------------------
                ' Start Frequency Sweep
                WritetToControlRegister &H80, &H20
                
                '--------------------------------------------------------------------
                ' Enter Frequency Sweep Loop
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H4 ' mask off bit D2
                Do While ((ReadbackStatusRegister <> 4) And (Increment <> 0))
                ' check to see if current sweep point complete
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H2 ' mask off bit D1
                '------------------------------------------------------------------------
                If (ReadbackStatusRegister = 2) Then
                ' this sweep point has returned valid data so we can proceed with sweep
                Else
                Do
                'if valid data has not been returned then we need to pole stat reg until such time as valid data
                'has been returned
                'i.e. if point is not complete then Repeat sweep point and pole staus reg until valid data returned
                WritetToControlRegister &H80, &H40 'repeat sweep point
                       Do
                ReadbackStatusRegister = PortRead(&HD, &H8F)
                ReadbackStatusRegister = ReadbackStatusRegister And &H2 ' mask off bit D1- Wait until dft complete
                       Loop While (ReadbackStatusRegister <> 2)
                
                Loop Until (ReadbackStatusRegister = 2)
                End If
                '-------------------------------------------------------------------------
                RealDataUpper = PortRead(&HD, &H94)
                RealDataLower = PortRead(&HD, &H95)
                RealData = RealDataLower + (RealDataUpper * 256)
                
                If RealData < &H7FFF Then  ' h7fff 32767
                ' Positive
                Else
                ' Negative
                RealData = RealData And &H7FF

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -