Advertisement
Guest User

test

a guest
Jul 17th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. """
  2. DWF Python Example
  3. Author: Digilent, Inc.
  4. Revision: 2018-07-28
  5.  
  6. Requires:
  7. Python 2.7, 3
  8. """
  9.  
  10. import ctypes
  11. from sys import platform, path
  12. from os import sep
  13.  
  14. import tkinter as tk
  15. import subprocess
  16. import matplotlib.pyplot as plt
  17. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  18.  
  19.  
  20.  
  21. from ctypes import *
  22. from dwfconstants import *
  23.  
  24. dwf = ctypes.cdll.dwf
  25. constants_path = "C:" + sep + "Program Files (x86)" + sep + "Digilent" + sep + "WaveFormsSDK" + sep + "samples" + sep + "py"
  26.  
  27.  
  28. path.append(constants_path)
  29. import dwfconstants as constants
  30. import math
  31. import time
  32. import sys
  33. import numpy
  34.  
  35. if sys.platform.startswith("win"):
  36. dwf = cdll.LoadLibrary("dwf.dll")
  37. elif sys.platform.startswith("darwin"):
  38. dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
  39. else:
  40. dwf = cdll.LoadLibrary("libdwf.so")
  41.  
  42. version = create_string_buffer(16)
  43. dwf.FDwfGetVersion(version)
  44. print("DWF Version: "+str(version.value))
  45.  
  46. hdwf = c_int()
  47. szerr = create_string_buffer(512)
  48. print("Opening first device")
  49. dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
  50.  
  51. if hdwf.value == hdwfNone.value:
  52. dwf.FDwfGetLastErrorMsg(szerr)
  53. print(str(szerr.value))
  54. print("failed to open device")
  55. quit()
  56.  
  57. # this option will enable dynamic adjustment of analog out settings like: frequency, amplitude...
  58. dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(3))
  59.  
  60. sts = c_byte()
  61. steps = 151
  62. start = 50
  63. stop = 20e3
  64. reference = 10
  65.  
  66. print("Reference: "+str(reference)+" Ohm Frequency: "+str(start)+" Hz ... "+str(stop/1e3)+" kHz for nanofarad capacitors")
  67. dwf.FDwfAnalogImpedanceReset(hdwf)
  68. dwf.FDwfAnalogImpedanceModeSet(hdwf, c_int(8)) # 0 = W1-C1-DUT-C2-R-GND, 1 = W1-C1-R-C2-DUT-GND, 8 = AD IA adapter
  69. dwf.FDwfAnalogImpedanceReferenceSet(hdwf, c_double(reference)) # reference resistor value in Ohms
  70. dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(start)) # frequency in Hertz
  71. dwf.FDwfAnalogImpedanceAmplitudeSet(hdwf, c_double(0.2)) # 0.2V amplitude
  72. dwf.FDwfAnalogImpedanceOffsetSet(hdwf, c_double(-0.02))
  73.  
  74. #under test
  75. # dwf.FDwfAnalogImpedanceProbeGet(hdwf, c_double())
  76. # dwf.FDwfAnalogInStatusRecord(hdwf, int *pcdDataAvailable, int *pcdDataLost, int *pcdDataCorrupt)
  77.  
  78. dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(1)) # start
  79. time.sleep(2)
  80.  
  81. import csv
  82.  
  83. # Open the CSV file for writing
  84. data_file = open("data.csv", "w")
  85. writer = csv.writer(data_file)
  86.  
  87. # Write header row
  88. writer.writerow(["Frequency (Hz)", "Resistance (Ohms)", "Reactance (Ohms)"])
  89.  
  90.  
  91.  
  92.  
  93. rgHz = [0.0]*steps
  94. rgRs = [0.0]*steps
  95. rgXs = [0.0]*steps
  96. for i in range(steps):
  97. hz = stop * pow(10.0, 1.0*(1.0*i/(steps-1)-1)*math.log10(stop/start)) # exponential frequency steps
  98. print("Step: "+str(i)+" "+str(hz)+"Hz")
  99. rgHz[i] = hz
  100. dwf.FDwfAnalogImpedanceFrequencySet(hdwf, c_double(hz)) # frequency in Hertz
  101. while True:
  102. if dwf.FDwfAnalogImpedanceStatus(hdwf, byref(sts)) == 0:
  103. dwf.FDwfGetLastErrorMsg(szerr)
  104. print(str(szerr.value))
  105. quit()
  106. if sts.value == 2:
  107. break
  108. resistance = c_double()
  109. reactance = c_double()
  110. dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceResistance, byref(resistance))
  111. dwf.FDwfAnalogImpedanceStatusMeasure(hdwf, DwfAnalogImpedanceReactance, byref(reactance))
  112. rgRs[i] = abs(resistance.value) # absolute value for logarithmic plot
  113. rgXs[i] = abs(reactance.value)
  114.  
  115.  
  116. print(f'Measurement {i}: Frequency = {hz}, Resistance = {resistance.value}, Reactance = {reactance.value}') # Debug print statement
  117.  
  118.  
  119. # Write data row
  120. writer.writerow([hz, abs(resistance.value), abs(reactance.value)])
  121.  
  122.  
  123. for iCh in range(2):
  124. warn = c_int()
  125. dwf.FDwfAnalogImpedanceStatusWarning(hdwf, c_int(iCh), byref(warn))
  126. if warn.value:
  127. dOff = c_double()
  128. dRng = c_double()
  129. dwf.FDwfAnalogInChannelOffsetGet(hdwf, c_int(iCh), byref(dOff))
  130. dwf.FDwfAnalogInChannelRangeGet(hdwf, c_int(iCh), byref(dRng))
  131. if warn.value & 1:
  132. print("Out of range on Channel "+str(iCh+1)+" <= "+str(dOff.value - dRng.value/2)+"V")
  133. if warn.value & 2:
  134. print("Out of range on Channel "+str(iCh+1)+" >= "+str(dOff.value + dRng.value/2)+"V")
  135.  
  136.  
  137. # close the csv file
  138. data_file.close()
  139.  
  140.  
  141. dwf.FDwfAnalogImpedanceConfigure(hdwf, c_int(0)) # stop
  142. dwf.FDwfDeviceClose(hdwf)
  143.  
  144. plt.plot(rgHz, rgRs, label='Resistance') # added label
  145. plt.plot(rgHz, rgXs, label='Reactance') # added label
  146. ax = plt.gca()
  147. ax.set_xscale('linear')
  148. ax.set_yscale('linear')
  149. plt.legend() # added legend
  150. plt.show()
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement