Controlling Digital Discovery With LabVIEW

The following guide presents how to create a virtual instrument library for LabVIEW from the functions available in WaveForms SDK, how to edit the created VIs, and how to control the Digital Discovery with these VIs. Demos for spying on a digital communication protocol, for programming an AVR microcontroller, and for interfacing devices with different digital voltages can be found in the Examples section of this document.


Prerequisites

Hardware
  • Jumper wires
  • Pmod TMP3 - required for the Spying on I2C Communication example
  • Arduino Pro Mini with 8MHz ATmega328P, or other 3V3 microcontroller (LilyPad, Arduino Fio, etc.) - required for the Programming an AVR Microcontroller example
  • Pmod LVLSHFT - required for the Programming an AVR Microcontroller example if a 5V microcontroller (like Arduino UNO) is used
Software

Note: WaveForms can be installed by visiting the WaveForms Getting Started Guide. By installing WaveForms, WaveForms SDK will be installed, which is needed later in this guide.


Creating VIs for a Shared Library

Digital Discovery, as well as other Test and Measurement Devices, can be controlled using the functions available in WaveForms SDK. For the list and description of the available functions see C:\Program Files (x86)\Digilent\WaveFormsSDK\WaveForms SDK Reference Manual.pdf, or WaveForms SDK Reference Manual. For examples in C and Python languages see C:\Program Files (x86)\Digilent\WaveFormsSDK\samples.

To use the functions available in WaveForms SDK within LabVIEW, virtual instruments (VIs) have to be created from these functions. Follow the instructions in this guide to see, how to import the WaveForms SDK shared library in LabVIEW.


Open LabVIEW and create a new Blank VI. From the menu bar, open ToolsImportShared Library (.dll)…. Click Create VIs for a shared library, then click Next.

Select C:\Windows\System32\dwf.dll as the shared library and C:\Program Files (x86)\Digilent\WaveFormsSDK\inc\dwf.h as the header file, then click on Next.

Leave the next window as it is, just click on the Next button. After the header file is parsed, a list of the available functions will appear. The wizard will create a VI for the functions selected on this list. Hit Next to continue.

In the following window, the name and the location of the VI library can be set. By default the wizard creates the library in the LabVIEW user libraries folder, with the same name as the shared library (dwf in this case). Press Next to continue.

In the Select Error Handling Mode, select Simple Error Handling, as the error signal makes debugging easier.

Certain properties of every function can be edited in the following, but as the dwf library contains several hundred functions, it is more time efficient to leave every function with the default settings and edit just certain functions before using them (this is discussed in the Editing Generated VIs section). After displaying a Generation Summary, the wizard starts to generate the VIs from the shared library functions. The process takes time, so be patient.

When the Generation Process is finished, the VI library is ready to use. However, in the case of certain functions, an edit to the generated VI is necessary.


Editing Generated VIs

In the case of some functions, it might be needed to make some changes to the generated VI, before using it. To do this, open LabVIEW, create a new Blank VI, then open the Block Diagram (with Ctrl+E, or from the menu bar under the Window menu). Right click on the blank diagram, and select User LibrariesdwfVIs, then place on the block diagram the function which you want to use. In this example the F Dwf Digital I2c Read VI will be edited (you can use the search button to find a specific function).

Double click a VI to open its content, then open the Block Diagram (with Ctrl+E, or from the menu bar under the Window menu). You will see the definition of the function, and below it, one single block with controls at the input (left) side and indicators at the output (right) side.

You can modify your function from here, as you wish. In this demo the data type of the read data buffer will be changed.

Double click on the block (orange rectangle, with some black pattern on it), then select Parameters in the window that opens.

In the WaveForms SDK Reference Manual search for the chosen function. In this case it is the FDwfDigitalI2cRead function. The function description shows that the buffer for the read data is rgbRx of type unsigned char*.

In LabVIEW select rgbRx from the Parameters window. The function can be used more easily if this parameter is not a string, but an array of characters. At the Type drop-down select Array and at the Data type drop-down select Unsigned 8-bit Integer (it is the same size as unsigned char). Accept the changes.

On the block diagram, delete the control and indicator elements connected to the selected parameter. Delete the wires connected to them as well. Hover the mouse over the rgbRx parameter on the output (right) side of the block, then right click it and select create indicator. As the selected parameter is a buffer for the read data, creating a control element for it is not necessary. Press Ctrl+U to organize the diagram.

