This example shows how to use a device counter output channel to generate pulses for an external clock in acquiring
In this example, you generate a clock in one DataAcquisition using a counter output channel and export the clock to another DataAcquisition that acquires digital data. The counter output and the digital subsystem can be on the same device or on different devices.
Importing an external clock does not automatically set the scan rate of your
DataAcquisition. Manually set the DataAcquisition
Rate property value
to match the expected external clock frequency.
Create a clocked DataAcquisition with a counter output channel that continuously generates frequency pulses in the background. You can use this channel as an external clock for a clocked digital acquisition.
Define the clock frequency to be used for synchronizing the scan rate of your counter output as well as the rate of your digital acquisition.
clockFreq = 100;
Create a DataAcquisition object and add a counter output channel for
PulseGeneration measurement type.
daqClk = daq("ni"); ch1 = addoutput(daqClk,"Dev1","ctr0","PulseGeneration");
Make sure the counter channel you add is not being used in a different DataAcquisition, otherwise a terminal conflict error occurs.
Save the counter output terminal ID to a variable so you can use it later to specify the external clock that synchronizes your digital clocked operations.
clkTerminal = ch1.Terminal;
Set the frequency of your counter channel to the clock frequency.
ch1.Frequency = clockFreq;
Create a DataAcquisition for digital input and import the external clock from the clock DataAcquisition.
Create a DataAcquisition and add a digital input line from port
daqDgt = daq("ni"); addDigitalChannel(daqDgt,"Dev1","Port0/Line2","InputOnly")
Devices that support clocked digital I/O operations might not support them on all ports. Check your device specifications.
PFI terminal resources might be shared. Check your device routing in the NI MAX app.
Set the DataAcquisition scan rate to the same value as the rate of the counter output channel.
daqDgt.Rate = clockFreq;
Import the clock from your clock DataAcquisition to synchronize your acquisition.
Start the counter output channel to run continuously in the background.
Pulse generation begins immediately on the counter output. It does not need data.
Acquire and plot digital input data.
dataIn = read(daqDgt,seconds(1),"OutputFormat","Matrix"); plot(dataIn(1:100,1))
Stop the clock DataAcquisition.