main content

generate pulse width modulated signals using ni devices -凯发k8网页登录

this example shows how to generate a pulse width modulated signal to drive a stepper motor.

create a counter output channel

use daq to create a dataacquisition. use addoutput to add a counter output channel with pulsegeneration measurement type, and addinput to add an analog input channel to monitor the pulse generated by the counter output channel. for this example, use compactdaq chassis ni c9178 and module ni 9402 with id cdaq1mod5 for the pulse generation and ni 9205 with id cdaq1mod1 for the voltage input.

dq = daq("ni");
addinput(dq,"cdaq1mod1", "ai0", "voltage");
ctr = addoutput(dq,"cdaq1mod5", "ctr0", "pulsegeneration");
dq.channels
ans = 
    index    type      device       channel    measurement type           range                 name      
    _____    ____    ___________    _______    _________________    __________________    ________________
      1      "ai"    "cdaq1mod1"    "ai0"      "voltage (diff)"     "-10 to  10 volts"    "cdaq1mod1_ai0" 
      2      "co"    "cdaq1mod5"    "ctr0"     "pulsegeneration"    "n/a"                 "cdaq1mod5_ctr0"

determine the terminal of the counter output channel

to connect the output signal to the correct terminal, examine the terminal property of the counter channel. the terminal is determined by the hardware.

ctr.terminal
ans =
    'pfi0'

clocked counter output

use counter output channel 0 to generate a fixed pulse width modulated signal on terminal pfi0. trigger the motor after 0.5 seconds, with a 75% duty cycle.

ctr.frequency = 10;
ctr.initialdelay = 0.5;
ctr.dutycycle = 0.75;
% startforeground returns data for input channels only. the data variable
% will contain one column of data.
start(dq, "duration", seconds(1));
while dq.running
    pause(0.1);
end
data = read(dq, seconds(1));
plot(data.time, data.variables);

网站地图