measure pulse width using ni devices -凯发k8网页登录
this example shows how to measure the width of an active high pulse. a sensor is used to measure distance from a point: the width of the pulse is correlated with the measured distance.
create a counter input channel
create a dataacquisition, and add a counter input channel with pulsewidth
measurement type. for this example, use compactdaq chassis ni c9178 and module ni 9402 with id cdaq1mod5.
dq = daq("ni"); ch = addinput(dq, "cdaq1mod5", "ctr0", "pulsewidth");
determine the terminal of the counter input channel
to connect the input signal to the correct terminal, examine the terminal
property of the channel. the terminal is determined by the hardware.
ch.terminal
ans = 'pfi1'
measure distance
to determine if the counter is operational, acquire a single scan. the sensor generates a high pulse of width 0.0010 seconds corresponding a distance of one meter.
1000*read(dq, "outputformat", "matrix")
ans = 5
measure distance over time
use the hardware clock to acquire multiple counter measurements over time. ni counter devices require an external clock. by adding an analog input channel for a module on the same chassis, the internal clock is shared with both modules.
dq = daq("ni"); addinput(dq, "cdaq1mod1", "ai0", "voltage"); addinput(dq, "cdaq1mod5", "ctr0", "pulsewidth"); dq.rate = 1; data = read(dq, seconds(10)); plot(data.time, 1000*data.cdaq1mod5_ctr0);