Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2. Lab...

28
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2. Lab #5 and Homework #5 due next week. Exam #1 next week.

Transcript of Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2. Lab...

Page 1: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

EET 2259 Unit 5Loops

Read Bishop, Sections 5.1 and 5.2.

Lab #5 and Homework #5 due next week.

Exam #1 next week.

Page 2: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Structures

Structures control the flow of a program’s execution.

This week we look at two kinds: For Loops While Loops

Later we’ll look at other kinds: Sequence Structures Case Structures …

(Bishop, p. 213)

Page 3: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

For Loop

A For Loop executes the code inside its borders a specified number of times.

The code inside the For Loop’s borders is called a subdiagram.

A For Loop has two terminals: the count terminal and the iteration terminal.

(Bishop, p. 214)

Page 4: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

For Loop Example

Page 5: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Placing a For Loop

For Loops are found on the Functions>> Programming>> Structures palette.

Click it, and then drag to create a loop on the block diagram.

Then place items inside the loop to build your subdiagram.

(Bishop, p. 214)

Page 6: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Be Careful to Place Items Inside the Loop

If you’re not careful, you can end up with items “hovering” over a loop instead of being located inside the loop.

Correctly located inside the loop.

Not inside the loop, but hovering over it.

Page 7: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Count Terminal

A For Loop’s Count Terminal, labeled N, lets you set how many times the loop will execute.

You can set the count to a constant, or to a value set by the user through a control, or to the output of a function, etc.

The count is available to be used inside the loop.

(Bishop, p. 214)

Page 8: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Iteration Terminal

A For Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed.

The iteration number is available to be used inside the loop.

It starts at 0 and increases to N-1.

(Bishop, p. 214)

Page 9: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Inserting a Time Delay

Loops usually run so quickly that the user can’t see what’s happening.

To add a time delay, use either the old-fashioned Wait (ms) function or the newer Time Delay Express VI.

Both are found on the Functions>> Programming >>Timing palette.

Place either one anywhere inside the loop.

Page 10: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Good Practice: Include a Time Delay in All Loops

What if you don’t want to slow down the loop to “human speed,” but want it to run as fast as possible?

It’s still a good programming practice to include a small (10 ms) delay in your loop. This prevents LabVIEW from consuming all of your processor’s time, and lets the operating system perform necessary tasks.

Page 11: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Tunnels

If a wire crosses the border of a loop (or other structure), a tunnel automatically appears on the border.

Doing this can be useful, but can also lead to confusion unless you keep in mind the following point….

Page 12: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Tunnels and Data Flow

No data passes into or out of a structure while the structure is executing.

Input data is read before the structure executes; subsequent changes to the input values are ignored.

Output data is not available until after the structure finishes executing.

Page 13: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Example For Loop in BASIC

The type of loop we’ve been discussing is called a “For Loop” because in text-based programming languages (such as BASIC or C++) it is coded using the word FOR.

Example:

CLS FOR i = 1 TO 15 PRINT i, 2 * i NEXT i

Page 14: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Integer Representations

Recall that blue terminals and blue wires represent integers.

Integer terminals can be further categorized into byte signed integer (I8), word signed integer (I16), long signed integer (I32), etc.

This is called the “representation” of the number, and you can change it by right-clicking on a terminal and choosing “Representation.”

Page 15: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Integer Representations (Continued) These representations differ in the range of

values they can handle and the amount of memory they use.Representation Max. Value Memory Bytes

Byte signed integer (I8) 127 1

Word signed integer (I16) 32,767 2

Long signed integer (I32) 2,147,483,647 4

Quad signed integer (I64) 11019 8

Byte unsigned integer (U8) 255 1

Word unsigned integer (U16) 65,535 2

Long unsigned integer (U32) 4,294,967,295 4

Quad unsigned integer (U64) 2 1019 8

Page 16: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Floating-Point Representations

Recall that orange terminals and orange wires represent floating–point numbers.

Floating-point terminals can be further categorized into single precision, double precision, and extended precision.

Page 17: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Floating-Point Representations (Continued)

These representations differ in the range of values they can handle and the amount of memory they use.

Representation Max. Value Memory Bytes

Single-precision (SGL) 3.40 x 1038 4

Double-precision (DBL) 1.79 x 10308 8

Extended-precision (EXT) 1.19 x 104932 16

Page 18: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Coercion Dots

If you wire together two terminals of different numeric representations, LabVIEW must convert the number from one representation to the other.

In these cases a red dot called a coercion dot will appear on the terminal where the conversion takes place.

Coercion dots are bad. They waste memory, and can lead to rounding errors that are difficult to find.

(Bishop, p. 216)

Page 19: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Numeric Conversion Functions

LabVIEW has functions for converting a number of any representation to any other representation. (For example, the To Word Integer function converts any number to the I16 representation.)

These functions are found on the Functions > Numeric > Conversion palette.

These are sometimes useful in eliminating coercion dots.

Page 20: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

While Loop

A While Loop executes the code inside its borders repeatedly until a certain condition is met.

A While Loop has two terminals: the iteration terminal and the conditional terminal.

(Bishop, p. 221)

Page 21: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

While Loop Example

Page 22: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Placing a While Loop

While Loops are found on the Functions >> Programming>> Structures palette.

Click it, and then drag to create a loop on the block diagram.

Then place items inside the loop to build your subdiagram.

Page 23: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Iteration Terminal

A While Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed.

It behaves just like a For Loop’s iteration terminal.

The iteration number is available to be used inside the loop.

(Bishop, p. 221)

Page 24: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal

A While Loop’s Conditional Terminal determines at the end of each loop execution whether the loop will be executed again.

You set the Conditional Terminal to either Stop if True or Continue if True.

Usually you’ll wire a Boolean control or the output of a Boolean function to this terminal.

(Bishop, p. 221)

Page 25: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal:Stop if True

When the Conditional Terminal is set to Stop if True, it looks like a red stop sign on the block diagram.

A true condition will cause the loop to stop executing, but a false condition will cause it to execute again.

(Bishop, p. 221)

Page 26: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Conditional Terminal:Continue if True

When the Conditional Terminal is set to Continue if True, it looks like a green looping arrow on the block diagram.

A true condition will cause the loop to execute again, but a false condition will cause it to stop executing.

(Bishop, p. 221)

Page 27: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

Example While Loop in BASIC

This type of loop is called a “While Loop” because in text-based programming languages it is coded using the word WHILE.

Example:

CLS INPUT “Guess my age. ”, guess WHILE guess <> 46 INPUT “No. Try again. ”, guess WEND PRINT “You got it!”

Page 28: Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.

Floyd, Digital Fundamentals, 10th ed

For Loop With a Conditional Terminal

It’s possible to add a conditional terminal to a For Loop, creating a loop that behaves like a cross between a For Loop and a While Loop.

To do this, right-click a For Loop’s border and select Conditional Terminal.

(Bishop, p.220)

We won’t use this feature in this course: Whenever I refer to a For Loop, I mean a plain For Loop without a conditional terminal.