Working Matlab Code!!!

Working Matlab Code!!!, a forum discussion on Cleverscope. Join us for more discussions on Working Matlab Code!!! on our Other Issues forum.

Back to Forum Index : Back to Other Issues   RSS

5 Mar 2009
Posts: 9

ok... I've written three functions for the clever scope: initialization, acquire, and close.

it works OK, but there are still a few things that need to be resolved such as:
- properly updating t0 and dT
- negative values for AcquireDefinition.value.StartTime don't seem to do anything
- acquisitions are lagging by 1 frame:

anyway... here is what I have so far, if anyone can help with the above three issues, I'd appreciate it.

-Bryan

---- cscope_init.m ----
function
ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData, DigInpAllocSpace, CAUStatus,
errorOut]=cscope_init
%---- Cleverscope library load ----%
%make sure the location of the .dll file and the .h file are added to the matlabpath!!!
if ~libisloaded('cscope')
disp('Matlab: Load Cscope Control Driver.dll')
funclist = loadlibrary('Cscope Control Driver', 'Cscope Control Driver.h', 'alias', 'cscope');
%if you do NOT have Cscope Control Driver.dll and Cscope Control Driver.h
%in your Matlab path, add full pathnames or copy the files.
%libfunctions('cscope', '-full') % use this to show the...
%libfunctionsview('cscope') % included function
disp('Matlab: cscope control driver.dll loaded')
disp('')
else
disp('Matlab: cscope control driver.dll already loaded')
disp('')
end

%%---- Sets up the default values ----%
acquire.AcquireMode = uint16(2); %How to acquire: 0 = Single, 1=automatic, 2 = triggered, 3 = stop
acquire.AcquisitionMode = uint16(0); %Method of acquisistion: 0 = sampled, 1= Peak captured,
2 = Filtered, 3= Repetitive, 4= Waveform avg If Waveform avg,
makesure there are at least waveform avg +1 buffers.
acquire.Acquirer = uint16(4); %Cleverscope is the acquirer (always 4)
acquire.TransferChans = uint16(2); %Transfer both channels (always 2)
acquire.AMaxScale = double(2); % Volts range = +/-2
acquire.AMinScale = double(-2);
acquire.BMaxScale = double(2);
acquire.BMinScale = double(-2);
acquire.AProbe = uint16(0); %x1
acquire.BProbe = uint16(0); %x1
acquire.ACoupling = uint16(1); %DC
acquire.BCoupling = uint16(1); %DC
acquire.ABandwidth = uint16(1); %100 MHz
acquire.BBandwidth = uint16(1); %100 MHz
acquire.TriggerSource = uint16(2); %Sets trigger source. 0 = A chan, 1 = B chan, 2 = Ext Trigger, 3 =Dig Input, 4 = Rear Input
acquire.TriggerAmplitude = double(2.5); %Trigger at 2.5 volts
acquire.ATriggerAmplitude = double(0);
acquire.BTriggerAmplitude = double(0);
acquire.TriggerFilter = uint16(0); %No trigger filter
acquire.TrigSlope = uint8(0); %rising
acquire.TriggerHoldoff = double(0);
acquire.DigPatternRqd = uint8(0); %not used
acquire.DigPattern = uint32(0); %not used
acquire.ExtTrigThreshold = double(0);
acquire.DigInputThreshold = double(2);
acquire.StartTime = double(0);%double(-0.005); %Sets the start time relative to the trigger, at which acquisition will begin. If positive delayed triggering is used.
acquire.StopTime = double(50e-6); %Sets the stop time relative to the trigger. Range is -22 ..
+ 22seconds. Resolution is 10 ns.
acquire.PreTrigTime = double(0.005); %not used
acquire.Port = uint16(0); %not used
acquire.NumDivisions = 10; int16(10); %set to 10
acquire.NumSeqFrames = int16(1); %Sets the number of frames captured sequentially.
If not waveform avg method of capture set to 1. If waveform avg capture, set to the number of averages used,
4, 16, 64, 128. If capturing sequential frames, set to number of frames to capture.
acquire.NumBuffers = int32(2); %Sets the number of buffers allocated for frame capture.
Must be at least num waveform averages + 1. Minimum value is 2.
acquire.SigGenFreq = double(1000); %1kHz output
acquire.SigGenAmp = double(1); %1V amplitude
acquire.SigGenOffset = double(0);
acquire.SigGenWaveform = uint16(0); %sine
acquire.SigGenSweep = uint16(0);
acquire.SigGenFunc = uint16(0);
acquire.SigGenFreq2 = double(0); %not used
acquire.SigGenPhase = double(0); %not used
acquire.Trig2Function = uint16(0);
acquire.MinTriggerPeriod = double(0.0000001);
acquire.MaxTriggerPeriod = double(1);
acquire.TriggerCount = uint32(1);
acquire.Trig2Slope = uint8(0);
acquire.Trig2SourceChan = uint16(0);
acquire.Trig2Level = double(0);
acquire.DigPattern2Rqd = uint8(0);
acquire.DigPattern2 = uint32(0);
acquire.Trigger2Source = uint16(0);
acquire.WaveformAverages = int32(1); %Values are 0 = 4, 1 = 16, 2 = 64, 3 = 128
acquire.ValueChanged = int32(1);
acquire.SamplerResolution = uint16(2); %0 = 10 bit (1 = 12 bit, 2 = 14 bit).
acquire.UnitsAreLinked = uint8(0);

