📄 form10.frm
字号:
Increment = NumberIncrements + 1 ' number of increments in the sweep
Frequency = StartFrequency ' the sweep starts from here
' AD5933 gain and offset
AD5933_Gain_cal = Val(Text1.Text)
AD5933_offset_cal = Val(Text2.Text)
' Enter normal mode
EnterStandbyMode
UpdateStartFrequency
UpdateNumberIncrements
UpdateFrequencyIncrement
UpdateSettlingTime
'----------------------------- systemn clock ---------------------
If (conductivity.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
'phase = phase2(ImagineryData, RealData)
GainFactor = Val(conductivity.Text5.Text)
Impedance = 1 / (Magnitude * GainFactor)
Admittance = 1 / ((Impedance - AD5933_offset_cal) / AD5933_Gain_cal)
' Write Data to each array.
MagnitudeArray(IndexArray) = Admittance
PhaseArray(IndexArray) = phase
'conductivityArray(IndexArray) = (((AD5933_Gain / (Impedance - AD5933_Offset)) - Conductivity_offset) / Conductivity_gain * 1000000#)
ImagineryDataArray(IndexArray) = ImagineryData
RealDataArray(IndexArray) = RealData
code(IndexArray) = Magnitude
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 the average impedance
Average_Admittance = MagnitudeArray(0)
Sum_Admittance = 0
For i = 0 To NumberIncrements - 1
Sum_Admittance = Sum_Admittance + MagnitudeArray(i)
Next i
Average_Admittance = Sum_Admittance / NumberIncrements
Sum_impedance = 0
'----------------------------------------------------------------------------
Text11.Text = Average_Admittance
Average_impedance = 0
MsgBox " sweep complete "
Command5.Enabled = True
Exit Sub
sweepErrorMsg3:
MsgBox "Error completing Calibration sweep check values"
End Sub
Private Sub Command5_Click()
On Error GoTo sweepErrorMsg3
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 Admittance As Double
Dim MaxMagnitude As Double
Dim MinMagnitude As Double
Dim MaxConductivity As Double
Dim MinConductivity As Double
Dim Sum_impedance As Double
Dim Sum_Admittance As Double
Dim Average_impedance As Double
Dim Average_Admittance 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
Dim AD5933_Gain_cal As Double
Dim AD5933_offset_cal As Double
IndexArray = 0
Increment = NumberIncrements + 1 ' number of increments in the sweep
Frequency = StartFrequency ' the sweep starts from here
' AD5933 gain and offset
AD5933_Gain_cal = Val(Text1.Text)
AD5933_offset_cal = Val(Text2.Text)
' Enter normal mode
EnterStandbyMode
UpdateStartFrequency
UpdateNumberIncrements
UpdateFrequencyIncrement
UpdateSettlingTime
'----------------------------- systemn clock ---------------------
If (conductivity.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
'phase = phase2(ImagineryData, RealData)
GainFactor = Val(conductivity.Text5.Text)
Impedance = 1 / (Magnitude * GainFactor)
Admittance = 1 / ((Impedance - AD5933_offset_cal) / AD5933_Gain_cal)
' Write Data to each array.
MagnitudeArray(IndexArray) = Admittance
PhaseArray(IndexArray) = phase
'conductivityArray(IndexArray) = (((AD5933_Gain / (Impedance - AD5933_Offset)) - Conductivity_offset) / Conductivity_gain * 1000000#)
ImagineryDataArray(IndexArray) = ImagineryData
RealDataArray(IndexArray) = RealData
code(IndexArray) = Magnitude
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 the average impedance
Average_Admittance = MagnitudeArray(0)
Sum_Admittance = 0
For i = 0 To NumberIncrements - 1
Sum_Admittance = Sum_Admittance + MagnitudeArray(i)
Next i
Average_Admittance = Sum_Admittance / NumberIncrements
Sum_impedance = 0
'----------------------------------------------------------------------------
Text12.Text = Average_Admittance
Average_impedance = 0
MsgBox " sweep complete "
Command7.Enabled = True
Exit Sub
sweepErrorMsg3:
MsgBox "Error completing Calibration sweep check values"
End Sub
Private Sub Command6_Click()
Dim rout1 As Double
' calculate the AD5933 impedance gain error
'rout1= r2*c2 -r1*c1/c1-c2
rout1 = ((Val(Text10.Text) * Val(Text6.Text)) - Val(Text9.Text) * Val(Text5.Text)) / (Val(Text9.Text) - Val(Text10.Text))
Text1.Text = rout1
Command1.Enabled = True
End Sub
Private Sub Command7_Click()
Dim gain_2 As Double
Dim offset_2 As Double
' calculate the probe impedance gain error
gain_2 = (Val(Text12.Text) - Val(Text11.Text)) / (Val(Text8.Text) - Val(Text7.Text))
Text4.Text = gain_2
' calculate the probe offset error
offset_2 = Val(Text11.Text) - (gain_2 * Val(Text7.Text))
Text3.Text = offset_2
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -