Clear Filters
Clear Filters

Callback function for frequency input channel from NI DAQ using Data Acquisition Toolbox

5 views (last 30 days)
Hello, I am trying to create a callback function for my NI 9422 DAQ which I'm using to read frequency input. I've added my DAQ and channels as below, and tried both with and without an external clock.
dq = daq("ni");
b1 = addinput(dq, "cDAQ2Mod2", "ctr1", "Frequency"); %input edge detection of pulses
b2 = addinput(dq, "cDAQ2Mod2", "ctr2", "Frequency"); %input edge detection of pulses
I then try to use the following callback function to display read outputs
dq.ScansAvailableFcn = @dispMyData;
start(dq, "continuous");
pause(12) % pausing to see how well it works
stop(dq)
% Callback function
function dispMyData(obj, ~)
data = read(obj, 1, "OutputFormat", "Matrix"); % This read works w/out a clock in a loop or code normally
disp(data)
end
From this code, my function dispMyData is never even called. How do I induce the callback? I've done similar code for an analog input, is the frequency different somehow?
Thank you for any help.

Answers (1)

Suman Sahu
Suman Sahu on 15 May 2023
Hi Marzena,
While acquiring data using the Data Acquisition Toolbox, the ScansAvailableFcn property is assigned a callback function as a function handle which is triggered every time after the number of scans specified by the ScansAvailableFcnCount property is available from the input channels.
Dq.ScansAvailableFcn = @(src, evt)callback(src, evt, arg1, arg2,…argn);
Dq.ScansAvailableFcnCount = numberOfScansAfterWhichCallbackIsTriggered;
Try making following changes to your code:
dq.ScansAvailableFcn = @(src, evt)dispMyData(src, evt, dq);
dq.ScansAvailableFcnCount = 50;
start(dq, "continuous");
pause(12);
stop(dq);
function dispMyData(src, evt, dq)
data = read(dq, 1, "OutputFormat", "Matrix");
disp(data);
end
To learn more about data acquisition using DAT, refer the following documentation: Interface to data acquisition device - MATLAB
Hope it is helpful!

Categories

Find more on Simultaneous and Synchronized Operations in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!