Vous êtes sur la page 1sur 21

Lecture II:

Basic Programming Techniques


of LabVIEW
ECE 439 Control System Lab

EE439 Fall 2005 1


Outline
 Structure
 Loop
 Case
 Sequence, etc.

 Array & Cluster

 Charts & Graphs

 File I/O

EE439 Fall 2005 2


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.
You could select a structure from Structures of Functions palette.

EE439 Fall 2005 3


The For Loop
Loop Counter N - Count Terminal
Numerical Input
I - Iteration Terminal

Numerical
Output

For I=0 to N-1


Equivalent Code
Execute Code
Next I - NOTE - ZERO indexed

EE439 Fall 2005 4


Numeric Conversion

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

EE439 Fall 2005 5


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

Thicker line means data


type changed to ARRAY

EE439 Fall 2005 6


The While Loop

Loop
Condition
Counter
Terminal

Do
Execute subdiagram
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.

EE439 Fall 2005 7


Programming Tip
• Updating indicators each time a loop iterates is time consuming.
See Figure 1 below.
• When execution speed is critical, only update indicators after
loop is done. See Figure 2 below.

EE439 Fall 2005 8


Class Exercise: Loop

 Generate 10 numeric numbers between 0 to


100 by using Random Number (0-1).vi in
Function->Numeric.
 Show the numbers generated above on a
Waveform Chart from Graph subpalette of
Controls palette by Autoindexing.

EE439 Fall 2005 9


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

EE439 Fall 2005 10


How Shift Registers Work
Before Loop Begins First Iteration

Initial
Value

Subsequent Iterations Last Iteration

New Last
Previous Value
Value Value

EE439 Fall 2005 11


Registers - 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

EE439 Fall 2005 12


Class Exercise (continued): Shift Register

 Following the question before, calculate the


average value x of the 10 numbers by Shift
Register
 Show the result x

EE439 Fall 2005 13


Case Structures
These structures are the equivalent of IF/ THEN/ ELSE
If condition=TRUE THEN
Execute code
ELSE
Execute Code
And CASE SELECT structures
CASE SELECT var
var=0, do code1
var=1, do code2
var=2, do code3
otherwise, do code

EE439 Fall 2005 14


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: x0 CASE 2: 0<x1 CASE3: 2  x

• You MUST specify a Default (Otherwise) condition.

Cases are very useful with ring buffers!

EE439 Fall 2005 15


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

EE439 Fall 2005 16


Class Exercise (continued): Case

 Following the question before, if x is larger


than 50, calculate the square root of x.
Otherwise, calculate the square root of
(x+50).
 Show the result y

EE439 Fall 2005 17


Sequence Structure
Execute a Series of commands in sequence

To pass data from one step to the next, you must use Sequence
Local by choosing Add Sequence Local from the Structure
border pop-up menu.

EE439 Fall 2005 18


Passing Data into/from Structures
Essentially, you tunnel data into/ from structure

Remember - DATA FLOW - Data read as enter structure,


passed on as you exit

EE439 Fall 2005 19


Formula Node (Structure)
This is useful when an equation is complicated or has many
values (enter using Labeling Tool on Tool pallette).

Inputs
Inputs

Output
Output

EE439 Fall 2005 20


Class Exercise (continued):
Sequence & Formula Node
 Use Sequence structure,
Frame 0: get the initial time t0 by Get Date/Time in
Seconds function in Time & Dialog palette;
Frame 1: all the program we made before;
Frame 2: get the current time t;
Frame 3: let the z=y^3+y*sin(t-t0)-10 by Formula
Node
 Show the result z

EE439 Fall 2005 21

Vous aimerez peut-être aussi