Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Programming Structures

LabVIEW has the equivalent of several programming structures:


• For Loop
• Do While
• Case Structure
• If/Then/Else
• Sequence Structure

These are analogous to the C++/ Fortran / Basic programming


structures.
If more complicated programming is required, one can incorporate
C++ code into a VI.
Updated 1/31/05 OPSE 310 - Week 2
Do While Loop
LabVIEW 7 automatically
includes a Boolean Control for
Condition Terminal
Loop Condition
Counter Terminal

Do
Execute Sub diagram
While condition is TRUE
•While Loop checks condition at the end of the loop.
•The loop executes at least once.
•The loop counter contains the number of times the loop has executed.
Updated 1/31/05 OPSE 310 - Week 2
For Loop
In LabVIEW 7, put a WHILE
Loop Counter N - Count Terminal loop on block diagram and then
Numerical Input
change to FOR loop
I - Iteration Terminal

Numerical
Output

For I=0 to N-1 Equivalent Code


Execute Code - NOTE - ZERO
Next I indexed
Updated 1/31/05 OPSE 310 - Week 2
Numeric Conversion
D o u b le P re c is io n
L o n g In te g e r

Grey Dot

When VI converts from floating point to integer, it rounds to


nearest integer. If 6.5, rounds to 6. 7.5 rounds to 8.0 - IEEE
Standard

Updated 1/31/05 OPSE 310 - Week 2


Autoindexing
You can use a DO LOOP to generate an array. This is called
autoindexing...

1 0 .0 0

Thicker line means data


type changed to ARRAY
Sample VI
Updated 1/31/05 OPSE 310 - Week 2
Programming Tip
• Updating indicators each time a loop iterates is time consuming
• When execution speed is critical, only update indicators after
loop is done.

LabVIEW does NOT have an optimizing compiler!!

Updated 1/31/05 OPSE 310 - Week 2


Shift Registers
Shift registers (WHILE LOOPS and FOR LOOPS) transfer
data from one loop iteration to another

These registers are added by the programmer


A typical application would be
• to display a newly acquired data point and the last one or two
data points as indicators.
• To average data points

Updated 1/31/05 OPSE 310 - Week 2


How Shift Registers Work
Before Loop Begins First Iteration

Initial
Value

Subsequent Iterations Last Iteration

New Last
Previous Value
Value Value

Updated 1/31/05 OPSE 310 - Week 2


Registers/ Feedback - Continued
• Always initialize the registers - otherwise initial value is
default value of data type the first time you run program
• Subsequent runs use values from previous runs.

Shift registers are required because it is difficult to wire


(graphical, data flow architecture) a VI such that a value
produced in one iteration is passed to next iteration
10
Alternative structure – Feedback Node
Initialization
1
Sample VI Numeric
Meter

FeedBack Example

Updated 1/31/05 OPSE 310 - Week 2


Case Structures
These structures are the equivalent of IF/ THEN/ ELSE
If condition=TRUE THEN F a ls e

Execute code
ELSE
Execute Code
And CASE SELECT structures
CASE SELECT var 0 , D e fa u lt

var=1, do code1
var=2, do code2 var

var=3, do code3
otherwise, do code

Updated 1/31/05 OPSE 310 - Week 2


Case Structures
• Change from IF/THEN/ELSE to CASE structure by wiring
either BOOLEAN or NUMERIC value to selection terminal
• You can add or remove CASES by popping up on CASE
structure
• You can specify RANGES for values for each case
eg. CASE 1: x 0 → ..0 CASE 2: 0  x1 → 0..1
CASE3: 2  x → 2.. CASE 4: x= 2, 3 x 4 → 2, 3..4
• While it is not REQUIRED, you MAY specify a Default
(Otherwise) condition.

Cases are very useful with ring buffers!


Updated 1/31/05 OPSE 310 - Week 2 Sample VI
Editing Case Select
• Use the labeling tool on tool palette.
• Specify a single value, or lists and ranges of values that select
the case. Examples

List : -1, 4, 6, 9
Range: 12..25
Range: ..0 (all numbers less than or equal to 0)
Combination: -1, 0..5, 8

Strings: “ON”

Updated 1/31/05 OPSE 310 - Week 2


Sequence Structure
Execute a Series of commands in sequence - Can
pass info from one step to the next

0 [0 ..1 ] 1 [0 ..1 ]

1 0 .0 0

Updated 1/31/05 OPSE 310 - Week 2


Sequence Structure
Use the sequence structure when you REQUIRE that items
process in a particular order...

Task 1

Task 1 Task 2 Task 3


Task 2

In data flow architecture, you


can’t determine in which order Task 3
tasks execute In sequence structure,
order is enforced...
Updated 1/31/05 OPSE 310 - Week 2
Passing Data into/from Structures
Essentially, you tunnel data into/ from structure
1

var

Remember - DATA FLOW - Data read as enter structure,


passed on as you exit
Sequence Example
Updated 1/31/05 OPSE 310 - Week 2
Formula Node (Arithmetic &
Comparison Pallete)
This is useful when an equation is complicated or has many
values. – Pop up on icon to reveal a “calculator type” interface

Inputs Output

Updated 1/31/05 OPSE 310 - Week 2


Other Simple Nodes
• Use a Formula Node (Functions>>All Functions>>Structures)
• Use an Expression node (Functions>>All
Functions>>Structures)

Example

Updated 1/31/05 OPSE 310 - Week 2


Even more complicated
formulas?
• Call a C subroutine
• Call Matlab scripts
• Call another application (eg. Excel) - Active X - more later
• Call HiQ - labview provided “Matlab-like” scripts

Updated 1/31/05 OPSE 310 - Week 2

You might also like