Double click on the created indicator to highlight it on the Front Panel. In the upper right corner of the Front Panel click on an empty terminal (white square) on the terminal selector (1), then click on the rgbRx indicator field (2).

Save the changes and close the Front Panel. It can be seen that an output terminal appeared on the block of the edited VI, with the name rgbRx of type Unsigned 8-bit Integer Array.


Examples

You can try the examples provided in this guide, or you can create your own examples using the created VI library.

Spying on I2C Communication

In the following project Arduino UNO and Pmod TMP3 are used to measure the ambient temperature and to send it to a PC. Digital Discovery is used to spy on this communication, to receive the data and to display the temperature on a LabVIEW user interface.


Preparations

As this guide focuses on controlling Digital Discovery, interfacing the Arduino UNO with Pmod TMP3 is not presented in detail. For a detailed guide about programming an Arduino to measure the temperature with Pmod TMP3, see: Using the Pmod TMP3 with Arduino UNO. Follow this guide before continuing.


Hardware Setup

To spy on the communication between the Arduino UNO and the Pmod TMP3, Digital Discovery must have a common reference (ground) with these devices, and the serial data and serial clock lines have to be connected to the Digital Discovery's DIO lines, like in the picture to the right.


Software Setup

A LabVIEW Virtual Instrument consists of two parts: the Front Panel and the Block Diagram. The Front Panel contains all controls and indicators for data input and output and serves as a user interface when the program is running. The Block Diagram contains the blocks which are present on the Front Panel, as well as other blocks which are necessary for information processing and the connections between these blocks. One can add a new block to both windows by right clicking on a blank space in the corresponding window and selecting the required block from a library. Blocks already present in the window can be modified by right clicking on the respective block. In the following the Front Panel and the Block Diagram of this example will be presented.

Download and unzip the provided example file spy_tmp3.zip then double click on it to open it with LabVIEW Community. All the functions used in the example can be used in your own VI, play around and see what else you can create.

Note: For this VI to work, the dwf VI library is necessary (see: Creating VIs for a Shared Library).


Front Panel

The Front Panel contains the Stop button, which stops the program and closes Digital Discovery to make it available for other software, as well as three control elements: input fields for defining Digital Discovery DIO lines connected to the Arduino's I2C data and clock lines and an input field for entering the I2C slave address in hexadecimal format.

Below the control elements, two thermometers are placed to show the measured temperature in degrees, in Celsius and Fahrenheit. At the bottom are two text fields: one for indicating errors, and one for displaying the received data in hexadecimal format.


Block Diagram

In this example, the Block Diagram has three main parts. The first part is responsible for configuring the Digital Discovery. The first function (F Dwf Device Close All) closes all Digilent Test and Measurement instruments to make them available for this program. The function F Dwf Device Open with the input parameter -1 opens the first available device and sends the device handler for all the other functions in this VI.

The I2C clock frequency is set to 100 KHz, then the SCL and SDA pins are defined. The DIO pins of the Digital Discovery are numbered from 24 (in default mode), so 24 has to be subtracted from the inputted numbers, 24 becoming DIO 0 and 25 becoming DIO 1. The F Dwf Digital I2c Clear function checks the pull-ups on the serial data and clock lines and returns 0 if there is an error at the connection.

Continuous reception of data is only started if there was no error at the pull-up check. If the check fails, an error message is displayed and data reception is not started.

At every iteration of a while loop, 2 bytes of data are received from the address defined on the front panel. The function F Dwf Digital I2c Read is edited, so the read data buffer is outputted to an Unsigned 8-bit Integer Array and not to a String (see: Editing Generated VIs).

The two received bytes are concatenated and shifted to the right with four bits, then raw data is converted to degrees Celsius and Fahrenheit and displayed on the Front Panel with the help of the thermometers. The received bytes are converted to hexadecimal strings, then concatenated with “0x” and displayed in the respective field on the Front Panel.

The loop is exited if there is an error, or when the Stop button is pressed.

When the loop is exited, the digital in- and out instrument parameters are reset, then the device is closed to make it available for other software. Errors are handled, if there were any.


Testing

After the devices are connected and the used DIO lines and slave address specified, the program can be started with the Run button on the Front Panel. The thermometers show the same temperature, as the one measured by the Arduino, while in the respective text field received raw data can be seen in hexadecimal format.

