Entering and Running LabVIEW Programs

advertisement
NAME ______________________________
EET 2259 Lab 1
Entering and Running LabVIEW Programs
OBJECTIVES
- Build the front panel and block diagram of some LabVIEW programs.
- Run these programs and verify that they work correctly.
-Use a data-acquisition device to read an input voltage or produce an output voltage.
Part 1. Introduction to Programming in LabVIEW
In this week’s class you’ve already entered and run a program that multiplied two numbers
together. The programs you’ll enter for this lab are a little more complicated than that one.
Recall the following key points:

Programs in LabVIEW are called VI’s, which is short for virtual instrument. The terms
virtual instrument (or VI) and LabVIEW program mean exactly the same thing.

Every VI has two main parts: the front panel and the block diagram.
o The front panel is the part that the user will interact with when he runs the VI.
The front panel contains two types of objects: controls and indicators. Controls
let the user enter information, and indicators display information to the user.
o The block diagram contains the VI’s logic, telling the program which
calculations or operations to perform on the information entered by the user.

To build a VI’s front panel, you use your mouse to place objects from the Controls
Palette on the front panel.

To build a VI’s block diagram, you use your mouse to place objects from the Functions
Palette on the block diagram, and then you use your mouse to create “wires” that connect
these objects to each other.
Now let’s put these concepts to work by entering and running a few programs.
EET 2259 Lab 1 – Page 1
Revised 12/29/2015
Part 2. Finding and Replacing a String
First you’ll enter and run a VI that is very easy to build and that does something similar to the
search-and-replace feature that you’ll find in any word processor. This VI will let the user type in
some text, and then it will search that text for a particular word or string of the user’s choice. If
the word or string is found, the VI will replace it with another word or string of the user’s choice.
Follow these steps to get started:
1. On your computer, start LabVIEW and create a new blank VI.
2. In your blank VI, click anywhere on the front panel.
3. If the Controls Palette is not already visible, make it visible by clicking View >
Controls Palette on the menu bar.
Now you’re ready to build your VI’s front panel. Here’s how your completed front panel will
look:
To build this front panel, follow these steps:
1. In the Controls Palette, under the Modern category, click the String & Path icon. The
palette will now show five kinds of text controls that you can place on the front panel.
The first of these is the String Control, and the second is the String Indicator.
2. Place three string controls on the front panel. Give these controls the following labels:
 Original text
 String to search for
 String to replace it with
3. Place one string indicator on the front panel, and give it the label Modified text.
4. Using your mouse, resize the objects labeled Original text and Modified text so that
they’re tall enough to hold several lines of text.
5. Move your mouse over the string control called Original text, and click the mouse’s
right button (not its left button). This will open a shortcut menu containing about a
dozen lines, the first of which is Visible Items.
6. Roll over Visible Items, and then select VerticalScrollbar.
7. Repeat the two previous steps for the string indicator called Modified text. Your two
large string objects should now have scrollbars.
8. Arrange the four objects so that your front panel looks like the picture shown earlier.
EET 2259 Lab 1 – Page 2
Revised 12/29/2015
Okay, you’re done with the front panel. Now let’s turn to the block diagram. You should find
that your block diagram already has four icons, which represent the controls and indicator on your
front panel. Your completed block diagram will look something like this:
To build this block diagram, follow these steps:
1. Click anywhere on your VI’s block diagram.
2. If the Functions Palette is not already visible, make it visible by clicking
View>Functions Palette on the menu bar.
3. In the Functions Palette, under the Programming category, click the String icon. This
will open a palette of about thirty icons. Most of these are string functions that you can
use in your block diagrams.
4. Place the Search and Replace String function on the block diagram.
5. In the Functions Palette, click the word Programming to go up to the top-level palette.
Then click the Boolean icon. This will open a palette of about twenty icons, one of
which is labeled True Constant.
6. Place a True Constant on your block diagram.
7. Arrange the objects so that your block diagram looks like the picture shown above.
8. Use the mouse to create wires connecting the objects together as shown in the picture.
Note that the Search and Replace String function has five connection points on its left
side. You will only connect wires to the first three of these, and leave the fourth and fifth
ones unconnected. Also, the function has four connection points on its right side. You
will connect a wire to the first of these, and leave the other ones unconnected. The
function has two connection points on its top side. You should connect the first of these
to the True Constant, and leave the second one unconnected.
Now you’re ready to test the program!
1. Type a few sentences in the string control labeled Original text.
2. In the string control labeled String to search for, type a word that appears once or more
in your sentences.
3. In the string control labeled String to replace it with, type another word.
4. Run the program, and it should display the modified text with the replacement that you
asked for.
5. Save this VI as Lab1Strings.vi. Then show me your working program and ask me to
sign my initials below.
________
EET 2259 Lab 1 – Page 3
Revised 12/29/2015
Part 3. Generating and Plotting Random Values
Our next program will illustrate a typical (but very simple) use of LabVIEW. Suppose you have
set up a temperature sensor to measure the temperature outside your home. You could hook this
sensor up to your computer and then tell LabVIEW to take temperature readings at some fixed
time interval, such as once every day at noon. Then at the end of each month you could have
LabVIEW make a graph of its temperature readings and provide you with statistics about the
temperature values, such as the maximum reading, the minimum reading, the average of all the
readings, and so on.
Instead of using an actual temperature sensor, we’ll simulate this process by using LabVIEW’s
random number generator. Our program will generate thirty random numbers between 60 and
100, which you can think of as representing temperature values outside your home at noon on
thirty consecutive days. Then the program will plot these values, and will display the minimum
and maximum values.
Your completed front panel will look something like this after you run the VI:
To build this front panel, follow these steps:
1. Create a new blank VI.
2. In your blank VI, click anywhere on the front panel.
3. In the Controls Palette, under the Modern category, click the Graph icon. This will
display a palette of about twelve icons. The second icon is a Waveform Graph.
4. Place a waveform graph on the front panel.
5. Notice that the graph is labeled Waveform Graph. Double-click this label and change it
to Temperature Graph.
6. Notice that the graph’s vertical axis is labeled Amplitude. Double-click this label and
change it to Degrees Fahrenheit.
7. Notice that the graph’s horizontal axis is labeled Time. Double-click this label and
change it to Day.
8. In the Controls Palette, click the word Modern to go up to the top-level palette. Then
click the Numeric icon, and place two Numeric Indicators on the front panel. Label one
of these numeric indicators Maximum and label the other one Minimum.
EET 2259 Lab 1 – Page 4
Revised 12/29/2015
You’re finished with the front panel. Now let’s turn to the block diagram. You should find that
your block diagram already has three icons representing the indicators on the front panel. Your
completed block diagram will look similar to the picture shown below.
To build this block diagram, follow these steps:
1. Click anywhere on your VI’s block diagram.
2. In the Functions Palette, under the Programming category, click the Structures icon.
3. Place a For Loop on your block diagram. Then use your mouse to resize it until it’s
approximately as big as shown in the picture above.
4. In the Functions Palette, click the word Programming to go up to the top-level palette.
Then click the Numeric icon. Place the following items inside the For Loop on your
block diagram:
 One Add
 One Multiply
 One Random Number
 Two Numeric Constants
5. Double-click one of the numeric constants and change its value to 40.
6. Double-click the other numeric constant and change its value to 60.
7. Also place a Numeric Constant outside the For Loop. Double-click this numeric
constant and change its value to 30.
8. In the Functions Palette, click the word Programming to go up to the top-level palette.
Then click the Array icon. Place an Array Max & Min on your block diagram outside
the For Loop.
9. Arrange the objects so that your block diagram looks like the picture shown above.
10. Use the mouse to create wires connecting the objects together as shown in the picture.
Note that the Array Max & Min function has four connection points on its right side.
You will only connect wires to the first and the third of these.
Now you’re ready to test the program!
1. Run the program several times. Each time, it should display a graph of thirty random
temperature values between 60 and 100, and it should also print the maximum and
minimum values.
2. Now modify the program so that instead of generating thirty random values between 60
and 100, it generates twenty random values between 50 and 120. (Hint: to do this, you’ll
need to change some or all of the constants on the program’s block diagram.)
3. Save this VI as Lab1Temperature.vi, and show me your working program.
________
EET 2259 Lab 1 – Page 5
Revised 12/29/2015
Part 4. Measuring a Voltage (Analog Input)
One of LabVIEW’s great strengths is that it lets you use
the computer to measure real-world voltages and
produce output voltages. For LabVIEW to be able to do
these things, you have two options: either your computer
must have a data-acquisition card (“DAQ card”)
installed, or you must connect your computer to an
external data-acquisition device. Each computer in
Sinclair’s room 1-230 has a DAQ card installed.
DAQ card (inside the computer)
Sinclair’s EET department also owns external DAQ
devices called myDAQs. These connect to a computer
through a USB cable. In this course we’ll use both the
myDAQ and the DAQ card inside the computer. For
this first lab we’ll use the myDAQ.
National Instruments myDAQ
The myDAQ has several types of connectors through which signals can enter or leave:
 Three banana plug connecters, like the ones you’d find on a multimeter.
 Audio In and Audio Out jacks, like the ones you’d find on an MP3 player.
 Twenty screw terminals, to which you can connect wires.
Let’s use the myDAQ to make a voltage measurement. Follow these steps:
1. Connect one end of the USB cable to the myDAQ, and the other end to your computer.
The myDAQ’s blue LED should light up to indicate that the device has power. Windows
may also pop up a “Found new hardware” bubble while it identifies the myDAQ device.
Also, Windows may automatically start a program called NI ELVISmx Instrument
Launcher, which you’ll learn about later in this course. For now, we won’t use this
program, so you can close this window.
2. We’ll use LabVIEW to measure a voltage produced by the DC power supply on the red
digital/analog trainer that you’ve used in many of your electronics courses. To do this:
 Run a black wire from the trainer’s GND terminal to the myDAQ’s analoginput (AI) screw terminal labeled 0-.
 Run a red wire from the trainer’s 0~+15V terminal to the myDAQ’s analoginput screw terminal labeled 0+.
In making these connections, you have attached the trainer’s power supply to the
myDAQ’s channel 0 analog input.
3. Run another black wire from the myDAQ’s0- terminal to its AGND terminal. In making
this connection, you have grounded the trainer’s power supply. You’ll learn later in the
course why this connection is necessary.
EET 2259 Lab 1 – Page 6
Revised 12/29/2015
4. Turn the trainer’s POWER switch on. Use a multimeter to measure the voltage being
produced by the power supply. Using the power supply’s knob, set this voltage to 3.0 V.
5. In LabVIEW, start a new blank VI, and click anywhere on the block diagram. On the
Functions palette, go into the Measurement I/O palette, then into the NI-DAQmx
palette, and place a DAQ Assistant on the block diagram. After a delay while the DAQ
Assistant initializes, you’ll see a dialog box that lets you choose the type of measurement
you want to make.
6. Select Acquire Signals, and then select Analog Input, and then select Voltage.
LabVIEW then gives you a list of all the data acquisition devices connected to your
computer. You’ll probably see two devices: PCI-6221 and NI myDAQ. We’re using the
NI myDAQ, so click the + sign to the left of NI myDAQ.
7. LabVIEW then gives you a list of the myDAQ’s five analog inputs, labeled ai0, ai1,
audioInputLeft, audioInputRight, and dmm.
8. Since you attached your wire to the channel 0 input, select ai0(which is short for “analog
input #0”), and then click the Finish button.
9. Next you’ll see a dialog box that lets you set some parameters before you make your
measurement. Make sure that Terminal Configuration is set to Differential, and make
sure that Acquisition Mode is set to 1 Sample (On Demand).
10. Click the OK button near the bottom right corner of the DAQ Assistant dialog box.
11. On the Functions palette, go into the Express palette, then into the Signal Manipulation
palette, and place a From DDT on the block diagram.
12. A dialog box will open.
 In the box labeled Resulting data type, select Single scalar.
 Make sure that Floating point numbers (double) is selected for the Scalar data type.
 Make sure that Channel is set to 0.
 Then click OK.
13. On the block diagram, run a wire between the DAQ Assistant’s data output and the From
DDT’s input.
14. Click anywhere on your VI’s front panel.
15. In the Controls Palette, under the Modern category, click the Numeric icon, and place a
Numeric Indicator on the front panel. Label it Input Voltage.
16. On the block diagram, connect the numeric indicator to the output of the From DDT.
Your completed block diagram should look like this:
Now you’re ready to test the program!
1. Run the VI several times, each time changing the voltage of the trainer’s power supply.
You should see that the value displayed in the numeric indicator is very close to the value
that you set on the trainer’s power supply.
2. Save this VI as Lab1VoltageInput.vi, and show me your working program.
________
3. Turn off the trainer’s power, and remove the wires that you connected earlier.
EET 2259 Lab 1 – Page 7
Revised 12/29/2015
Part 5. Producing a Voltage (Analog Output)
You just learned how to use LabVIEW to measure an input voltage: this
operation is called analog input. Now let’s learn about analog output, which
means using LabVIEW to produce an output voltage.
1. Start a new blank VI, and place a knob (from the Modern > Numeric category of the
Controls Palette) on the front panel. Change this knob’s label to Output Voltage.
2. Place a DAQ Assistant on the block diagram.
3. When the DAQ Assistant’s dialog box opens, select Generate Signals, then select
Analog Output, and then select Voltage. Then select ao0 (which is short for “analog
output #0”) and click the Finish button.
4. Set the Generation Mode to 1 Sample (On Demand). Then click the OK button.
5. On your VI’s block diagram, your DAQ Assistant will have an input labeled data. On
the Functions palette, go into the Express palette, then into the Signal Manipulation
palette, and place a To DDT on the block diagram.
6. A dialog box will open to let you choose the type of the data that you’re converting from.
 In the box labeled Input data type, select Single scalar.
 Make sure that Floating point numbers (double) is selected for the Scalar data type.
 For the Start Time, make sure that Now is selected.
 Then click OK.
7. Run a wire between the To DDT’s output and the DAQ Assistant’s data input.
8. Run a wire between the knob’s output and the To DDT’s input.
9. On the front panel, set the knob to approximately 4, and then run the VI. Use your
multimeter to measure the voltage being produced at the myDAQ’s analog output #0. To
do this:
 Touch the meter’s red lead to the myDAQ’s analog-output (AO) screw terminal
labeled 0.
 Touch the meter’s black lead to the myDAQ’s analog-ground (AGND) screw
terminal.
Your measured value should be very close to the value that you set on your front panel’s
knob. Save this VI as Lab1VoltageOutput.vi, and show me your working program.
________
10. When you’re finished with this program (or any other programs that produces an analog
output voltage), run the program once more with the knob set to 0. That way, there won’t
be a live voltage left on the myDAQ’s terminals.
EET 2259 Lab 1 – Page 8
Revised 12/29/2015
Part 6. Simulating Lissajous Patterns
Using an oscilloscope and two function generators, you can produce interesting moving patterns
called Lissajous patterns. You may have seen these patterns before, either in real life or in a
movie. (Just about every science-fiction movie from the1950’s had a scene in a science
laboratory equipped with oscilloscopes displaying Lissajous patterns.) For our next program
we’ll write a VI that generates these same patterns on the computer screen.
Your completed front panel will look something like this after you run the VI:
To build this front panel, follow these steps:
1. Create a new blank VI.
2. In your blank VI, click anywhere on the front panel.
3. From the top-level palette in the Controls Palette, click the Graph icon, and then place an
ExpressXY Graph on the front panel.
4. In the Controls Palette, click the word Modern to go up to the top-level palette. Then
click the Boolean icon, and place a Stop Button on the front panel. Delete its label.
5. In the Controls Palette, click the word Modern to go up to the top-level palette. Then
click the Numeric icon, and place two Horizontal Pointer Slides on the front panel.
Label one of these X and label the other one Drift.
6. Place a Vertical Pointer Slide on the front panel and label it Y.
7. Point at the horizontal pointer slide labeled X and then right-click to open its shortcut
menu.
8. In the shortcut menu, roll over the item called Representation, and then select I32 from
the menu that opens.
9. Repeat the two previous steps for the vertical slide pointer labeled Y.
10. Arrange the controls so that your front panel looks like the picture shown above.
You’re finished with the front panel. Now let’s turn to the block diagram. You should find that
your block diagram already has several icons representing the objects on the front panel. Your
completed block diagram will look similar to the picture shown below.
EET 2259 Lab 1 – Page 9
Revised 12/29/2015
To build this block diagram, follow these steps:
1. Click anywhere on your VI’s block diagram.
2. In the top-level palette of the Functions Palette, click the Structures icon.
3. Place a While Loop on your block diagram. Then use your mouse to resize it until it’s
approximately as big as shown in the picture above. (But don’t move it over the other
icons that are already on your block diagram.)
4. Drag all of the other icons on your block diagram into the While Loop.
5. On the Functions Palette, click the word Programming to go up to the top-level palette,
and then click the Timing icon. Then place a Time Delay inside your While Loop.
When you place it on the block diagram, a dialog box will open to let you customize the
time delay. Change the value in the box from 1.000 to 0.010, and click the OK button.
6. In the Functions Palette, click the word Programming to go up to the top-level palette.
Then click the Numeric icon. Place an Add, a Divide, and a Numeric Constant inside
your While Loop.
7. Double-click the numeric constant and change its value to 200.
8. In the Functions Palette, go back up to the top-level palette, and then click the Waveform
icon. Then click the AnalogWaveform icon, and then click the Waveform Generation
icon. Place a Simulate Signal inside the loop on your block diagram. When you place it
on the block diagram, a dialog box will open to let you customize it. Leave most of the
original values alone, but make the following changes:
 Remove the checkmark in the checkbox labeled Automatic.
EET 2259 Lab 1 – Page 10
Revised 12/29/2015
9.
10.
11.
12.
 Inside the box labeled Number of samples, change the number to 1010.
 Click the OK button to close the dialog box.
Place another Simulate Signal inside the loop on your block diagram. When its dialog
box opens, make the same changes that you made for the other one.
Notice that each Simulate Signal icon has downward-pointing arrows at the bottom of the
icon. Place your mouse over these arrows, and then click and drag downward to expand
these two icons to their maximum size.
Arrange the objects so that your block diagram looks like the picture shown earlier.
Use the mouse to create wires connecting the objects together as shown in the picture.
The inputs and outputs on your Simulate Signals might be in a different order from the
way they’re shown in my picture. Be sure to run your wires to the inputs called
Frequency and the outputs called Sine.
You’re ready to test the program!
1. Run the program, and adjust the slides on the front panel to display various patterns.
2. Save this VI as Lab1Lissajous.vi, and show me your working program.
________
Part 7. Combining Analog Input and Output
Next, let’s write a program that combines analog input with analog output.
You’ll need to recall everything that you did earlier in the two programs that
used the myDAQ.
1. Create a new VI whose front panel has a numeric indicator labeled Input Voltage. Wire
the block diagram and make physical connections to the myDAQ so that when the VI
runs, it:
 Reads an external voltage (from the trainer) using the myDAQ’s analog input #0.
 Displays that value on the front panel’s indicator.
 Divides that value by 2 and outputs the resulting voltage value to the myDAQ’s
analog output #0.
(Hint: Your program will need two copies of the DAQ Assistant: one to read the input
voltage and one to produce the output voltage. Also remember to use To DDT and From
DDT as you did earlier.) Save this VI as Lab1VoltageInputOutput.vi, and show me
your working program.
________
EET 2259 Lab 1 – Page 11
Revised 12/29/2015
Part 8. Displaying a Triangle Wave
For this last program I’ll just show you what the front panel and block diagram should look like.
Your completed front panel should look similar to this after you run the VI:
And your block diagram should look similar to this:
Save this VI as Lab1Triangle.vi, and show me your working program.
EET 2259 Lab 1 – Page 12
________
Revised 12/29/2015
Part 9. Review Questions (Fill in the blanks.)
1. What does the abbreviation DAQ stand for? ____________________________________
2. When you want to input an analog voltage, you must configure the DAQ Assistant to tell
LabVIEW which input channel you’re using. When you’re configuring the DAQ
Assistant, what is LabVIEW’s abbreviation for analog input channel #0? _________
3. When you use a DAQ Assistant to input an analog voltage, you should set the DAQ
Assistant’s terminal configuration to __________________________.
4. When you use a DAQ Assistant to input an analog voltage, you should set the DAQ
Assistant’s acquisition mode to _______________________________.
5. By itself, LabVIEW’s Random Number function generates random numbers between 0
and 1. But by using a little math, you can generate random numbers between any desired
minimum and maximum. Suppose you want to generate random numbers between 10
and 100. To do this, multiply LabVIEW’s Random Number function times _______ and
then add ________.
6. The objects you place on a VI’s front panel are either controls (which let the user enter
information) or indicators (which display information back to the user). Listed below are
the front-panel objects from this lab’s programs. In the blank next to each object, write a
“C” if the object is a control or an “I” if the object is an indicator.
Lab1Strings.vi: Original text_____ String to search for_____
String to replace it with____ Modified text____
Lab1VoltageInput.vi: Input Voltage_____
Lab1VoltageOutput.vi: Output Voltage_____
Lab1Temperature.vi: Temperature graph_____ Maximum_____ Minimum_____
Lab1VoltageInputOutput.vi: Input Voltage_____
Lab1Lissajous.vi: Y_____ XY graph_____ Stop_____ X_____ Drift_____
Lab1Triangle.vi: Amplitude_____ Frequency_____ Offset_____
Waveform Graph_____
***** This lab had 7 named programs for me to check. If you didn’t finish all of these
during class, finish them after class. Then upload all 7 programs (including the ones that I
checked in class) to the website by the due date. Also turn in your lab sheets at the
beginning of class.******
EET 2259 Lab 1 – Page 13
Revised 12/29/2015
Download