How can I output a series of pulses using the Data Acquisition Toolbox?

4 views (last 30 days)
I want to output a series of digital pulses using the Data Acquisition Toolbox in MATLAB. I would like the pulses to have a period of 7 miliseconds: "off" for 5 miliseconds and "on" for 2 miliseconds.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This can be done using the following code:
function sendPulse(numofperiods, ontime, offtime)
% creates a digital I/O object
parport = digitalio('parallel','LPT1');
% adds out line to the digital I/O object
hwline = addline(parport,0,'out');
%configures the digital I/O object
set(parport, 'TimerPeriod', offtime);
set(parport, 'TimerFcn', {@puton, numofperiods, ontime, offtime});
set(parport, 'UserData', 0);
putvalue(parport.Line(1), 0); %pulse is off
start(parport);
end
% Subfunction that switches the pulse "on" or "off"
% and also stops the object when the specified
% number of periods has been reached.
function puton(obj, str, numofperiods, ontime, offtime)
obj.TimerFcn = {@putoff, numofperiods, ontime, offtime};
obj.TimerPeriod = offtime;
putvalue(obj.Line(1), 1); %pulse is on
if (obj.UserData == numofperiods)
obj.TimerFcn = ' ';
stop(obj);
end
obj.UserData = obj.UserData +1
end
function putoff(obj, str, numofperiods, ontime, offtime)
obj.TimerFcn = {@puton, numofperiods, ontime, offtime};
obj.TimerPeriod = ontime;
putvalue(obj.Line(1), 0); %pulse is off
end
To call the the function, run the following command:
% outputs 10 periods of a pulse that's
% "off" for 5 ms and "on" for 2 ms
sendPulse(10,.002,.005)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!