Programming an AVR Microcontroller

In the following project Digital Discovery is used to program an AVR microcontroller (Arduino Pro Mini in this case). To control the programming parameters and to receive feedback about the state of the devices, a LabVIEW user interface is used.

Note: This programming method overwrites Arduino bootloaders, which means, that the microcontrollers can't be programmed from USB. To re-enable this feature, after finishing the testing of this example, burn the bootloader to the used microcontroller following this guide: ArduinoISP.


Preparations

As this guide focuses on controlling Digital Discovery, creating an Arduino project is not presented in detail. For a detailed guide about creating a project for Arduino, check Arduino - Tutorials. For this guide, the built in Blink example will be used from Examples → Basics → Blink. Save this project in a chosen location, or use a project of your choice.


Hardware Setup

To program the microcontroller, SPI interface will be used, but the Chip Select signal will be connected to the Reset pin of the microcontroller. The MCU also needs to be powered, for which a VIO line of the Digital Discovery will be used, like in the picture to the right.

Using a 5V microcontroller

As the Digital Discovery's maximum output voltage is 3.3V, a logic level converter must be used to program a 5V MCU. To translate the signals coming from the Digital Discovery, the Pmod LVLSHFT is used. If the microcontroller is connected to the B side of the level converter and the Digital Discovery to the A side, the direction switches of the used channels must be switched to the B side.

After the program is uploaded to the MCU, the VI has to be stopped, or the connections between the Pmod LVLSHFT and the MCU must be removed, to start running the uploaded program.

The microcontroller must be powered from an external power source, for example an USB cable in the case of Arduino UNO.


Software Setup

A LabVIEW Virtual Instrument consists of two parts: the Front Panel and the Block Diagram. The Front Panel contains all controls and indicators for data in- and output and serves as a user interface when the program is running. The Block Diagram contains the blocks which are present on the Front Panel, but also other blocks which are necessary for information processing and the connections between these blocks. One can add a new block to both windows by right clicking on a blank space in the corresponding window and selecting the required block from a library. Blocks already present in the window can be modified by right clicking on the respective block. In the following the Front Panel and the Block Diagram of this example will be presented.

Download and unzip the provided example file (avr_programmer.zip) then double click on the main VI (program_AVR.vi) to open it with LabVIEW Community. All the functions and subVIs used in the example can be used in your own VI, play around and see what else you can create.

Note: For this VI to work, the dwf VI library is necessary (see: Creating VIs for a Shared Library).


Front Panel

The Front Panel contains the Stop button, which stops the program and closes Digital Discovery to make it available for other software, four input fields for defining Digital Discovery DIO lines connected to the microcontroller's SPI data, reset, and clock lines, as well as a drop-down list for selecting the microcontroller type and an input field for selecting the Arduino project.

To the right of these, four virtual LEDs are placed, for indicating the state of the MCU: the Power on LED is on if the MCU is powered, the Compiled LED is on if the project is compiled, the Erased LED turns on after the flash memory and the EEPROM of the MCU are deleted, and the Uploaded LED turns on after the compiled program is copied in the flash memory.


Block Diagram

The main VI (program_AVR.vi) contains several parts separated by decorative and functional structures, as well as multiple subVIs. In the following, these will be presented.

The first block contains functions for closing every opened Test and Measurement device, then opening the first available device. Following that, the digital voltage is configured to be 3.3V, then power on the VIO pins is turned on. For more information on these functions, see: WaveForms SDK Reference Manual. The power on indicator LED is turned on after the functions are executed.

The next block compiles the project, using the Arduino IDE compiler from command line, with the System Exec VI. The command line is started using the default installation directory for Arduino IDE. Using the default directory in Windows, this is “C:\Program Files (x86)\Arduino”. If you changed the installation directory when you installed the IDE this will be different. Choosing the microcontroller on the front panel results in passing the correct board definition as a command line argument to a block, which concatenates strings to get the command. The project path is used to locate the project and to define the project name.

After the concatenation the command to be executed is: “cmd /c arduino –pref build.path=path\to\project\build –board arduino:avr:board:definition –verify –preserve-temp-files path\to\project\project_name.ino”. This command compiles the selected project for the selected microcontroller, keeping the temporary files in the build folder inside the project. For more information, see the Arduino Manual Page.

