Code covered by the BSD License  

Highlights from
Creating and downloading waveforms -Tektronix 5000/7000 series AWGs

Creating and downloading waveforms -Tektronix 5000/7000 series AWGs

by

 

MATLAB® function to download arbitrary waveform to a Tektronix® 5000/7000 series AWG

download2TekAWG(AWGAddress, waveform, sampleRate, outputVoltage)
function download2TekAWG(AWGAddress, waveform, sampleRate, outputVoltage)
% Download2TekAWG This function uses SCPI commands to download an
% input waveform, specified by the 1-dimensional vector waveform, whose 
% sample rate is specified by sampleRate to the a Tektronix AWG whose VISA 
% resource string is specified by AWGAddress. Once downloaded, the waveform
% is played out continuously, at an output voltage specified by
% outputVoltage
% 
% Example: 
% AWGAddress = 'TCPIP0::172.28.16.148::inst0::INSTR'; 
% Fs = 10e6; timeVector = 0:(1/(100*Fs)):(1/Fs); waveform = 2*sin(2*pi*Fs*timeVector); 
% download2TekAWG(AWGAddress,waveform,Fs,0.5);

%% Version history
% Copyright 2012, The MathWorks, Inc.
% v1.0 - 08/2012 - Vinod Cherian

% Calculate length of waveform
waveformLength = numel(waveform);

%% Set up connection to instrument using VISA
% Find and use the first available VISA installation for instrument
% communication. Throw error if no VISA found.
VISAInfo = instrhwinfo('visa');
if isempty(VISAInfo.InstalledAdaptors)
    throw(MException('Download2TekAWG:NoVISA','No VISA installation found. Please ensure that you have a VISA installation supported by Instrument Control Toolbox on this computer.'));
end

myAWG = visa(VISAInfo.InstalledAdaptors{1}, AWGAddress);
set(myAWG, 'ByteOrder', 'littleEndian');
myAWG.OutputBufferSize = waveformLength*5 + 1024;
fopen(myAWG);

%% Reset AWG to factory defaults 
clrdevice(myAWG);
fwrite(myAWG, '*RST');
fwrite(myAWG, '*CLS');
localWaitForComplete;

%% Process data for AWG
% Generate marker byte (0) for each point
binblockData = zeros(1,waveformLength*5,'uint8');
for iLoop=1:waveformLength
    binblockData((iLoop-1)*5+1:(iLoop-1)*5+5) = [typecast(single(waveform(iLoop)),'uint8') 0];
end
% Build IEEE488.2 binblock header
binblockNumBytes = num2str(length(binblockData));
binblockHeader = ['#' num2str(length(binblockNumBytes)) binblockNumBytes];

%% Write data to AWG
% Create waveform destination on AWG and write data
fwrite(myAWG,[':WLISt:WAVeform:NEW "MATLABSignal",' num2str(waveformLength) ',REAL;']);
fwrite(myAWG,[':WLISt:WAVeform:DATA "MATLABSignal",' binblockHeader binblockData ';']);
localWaitForComplete;

%% Set parameters and enable output
% Set output on channel 1
fwrite(myAWG, sprintf('SOURce1:WAVeform "MATLABSignal"\n'));
% Set the output parameters
fwrite(myAWG, sprintf('SOURce1:VOLTage:LEVel:IMMediate:AMPLitude %f\n',outputVoltage));
% Set the sample rate
fwrite(myAWG, sprintf('SOURce1:FREQ %d\n',sampleRate));
% Enable output on channel 1
fwrite(myAWG, sprintf('OUTPut1:STATe 1\n'));
% Set the run mode to continuous
fwrite(myAWG, sprintf('AWGC:RMOD CONT\n'));
% set the AWG operation state
fwrite(myAWG, sprintf('AWGC:RUN\n'));

% Display any instrument errors
lastInstrumentError = query(myAWG,'SYSTEM:ERR?');
while isempty(strfind(lastInstrumentError,'No error'))
    disp(['Instrument Error: ' lastInstrumentError]);
    lastInstrumentError = query(myAWG,'SYSTEM:ERR?');
end

%% Clean up 
fclose(myAWG); delete(myAWG); clear myAWG

    % Local function to wait till all instrument commands up to this point have been executed
    function localWaitForComplete()
        % LOCALWAITFORCOMPLETE This function causes the AWG to execute all 
        % commands up till function call and returns when done
        fwrite(myAWG, '*WAI');
        while (str2double(query(myAWG, '*OPC?'))~=1)
            pause(1);
        end
    end

end

Contact us