Synchronizing digital output with analog input using NI-DAQ

8 views (last 30 days)
Hi,
I'm using MATLAB R2015a (32-bit) on Windows 7, and I have a NI BNC-2110 with a PCI-6221 DAQ device. I have to simultaneously switch on a current source using a 5 V output and begin an analog recording; then, after 500 ms, I need to switch the current source off by switching off the 5 V output. Then, after 4 s, I need to turn the analog recording off. I was planning to turn the current source on using a digital output (since this would circumvent being "partially on"). I found an example in the MATLAB documentation describing how to synchronize analog output with analog input using the RTSI bus. This is given here. I couldn't find anything for synchronizing digital output with analog input.
My current code for synchronizing analog output with analog input is here:
daqreset
%%Set up analog input
ai = analoginput('nidaq','Dev1');
addchannel(ai,0);
ai.SampleRate = 10000;
ai.SamplesPerTrigger = 40000;
ai
%%Set up analog output
ao = analogoutput('nidaq','Dev1');
addchannel(ao,0);
ao.SampleRate = 1000;
ao
%%Perform the output and recording
% Set the analog input subsystem to start when the START command is issued.
ai.TriggerType = 'Immediate';
% Set the analog input system to signal on PFI line 1 when it starts.
ai.ExternalTriggerDriveLine = 'PFI1';
% Set the analog output system to receive its trigger from a
% hardware line, in this case, PFI1.
ao.TriggerType = 'HwDigital';
ao.HwDigitalTriggerSource = 'PFI1';
ao.TriggerCondition = 'PositiveEdge';
% Make sure the analog output is at zero.
putsample(ao,0)
% Load test signal into the analog output buffer.
putdata(ao,outputSignal)
% Start the acquisition and generation again.
start(ao)
start(ai)
wait([ai,ao],2)
[data,time] = getdata(ai);
plot(time * 1000,data)
xlim([0 20])
title('Synchronization using PFI (Positive Edge)')
xlabel('milliseconds')
ylabel('volts')
%%Clean up
delete(ao)
delete(ai)
Most of this code is copied from the MATLAB documentation. I believe what's happening is when I call start(a0), the output is queued up. Then when I call start(ai), the input sends a signal through the RTSI to the output to begin generating data. Then, the input and output are synchronized and the analog output appears without a phase shift on the plot of the analog input.
Could you please help me modify this code to achieve the desired result? For example, is there any way to get rid of the analog output completely and make the output purely digital but synchronized with the analog recording? Also, is there any way to stop the digital output precisely after 500 ms or after a fixed number of samples is acquired?
Thanks!

Answers (0)

Community Treasure Hunt

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

Start Hunting!