Intro: Controlling the Analog Discovery™ Waveform Generator in LabVIEW
The goal of this project is to teach you how to control the analog discovery with LabVIEW. In particular this project will demonstrate how to control the analog discoveries wave form generator. You can control the frequency and amplitude using a 2d box. Depending on where you click inside the 2d box, a different frequency and amplitude will be played. The intention was to make an audible tone generator similar to this one. To hear the tones being played you can plug headphones or speakers into the Analog Discovery’s 3.5mm jack.
Waveform Generator Details
The LabVIEW project lets you generate
- Sine wave
- Square wave
- Triangle wave
- Ramp Up Wave
- Ramp Down Wave
The project allows you to control the following aspects of the waveforms
- Waveform type
- Frequency Amplitude/Volume
- Phase shift
- DC offset
NOTE: For this project DC offset is set to a constant of 0. DC offsets can potentially damage speakers/headphones. If you do not intend to use the project for generating audible tones with speakers connected, feel free to replace the DC offset constant with a control.
Click here to see a preview of the project.
Step 1: Materials
Step 2: Preface
To properly understand how this project functions, it is important to understand the Waveforms SDK. Normally the analog discovery is controlled through the free software package Waveforms. WaveForms also comes with an open source development kit. This SDK gives you access to a C/C++ API library allowing anyone to write their own software interface for the Analog Discovery. An overview of all the functions available in the API can be found in the WaveForms Reference Manual.
At its core this LabVIEW project is simply calling the C/C++ API functions. Special VIs used
to call each individual Waveforms API function in LabVIEW. You can see list of the available API functions by clicking the digilent.lvllib in the project window. Every VI in this list has a corresponding C/C++ API function. For more info about how each function works refer to the WaveForms Reference Manual, as illustrated in the image above.
If you want to learn more about how to call C functions in LabVIEW check out the following links.
Step 3: Main GUI Code Overview
In GUI Main, the code is a simplified derivative of GUI Main from the previous LabForms Project. For neatness a lot of the Waveforms API calls have been grouped together into subVIs. In the new GUI Main there are two primary sections to focus on, the Device Selection Loop and the Run Device Functions section.
Device Selection Loop
The Device Selection Loop has a couple primary functions. First it searches for compatible devices to connect to using the "Find Connected Devices.vi". A refresh button option has been added so you can connect a device after starting the GUI Main.vi. The "Find Connected Devices.vi" checks to make sure no other applications are already using the device you want to open. If the device is already in use, the front panel controls are configured so the "OK" button is grayed out. This prevents you from opening a device that is already in use. If the device isn't in use you can hit 'OK' to move on to the opening process. Once the opening process beings the Device Selection Loop terminates, and the Run Device Functions section is initiated.
Run Device Functions
The first portion of this section opens the selected device. The process is rather self explanatory and does not need to be explained in detail. Refer to the numbered comments in the code for more info. Once the device is opened, any custom VI you designed will begin to run. In this project the " GUI Custom Waveform Gen.vi" is run. If you want to scope your custom signal output, you can click the Enable Scope button before selecting the device. This will open the standard oscilloscope window from LabForms.
Step 4: Custom Wavefrom Generation Code Overview
The custom waveform generator does a couple of things. The first time it is called, both waveform Channels W1 and W2 are enabled (see the code in the top left of the loop). Next some calculations are done to convert cursor position with in a 2d box, into amplitude and frequency of a waveform. Refer the the comments in the code for details.
Generate Wavefrom/Event Structure
The core of this project is generating analog signals. This is done in event structure, using "Generate Waveform.vi". This sub VI simply calls a handful of WaveForms API functions that control the Analog discovery. A picture of the Generate Waveform.vi code is included above. It depicts which API functions are used to control the Analog Discovery's signal generator. For more info on how these API functions work refer to the WaveForms SDK reference manual.
For the waveform generation to work well it is important to note the context in which the Generate Waveform.vi is called. This VI should be called in a Event Structure so it is only activated when some event occurs. Calling G
Event Structure Case:
The fist is a timeout case. It is important that a timeout duration is supplied by wiring a value to the small hourglass in the top left of the event structure. Failing to do so can cause the whole project to freeze running.
Event Structure Case:
This event case is the one that calls the
Drawing a Cursor on the 2d Picture
If you are curious about how to draw a cursor on the 2d picture check out the subVI code above. The comments in the code explain how the cursor is drawn.
Step 5: Running the Project
To get things running open the LabVIEW project. Once the project window has opened double click the "GUI Main.vi". Once the the front panel has open for this VI do what is depicted in this gif to get the waveform generator running.
Physical Wiring for WaveGen/Scope
If you want to view the waveform being generated in the scope panel, you must wire the waveform an analog output and analog input pins (see image above). For pin details refer to the Analog Discovery pinout.
Properly Stopping the Project
To get the entire project to stop, you must hit the stop buttons on all the windows that popped up. The GUI main window does not have a stop button, and will automatically stop once you have properly closed the other windows.