Understanding Analog and Digital I/OThe 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.
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 taskThis 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 taskDon'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
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:
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 DefinitionsWe'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 applicationAs 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:
BuffersA 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
Use nonbuffered I/O when
We'll talk more about buffering soon. TriggeringTriggering 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
Use hardware triggering when
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 InputRemember 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.
Digital I/O Terms and DefinitionsAh, 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 InputsNow you will get to read some digital data! You will use the DAQ Assistant Express VI to read digital lines on your DAQ device.
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. |