acquire.IntfSource = uint16(0); %0 = USB, 1 = Ethernet - open given IP address,

acquire.ExtSampleClock = uint8(0);

acquire.TCPPort = uint32(53270);
%acquire.TCPAdr = 0xC0A80169; //192.168.1.105

acquire.UpdateRate = uint16(0);
acquire.TransferSize = uint16(0);

AcquireDefinition= libpointer('TD1Ptr', acquire);
%% 1. Allocate memory for the data arrays.
nsamp=2048;

AcquisitionUnit= int32(0); %use 0
ReplayStartTime= double(acquire.StartTime); %This value specifies, in seconds,
the stop time (inclusive) of the samples to be returned in the decimated replay from the
%sample buffer. If the start time is outside the actual available buffer start and stop times
(relative to the trigger), the start %time will be clipped to either the beginning or end of the buffer,
as necessary.
ReplayStopTime= double(acquire.StopTime); %This value specifies, in seconds,
the stop time (inclusive) of the samples to be returned in the decimated replay from the
%sample buffer. If the start time is outside the actual available buffer start and stop times
(relative to the trigger), the start %time will be clipped to either the beginning or end of the buffer,
as necessary.
SamplesInReplay= int32(nsamp); %The maximum size is the acquisition storage size (4 or 8M) divided by 2.
FrameNumber= int32(0); %Default is 0
GotSamples= libpointer('uint8Ptr', 1); %Create a pointer for samplesreceived?.
Returns 0 if samples are not yet all received. 1 = received the values.
T0= libpointer('doublePtr'); %Returns the start stringtime of thewaveform being replayed
relative to the trigger, which is time 0, in seconds.
dT= libpointer('doublePtr'); %Returns the interval between successive samples, in seconds.
NumSamples= libpointer('uint32Ptr', 0);
NumFrames= libpointer('uint32Ptr', 0);
ChanAData= libpointer('singlePtr', zeros(nsamp, 1));
ChanAAllocSpace= int32(nsamp);
ChanBData= libpointer('singlePtr', zeros(nsamp, 1));
ChanBAllocSpace= int32(nsamp);
DigitalInputData= libpointer('uint16Ptr', zeros(nsamp, 1));
DigInpAllocSpace= int32(nsamp);
CAUStatus= libpointer('uint16Ptr', 0); %Status values are 0=Runtime Closed, 1=Closed,
2=Open and 3=Fault ErrorOut= libpointer('TD2Ptr'); %Defines any errors using the TD2 data structure.

%% 2. Set the Acquisition Unit number (0..7). Call the DLL with the Inititialize (0) command.
You will need to
% Inititialize each Cleverscope slot used.
Command=uint16(0);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT,
NumSamples, NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace,
DigitalInputData, DigInpAllocSpace, CAUStatus, errorOut)
status=get(CAUStatus, 'Value')
%% 3. Use the CAU status (6) command until the status is ""Open"".
Command=uint16(6);
while get(CAUStatus, 'Value')~=2
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT,
NumSamples, NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace,
DigitalInputData, DigInpAllocSpace, CAUStatus, errorOut)
status=get(CAUStatus, 'Value')
end

%% 4. Setup the Acquire Definition, and call using the Acquire (1) command. The Acquire call automatically updates the
%acquisition unit to the contents of the acquire structure.
Command=uint16(4);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)

---- cscope_acquire.m ----
function cscope_acquire(AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)
%5.1 Wait for Samples
Command=uint16(3);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut);

%5.2 Acquire Samples
Command=uint16(1);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut);

%5.2 Acquire Samples
% Command=uint16(1);
% calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut);

pause(0.05)
%data=get(ChanAData, 'Value');

---- cscope_close.m ----
function cscope_close(AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)

%%Close this Acquisitions Unit by using the Close (5) command. Following the close
command the CAU Status will return ""Runtime Closed"".
Command=uint16(5);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)

Command=uint16(6);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)

status=get(CAUStatus, 'Value')

%% After having closed all other Acquisition Unit slots used, finish by calling the Finish (9) command.
The Finish %command automatically closes any slots you have not explicitly closed.
Command=uint16(7);
calllib('cscope', 'CscopeControlDriver', AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)



---- cscope_example.m ----
%initialize

NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut]=cscope_init;


%%
for a =1:100
%Set Trigger delay and window size
tic
Tdelay=a*.5e-6;
Twidth=100e-6;
AcquireDefinition.value.StartTime=Tdelay;
AcquireDefinition.value.StopTime=Tdelay+Twidth;

%acquire waveform
cscope_acquire(AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut);
data=ChanAData.value;
st(a)=ReplayStopTime;
plot(t, data)
dataall(a, :)=data;
drawnow
speed(a)=toc;
npts(a)=length(data);
pause
end
pcolor(double(dataall))
shading flat
%% close at the end
cscope_close(AcquisitionUnit, Command, ReplayStartTime, ReplayStopTime, SamplesInReplay, FrameNumber, AcquireDefinition, GotSamples, T0, dT, NumSamples,
NumFrames, ChanAData, ChanAAllocSpace, ChanBData, ChanBAllocSpace, DigitalInputData,
DigInpAllocSpace, CAUStatus, errorOut)
Back to Forum Index : Back to Other Issues   RSS
You must be logged in to post a reply



You need to Register or Log In before posting on these forums.

×

Your shopping cart is empty.