Understanding Analog and Digital IO


Understanding Analog and Digital I/O

The Measurement I/O>>DAQmxData Acquisition palette (see Figure 11.1) contains all of the VIs, and other tools that you will need to explore DAQ, or Data Acquisitionone of LabVIEW's great strengths. In fact, LabVIEW's DAQ capabilities might even be the reason why you chose to learn LabVIEWso that you could quickly acquire data and generate signals to measure, control, turn on and off, or blow up stuff in the external world!

Figure 11.1. DAQmxData Acquisition palette


To do this effectively, you first need to understand a little bit about the interface between LabVIEW and the DAQ devices, and some basic concepts about how analog and digital I/O are handled in software.

Before we get into some of this theory, though, let's first see how easy it is to do DAQ in LabVIEW using the DAQ Assistant.

Using the DAQ Assistant

Before we cover analog and digital I/O in depth, it's time to try acquiring data hands-on in LabVIEW first. You can jump right in using the DAQ Assistant. The DAQ Assistant is an Express VI that creates, edits, and runs tasks using NI-DAQmx.

You can find the DAQ Assistant (see Figure 11.2) on the Measurement I/O>>NI-DAQmxData Acquisition palette of the Functions palette.

Figure 11.2. DAQ Assistant express VI


The first time the configuration dialog of this express VI is opened (by placing it onto the block diagram, for example), you will be presented with a dialog that guides you through the process of selecting a signal measurement (input) or generation (output) type (shown in Figure 11.3).

Figure 11.3. DAQ Assistant configuration dialog when creating a new task


This dialog (see Figure 11.3) is used for creating new NI-DAQmx tasks, and it is accessible from a variety of locations in LabVIEW and MAX. You will learn all about NI-DAQmx tasks in the section, "NI-DAQmx Tasks."


From this dialog, you can choose from any type of signal measurement or generation supported by NI-DAQmx. After you have finished selecting the measurement type and channels, you will be presented with the DAQ Assistant property page for configuring the task.

Once the measurement type for a DAQ Assistant Express VI instance is selected, it cannot be changed. You can, however, add and remove channels of the same measurement type; but if you wish to change measurement type, you will have to start over with a new DAQ Assistant VI.


After pressing the OK button, you will see a message showing the progress while LabVIEW builds your DAQ Assistant VI. Once complete, you will see that there is now a "data" terminal for reading or writing the signal, as shown in Figure 11.5. This "data" terminal is a dynamic data type, which we learned about in Chapter 8, "LabVIEW's Exciting Visual Displays: Charts and Graphs." This can contain scalars and waveforms compatible with any I/O type that the DAQ Assistant supports.

Figure 11.5. DAQ Assistant express VI with "data" terminal visible


Now, let's take a look at some practical examples of using the DAQ Assistant to achieve some common tasks.

Figure 11.4. DAQ Assistant configuration dialog when configuring a task


Don't have a DAQ system yet? The following activity, and others in this chapter, can be completed (on a computer running Windows) even if you don't have actual DAQ hardware. You can use the NI-DAQmx simulated devices to set up a simulated device that will generate signals for you. In LabVIEW, the DAQ VIs will behave the same way (although the actual signal may look different, of course).

For help on setting up a simulated device, see the section, "NI-DAQmx Simulated Devices," in Chapter 10, "Signal Measurement and Generation: Data Acquisition."


Activity 11-1: Analog Input

1.

Connect a voltage source, such as a function generator or a battery, to channel 0 on your DAQ device. Be sure you know whether you've configured your device for differential or single-ended measurements. If you are using a simulated NI-DAQmx device (as described in Chapter 10), there will be a simulated sine wave signal on the analog input channels.

2.

Open a new VI, and place a DAQ Assistant VI (Measurement I/O>>NI-DAQmxData Acquisition palette) onto the block diagram.

3.

From the configuration dialog of your DAQ Assistant VI, choose an Analog Input>>Voltage measurement type (see Figure 11.6).

Figure 11.6. Configuring your task as a voltage analog input type from the DAQ Assistant configuration dialog


4.

Select the Physical Channel "ai0," and then press the Finish button (see Figure 11.7).

Figure 11.7. Selecting the physical channel for your measurement task


This will complete the process of selecting the measurement type, and you will be presented with the DAQ Assistant task configuration dialog.

5.

On the Task Timing tab, select an Acquisition Mode of 1 Sample (On Demand). This will configure your task to return a single measurement value each time we call the DAQ Assistant VI (see Figure 11.8).



Figure 11.8. Configuring the task timing acquisition mode as 1 Sample (On Demand)


6.

