📄 vb2d9.tmp
字号:
'division of the above "product" by the "sum" will result in a complex number with a "real" and "imag" component
real3 = ((real1 * real2) + (imag1 * imag2)) / (real2 ^ 2 + imag2 ^ 2)
imag3 = ((-real1 * imag2) + (real2 * imag1)) / (real2 ^ 2 + imag2 ^ 2)
total_impedance = (real3 ^ 2 + imag3 ^ 2) ^ 0.5
total_admittance = 1 / total_impedance
GainFactor = total_admittance / Magnitude
Text5.Text = GainFactor
ElseIf (impedancechoice = 4) Then '( resistor r1 || cap c1) in series with resistor r2
' first calculate impedance of resistor r1 and cap 1 in parallel and then add to resistor r2
CalibratedImpedance1 = Val(Text10.Text) 'resistor value
CalibratedImpedance2 = Val(Text11.Text) 'capacitor value
cap_reactance = 1 / (2 * 3.141592654 * midfrequency * (CalibratedImpedance2))
res_reactance = CalibratedImpedance1
'----------------------------------------------------------------------------------
'the parallel reactance = prod/sum = (R*-jXc)/(R-jXc)
'in order to dividea complex number by a complex number you mul num and denom by the complex conj of the denom complex number
'=((R*-jXc)*(R+jXc))/(R^2+XC^2)) = X+jY
'the parallel impedance = |ZT| = SQRT(X^2 + Y^2)
'-----------------------------------------------------------------------------------
'expressing both resistor (x1+jy1) and capacitor (x2 +jy2) as a complex number
x1 = res_reactance
y1 = 0
x2 = 0
y2 = cap_reactance
'the "product" of the two complex numbers is still a complex number with a "real" and "imag" component
'prod = real and j imag
real1 = (x1 * x2) - (y1 * y2)
imag1 = (x1 * y2) + (x2 * y1)
' the "sum" of two complex numbers is still a complex number with a "real" and "imag" component
real2 = x1 + x2
imag2 = y1 + y2
'division of the above "product" by the "sum" will result in a complex number with a "real" and "imag" component
real3 = ((real1 * real2) + (imag1 * imag2)) / (real2 ^ 2 + imag2 ^ 2)
imag3 = ((-real1 * imag2) + (real2 * imag1)) / (real2 ^ 2 + imag2 ^ 2)
total_impedance = (real3 ^ 2 + imag3 ^ 2) ^ 0.5
' add parallel impedance to resistor r2
res_reactance_2 = Text6
total_impedance = ((real3 ^ 2 + imag3 ^ 2) ^ 0.5) + res_reactance_2
total_admittance = 1 / total_impedance
GainFactor = total_admittance / Magnitude
Text5.Text = GainFactor
End If
MsgBox "The calibration gain factor has been calculated."
Exit Sub
GainErrorMsg:
MsgBox "Error IN-VALID gain factor:Check feedback resistor/calibration impedance connection. Ensure clock signal at MCLK"
End Sub
Private Sub CalculateGainFactor_multipoint()
On Error GoTo ErrorMsg_multipoint
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 GainFactor_multipoint As Double
Dim sweep_phase As Double
Dim Frequency As Double
Dim Increment As Long ' number of increments
Dim IndexArray As Double 'counter
Dim i As Integer 'counter
'gain factor data
Dim total_admittance As Double
Dim total_impedance As Double
Dim res_reactance As Double
Dim res_reactance_2 As Double
Dim cap_reactance As Double
Dim x1 As Double
Dim y1 As Double
Dim x2 As Double
Dim y2 As Double
Dim real1 As Double
Dim real2 As Double
Dim real3 As Double
Dim imag1 As Double
Dim imag2 As Double
Dim imag3 As Double
IndexArray = 0
calibration_AveGainFactor = 0
Increment = NumberIncrements + 1 ' number of increments in the sweep
Frequency = StartFrequency ' the sweep starts from here
' 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
sweep_phase = phase2(ImagineryData, RealData)
' sweep_phase = -(calibration_phase_mid_point - phase2(ImagineryData, RealData))
'--------------------------- gain factor---------------------------------------
'impedancechoice = 0 resistor r1
'impedancechoice = 1 capacitor c1
'impedancechoice = 2 resistor r1 and cap c1 in series
'impedancechoice = 3 resistor r1 and cap c1 in parallel
'text 10 is the resistor r1 value
'text 11 is the capacitor c1 value
'text 6 is the resistor r2 value
'------------------------------------------------------------------------------
If (impedancechoice = 0) Then
CalibratedImpedance1 = Val(Text10.Text)
' GainFactor_multipoint = 1 / ((CalibratedImpedance1 + Val(Form10.Text1.Text)) * Magnitude)
GainFactor_multipoint = 1 / (CalibratedImpedance1 * Magnitude)
ElseIf (impedancechoice = 1) Then
CalibratedImpedance1 = Val(Text11.Text)
GainFactor_multipoint = (2 * 3.141592654 * midfrequency * CalibratedImpedance1) / Magnitude
ElseIf (impedancechoice = 2) Then 'resistor and cap in series
CalibratedImpedance1 = Val(Text10.Text) 'resistor value
CalibratedImpedance2 = Val(Text11.Text) 'capacitor value
'ZT = r - jXc = cartesian expression
'ZT may be expressed in polar format
'ZT = |zt|*arg(zt)
'|ZT| = SQRT(r^2 + (-Xc)^2)
'
' impedance diagram
'
' |
' ---------0--------->real
' |
' |
' |
' V 0 = |zt|arg(zt) = impedance = having both magnitude and phase
' imaginary
cap_reactance = 1 / (2 * 3.141592654 * midfrequency * (CalibratedImpedance2)) ' value of imaginary reactance
res_reactance = CalibratedImpedance1
'total impedance = |ZT| = SQRT(r^2 + (Xc)^2)
total_impedance = ((res_reactance ^ 2 + (cap_reactance) ^ 2)) ^ 0.5
total_admittance = 1 / total_impedance
GainFactor_multipoint = total_admittance / Magnitude
ElseIf (impedancechoice = 3) Then 'resistor and cap in parallel
CalibratedImpedance1 = Val(Text10.Text) 'resistor value
CalibratedImpedance2 = Val(Text11.Text) 'capacitor value
cap_reactance = 1 / (2 * 3.141592654 * midfrequency * (CalibratedImpedance2))
res_reactance = CalibratedImpedance1
'----------------------------------------------------------------------------------
'the parallel reactance = prod/sum = (R*-jXc)/(R-jXc)
'in order to dividea complex number by a complex number you mul num and denom by the complex conj of the denom complex number
'=((R*-jXc)*(R+jXc))/(R^2+XC^2)) = X+jY
'the parallel impedance = |ZT| = SQRT(X^2 + Y^2)
'-----------------------------------------------------------------------------------
'expressing both resistor (x1+jy1) and capacitor (x2 +jy2) as a complex number
x1 = res_reactance
y1 = 0
x2 = 0
y2 = cap_reactance
'the "product" of the two complex numbers is still a complex number with a "real" and "imag" component
'prod = real and j imag
real1 = (x1 * x2) - (y1 * y2)
imag1 = (x1 * y2) + (x2 * y1)
' the "sum" of two complex numbers is still a complex number with a "real" and "imag" component
real2 = x1 + x2
imag2 = y1 + y2
'division of the above "product" by the "sum" will result in a complex number with a "real" and "imag" component
real3 = ((real1 * real2) + (imag1 * imag2)) / (real2 ^ 2 + imag2 ^ 2)
imag3 = ((-real1 * imag2) + (real2 * imag1)) / (real2 ^ 2 + imag2 ^ 2)
total_impedance = (real3 ^ 2 + imag3 ^ 2) ^ 0.5
total_admittance = 1 / total_impedance
GainFactor = total_admittance / Magnitude
ElseIf (impedancechoice = 4) Then '( resistor r1 || cap c1) in series with resistor r2
' first calculate impedance of resistor r1 and cap 1 in parallel and then add to resistor r2
CalibratedImpedance1 = Val(Text10.Text) 'resistor value
CalibratedImpedance2 = Val(Text11.Text) 'capacitor value
cap_reactance = 1 / (2 * 3.141592654 * midfrequency * (CalibratedImpedance2))
res_reactance = CalibratedImpedance1
'----------------------------------------------------------------------------------
'the parallel reactance = prod/sum = (R*-jXc)/(R-jXc)
'in order to dividea complex number by a complex number you mul num and denom by the complex conj of the denom complex number
'=((R*-jXc)*(R+jXc))/(R^2+XC^2)) = X+jY
'the parallel impedance = |ZT| = SQRT(X^2 + Y^2)
'-----------------------------------------------------------------------------------
'expressing both resistor (x1+jy1) and capacitor (x2 +jy2) as a complex number
x1 = res_reactance
y1 = 0
x2 = 0
y2 = cap_reactance
'the "product" of the two complex numbers is still a complex number with a "real" and "imag" component
'prod = real and j imag
real1 = (x1 * x2) - (y1 * y2)
imag1 = (x1 * y2) + (x2 * y1)
' the "sum" of two complex numbers is still a complex number with a "real" and "imag" component
real2 = x1 + x2
imag2 = y1 + y2
'division of the above "product" by the "sum" will result in a complex number with a "real" and "imag" component
real3 = ((real1 * real2) + (imag1 * imag2)) / (real2 ^ 2 + imag2 ^ 2)
imag3 = ((-real1 * imag2) + (real2 * imag1)) / (real2 ^ 2 + imag2 ^ 2)
total_impedance = (real3 ^ 2 + imag3 ^ 2) ^ 0.5
' add parallel impedance to resistor r2
res_reactance_2 = Text6
total_impedance = ((real3 ^ 2 + imag3 ^ 2) ^ 0.5) + res_reactance_2
total_admittance = 1 / total_impedance
GainFactor_multipoint = total_admittance / Magnitude
End If
'-------------------------------------------------------------------------------
' Write Data to each array.
calibration_GainFactors(IndexArray) = GainFactor_multipoint
calibration_phase_multi_point(IndexArray) = sweep_phase
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
'calculate the average gain factor to display in the box
For i = 0 To IndexArray - 1
calibration_AveGainFactor = calibration_AveGainFactor + calibration_GainFactors(i)
Next
Text5.Text = calibration_AveGainFactor / IndexArray
MsgBox " Multipoint gain factor complete "
DoEvents
Exit Sub
ErrorMsg_multipoint:
MsgBox "Error completing multipoint gainfactors"
End Sub
Private Sub Sweep()
On Error GoTo sweepErrorMsg
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -