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

📄 vb2d9.tmp

📁 AD5933评估板下位机程序(keil C)和上位机程序(VB)
💻 TMP
📖 第 1 页 / 共 5 页
字号:
      Width           =   2775
      Begin VB.Label Label7 
         Caption         =   "MCLK,Ref Clock Frequency"
         Height          =   255
         Left            =   120
         TabIndex        =   57
         Top             =   3240
         Width           =   2535
      End
      Begin VB.Label Label4 
         Caption         =   "DDS Settling Time Cycles"
         Height          =   255
         Left            =   120
         TabIndex        =   56
         Top             =   2400
         Width           =   2535
      End
      Begin VB.Label Label3 
         Caption         =   "Number of Increments (9 Bit)"
         Height          =   375
         Left            =   120
         TabIndex        =   55
         Top             =   1680
         Width           =   2535
      End
      Begin VB.Label Label1 
         Caption         =   "Delta Frequency (Hz)"
         Height          =   255
         Left            =   120
         TabIndex        =   53
         Top             =   960
         Width           =   2535
      End
      Begin VB.Label Label6 
         Caption         =   "Start Frequency (Hz)"
         Height          =   255
         Left            =   120
         TabIndex        =   52
         Top             =   240
         Width           =   2295
      End
   End
   Begin VB.Label Label2 
      Height          =   255
      Left            =   240
      TabIndex        =   54
      Top             =   2160
      Width           =   2535
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
   
Private Sub MeasureTemperature()

Dim TemperatureUpper As Long
Dim TemperatureLower As Long
'Ensure the
 EnterStandbyMode
 'Write xH90  to the control register to take temp reading.
 WritetToPart &H80, &H90
 msDelay 5
  'set add pointer to point to status reg"
 SetAddressPointer &H8F
 ReadbackStatusRegister = PortRead(&HD, &H8F)
 ReadbackStatusRegister = ReadbackStatusRegister And &H1   '1st bit of the status reg is the valid temp bit
If ReadbackStatusRegister <> 1 Then
    MsgBox "temperature measurement incomplete"
Do
 ReadbackStatusRegister = PortRead(&HD, &H8F)
 ReadbackStatusRegister = ReadbackStatusRegister And &H1
    Loop Until (ReadbackStatusRegister = 1)
MsgBox "Temperature Measurement Complete"
End If

'SetAddressPointer &H92

TemperatureUpper = PortRead(&HD, &H92)
'Setup Pointer to point to &H93
SetAddressPointer &H93
TemperatureLower = PortRead(&HD, &H93)
Temperature = TemperatureLower + (TemperatureUpper * 256)
If Temperature > &H1FFF Then
' Negative Temperature
Label8.Caption = (Temperature - 16384) / 32
Else
' Positive Temperature
Label8.Caption = Temperature / 32
End If
'MsgBox "temperature measurement complete"
End Sub

Private Sub CalculateGainFactor()
On Error GoTo GainErrorMsg
Dim TempFrequency As Double
Dim DDSRefClockFrequency As Double
Dim ReadbackStatusRegister As Long
Dim Frequencybyte0 As Long
Dim Frequencybyte2 As Long
Dim Frequencybyte1A As Long
Dim Frequencybyte1B As Long
Dim RealData As Long
Dim RealDataUpper As Long
Dim RealDataLower As Long
Dim ImagineryData As Long
Dim ImagineryDataLower As Long
Dim ImagineryDataUpper As Long
Dim Magnitude As Double
Dim phase As Long
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 Frequency As Long
Dim IndexArray 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
Dim i As Integer
'----------------------------- systemn clock ---------------------
If (ExternalClock.Value = True) Then
' Enable external Oscillator
WritetToControlRegister2 &H81, &H8
Else
' Enable internal Oscillator
WritetToControlRegister2 &H81, &H0
End If
'------------------------------------------------------------------
' Ensure the correct gain calibration frequency (mid sweep freq) is in RAM
' setting up the gain factor frequency point
'------------------------------------------------------------------
DDSRefClockFrequency = Abs(Val(Text7.Text))
midfrequency = (StartFrequency + (FrequencyIncrements * NumberIncrements) / 2) 'calibration frequency point
TempFrequency = (midfrequency / (DDSRefClockFrequency / 4)) * &HFFFFFF * 8
TempStartFrequency = Int(TempFrequency)
' Convert Start Frequency to 3 Byte local Variables.
    Frequencybyte0 = TempStartFrequency And &HFF
    Frequencybyte1A = ((TempStartFrequency And &HFF00) / 256)
    Frequencybyte2 = ((TempStartFrequency And &HFF0000) / 65536)

Frequencybyte1B = Frequencybyte1A 'Temp copy OF Frequencybyte1A

If (Frequencybyte1A >= &HFF) Then  ' is Dim StartFrequencybyte1a too big to transmit as 1 byte i.e ffhex
                                        'if it is it must be broken up resulting in any overflow into a second byte being carried over to the third byte
                                        'remember that the part will only store the bottom three bytes
Frequencybyte1B = Frequencybyte1B And &HFF
MSBfromFrequencybyte1B = (Frequencybyte1A And &HFF00) / 256
Frequencybyte2 = (Frequencybyte2 * 256) + MSBfromFrequencybyte1B
Frequencybyte2 = ((Frequencybyte2 * 256) + MSBfromFrequencybyte1B) And &HFF

End If


' Write in data to Start frequency register and verify
' correct contents written
WritetToPart &H84, Frequencybyte0   '84 hex lsb
WritetToPart &H83, Frequencybyte1B  '83 hex
WritetToPart &H82, Frequencybyte2   '82 hex msb


'----------------------------------------------------------------------------

'initialise sensor with the mid frequency
WritetToControlRegister &H80, &H10

'set the number of increments to 2
'Write in data to Number Increments register and verify
'correct contents written
WritetToPart &H89, 2
WritetToPart &H88, 0
'set the frequuency increment to 1hz
'Write in data to frequency Increments register and verify
'correct contents written
WritetToPart &H87, 33   '84 hex lsb
WritetToPart &H86, 0    '83 hex
WritetToPart &H85, 0    '82 hex msb
'------------------------------------------------------------------------------
' Start Frequency Sweep at mid point
  WritetToControlRegister &H80, &H20
    'wait unt adc conversion is complete
    msDelay 1
'------------------------------------------------------------------------------

' Verify that the returned data @gain factor mid point freq is complete complete
ReadbackStatusRegister = PortRead(&HD, &H8F)
ReadbackStatusRegister = ReadbackStatusRegister And &H2

If ReadbackStatusRegister <> 2 Then
    Do
    Do
                '     WritetToControlRegister &H80, &H40 'repeat current sweep point
               
                     'initialise sensor with the mid frequency
                       WritetToControlRegister &H80, &H10
                     'Start Frequency Sweep at mid point
                       WritetToControlRegister &H80, &H20
                       msDelay 1
                                             
                        ReadbackStatusRegister = PortRead(&HD, &H8F)
                        ReadbackStatusRegister = ReadbackStatusRegister And &H2
'                        MsgBox "Ensure power connections are correct and clock signal is present at MCLK pin"
                        
              Loop Until (ReadbackStatusRegister = 2)
     
    Loop Until ReadbackStatusRegister = 2

End If

'---------------------------the gain factor data is available for use -----------------------------------------------------
RealDataUpper = PortRead(&HD, &H94)
RealDataLower = PortRead(&HD, &H95)
RealData = RealDataLower + (RealDataUpper * 256)
'the data is stored in 2's complement format.in oreder to use this data
'you must convert it from 2's complement to decimal format
If RealData < &H7FFF Then
' 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
calibration_phase_mid_point = phase2(ImagineryData, RealData)
Magnitude = ((RealData ^ 2) + (ImagineryData ^ 2)) ^ 0.5  ' magnitude = code

'-------------------------------------------------------------------------------------------------------------------------
'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 = 1 / ((CalibratedImpedance1 + Val(Form10.Text1.Text)) * Magnitude)
GainFactor = 1 / (CalibratedImpedance1 * Magnitude)
Text5.Text = GainFactor
ElseIf (impedancechoice = 1) Then
CalibratedImpedance1 = Val(Text11.Text)
GainFactor = (2 * 3.141592654 * midfrequency * CalibratedImpedance1) / Magnitude
Text5.Text = GainFactor
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 = total_admittance / Magnitude
 Text5.Text = GainFactor

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

⌨️ 快捷键说明

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