Strukturen in LabVIEW
Strukturen in LabVIEW
Sarah Fenney
Arizona State University
Introduction
In this homework, you will write simple VIs to incorporate basic
programming structures in LabVIEW. The structures featured include
For Loops, While Loops, Case Structures, Sequence Structures, and
Formula Nodes.
Objectives
Theory
LabVIEW Programming Structures
Introduction
Structures are graphical representations of the loops and case
statements of text-based programming languages. Use structures on the
block diagram to repeat blocks of code and to execute code
conditionally or in a specific order.
Like other nodes, structures have terminals that connect them to other
block diagram nodes, execute automatically when input data are
available, and supply data to output wires when execution completes.
Each structure has a distinctive, resizable border to enclose the section
of the block diagram that executes according to the rules of the
structure. The section of the block diagram inside the structure border
is called a subdiagram. The terminals that feed data into and out of
structures are called tunnels. A tunnel is a connection point on a
structure border.
For Loops
While Loops
Use shift registers with For Loops and While Loops to transfer values
from one loop iteration to the next. Shift registers are similar to static
variables in text-based programming languages.
A shift register appears as a pair of terminals directly opposite each
other on the vertical sides of the loop border. The right terminal
contains an up arrow and stores data on the completion of an iteration.
LabVIEW transfers the data connected to the right side of the register
to the next iteration. Create a shift register by right-clicking the left or
right border of a loop and selecting Add Shift Register from the
shortcut menu.
A shift register transfers any data type and automatically changes to
the data type of the first object wired to the shift register. The data you
wire to the terminals of each shift register must be the same type. You
can create multiple shift registers on a structure, and you can have
more than one left terminal to retain more than one previous value.
After the loop executes, the last value stored in the shift register
remains at the right terminal. If you wire the right terminal outside the
loop, the wire transfers the last value stored in the shift register.
If you do not initialize the register, the loop uses the value written to
the register when the loop last executed or the default value for the
data type if the loop has never executed.
Use a loop with an uninitialized shift register to run a VI repeatedly so
that each time the VI runs, the initial output of the shift register is the
last value from the previous execution. Use an uninitialized shift
register to preserve state information between subsequent executions of
a VI.
Case Structures
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.
Sequence Structures
Formula Node
Homework Procedure
Part 1. Auto Match
Complete the following steps to build a VI that generates random
numbers until the number generated matches a number you specify.
The loop count terminal records the number of iterations before a
match occurs.
1. Open a new front panel.
2. Build the following front panel and modify the controls and
indicators as shown and described in this exercise.
8. Type 0 in the Digits of Precision text box and click the OK button.
9. Repeat steps 7 and 8 for the # of iterations indicator.
10. Build the following block diagram.
10
11
12
The X(i) indicator displays the current value, which shifts to the left
terminal at the beginning of the next iteration. The X(i-1) indicator
displays the value one iteration ago, the X(i-2) indicator displays the
value two iterations ago, and so on.
2. Build the following block diagram and make sure both the front
panel and block diagram are visible. If necessary, close or move the
Tools and Functions palettes.
The 0 wired to the left terminals initializes the elements of the shift
register to 0.
3. Save the VI as Shift Register.vi.
4. Click the Highlight Execution button to enable execution
highlighting.
13
5. Run the VI and watch the bubbles that move along the wires. If the
bubbles are moving too fast, click the Pause and Step Over buttons to
slow the execution.
In each iteration of the For Loop, the VI funnels the previous values
through the left terminals of the shift register. Each iteration of the
loop adds 5 to the current data, X(i). This value shifts to the left
terminal, X(i-1), at the beginning of the next iteration. The values at
the left terminal funnel downward through the terminals. This VI
retains the last three values. To retain more values, add more elements
to the left terminal of the shift register by right-clicking the left
terminal and selecting Add Element from the shortcut menu.
6. Close the VI.
14
15
16
17
18
Homework Report
For your homework report, email the VIs and the answers to the
questions below to the instructor no later than <date>.
Data Sheet
Questions
1. What are the differences between a For Loop and a While Loop?
5. When using a formula node, what happens if your input name is the
same as your output name?
19