Tektronix Technical Forums are maintained by community involvement. Feel free to post questions or respond to questions by other members. Should you require a time-sensitive answer, please contact your local Tektronix support center here.

AFG3000 MATLAB ICT Send Waveform 1

Questions in this forum area are community supported. Tektronix does not regularly monitor or guarantee responses in this area.
Post Reply
Carl M
Tektronix Applications
Tektronix Applications
Posts: 797
Joined: July 30th, 2008, 4:45 pm
Country: United States

AFG3000 MATLAB ICT Send Waveform 1

Post by Carl M » November 13th, 2009, 5:46 pm

Use the table view of the samples on the instrument to verify the individual samples.

Code: Select all

%% AFG3000 MATLAB ICT Send Waveform 1
% Date: 11-18-2008
% ==================
% Send 14 bit waveform data to an AFG3000.
% 
% PREREQUISITE EXAMPLES
% ==================
% MATLAB ICT Control 1 (Hello World)
% ==================
%
% COMPATIBILITY
% ==================
% AFG3000, AFG3000B
% ==================
%
% TESTED & DEVELOPED
% ==================
% Microsoft Windows XP SP2
% TekVISA 3.3.2.7
% MATLAB Version 7.6.0.324 (R2008a)
% Instrument Control Toolbox Version 2.6
% GPIB: National Instruments PCMCIA-GPIB (ni488k.sys v2.6.0f0)
% AFG3252 FW 3.0.1
% ==================
%
% Tektronix provides the following example "AS IS" without any guarantees
% or support.  This example is for instructional guidance only.
 
%% variables
visa_vendor = 'tek';
visa_address = 'GPIB0::3::INSTR';
% example waveform: sine wave using 12 sample points, integers between 0 to
% 16382 (range of AFG).  hex notation is easier to verify later
% (note: max value of afg3000 is '3FFE' not '3FFF')
wave = {'2000' '2FFF' '3BB6' '3FFE' '3BB6' '2FFF' '2000' '1000' '0449' ...
    '0000' '0449' '1000'};
wave = hex2dec(wave);
 
%% pre-processing
% encode variable 'wave' into binary waveform data for AFG.  This is 
% the same as AWG5000B but marker bits are ignored.  see AWG5000B series 
% programmer manual for bit definitions.
binblock = zeros(2 * length(wave), 1);
binblock(2:2:end) = bitand(wave, 255);
binblock(1:2:end) = bitshift(wave, -8);
binblock = binblock';
 
% build binary block header
bytes = num2str(length(binblock));
header = ['#' num2str(length(bytes)) bytes];
 
%% instrument communication
afg = visa(visa_vendor, visa_address);
fopen(afg);
fwrite(afg, '*rst;');
fwrite(afg, '*cls;');
 
% clear edit memory and set to 12 samples
fwrite(afg, ':trace:define ememory, 12;');
 
% send the data to edit memory
fwrite(afg, [':trace ememory,' header binblock ';'], 'uint8');
 
% set channel 1 to arb function found in edit memory
fwrite(afg, ':source1:function ememory;');
 
% turn on channel 1
fwrite(afg, ':output1 on;');
 
% gracefully disconnect
fclose(afg);
delete(afg);
clear afg;
Please discuss.
Tektronix Application Engineer

MrKel
Posts: 1
Joined: April 25th, 2013, 10:04 am

Re: AFG3000 MATLAB ICT Send Waveform 1

Post by MrKel » July 8th, 2013, 2:57 pm

Hi, Im trying to make my own waveform and I just cant set it more than 245 samples... I have downloaded a manual with the commands of how to communicate with the AFG but I just cant make it work with more samples. Im using the code from here as a base.
Any help about it?

Carl M
Tektronix Applications
Tektronix Applications
Posts: 797
Joined: July 30th, 2008, 4:45 pm
Country: United States

Re: AFG3000 MATLAB ICT Send Waveform 1

Post by Carl M » July 19th, 2013, 10:57 am

MrKel wrote:Hi, Im trying to make my own waveform and I just cant set it more than 245 samples... I have downloaded a manual with the commands of how to communicate with the AFG but I just cant make it work with more samples. Im using the code from here as a base.
Any help about it?
sounds like a buffer problem. you'll have to increase the buffer of your visa object in matlab or use multiple writes with EOI off. I do this in my AWG example: http://www1.tek.com/forum/viewtopic.php?f=6&t=3217

