guis06 Posted March 9, 2021 Share Posted March 9, 2021 Hello, To explain my wish :) I am using Analog Discovery 2. I would like to create a pulse of 1ms for example on DIO_1 when i counted 2000 pulse in DIO_0 I tryed o use DigitalIn_Sync.py and DigitalOut_Pulse ....to mix them and ...i failed. Do you have idea how i can do it better? my code : # set number of sample to acquire nSamples = 5000 rgwSamples = (c_uint16*nSamples)() cAvailable = c_int() cLost = c_int() cCorrupted = c_int() cSamples = 0 fLost = 0 fCorrupted = 0 # record mode dwf.FDwfDigitalInAcquisitionModeSet(hdwf, acqmodeRecord) # for sync mode set divider to -1 dwf.FDwfDigitalInDividerSet(hdwf, c_int(-1)) # 1bit per sample format dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(1)) # number of samples dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(nSamples)) # in sync mode the trigger is used for sampling condition # trigger detector mask: low & hight & ( rising | falling ) dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(0), c_int(1<<0), c_int(0)) # DIO-0 rising edge # begin acquisition dwf.FDwfDigitalInConfigure(hdwf, c_bool(0), c_bool(1)) print("Starting sync record...") while cSamples < nSamples: dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts)) if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) : # acquisition not yet started. continue dwf.FDwfDigitalInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted)) cSamples += cLost.value if cLost.value : fLost = 1 if cCorrupted.value : fCorrupted = 1 if cAvailable.value==0 : continue if cSamples+cAvailable.value > nSamples : cAvailable = c_int(nSamples-cSamples) # get samples dwf.FDwfDigitalInStatusData(hdwf, byref(rgwSamples, 2*cSamples), c_int(2*cAvailable.value)) cSamples += cAvailable.value dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.001)) # 1ms run dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(1)) # once dwf.FDwfDigitalOutIdleSet(hdwf, c_int(4), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(4), c_int(1), c_int(0)) # initialize high on start dwf.FDwfDigitalOutCounterSet(hdwf, c_int(4), c_int(0), c_int(0)) # low/high count zero, no toggle during run dwf.FDwfDigitalOutEnableSet(hdwf, c_int(4), c_int(1)) print("Generating 1ms pulse") dwf.FDwfDigitalOutConfigure(hdwf, c_int(1)) print(" done") if fLost: print("Samples were lost! Reduce sample rate") if cCorrupted: print("Samples could be corrupted! Reduce sample rate") f = open("record.csv", "w") for v in rgwSamples: f.write("%s\n" % v) f.close() dwf.FDwfDeviceClose(hdwf) Link to comment Share on other sites More sharing options...
attila Posted March 10, 2021 Share Posted March 10, 2021 Hi @guis06 See the following example: from ctypes import * import sys if sys.platform.startswith("win"): dwf = cdll.dwf elif sys.platform.startswith("darwin"): dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf") else: dwf = cdll.LoadLibrary("libdwf.so") version = create_string_buffer(16) dwf.FDwfGetVersion(version) print("DWF Version: "+str(version.value)) # continue running after device close dwf.FDwfParamSet(c_int(4), c_int(0)) # DwfParamOnClose 0 = run, 1 = stop, 2 = shutdown print("Opening first device") hdwf = c_int() dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) if hdwf.value == 0: print("failed to open device") szerr = create_string_buffer(512) dwf.FDwfGetLastErrorMsg(szerr) print(str(szerr.value)) quit() # the device will be configured only when calling FDwf###Configure dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # digital output dwf.FDwfDigitalOutTriggerSourceSet(hdwf, c_int(3)) # trigsrcDetectorDigitalIn dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(0)) # infinite repeat dwf.FDwfDigitalOutRepeatTriggerSet(hdwf, c_int(1)) # wait for trigger in each cycle dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.001)) # 1ms run # DIO 1 pulse dwf.FDwfDigitalOutEnableSet(hdwf, c_int(1), c_int(1)) dwf.FDwfDigitalOutIdleSet(hdwf, c_int(1), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(1), c_int(1), c_int(0)) # initialize with high when running dwf.FDwfDigitalOutCounterSet(hdwf, c_int(1), c_int(0), c_int(0)) # low/high count zero, no toggle during run # configure and start dwf.FDwfDigitalOutConfigure(hdwf, c_int(1)) # digital input trigger dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(1<<0), c_int(0), c_int(0)) # DIO 0 rising edge dwf.FDwfDigitalInTriggerLengthSet(hdwf, c_double(1e-7), c_double(-1), c_int(0)) # 100ns glitch filter dwf.FDwfDigitalInTriggerCountSet(hdwf, c_int(20000), c_int(1)) # count 20k and restart dwf.FDwfDigitalInConfigure(hdwf, c_bool(1), c_bool(1)) dwf.FDwfDeviceCloseAll() Link to comment Share on other sites More sharing options...
guis06 Posted March 10, 2021 Author Share Posted March 10, 2021 Hello Attila. A big thnaks... you are the best. Everything works well Link to comment Share on other sites More sharing options...
Question
guis06
Hello,
To explain my wish :)
I am using Analog Discovery 2.
I would like to create a pulse of 1ms for example on DIO_1 when i counted 2000 pulse in DIO_0
I tryed o use DigitalIn_Sync.py and DigitalOut_Pulse ....to mix them and ...i failed.
Do you have idea how i can do it better?
my code :
# set number of sample to acquire
nSamples = 5000
rgwSamples = (c_uint16*nSamples)()
cAvailable = c_int()
cLost = c_int()
cCorrupted = c_int()
cSamples = 0
fLost = 0
fCorrupted = 0
# record mode
dwf.FDwfDigitalInAcquisitionModeSet(hdwf, acqmodeRecord)
# for sync mode set divider to -1
dwf.FDwfDigitalInDividerSet(hdwf, c_int(-1))
# 1bit per sample format
dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(1))
# number of samples
dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(nSamples))
# in sync mode the trigger is used for sampling condition
# trigger detector mask: low & hight & ( rising | falling )
dwf.FDwfDigitalInTriggerSet(hdwf, c_int(0), c_int(0), c_int(1<<0), c_int(0)) # DIO-0 rising edge
# begin acquisition
dwf.FDwfDigitalInConfigure(hdwf, c_bool(0), c_bool(1))
print("Starting sync record...")
while cSamples < nSamples:
dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) :
# acquisition not yet started.
continue
dwf.FDwfDigitalInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
cSamples += cLost.value
if cLost.value :
fLost = 1
if cCorrupted.value :
fCorrupted = 1
if cAvailable.value==0 :
continue
if cSamples+cAvailable.value > nSamples :
cAvailable = c_int(nSamples-cSamples)
# get samples
dwf.FDwfDigitalInStatusData(hdwf, byref(rgwSamples, 2*cSamples), c_int(2*cAvailable.value))
cSamples += cAvailable.value
dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.001)) # 1ms run
dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(1)) # once
dwf.FDwfDigitalOutIdleSet(hdwf, c_int(4), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running
dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(4), c_int(1), c_int(0)) # initialize high on start
dwf.FDwfDigitalOutCounterSet(hdwf, c_int(4), c_int(0), c_int(0)) # low/high count zero, no toggle during run
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(4), c_int(1))
print("Generating 1ms pulse")
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))
print(" done")
if fLost:
print("Samples were lost! Reduce sample rate")
if cCorrupted:
print("Samples could be corrupted! Reduce sample rate")
f = open("record.csv", "w")
for v in rgwSamples:
f.write("%s\n" % v)
f.close()
dwf.FDwfDeviceClose(hdwf)
Link to comment
Share on other sites
2 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.