This example shows how to acquire digital data using a counter output channel that generates pulses as an external clock. The counter provides the clock in this acquisition.
In this example, we will generate a clock in one session using a counter output channel and export the clock to another session that acquires clocked digital data.
Importing an external clock does not automatically set the rate of your session. Manually set the session’s rate to match the expected external clock frequency.
This example shows how to create a clock session with a counter output channel that will continuously generate frequency pulses in the background. Use this channel as an external clock in your clocked digital acquisition.
Create a clock frequency that you will use to synchronize the frequency and rate of your counter output as well as the rate of your digital acquisition in the next step.
clockFreq = 100;
Create a session and add a counter output channel with
sClk = daq.createSession('ni'); ch1 = addCounterOutputChannel(sClk,'Dev1',0,'PulseGeneration')
Make sure the counter channel you add is not being used in a different session. You will get a terminal conflict error if the hardware is reserved in another session.
Save the counter output terminal to a variable. You will use this terminal in your digital session to specify the external clock that synchronizes your digital clocked operations.
clkTerminal = ch1.Terminal;
You will use this terminal in your digital session to specify the external clock that synchronizes your digital clocked operations.
Set the frequency of your counter session to the clock frequency.
ch1.Frequency = clockFreq
Set the session to continuous mode.
sClk.IsContinuous = true;
This example shows how to create a digital input session and import an external clock from the clock session.
Create a session and add a line from port
s = daq.createSession('ni'); addDigitalChannel(s,'Dev1','Port0/Line2','InputOnly')
Not all devices support clocked (hardware timed) digital I/O
startBackground. For these devices you can
use software timed operations with
Devices that support clocked digital I/O operations might not support them on all ports. Check your device specifications.
PFI terminal resources may be shared. Check your device routing in NI MAX.
Set the sessions scan rate to the same as the rate and the frequency of the counter output channel.
s.Rate = clockFreq;
Import the clock from your clock session to synchronize your acquisition.
Start the counter output channel in the background and ensure it is running.
startBackground(sClk); for i = 1:10 if sClk.IsRunning break; else pause(0.1); end end
Acquire and plot data.
dataIn = startForeground(s); plot(dataIn)