Documentation

Acquire Digital Data Using Counter Channels

This example shows how to acquire clocked 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.

    Note:   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.

Generate a Clock Using a Counter Output Channel

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 PulseGeneration measurement type.

sClk = daq.createSession('ni');
ch1 = addCounterOutputChannel(sClk,'Dev1',0,'PulseGeneration')

    Tip   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;

Use Counter Clock To Acquire Clocked Digital Data

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 0 line 2 on Dev1.

s = daq.createSession('ni');
addDigitalChannel(s,'Dev1','Port0/Line2','InputOnly')

    Tip   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.

addClockConnection(s,'External',['Dev1/' clkTerminal],'ScanClock');

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)

Related Examples

Was this topic helpful?