Carl M
Tektronix Applications
Tektronix Applications
Posts: 797
Joined: July 30th, 2008, 4:45 pm
Country: United States

Re: AFG3000 MATLAB ICT Send Waveform 1

Post by Carl M » January 25th, 2017, 4:57 pm

rewrite to show normalization to dac values as discussed in topic: viewtopic.php?f=529&t=139184#p282112

Code: Select all

%% AFG3000 MATLAB ICT Send Waveform 1
%{ 
date: 01-25-2017

demonstrate 'data:define' and 'data' remote programmatic commands for the
AFG3000 series and AFG2000 series.

The data following the binary block header is unsigned 16-bit integer 
values, most significant byte first (big-endian). Bits 15 and 16 are 
ignored as the AFG has 14-bits of dynamic range. The key concept is the 
digital values are unit-less. Value 0 is the minimum output and value 16382 
is the maximum output. Amplitude (and offset, and timing) are applied at 
run-time based on the AFG settings.

MATLAB R2016a v9.0.0.341360 64-bit
Instrument Control Toolbox v3.9 
NI-VISA v15.5
AFG2021 v1.1.9

Tektronix provides the following example "AS IS" without any guarantees
or support.  This example is for instructional guidance only.
%}

%% constants
visa_vendor = 'ni';
visa_descriptor = 'tcpip::134.62.36.91::instr';
record_length = 12;

%% calculations
% example arbitrary function: 1-cycle sine
sample = 0:(record_length - 1);
vector = sin(2 * pi * sample / length(sample));

% normalize to dac values
m = 16382 / (max(vector) - min(vector));
b = -m * min(vector);
dac_values = uint16(m * vector + b);
dac_bytes = typecast(swapbytes(dac_values), 'uint8');

% binary-block header
bbh = sprintf('%u', length(dac_bytes));
bbh = sprintf('#%u%s', length(bbh), bbh);

%% instrument communication
afg = visa(visa_vendor, visa_descriptor);
fopen(afg);
fprintf('connected to...\n')
r = query(afg, '*idn?');
fprintf(r);
fprintf('\n')

fprintf('clearing event status register & flushing messages\n')
fwrite(afg, '*cls');
r = query(afg, '*esr?');
fprintf(' esr value: %s', r);
r = query(afg, 'system:error?');
fprintf(' msg: %s', r);
fprintf('\n')

cmd = sprintf('data:define EMEM,%u', record_length);
fprintf('writing: "%s"\n', cmd);
fwrite(afg, cmd);
r = query(afg, '*esr?');
fprintf(' esr value: %s', r);
r = query(afg, 'system:error?');
fprintf(' msg: %s', r);
fprintf('\n')

cmd = ['data EMEM,' bbh dac_bytes];
fprintf('writing bytes (hex)...\n');
for r = 1:length(cmd)
    fprintf('%02X ', cmd(r));
    if (mod(r,16) == 0)
        fprintf('\n')
    end
end
fprintf('\n')
fwrite(afg, cmd);
r = query(afg, '*esr?');
fprintf(' esr value: %s', r);
r = query(afg, 'system:error?');
fprintf(' msg: %s', r);
fprintf('\n')

fprintf('disconnecting...\n\n')
fclose(afg);
delete(afg);

fprintf('done\n')
MATLAB Command Window output

Code: Select all

>> AFG3000_MATLAB_ICT_Send_Waveform_1
connected to...
TEKTRONIX,AFG2021,C010334,SCPI:99.0 FV:1.1.9

clearing event status register & flushing messages
 esr value: 0
 msg: 0,"No error"

writing: "data:define EMEM,12"
 esr value: 0
 msg: 0,"No error"

writing bytes (hex)...
64 61 74 61 20 45 4D 45 4D 2C 23 32 32 34 1F FF 
2F FF 3B B5 3F FE 3B B5 2F FF 1F FF 10 00 04 49 
00 00 04 49 0F FF 
 esr value: 0
 msg: 0,"No error"

disconnecting...

done
NI IO Trace correlation to AFG2021 display
afg2k_trace_data.png
afg2k_trace_data.png (10.55 KiB) Viewed 16748 times
Tektronix Application Engineer

Post Reply

Return to “Older and Obsolete Signal Sources”

Who is online

Users browsing this forum: No registered users and 2 guests