The Compiled virtual LED is turned on after the command is executed. The flash memory size and page size of the microcontroller and the path to the compiled HEX file are also set in this block.

The next parts are responsible for initializing the SPI communication on the selected lines, with a clock frequency of 1MHz, then ereasing the memory of the microcontroller. The respective indicator LED is turned on after this step.

After 100ms, the content of the HEX file is read, and the data to be written in the flash memory of the MCU is put into an array. The program offset is also extracted from the file. The next block (AVR_write.vi) gets the flash memory size, the flash page size, the program offset, and the data from the previous block as parameters, and saves this data in the flash memory of the MCU.

The function F Dwf Digital Spi Write, from the dwf VI libary, used in the AVR_write subVI, has to be edited: The type of parameter rgTX needs to be changed from String to an Array of Unsigned 8-bit Integers. A terminal should be also connected to the control field of this parameter. For more information see Editing Generated VIs.

After the Uploaded indicator is turned on, the digital in- and out instruments of the Digital Discovery are reset, to ensure the proper functioning of the MCU.

A while loop ensures that the MCU remains powered, until the Stop button is pressed in the front panel. When the loop is exited, the power supply and the indicator LEDs are turned off, then the device is closed to make it available for other software. Errors are also handled here, if there were any.


SPI_init.vi

This subVI uses the WaveForms SDK functions from the dwf VI library to initialize SPI communication with a connected Test and Measurement device. After the clock frequency is set, the clock and data lines (MISO and MOSI) are specified. In default mode, the pins of the Digital Discovery are numbered from DIO 24, so 24 has to be subtracted from the inputted numbers.

The idle state of the data lines is set (default is high impedance state), then the SPI mode (0) and the endianness (MSB first) are set. The chip select line is specified and pulled up. Finally a dummy write command is issued while keeping the chip select high, to initialize the serial clock.


AVR_erase.vi

This subVI uses the WaveForms SDK functions from the dwf VI library to pull the chip select line (connected to the RESET pin of the MCU) low, then send the command for the MCU to enter into programming mode (0xAC530000) and to erase the flash memory and EEPROM (0xAC800000).


HEX_parse.vi

This subVI opens the HEX file created by the compiler, then loads its content to a string. From the first line of this string, the program offset is extracted. From every other line, the start code, the byte count, and the address are removed (first 7 bytes), then it is checked whether the current line contains data or not. If it contains data, this data and the checksum are concatenated to the string of previously saved data, then each two consecutive bytes are converted into a number, creating an array of numbers from the string containing the data.


AVR_write.vi

In this subVI data received is saved in the previously erased flash memory of the microcontroller. As a first step, the number of addresses to write is calculated. A for loop, repeated for every flash page, calculates the number of bytes in the current page, then creates an array from the load program memory commands, the addresses and the data: 0x4000-address-data for low byte, 0x4800-address-data for high byte (see the ATmega328PB datasheet).

It is also checked whether all the values on the page are equal to 0xFF. If yes, the page is not saved. If no, data is transferred through the SPI interface, then the write program memory page command (0x4C-page_address) is issued. When the page is saved, the next page follows. The loop is exited after all the pages are full, or all data is saved. The chip select line (connected to the RESET pin of the MCU) is pulled high, so the MCU exits the programming mode and starts to operate normally.

Note: The type of the parameter rgTX, in the function F Dwf Digital Spi Write, is changed from String to an Array of Unsigned 8-bit Integers.


Testing

After the devices are connected to the DIO lines, the microcontroller type and the project location are specified, the program can be started with the Run button on the Front Panel. The virtual LEDs on the Front Panel will light up in order, as the programming stages are finished.

After the MCU is programmed, the Digital Discovery continues to supply power for it, until the Stop button is pressed. In the case of this example, the blinking of the built in LED on the Arduino Pro Mini can be observed.

Note: If a 5V MCU and the Pmod LVLSHFT are used, after the program is uploaded, the VI has to be stopped, or the MCU must be disconnected from the Pmod LVLSHFT, to start normal operation.


References

Final Notes

For more guides and example projects for your Digital Discovery, please visit its Digital Discovery.

For more information about how to use the software featured in this demo, please visit its respective guide: Getting Started with LabVIEW and a Test and Measurement Device.

For technical support, please visit the Digilent Forums.