Contents

Using NI-FGEN Instrument Driver To Generate A Sine Wave

This example shows how to generate a sine wave on a function generator using the NI-FGEN software.

Instrument Control Toolbox™ supports communication with instruments through interfaces and drivers.

For more details on the toolbox, visit the Instrument Control Toolbox product page.

Requirements

In this example, you will learn to generate a sine wave using the NI-FGEN software package version 2.7.2 or higher and a NI PXI-5402 function generator. You can also use any other function generator supported by the NI-FGEN software package version 2.7.2 or higher.

Verify NI-FGEN Installation

Use the instrhwinfo command to check if the NI-FGEN software package is installed correctly. This example uses libraries installed with it.

NI-FGEN is an IVI-C compliant driver, and is listed as one of the Modules installed on the Windows machine.

driverInfo = instrhwinfo ('ivi');
disp(driverInfo.Modules');
    'niFgen'
    'nisACPwr'
    'nisCounter'
    'nisDCPwr'
    'nisDigitizer'
    'nisDmm'
    'nisDownconverter'
    'nisFGen'
    'nisPwrMeter'
    'nisRFSigGen'
    'nisScope'
    'nisSpecAn'
    'nisSwtch'
    'nisUpconverter'

Generate a MATLAB Instrument Driver from NI-FGEN Driver

Before you use an IVI-C driver, create a MATLAB Device Driver (MDD) for it, using the makemid function.

makemid takes the IVI-C module name as the input and outputs the MDD file in the present working directory (pwd).

makemid('niFgen');

Create a MATLAB Instrument Object

Use the icdevice function to create an instrument object from the MDD you generated, and establish a connection to the function generator using that object.

icdevice function takes two or more input arguments. The MDD file name, the resource name for the function generator and optionally, setting specific parameters.

You can get the resource name for the function generator from NI Measurement and Automation tool. For example: A resource name of PXI1Slot6 in NI MAX would be DAQ::PXI1Slot6 and Device 2 would be DAQ::2. You can remove the optionstring argument and the corresponding string parameter if you have the actual hardware.

You can establish a connection to the function generator using the connect command.

resourceID = 'DAQ::PXI1Slot6';
ictObj = icdevice('niFgen',resourceID,'optionstring','Simulate=true,DriverSetup=Model:5402');
connect(ictObj);

Configure the Function Generator

For the purpose of this example, the function generator is configured to generate a sine wave on Channel 0 with a frequency of 10E6, amplitude of 2, DC Offset of 0 and starting phase of 0.

Use the MATLAB Instrument Driver Editor (midedit) to view other properties and functions that allow you to configure a device. The tool shows all the properties and functions that the NI-FGEN software package supports.

Waveform = 1; %The number 1 is the enumerated value for Sine Waveform
ChannelName = '0';
Frequency = 10E+6;
Amplitude = 2.0;
DCOffset = 0;
StartPhase = 0.0;

invoke(ictObj.Configurationfunctionsstandardfunctionoutput,'configurestandardwaveform',ChannelName, Waveform, Amplitude, DCOffset, Frequency, StartPhase);

Initiate the Waveform Generation

Once you configure the function generator with the required settings, use an appropriate function call to initiate the waveform.

invoke(ictObj.Waveformcontrol,'initiategeneration');

Enable the Output

Once the waveform generation begins, enable the output of the function generator.

Enabled = 1;
invoke(ictObj.Configuration,'configureoutputenabled', ChannelName, Enabled);

Clean Up

Delete the MATLAB Instrument Object.

Note: If you delete the MATLAB Instrument Object, it will stop the waveform generation.

disconnect(ictObj);
delete(ictObj);
clear ictObj;
Was this topic helpful?