Press the OK button to close the DAQ Assistant task configuration dialog. LabVIEW will now build your DAQ Assistant VI.

7.

Build the front panel and block diagram shown in Figures 11.9 and 11.10.

Figure 11.9. Front panel of the VI you will create during this activity


Figure 11.10. Block diagram of the VI you will create during this activity


Convert from Dynamic Data

Convert from Dynamic Data (Express>>Signal Manipulation palette). This Express VI converts the dynamic data type to a numeric scalar. It has been configured (via its Express VI configuration dialog) to return a single scalar floating point number from channel 0.

8.

Save your VI as Quick Analog In.vi.

9.

Run the VI and then press stop after a few seconds.

10.

Examine the data in the Acquired Samples array.

The While Loop in the previous activity is the trade-off for simplicity: You add unnecessary software overhead. Not a problem for a quick look at your data, but in a decent-sized block diagram, you really don't want the software trying to do all the sampling controlthe DAQ devices and low-level drivers are designed to do that.

This method of DAQ works perfectly fine under many circumstances, provided:

  1. The sampling rate is slow (once per second or slower).

  2. Other time-consuming operating system (OS) events are not occurring while the VI is running.

  3. Slight variations in the sampling times are acceptable.

Suppose you are running a VI using a LabVIEW timing function in a loop, sampling once per second, and you decide to drag the window around. Your VI might not acquire all the data during the drag! Suppose you drag the window for five seconds. When you finally release the mouse button, the VI continues, but it gives no indication that some of the data was not sampled for a five-second period!

We'll talk about more robust methods for acquiring data, such as buffered analog input, in the following sections.

Analog I/O Terms and Definitions

We've already covered some theory of data acquisition and analog I/O in Chapter 10. You learned about sampling rates, scales, virtual channels, and input ranges. If these terms are not familiar to you, you might want to go back and review Chapter 10 again.

In this section, you will expand on what you learned about analog I/O. You will see how analog data is handled when going from your DAQ device to software, and several considerations for how to acquire data.

The concepts that you are about to learn, buffering and triggering, are also applicable to digital I/O. However, they are much more commonly used for analog I/O, so we will introduce them here.


Figure 11.11 shows the relationship between an analog physical signal and LabVIEW.

Figure 11.11. The path of a signal in a LabVIEW DAQ application


As you can see in Figure 11.11, initiating a DAQ operation involves LabVIEW calling NI-DAQmx (which is the catch-all driver for the device in use), which in turn signals the hardware to initiate the I/O operation. DAQ devices use on-board buffers (called FIFOs, for "First-In, First-Out") and RAM buffers as an intermediate place to store the data they acquire. Also note that the software isn't the only place an initiation of an I/O operation takes placean external piece of hardware can also trigger the operation.

Two important characteristics help classify the type of analog DAQ operation you are performing:

  • Whether you use a buffer.

  • Whether you use an external trigger to start, stop, or synchronize an operation.

Buffers

A buffer, as used in this context, is an area of memory in the PC (not the on-board FIFO) reserved for data to reside in temporarily before it goes somewhere else. For example, you may want to acquire a few thousand data samples in one second. It would be difficult to display or graph all that data in the same one second. But by telling your DAQ device to acquire the data into a memory buffer, you can quickly store the data there first, and then later retrieve it for display or analysis. Remember, buffers are related to the speed and volume of the DAQ operation (generally analog I/O). If your DAQ device has DMA capability, analog input operations have a faster hardware path to the RAM in your computer, meaning the data can be acquired directly into the computer's memory.

Not using a buffer means you must handle (graph, save to disk, analyze, whatever) each data point one at a time as it is acquired because there is no place to "keep" several points of data until you can handle them.

Use buffered I/O when

  • You need to acquire or generate many samples at a rate faster than is practical to display, store on a hard drive, or analyze in real time.

  • You need to acquire or generate AC data (>10 samples/sec) continuously and be able to do analysis or display of some of the data on the fly.

  • The sampling period must be precise and uniform throughout the data samples.

Use nonbuffered I/O when

  • The data set is small and short (for example, acquiring one data point from each of two channels every second).

  • You need to reduce memory overhead (the buffer takes up memory).

We'll talk more about buffering soon.

Triggering

Triggering refers to any method by which you initiate, terminate, or synchronize a DAQ event. A trigger is usually a digital or analog signal whose condition is analyzed to determine the course of action. In physical instruments such as oscilloscopes, for example, you can similarly use triggers to tell the oscilloscope to wait for a user-specified event such as voltage reaching a certain level or a digital "trigger" input changing state, before drawing a trace signal.

Software triggering is the easiest and most intuitive way to do ityou control the trigger directly from the software, such as using a Boolean front panel control to start/stop data acquisition.

