unit07 (1)

22
EET 2259 Unit 7 Case Structures; Sequence Structu Read Bishop, Sections 5.4 and 5.5. Lab #7 and Homework #7 due next week. Quiz #3 next week.

Upload: rmsp

Post on 08-Oct-2015

221 views

Category:

Documents


0 download

DESCRIPTION

unit07 (1)

TRANSCRIPT

EET 159 PowerPoint Slides

EET 2259 Unit 7Case Structures; Sequence StructuresRead Bishop, Sections 5.4 and 5.5.

Lab #7 and Homework #7 due next week.Quiz #3 next week.1-This weeks big idea is case structure. Well also look at sequence structures.

Review: StructuresStructures control the flow of a programs execution.Weve looked at two kinds: For Loops While LoopsThis week well look at two more:Case StructuresSequence Structures2-Example: E-Mail Notification.vi from two weeks ago contains a loop, a sequence structure, and case structures.-In programs up to now, all of the code on block diagram executed whenever the program ran; case structures let a program decide which code to execute, depending on conditions defined by the programmer.

Case StructureCase structures provide a way to execute different code depending on which one of several possible conditions is true.Traditional programming languages accomplish the same thing using IF statements or CASE statements.(Bishop, p. 236)3-Case structures, or something like them, exist in all programming languages.-Case structures provide a more powerful and more flexible way to do the sort of thing that the Select function lets you do. (Recall using Select to choose Hello or Goodbye.) Two examples where case structures are better: 1) you need to choose between more than two options; 2) your choice is between different kinds of actions (displaying a string vs making an LED blink).

Placing a Case StructureCase structures are found on the Programming>Structures palette.As with loops, click it, and then drag to create a Case structure on the block diagram. (Bishop, p. 236)

4Demo; and point out selector label and selector terminal.SubdiagramsA Case structure has multiple subdiagrams, one of which is executed when the structure runs. These subdiagrams are stacked on top of each other like a deck of cards, so that only one is visible at a time.The selector label on the top border tells which case is visible, and has arrow buttons to let you step through the cases.(Bishop, pp. 237)5Selector TerminalA Case structure has one terminal, called the selector terminal.This terminal, labeled ?, determines which one of the structures subdiagrams will be executed.Youll usually wire this terminal to a control or to the output of a function.(Bishop, p. 238)6-Following Bishops example on pp. 201ff, on front panel place two numeric controls labeled First number and Second number and a toggle switch labeled Add or Subtract? and a numeric indicator labeled Result. On block diagram, on False subdiagram place and wire an add function, and on True subdiagram place and wire a subtract function. -Run it a few times, and turn on execution highlighting to show how it selects one subdiagram or the other.-Show how you could same thing using a Select function.-Then place the entire block diagram inside a While loop with a Stop button.

Selecting Which Case to Execute: Boolean ExampleA Case structure with a Boolean selector terminal will have two subdiagrams: one that executes if the condition wired to the terminal is true, and one that executes if the condition is false.

7This is the example weve been dealing with.Selecting Which Case to Execute: Numeric ExampleA Case structure with a numeric selector terminal can have any number of subdiagrams: the one thats executed depends on the value wired to the terminal.

