Lab1 419322W2
Lab1 419322W2
419322
Hassaan Ahmed Waqas
Introduction
In this Lab, you will learn about the LabVIEW programming environment.
You will also write a simple Virtual Instrument (VI) to incorporate basic
operations and programming structures in LabVIEW. The structures
featured include For Loops, While Loops, Case Structures, Sequence
Structures, and Formula Nodes.
1
Objectives
Learn the three parts of a VI.
Theory
LabVIEW Programming Basics
Introduction
In LabVIEW, you build a user interface by using a set of tools and objects.
The user interface is known as the front panel. You then add code using
graphical representations of functions to control the front panel objects.
The block diagram contains this code. In some ways, the block diagram
resembles a flowchart.
2
Front Panel
The front panel is the user interface of the VI. You build the front panel
with controls and indicators, which are the interactive input and output
terminals of the VI, respectively. Controls are knobs, pushbuttons, dials,
and other input devices. Indicators are graphs, LEDs, and other displays.
Controls simulate instrument input devices and supply data to the block
diagram of the VI. Indicators simulate instrument output devices and
display data the block diagram acquires or generates.
3
Block Diagram
After you build the front panel, you add code using graphical
representations of functions to control the front panel objects. The block
diagram contains this graphical source code. Front panel objects appear as
terminals on the block diagram.
LabVIEW Palettes
LabVIEW palettes give you the options you need to create and edit the
front panel and block diagram.
The Tools palette is available on the front panel and the block diagram.
A tool is a special operating mode of the mouse cursor. When you select a
tool, the cursor icon changes to the tool icon. Use the tools to operate and
modify front panel and block diagram objects.
Select Window»Show Tools Palette to display the Tools palette. You can
place the Tools palette anywhere on the screen.
If automatic tool selection is enabled and you move the cursor over objects
on the front panel or block diagram, LabVIEW automatically selects the
corresponding tool from the Tools palette.
4
The Controls palette is available only on the front panel. The Controls
palette contains the controls and indicators you use to create the front
panel. Select Window»Show Controls Palette or right-click the front
panel workspace to display the Controls palette. You can place the
Controls palette anywhere on the screen.
Dataflow Programming
Equations
Converting ºC to ºF
Slope of a Line
The formula for the slope of a line is as follows:
Slope = (Y2 – Y1) / (X2 – X1)
where (X1, Y1) and (X2, Y2) are points on the line.
5
Procedure
Part 1. Converting ºC to ºF
3. Create a numeric digital control. You will use this control to enter the
value for degrees Centigrade.
b. Move the control to the front panel and click to place the
control.
c. Type deg C inside the label and click outside the label or click
the Enter button on the toolbar, shown at left. If you do not type
the name immediately, LabVIEW uses a default label. You can
edit a label at any time by using the Labeling tool, shown at left.
4. Create a numeric digital indicator. You will use this indicator to display
the value for degrees Fahrenheit.
b. Move the indicator to the front panel and click to place the
indicator.
c. Type deg F inside the label and click outside the label or click
the Enter button.
6
indicator. For example, a DBL terminal, shown at left, represents a
double-precision, floating-point numeric control or indicator.
If you moved the constants before you typed a value, use the Labeling tool
to enter the values.
9. Use the Wiring tool to wire the icons as shown in the block diagram.
• To wire from one terminal to another, use the Wiring tool to click
the first terminal, move the tool to the second terminal, and click
the second terminal, as shown in the following illustration. You
can start wiring at either terminal.
• You can bend a wire by clicking to tack the wire down and
moving the cursor in a perpendicular direction. Press the spacebar
to toggle the wire direction.
• When you move the Wiring tool over a terminal, the terminal
area blinks, indicating that clicking will connect the wire to that
terminal and a tip strip appears, listing the name of the terminal.
7
• To cancel a wire you started, press the <Esc> key, right-click, or
click the source terminal.
11. Save the VI because you will use this VI later in the course. Select
File»Save. Type Convert C to F.vi in the dialog box. Click the
Save button.
12. Enter a number in the digital control and run the VI.
Open a new VI and complete the front panel and block diagram as
follows:
Front Panel
8
Block Diagram before subVI
After creating the subVI, your block diagram will resemble the one below.
9
Part 3. Calculator VI
Write a VI that adds, subtracts, multiplies, divides, and averages two input
numbers and displays the results on the front panel. Save your VI as
Calculator.vi.
10
Case Structures
The case selector label at the top of the Case structure contains the name
of the selector value that corresponds to the case in the center and
decrement and increment arrows on each side. Click the decrement and
increment arrows to scroll through the available cases. You also can click
the down arrow next to the case name and select a case from the pull-
down menu.
Specify a default case for the Case structure to handle out-of-range values.
Otherwise, you must explicitly list every possible input value. For
example, if the selector is an integer and you specify cases for 1, 2, and 3,
you must specify a default case to execute if the input value is 4 or any
other valid integer value.
You can create multiple input and output tunnels for a Case structure.
Inputs are available to all cases, but cases do not need to use each input.
11
However, you must define each output tunnel for each case. When you
create an output tunnel in one case, tunnels appear at the same position on
the border in all the other cases. If at least one output tunnel is not wired,
all output tunnels on the structure appear as white squares. You can define
a different data source for the same output tunnel in each case, but the data
types must be compatible for each case. You also can right-click the
output tunnel and select Use Default If Unwired from the shortcut menu
to use the default value for the tunnel data type for all unwired tunnels.
Formula Node
The Formula Node is a convenient text-based node you can use to perform
mathematical operations on the block diagram. You do not have to access
any external code or applications, and you do not have to wire low-level
arithmetic functions to create equations. In addition to text-based equation
expressions, the Formula Node can accept text-based versions of if
statements, while loops, for loops, and do loops, which are familiar to C
programmers. These programming elements are similar to what you find
in C programming but are not identical.
Formula Nodes are useful for equations that have many variables or are
otherwise complicated and for using existing text-based code. You can
copy and paste the existing text-based code into a Formula Node rather
than recreating it graphically.
Formula Nodes use type checking to make sure that array indexes are
numeric data and that operands to the bit operations are integer data.
Formula Nodes also check to make sure array indexes are in range.
For arrays, an out-of-range value defaults to zero, and an out-of-range
assignment defaults to nop to indicate no operation occurs.
12
When you work with variables, remember the following points:
• No two inputs and no two outputs can have the same name, but an
output can have the same name as an input.
• You can declare and use a variable inside the Formula Node without
relating it to an input or output wire.
13
Square Root VI
14
e. Place the One Button Dialog function located on the
Functions»Time & Dialog palette. This function displays a dialog
box that will contain the message Error...Negative
Number.
g. Select the TRUE case and place the Square Root function
located on the Functions»Numeric palette, as shown in the
following block diagram. This function returns the square root of
Number.
If Number is positive, the VI executes the TRUE case and returns the
square root of Number. If Number is negative, the VI executes the
FALSE case, returns –99999.0, and displays a dialog box with the
message Error...Negative Number.
15
Screenshots:
### F TO C CONVERSION SCREENSHOTS START HERE ###
16
### CALCULAROT VI SCREENSHOTS END HERE ###
Questions
1. What is a VI? What are the three main parts of a VI? Briefly describe
each.
A VI is a Virtual Instrument; a computer program that ‘emulates’ a
physical instrument. The three components are:
i) A Front Panel, in which components are placed
ii) A Block Diagram, where functions are programmed
iii) Icon and Connector panel
17
ii) Controls allow you to select hardware components to place in the
front panel
iii) Functions allow you to program in the block diagram
4. How can you tell the difference between controls and indicators on the
front panel and on the block diagram?
On the front panel, controls usually have an interactable interface where
you can directly manipulate the variable, e.g a knob you can turn, or text
you can type. Indicators do not.
On the block diagram, controls have output wires on their right side,
while indicators have input wires on their left side.
References
Getting Started with LabVIEW. November 2001. Part Number
321527E-01. http://www.ni.com/manuals.
18
LabVIEW Introduction Course - Six Hours.
You can watch videos from the website for additional help:
http://www.ni.com/academic/students/learn-labview/
19