I am trying to generate digital sequence of outputs, with four channels (using Digital Discovery). I want them to be synchonized.
My problem is I only want the pattern output to be finite sequence (not looping). I used the example DigitalOut_Custom.py to do the job. But the sequence always restart. Could you please tell me the right way to generate a finite sequence of 0 and 1s? Is using the custom output function the right way? Thanks.
I have attached the code. Currently it uses only two channels.
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2019-09-02
Requires:
Python 2.7, 3
Description:
Generates a custom pattern
"""
from ctypes import *
from WF_SDK.dwfconstants import *
import sys
import time
Question
minghan chao
I am trying to generate digital sequence of outputs, with four channels (using Digital Discovery). I want them to be synchonized.
My problem is I only want the pattern output to be finite sequence (not looping). I used the example DigitalOut_Custom.py to do the job. But the sequence always restart. Could you please tell me the right way to generate a finite sequence of 0 and 1s? Is using the custom output function the right way? Thanks.
I have attached the code. Currently it uses only two channels.
"""
DWF Python Example
Author: Digilent, Inc.
Revision: 2019-09-02
Requires:
Python 2.7, 3
Description:
Generates a custom pattern
"""
from ctypes import *
from WF_SDK.dwfconstants import *
import sys
import time
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")
hdwf = c_int()
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))
print("Opening first device")
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()
print("Configuring Digital Out")
hzSys = c_double()
dwf.FDwfDigitalOutInternalClockInfo(hdwf, byref(hzSys))
print(hzSys)
hzRate = 10e3
data_py_clk=[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0]
data_py_din=[0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1]
# how many bytes we need to fit this many bits, (+7)/8
rgbdata=(c_ubyte*((len(data_py_clk)+7)>>3))(0)
# array to bits in byte array
for i in range(len(data_py_clk)):
if data_py_clk[i] != 0:
rgbdata[i>>3] |= 1<<(i&7)
#clk pin session
clk_pin=0
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(clk_pin), c_int(1))
dwf.FDwfDigitalOutTypeSet(hdwf, c_int(clk_pin), DwfDigitalOutTypeCustom)
# 10kHz sample rate
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(clk_pin), c_int(int(hzSys.value/hzRate))) # set sample rate
dwf.FDwfDigitalOutDataSet(hdwf, c_int(clk_pin), byref(rgbdata), c_int(len(data_py_clk)))
rgbdata_din=(c_ubyte*((len(data_py_din)+7)>>3))(0)
# array to bits in byte array
for i in range(len(data_py_din)):
if data_py_din[i] != 0:
rgbdata_din[i>>3] |= 1<<(i&7)
#din pin session
din_pin=1
# generate pattern
dwf.FDwfDigitalOutEnableSet(hdwf, c_int(din_pin), c_int(1))
# 10kHz sample rate
dwf.FDwfDigitalOutDividerSet(hdwf, c_int(din_pin), c_int(int(hzSys.value/hzRate))) # set sample rate
dwf.FDwfDigitalOutDataSet(hdwf, c_int(din_pin), byref(rgbdata_din), c_int(len(data_py_din)))
print("Generating pattern...")
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))
dwf.FDwfDigitalOutReset(hdwf)
dwf.FDwfDeviceCloseAll()
Link to comment
Share on other sites
1 answer to this question
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now