PS5000a: code for rapid block mode and function generator

Post your MATLAB discussions here
Post Reply
deep78
Newbie
Posts: 0
Joined: Mon Jul 15, 2013 5:51 pm

PS5000a: code for rapid block mode and function generator

Post by deep78 »

Hello everyone,
I would like to share with you the following code, which implements a rapid block mode capture of channel A triggered by edges of a signal generated by the function generator.
It works with the following files:
  • PS5000a.dll ver. 1.0.1.8
  • PicoIpp.dll ver. 1.0.2.9
  • the PS5000aMFile.m file attached below
Hope this helps.
Marco Michele Sisto
2013-07-18

Code: Select all

% AUTHOR
% Creation: Marco Michele Sisto  2013-07-17

clc;
clear all;
close all;

if ~libisloaded('PS5000a')
    loadlibrary('PS5000a.dll','PS5000aMFile');
    if ~libisloaded('PS5000a')
        error('library PS5000a.dll or PS5000a.h not found') %check if the library is loaded
    end
end

[methodinfo,structs,enuminfo] = PS5000aMFile; % Data and method structure for the ps5000a api.


% oscilloscope parameters

unithandle = 0; % Handle to the opened unit.
offsetVoltage = 0; % Offset in microvolt (±1 V).
Trigger_channel = enuminfo.enPS5000AChannel.PS5000A_CHANNEL_A;
channel_range = enuminfo.enPS5000ARange.PS5000A_2V;
autoTrigger_us = 0;
Trigger_threshold_ADC_counts = 15000; %16 bits, bipolar
Trigger_direction = enuminfo.enPS5000AThresholdDirection.PS5000A_RISING;
number_of_samples_required=12500;
number_of_traces=1000;

% function generator parameters
pkToPk_uV = 1000000; % Peak-to-peak amplitude in microvolt (±250 mV to ±2 V).
offsetVoltage_uV = 1000000;
Frequency_Hz = 10000;
waveType = enuminfo.enPS5000AWaveType.PS5000A_SQUARE; % Type of Wave.
sig_generator_number_of_cycles = number_of_traces*2; % factor 2 ensures that number_of_traces trigger events are processed even if trigger signal is fast
Sig_gen_trigger_source = enuminfo.enPS5000ASigGenTrigSource.PS5000A_SIGGEN_SOFT_TRIG;
Sig_gen_trigger_type = enuminfo.enPS5000ASigGenTrigType.PS5000A_SIGGEN_RISING;

% other variables
timeIntervalNanoseconds = 0;
maxSamples = 0;
timeIndisposedMs = 0;
IsReady = 0;
overflow=0;
number_of_samples_acquired = zeros(number_of_traces,1);
timebase = 3;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Init
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

[status.open,unithandle] = calllib('PS5000a','ps5000aOpenUnit',unithandle,'CQ193/013',enuminfo.enPS5000ADeviceResolution.PS5000A_DR_15BIT);
status.USB = calllib('PS5000a','ps5000aChangePowerSource',unithandle,282); % activate USB power

disp('Init end')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Setup function generator
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

status.SetSigGen = calllib('PS5000a','ps5000aSetSigGenBuiltIn',unithandle,offsetVoltage_uV,pkToPk_uV,waveType,Frequency_Hz,Frequency_Hz,0,0,enuminfo.enPS5000ASweepType.PS5000A_UP,enuminfo.enPS5000AExtraOperations.PS5000A_ES_OFF,sig_generator_number_of_cycles,0,Sig_gen_trigger_type,Sig_gen_trigger_source,0);

disp('Sig Gen Setup end')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Setup capture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

status.SetChannel_A = calllib('PS5000a','ps5000aSetChannel',unithandle,enuminfo.enPS5000AChannel.PS5000A_CHANNEL_A,true,enuminfo.enPS5000ACoupling.PS5000A_DC, channel_range,offsetVoltage);
status.SetChannel_B = calllib('PS5000a','ps5000aSetChannel',unithandle,enuminfo.enPS5000AChannel.PS5000A_CHANNEL_B,false,enuminfo.enPS5000ACoupling.PS5000A_DC, channel_range,offsetVoltage);
% Disabling channel B is important as I am reading only channel A and
% setting output buffers only for channel A. If channel B is lenft enabled
% matlab crashes when calling ps5000aGetValues

[status.GetTimebase,timeIntervalNanoseconds,maxSamples] = calllib('PS5000a','ps5000aGetTimebase2',unithandle,timebase,number_of_samples_required,timeIntervalNanoseconds,maxSamples,0);
time_ns = (0:1:(number_of_samples_required-1))*timeIntervalNanoseconds;
status.SetTrigger = calllib('PS5000a','ps5000aSetSimpleTrigger',unithandle,1,Trigger_channel,Trigger_threshold_ADC_counts,Trigger_direction, 0,autoTrigger_us);
[status.SetMemorySegments,maxSamples_on_each_segment] = calllib('PS5000a','ps5000aMemorySegments',unithandle,number_of_traces,maxSamples);
[status.SetNoOfCaptures] = calllib('PS5000a','ps5000aSetNoOfCaptures',unithandle,number_of_traces);

disp('Capture Setup end')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Execute capture
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic
% start acqusition in rapid block mode as soon as trigger events are
% detected
[status.RunBlock,timeIndisposedMs] = calllib('PS5000a','ps5000aRunBlock',unithandle, 0, number_of_samples_required, timebase, timeIndisposedMs, 0, [], []);

% run trigger signal
status.SigGenSoftwareControl = calllib('PS5000a','ps5000aSigGenSoftwareControl',unithandle,0);

% check regularly if capture is complete
IsReady=0;
while IsReady==0
    pause(.1)
    [status.IsReady,IsReady] = calllib('PS5000a','ps5000aIsReady',unithandle,IsReady);
end
toc

nProcessedCaptures=0;
[status.NoOfProcessedCaptures,nProcessedCaptures] = calllib('PS5000a','ps5000aGetNoOfProcessedCaptures',unithandle,nProcessedCaptures);
fprintf('Number of captures: %d\n',nProcessedCaptures);
disp('Capture end')


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Download data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

buffer_size = number_of_samples_required;
pBufferChA = libpointer('int16Ptr', zeros(buffer_size, 1));
ChA_data = zeros(buffer_size,number_of_traces);
for k_number_of_traces=1:nProcessedCaptures
    status.SetDataBuffer(k_number_of_traces) = calllib('PS5000a','ps5000aSetDataBuffer',unithandle,enuminfo.enPS5000AChannel.PS5000A_CHANNEL_A,pBufferChA,buffer_size,k_number_of_traces-1,enuminfo.enPS5000ARatioMode.PS5000A_RATIO_MODE_NONE);
    [status.GetValues,number_of_samples_acquired(k_number_of_traces),overflow]  = calllib('PS5000a','ps5000aGetValues',unithandle,0,number_of_samples_required,0,enuminfo.enPS5000ARatioMode.PS5000A_RATIO_MODE_NONE,k_number_of_traces-1,overflow);
    ChA_data(:,k_number_of_traces) = pBufferChA.Value;
end
clear pBufferChA
disp('Data download end')


%%% averaging
average_trace = mean(ChA_data.').';

figure
plot(time_ns/1000,ChA_data(:,1))
hold all
plot(time_ns/1000,average_trace)
xlabel('microseconds')

status.Stop = calllib('PS5000a','ps5000aStop',unithandle);
status.close = calllib('PS5000a','ps5000aCloseUnit',unithandle);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% END
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('unload library...');
unloadlibrary('PS5000a');

Attachments

[The extension m has been deactivated and can no longer be displayed.]


Post Reply