Hardware triggering lets the circuitry in the DAQ device take care of the triggers, adding much more precision and control over the timing of your DAQ events. Hardware triggering can be further subdivided into external and internal triggering. An example of an internal trigger is to program the DAQ device to output a digital pulse when an analog-in channel reaches a certain voltage level. All National Instruments DAQ devices have an external trigger pin, which is a digital input used for triggering. Many instruments provide a digital output (often called "trigger out") used specifically to trigger some other device or instrument: in this case, the DAQ device.

Use software triggering when

  • The user needs to have explicit control over all DAQ operations and

  • The timing of the event (such as when an analog input operation begins) needn't be very precise.

Use hardware triggering when

  • Timing a DAQ event needs to be very precise,

  • You want to reduce software overhead (for example, a While Loop that watches for a certain occurrence can be eliminated), or

  • The DAQ events must be synchronized with an external device.

In the following sections, you will see how you can use the DAQ VIs to configure an I/O operation as buffered or nonbuffered and configure the type of triggering.

Activity 11-2: Buffered Analog Input

Remember buffered I/O, from the beginning of the chapter? Here is where you get to see it done. Previously, we used a While Loop to continuously acquire multiple points. Now we'll see how we can avoid using a While Loop by programming the hardware to acquire a set number of points at a certain sampling rate.

1.

Connect four DC or low-frequency voltage sources to channels 03. If you don't have this many voltage sources, you can connect one source to all channels or make a resistor network to vary the amplitude of the voltage at each channel. Again, if you are using a simulated NI-DAQmx device (as described in Chapter 10), there will be a simulated 10Hz sine wave signal on your simulated device's analog input channelsthese will all be slightly out of phase.

2.

Open a new VI, and place a DAQ Assistant VI (Measurement I/O>>NI-DAQmxData Acquisition palette) onto the block diagram.

3.

From the configuration dialog of your DAQ Assistant VI, choose an Analog Input>>Voltage measurement type (see Figure 11.12).

Figure 11.12. Configuring your task as a voltage analog input type from the DAQ Assistant configuration dialog


4.

Select the Physical Channels "ai0" through "ai3" (<Ctrl> or <Shift> click to select multiple channels), and then press the Finish button (see Figure 11.13).

Figure 11.13. Selecting multiple physical channels for your measurement task


This will complete the process of selecting the measurement type, and you will be presented with the DAQ Assistant task configuration dialog.

5.

On the Task Timing tab, select an Acquisition Mode of N Samples. This will configure your task to return multiple measurement values for each channel. On the same tab, select the Samples To Read and Rate (Hz). This will determine how many data points are acquired and the rate they are acquired, each time we call the DAQ Assistant VI (see Figure 11.14).

Figure 11.14. Configuring the task timing acquisition mode and clock settings


6.

Press the OK button to close the DAQ Assistant task configuration dialog. LabVIEW will now build your DAQ Assistant VI.

7.

Build the front panel and block diagram shown in Figures 11.15 and 11.16.

Figure 11.15. Front panel of the VI you will create during this activity


Figure 11.16. Buffered Analog In.vi using DAQ Assistant


8.

Save your VI as Buffered Analog In.vi.

9.

Run the VI once and look at the graphed data.

10.

Let's take a look at what the DAQ Assistant does "behind the scenes." Remember the DAQ Assistant is an Express VI. Like all Express VIs, you can convert the VI to traditional LabVIEW code. Right-click on the DAQ Assistant VI on your block diagram and select "Generate NI-DAQmx Code" from the pop-up menu.

11.

After a moment, you will see some LabVIEW code generated on your block diagram. The conversion to code will actually generate a new graph named "data," so delete the "Voltage Graph" you created earlier from the front panel. Then hit <ctrl>-B to delete any leftover broken wires. Your block diagram should look like Figure 11.17. (Annotations have been added, and labels shown, for illustration purposes.)



Figure 11.17. Buffered Analog Inconverted code from DAQ Assistant


12.

Notice that the DAQ Assistant has been replaced with some of the VIs from the DAQmx palette. What this code does is actually create and use an NI-DAQmx Task, employing the Configure, Start, Read, and Stop NI-DAQmx Task operations.

Digital I/O Terms and Definitions

Ah, the digital world, where everythingfor the most partis much simpler than its analog counterpart. 1 or 0, high or low, on or off; that's about it. Actually, some knowledge of binary representation and arithmetic is required, along with knowing the following LabVIEW definitions.

