control stepper motor using digital outputs -pg电子麻将胡了
this example shows how to control a stepper motor using digital output ports.
discover devices supporting digital output
use daqlist to discover devices. this example uses a national instruments® elvis ii with id dev2.
d = daqlist
d =
12×5 table
vendorid deviceid description model deviceinfo
________ ___________ __________________________________ _____________ ____________________
"ni" "cdaq1mod1" "national instruments ni 9205" "ni 9205" [1×1 daq.deviceinfo]
"ni" "cdaq1mod2" "national instruments ni 9263" "ni 9263" [1×1 daq.deviceinfo]
"ni" "cdaq1mod3" "national instruments ni 9234" "ni 9234" [1×1 daq.deviceinfo]
"ni" "cdaq1mod4" "national instruments ni 9201" "ni 9201" [1×1 daq.deviceinfo]
"ni" "cdaq1mod5" "national instruments ni 9402" "ni 9402" [1×1 daq.deviceinfo]
"ni" "cdaq1mod6" "national instruments ni 9213" "ni 9213" [1×1 daq.deviceinfo]
"ni" "cdaq1mod7" "national instruments ni 9219" "ni 9219" [1×1 daq.deviceinfo]
"ni" "cdaq1mod8" "national instruments ni 9265" "ni 9265" [1×1 daq.deviceinfo]
"ni" "dev1" "national instruments pcie-6363" "pcie-6363" [1×1 daq.deviceinfo]
"ni" "dev2" "national instruments ni elvis ii" "ni elvis ii" [1×1 daq.deviceinfo]
"ni" "dev3" "national instruments pcie-6363" "pcie-6363" [1×1 daq.deviceinfo]
"ni" "dev4" "national instruments pcie-6363" "pcie-6363" [1×1 daq.deviceinfo]
d{10, "deviceinfo"}
ans =
ni: national instruments ni elvis ii (device id: 'dev2')
analog input supports:
7 ranges supported
rates from 0.0 to 1250000.0 scans/sec
16 channels ('ai0' - 'ai15')
'voltage' measurement type
analog output supports:
-5.0 to 5.0 volts,-10 to 10 volts ranges
rates from 0.0 to 2857142.9 scans/sec
2 channels ('ao0','ao1')
'voltage' measurement type
digital io supports:
39 channels ('port0/line0' - 'port2/line6')
'inputonly','outputonly','bidirectional' measurement types
counter input supports:
rates from 0.1 to 80000000.0 scans/sec
2 channels ('ctr0','ctr1')
'edgecount' measurement type
counter output supports:
rates from 0.1 to 80000000.0 scans/sec
2 channels ('ctr0','ctr1')
'pulsegeneration' measurement type
hardware setup description
this example uses a portescap 20m020d1u motor (5 v, 18 degree unipolar stepper). the ttl signals produced by the digital i/o system are amplified by a texas instruments uln2003ain (high voltage, high current darlington transistor array), as shown in this schematic:

add digital output-only channels
create a dataacquisition and add 4 digital channels on port 0, lines 0-3. set the measurement type to outputonly. these are connected to the 4 control lines for the stepper motor.
dq = daq("ni"); addoutput(dq,"dev2","port0/line0:3","digital")
warning: added channel does not support clocked sampling: clocked operations are disabled. only on-demand operations are allowed.
define motor steps
refer to the portescap motor wiring diagram describing the sequence of 4-bit patterns. send this pattern sequentially to the motor to produce counterclockwise motion. each step turns the motor 18 degrees. each cycle of 4 steps turns the motor 72 degrees. repeat this cycle five times to rotate the motor 360 degrees.
step1 = [1 0 1 0]; step2 = [1 0 0 1]; step3 = [0 1 0 1]; step4 = [0 1 1 0];
rotate motor
use write to output the sequence to turn the motor 72 degrees counterclockwise.
write(dq,step1); write(dq,step2); write(dq,step3); write(dq,step4);
repeat sequence 50 times to rotate the motor 10 times counterclockwise.
for motorstep = 1:50 write(dq,step1); write(dq,step2); write(dq,step3); write(dq,step4); end
to turn the motor 72 degrees clockwise, reverse the order of the steps.
write(dq,step4); write(dq,step3); write(dq,step2); write(dq,step1);
turn off all outputs
after you use the motor, turn off all the lines to allow the motor to rotate freely.
write(dq,[0 0 0 0]);