main content

count pulses on a digital signal using ni devices -凯发k8网页登录

this example shows how to determine the rate of rotation of an anaheim automation motor controller by counting the number of rising edges in the signal. the controller returns hall effect pulses (square waves) that serve as frequency feedback for motor rotation speeds.

create a counter input channel

use daq to create a dataacquisition and addinput to add a counter input channel with edgecount 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", "edgecount");
ch
ch = 
    index    type      device       channel    measurement type    range          name      
    _____    ____    ___________    _______    ________________    _____    ________________
      1      "ci"    "cdaq1mod5"    "ctr0"       "edgecount"       "n/a"    "cdaq1mod5_ctr0"

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 =
    'pfi0'

read the counter channel

to determine if the counter is operational, input a single scan, pause while the motor rotates, then read the counter again.

read(dq)
ans =
  timetable
    time     cdaq1mod5_ctr0
    _____    ______________
    0 sec          3       
pause(0.1);
read(dq)
ans =
  timetable
    time     cdaq1mod5_ctr0
    _____    ______________
    0 sec          14      
pause(0.1);
read(dq)
ans =
  timetable
    time     cdaq1mod5_ctr0
    _____    ______________
    0 sec          27      

measure revolutions per second

count the number of pulses by resetting the counter to zero, pause for one second, and read the counter. the hall effects are oriented every 120 degrees and generate three square wave pulses for every rotation.

resetcounters(dq);
pause(1);
read(dq, "outputformat", "matrix")/3
ans =
   33.6667

use hardware clock for higher accuracy

the hardware clock is highly accurate. use the hardware clock to acquire multiple counter measurements. ni counter devices require an external clock. by adding an analog input channel for a module on the same chassis, the dataacquisition shares an internal clock with both modules.

dq = daq("ni");
addinput(dq,"cdaq1mod1", "ai0", "voltage");
addinput(dq,"cdaq1mod5", "ctr0", "edgecount");
data = read(dq, seconds(0.25));
plot(data.time, data.variables);

网站地图