A digital line is the equivalent of an analog channel: a path where a single digital signal is set or retrieved. Digital lines are usually either input lines or output lines, but sometimes can be bi-directional. On most DAQ devices, digital lines must be configured as input or output; they can't act as both at the same time.

A port is a collection of digital lines that are configured in the same direction and can be used at the same time. The number of digital lines in each port depends on the DAQ device, but most ports consist of four or eight lines. For example, a multifunction device could have eight digital lines, configurable as one eight-line port, two four-line ports, or even eight one-line ports. Ports are specified as digital channels, just like analog channels. Figure 11.18 shows the conceptual relationships between digital ports and digital lines on a typical DAQ device.

Figure 11.18. Digital ports and lines on a typical DAQ device


Port width is the number of lines in a port.

State refers to the one of two possible cases for a digital line: a Boolean TRUE (same as logical 1 or "on"), or a Boolean FALSE (same as logical 0 or "off").

A pattern is a sequence of digital states, often expressed as a binary number, which describes the states of each of the lines on a port. For example, a four-line port might be set with the pattern "1101," meaning the first, third, and fourth lines are TRUE, and the second line is FALSE. The first bit, or least-significant bit (LSB) is the rightmost bit on the pattern. The last (fourth in this example), or most-significant bit (MSB) is the leftmost bit in the pattern. This pattern can also be converted from its binary equivalent to the decimal number 13.

Incidentally, National Instruments' DAQ devices use TTL positive logic, which means a logical low is somewhere in the 00.8 V range; a logical high is between 2.2 V and 5.5 V.

If a voltage signal ranging between 0.8 volts and 2 volts were to be sent into the input of a TTL gate, there would be no certain response from the gate. Such a signal is considered uncertain, because it is uncertain how the gate circuit would interpret such a signal.


Activity 11-3: Reading Digital Inputs

Now you will get to read some digital data! You will use the DAQ Assistant Express VI to read digital lines on your DAQ device.

1.

Connect four digital signals (0 or 5 Volts DC) to port 0, lines 03. If you don't have this many digital signals, you can connect one source to all channels. If you are using a simulated NI-DAQmx device (as described in Chapter 10), there will be a simulated digital signal on your simulated device's digital input channelsthe digital signal will be a binary counter that starts at zero (0) and increments each time you read the data.

2.

Open a new VI, and place a DAQ Assistant VI (Measurement I/O>>NI-DAQmxData Acquisition palette) onto the block diagram.

3.

From the configuration dialog of your DAQ Assistant VI, choose a Digital I/O>>Line Input measurement type (see Figure 11.19).

Figure 11.19. Configuring your task as a digital line input type from the DAQ Assistant configuration dialog


4.

Select your DAQ device and channels "port0/line0" through "port0/line3" (<Ctrl> or <Shift> click to select multiple channels), and then press the Finish button (see Figure 11.20).

Figure 11.20. Selecting the physicals channel for your measurement task


This will complete the process of selecting the measurement type and you will be presented with the DAQ Assistant task configuration dialog.

5.

On the Task Timing tab, select an Acquisition Mode of 1 Sample (On Demand). This will configure your task to return a single measurement value each time you call the DAQ Assistant VI (see Figure 11.21).

Figure 11.21. Configuring the task timing acquisition mode as 1 Sample (On Demand)


6.

Press the OK button to close the DAQ Assistant task configuration dialog. LabVIEW will now build your DAQ Assistant VI.

7.

Build the front panel and block diagram shown in Figures 11.22 and 11.23.



Figure 11.22. Front panel of the VI you will create during this activity


Figure 11.23. Block diagram of the VI you will create during this activity


Boolean Array To Number Function

The Boolean Array To Number function (found on the Programming>> Boolean palette) converts a Boolean array to a 32-bit unsigned integer by interpreting the array as the binary representation of an integer, with the first element of the array being the least significant bit.

8.

Save your VI as Read Digital Inputs.vi.

9.

Run the VI and watch the LEDs change state and the number change values.

Congratulationsyou're reading digital data! If you are feeling adventurous, try using the DAQ Assistant for performing other types of DAQ operations. For example, you could try doing buffered digital acquisition, or continuous digital acquisition. Try configuring a DAQ Assistant VI for writing a digital output and see if you can switch relays or turn your lights on and off. With the DAQ Assistant, it is so easy!

Now we will take a look under the hood and see how NI-DAQmx works, and then start using the DAQmx VIs in LabVIEW to configure our NI-DAQmx tasks programmatically.




LabVIEW for Everyone. Graphical Programming Made Easy and Fun
LabVIEW for Everyone: Graphical Programming Made Easy and Fun (3rd Edition)
ISBN: 0131856723
EAN: 2147483647
Year: 2006
Pages: 294

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net