AnsweredAssumed Answered

Binary arb sequence Matlab 33522A

Question asked by lhornburg Employee on Apr 13, 2012
I created this example for a customer who had an older version of Matlab.  Matlab versions less than 2010 have issue with the binblockwrite command.  This program shows you how to download binary arbitrary waveforms to the 33522A, without the need for the binblockwrite command, then create a sequence using these waveforms:




% clears all variables, closes all open files

clear all; close all; clc;

%opens and creates a visa session for communication with function generator

fgen = visa('AGILENT','USBInstrument1');
fgen.Timeout = 10;
set (fgen,'OutputBufferSize',2000000000);

fopen(fgen);

%Query Idendity string and report
fprintf (fgen, '*IDN?');
idn = fscanf (fgen);
fprintf (idn)
fprintf ('\n\n')

%Clear and reset instrument
fprintf (fgen, '*RST');
fprintf (fgen, '*CLS');

%Clear volatile memory
fprintf(fgen, 'SOURce1:DATA:VOLatile:CLEar');
fprintf(fgen, 'FORM:BORD SWAP');  %configure the box to correctly accept the binary arb points
fprintf(fgen,'TRIGGER1:SOURCE EXT'); %set trigger source for channel 1 to external

%---------------------------------------------------------------------------------------------
%create 100Kpoint arb named z
numDataPoints = 100000;
z = zeros(1, numDataPoints, 'single');
i = single(1:1:numDataPoints);      
z = (i-1)/numDataPoints;  %creates a simple rising waveform 100Kpoints long

bytes=num2str(length(z) * 4); %# of bytes
header= ['SOURce1:DATA:ARBitrary testrise1, #' num2str(length(bytes)) bytes]; %create header
binblockBytes = typecast(z, 'uint8');  %convert datapoints to binary before sending
fwrite(fgen, [header binblockBytes], 'uint8'); %combine header and datapoints then send to instrument


fprintf(fgen, '*WAI');   %Make sure no other commands are exectued until arb is done downloading
fprintf('Download1 Complete\n\n')


fprintf(fgen,'SOURce1:FUNCtion:ARBitrary testrise1'); % set current arb waveform to defined arb testrise
fprintf(fgen, 'MMEM:STOR:DATA "INT:\RAMP1.barb"');

%-----------------------------------------------------------------
%create 2nd 100Kpoint arb named z
numDataPoints = 100000;
z = zeros(1, numDataPoints, 'single');
i = single(1:1:numDataPoints);      
z = (1-i)/numDataPoints;  %creates a simple falling waveform 100Kpoints long

bytes=num2str(length(z) * 4); %# of bytes
header= ['SOURce1:DATA:ARBitrary testrise2, #' num2str(length(bytes)) bytes]; %create header
binblockBytes = typecast(z, 'uint8');  %convert datapoints to binary before sending
fwrite(fgen, [header binblockBytes], 'uint8'); %combine header and datapoints then send to instrument

fprintf(fgen, '*WAI');   %Make sure no other commands are exectued until arb is done downloading
fprintf('Download2 Complete\n\n')

fprintf(fgen,'SOURce1:FUNCtion:ARBitrary testrise2'); % set current arb waveform to defined arb testrise
fprintf(fgen, 'MMEM:STOR:DATA "INT:\RAMP2.barb"');
%-------------------------------------------------------------------

%load waveforms into volatile memory so they can be used in sequence

fprintf(fgen, 'SOURce1:DATA:VOLatile:CLEar');
fprintf(fgen,'MMEM:LOAD:DATA "Int:\RAMP1.barb"');
fprintf(fgen,'MMEM:LOAD:DATA "Int:\RAMP2.barb"');

%-----------------------------------------------------------------------------------
%build sequence
sequencename = 'mySequence';
RAMP1 = '"INT:\RAMP1.barb",0,onceWaitTrig,lowAtStart,10';
RAMP2= '"INT:\RAMP2.barb",0,onceWaitTrig,lowAtStart,10';
%combine all of the above strings into one string
SeqDescriptor = [sequencename ',' RAMP1 ',' RAMP2];


%Calculate the header of sequence command.
     strLength = length(SeqDescriptor);
    header = num2str(strLength); %converts to string
    headerlength = length(header);
    digits = num2str(headerlength); %converts to string
     

     %Combine the header information and the sequence.
     sequencestring = ['SOURce1:DATA:SEQ #' digits header SeqDescriptor]

     %Send the sequence and make active.
     fprintf(fgen, sequencestring);
     fprintf(fgen, '*WAI');
    fprintf(fgen,'SOURce1:FUNCtion:ARBitrary mySequence');

%-------------------------------------------------------------------------------------------------------------    
    
    %Set waveform parameters 
fprintf(fgen,'SOURce1:FUNCtion ARB'); % turn on arb function
fprintf(fgen,'SOURCE1:VOLT 2'); % set max waveform amplitude to 2 Vpp
fprintf(fgen,'SOURCE1:VOLT:OFFSET 0'); % set offset to 0 V
fprintf(fgen,'OUTPUT1:LOAD 50'); % set output load to 50 ohms
fprintf(fgen,'SOURCE1:FUNCtion:ARB:SRATe 10000000'); % set sample rate


%Enable Output for channel 1
fprintf(fgen,'OUTPUT1 ON');



% Read Error
fprintf(fgen, 'SYST:ERR?');
errorstr = fscanf (fgen);

% error checking
if strncmp (errorstr, '+0,"No error"',13)
   errorcheck = 'Arbitrary waveform generated without any error';
   fprintf (errorcheck)
else
   errorcheck = ['Error reported: ', errorstr];
   fprintf (errorcheck)
end

%Save Arb to USB stick, titled test arb

% fprintf(fgen, 'MMEM:STOR:DATA "USB:\RAMP100_40M.arb"');

%closes the visa session with the function generator
fclose(fgen);  

Outcomes