8-First use a 2-position knob (rep=I8) with text labels to do same thing as above. Show that even though it has these text labels, its output is an integer. Then connect it to a case structure to perform the correct function.-Extending previous example, add two more text labels for Multiply, Divide. -You could also do this with Select functions, but it would be a lot harder.Numeric Selector LabelsA numeric selector label can be:a single integer, such as 24a list of integers, such as 1, 5, 11a range of integers, such as 10..20 meaning all integers from 10 to 20..10 meaning all integers less than or = 1010.. meaning all integers greater than or = 10Selector labels cannot be floating point numbers. (Bishop, p. 238)9Demo by adding a 5th case, Product, and have it do same case as Multiply.Adding and Deleting CasesRight-click a Case structures border and thenchoose Add Case Before or Add Case After to add a case.choose Duplicate Case to copy a subdiagram to a new case.choose Delete This Case to remove a case.(Bishop, p. 239)10DemoDefault CaseNumeric case structures and string case structures usually have a default case, which will execute whenever none of the other cases apply.To make a case the default case, right-click its border and choose Make This The Default Case.(Bishop, p. 240)11Demo what happens if you dont have a default case in the previous example.TunnelsRecall that a tunnel automatically appears on a structures border when you run a wire across the border.If data flows from outside the structure to inside, the tunnel is an input tunnel. If data flows from inside the structure to outside, its an output tunnel. If an output tunnel is wired on one of a Case structures subdiagrams, then it must be wired on all of the subdiagrams.(Bishop, pp. 240-241)12-Show how tunnel is white and the run arrow is broken until output tunnels are wired on all cases.Example IF Statement in BASICCLSINPUT How many CDs are you buying? , CDsIF CDs >= 5 THEN Cost = 9 * CDsPRINT You get a discount!ELSE Cost = 10 * CDsEND IFPRINT Total cost is $; Cost13-IF statement is good if you have two different possibilities.-Have them enter it and run it.Example CASE Statement in BASICCLSINPUT How many CDs are you buying? , CDsSELECT CASE CDsCASE IS >= 10 Cost = 8 * CDsCASE IS >= 5Cost = 9 * CDsCASE ELSECost = 10 * CDsEND SELECTPRINT Total cost is $; Cost14-CASE statement is good if you have many different possibilities.-Do this example in LabVIEW. On front panel place a numeric control labeled How many CDs? and two numeric indctrs labeled Cost per CD and Total cost and a string indctr with no label. On block diagram have 4 cases: ..0 that output 0s and Please enter a positive number, and 1..4 that multiples CDs by 10 and outputs No discount for you, and 5..9 that multiples CDs by 9 and outputs You get a discount!, and 10.. that multiples CDs by 8 and outputs You get our super-discount! Order of ExecutionIn text-based programming languages, the order of the statements determines the order of execution.

In LabVIEW, if two functions are connected by wires, you can tell which one must execute first.

15Sequence StructuresBut in LabVIEW, you cant always predict the order of execution. For example, in this code, which function executes first: the Subtract or the Greater Than?

Sequence structures can be used to force code to execute in a specific sequence.(Bishop, p. 247)16-Show file on h drive called SeqStrucExample.vi. What will be the order of the operations?Frames in Sequence StructuresThe code to be executed sequentially is entered on subdiagrams called frames. The analogy is to frames of movie film, which are projected one after another.(Bishop, p. 247)

17-Modify previous example. Run it, then run with execution highlighting on.-Then use template above to measure speed of plotting 1,000,000 random numbers. (Then cube each number before plotting using built-in function and using subVI from Week 4.)Tick CountThe example on the previous slide used LabVIEWs Tick Count function, which returns the number of milliseconds (ms) on your computers millisecond timer. Generally this timer is reset to zero whenever the computer is rebooted. Knowing that Tick Count uses the U32 datatype, lets figure out how long it will take for it to roll over to zero again.18Tick Count ComputationsWhat is the largest integer we can represent with U32? _____________If that number is in ms, how many seconds is that? ________________

How many minutes is that? ___________

How many hours is that? ____________

How many days is that? ____________19Flat or Stacked?LabVIEW has two kinds of Sequence structures:Flat Sequence structures, in which the frames are laid out side by side, as in the image on the previous slide.Stacked Sequence structures, in which the frames are stacked like a deck of cards, with only one visible at a time. (Bishop, p. 247)20-Use shortcut menu to convert previous example to a stacked sequence structure, and run again (with execution highlighting on).Flat or Stacked? (Continued)The two kinds of Sequence structure are similar. The main difference is how much space they take up on the block diagram. But there are other differences, such as:If data is wired out through a tunnel from a frame in a Flat Sequence structure, the data passes out as soon as that frame finishes executing.If data is wired out through a tunnel from a frame in a Stacked Sequence structure, the data does not pass out until the entire structure finishes executing. 21-Continuing example from previous slide, convert it back to a flat structure, then move controls and indicators outside the frames so that data passes out through tunnels. Run it.-Then convert back to a stacked structure again, and run again.Use Sequence Structures Only When NecessarySequence structures serve a valid purpose, but they also tend to hide parts of the program and interfere with the natural flow of data in LabVIEW.Avoid using them unless you have a situation where you need to guarantee the order of execution, and LabVIEWs natural data flow does not provide such a guarantee.(Bishop, p. 250)22-Example: Look at sequence structure in E-Mail Notification.vi.