User:DukeEgr93/DAQ

From PrattWiki
Jump to navigation Jump to search

Channel Checker

% Clear out workspace
clear

% Clear out DAQ objects
delete(daqfind)

% Create Analog Output Object
AO = analogoutput('nidaq', 'Dev1');

% Change sample rate
set(AO, 'SampleRate', 500);

% View Analog Output Object
AO

% Add channels to Analog Output Object
addchannel(AO, [0])

% Create Analog Input Object
AI = analoginput('nidaq', 'Dev1')

% Add channels to Analog Input Object
addchannel(AI, [0 1 2 3 4 5 6 7])

% Write values to output channels
putsample(AO, [5])
%pause
putsample(AO, [0])

% Use loop to set several different voltages
for k=1:300
    % Calculate voltages for each channel
    Vout0 = 4*sin(2*pi*k/100);
    % Put voltages to each output channel
    putsample(AO, [Vout0])
    % Read voltages from all input channels
    Voltages(k,:) = getsample(AI);
    pause(0.02)
end

% Plot voltages versus index
figure(1); clf
for k=1:8
    subplot(4, 2, k)
    plot(Voltages(:,k));
    title(sprintf('%d',k))
end
figure(2);
plot(Voltages)

% Turn all outputs off
putsample(AO, [0])

For MX

% Clear out workspace
clear

% Create a session
s = daq.createSession('ni');

% Create Analog Input Object
% Add channels to Analog Input Object
addAnalogInputChannel(s, 'Dev1', 0:7, 'Voltage')

% Create Analog Output Object
addAnalogOutputChannel(s, 'Dev1', [0 1], 'Voltage')

% Write values to output channels
outputSingleScan(s, [5 5])
pause
outputSingleScan(s, [0 0])

% Use loop to set several different voltages
for k=1:300
    % Calculate voltages for each channel
    Vout0 = 2.5*sin(2*pi*k/100);
    Vout1 = 2.5*cos(2*pi*k/100);
    % Put voltages to each output channel
    outputSingleScan(s, [Vout0 Vout1])
    % Read voltages from all input channels
    Voltages(k,:) = s.inputSingleScan;
    pause(0.02)
end

% Plot voltages versus index
figure(1); clf
for k=1:8
    subplot(4, 2, k)
    plot(Voltages(:,k));
    title(sprintf('%d',k))
end
figure(2);
plot(Voltages)
    

% Turn all outputs off
outputSingleScan(s, [0 0])