timing, shift registers, and case structures - national instruments

Upload: ra-balamurugan

Post on 07-Aug-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    1/13

     

    Tutorial: Timing, Shift Registers, and Case StructuresPublish Date: Apr 01, 2015 | 43 Ratings | 4.60 out of 5 | PDF

    Overview

    In this tutorial you will learn to expand the potential of your VIs with timing functions, iterative data transfer, and caseselectors. After you complete this tutorial, you will be able to time for and while loops, pass data between iterations of aloop, and programmatically select which sections of your code to run.

    Table of Contents

    Loop Timing in LabVIEW

    Normally when a loop, such as a while loop, finishes executing one iteration, it immediately begins running the next. It is

    often beneficial to control how often a loop executes, or its frequency. For example, if you wanted to acquire data in a loop,you would need a method to control the frequency of the data acquisition. Timing a loop also allows the processor time tocomplete other tasks such as updating and responding to the user interface. In the following figures, the processor usagefor a simple VI with a while loop running untimed and timed are shown. Timing a loop can drastically increase performance.

    Untimed Loop

    Timed Loop Executing at 1000 Times a Second

    Wait Functions

    After you create a loop, you can place a wait VI inside of the loop to control how long it waits before performing the nextiteration.

    There are two basic wait functions in LabVIEW: Wait (ms) and Wait Until Next ms Multiple.

     

    The Wait (ms) function forces the loop to wait for a user-specified amount of time, in milliseconds, before running the nextiteration.

    The Wait Until Next ms Multiple function watches the millisecond counter and waits for it to reach a multiple of the user-specified time, in milliseconds, before running the next iteration of the loop. You can use this VI to synchronize differentactivities. For example, you can configure multiple loops to execute at each multiple of 200 ms.

    Tutorial: Implementing a Wait Function in a LoopConsider using one of the wait functions in a while loop. Using the information from previous tutorials, create a blank VI inLabVIEW.

    1. Place a knob numeric control on the front panel by right-clicking on the front panel and navigating toControls»Modern»Numeric»Knob.

    http://googleweblight.com/?lite_url=http://www.ni.com/white-paper/7592/en/pdf&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFnoOwEV0Zl-ypc61PvmrvRfazhnkQhttp://googleweblight.com/?lite_url=http://www.ni.com/white-paper/7592/en/&lite_escape=&fs=https://support.google.com/webmasters/answer/6211428?hl=enhttp://googleweblight.com/?lite_url=http://www.ni.com/white-paper/7592/en/&lite_escape=&fs=http://googleweblight.com/?lite_url=http://www.ni.com/white-paper/7592/en/pdf&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFnoOwEV0Zl-ypc61PvmrvRfazhnkQ

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    2/13

    2. Change the knob’s limits to 1 and 1000 by double-clicking on the knob’s current limits and entering the new values. Youwill use the knob to control a while loop’s wait time.

     

    3. Place a numeric indicator on the front panel by right-clicking on the front panel and navigating toControls»Modern»Numeric»Numeric Indicator.

    4. This indicator displays the while loop iterations.

    5. Change the Representation of the numeric indicator to I-32 (long integer) by right-clicking on the indicator andselecting Representation. Click on I32.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    3/13

    6. Place a Stop Boolean control on the front panel. You can find this at Controls»Modern»Boolean»Stop Button. Use thisStop button to stop the while loop.

    7. Double-click the name of the knob and change its name to “Wait Time (ms).”

     

    8. Double-click the name of the numeric indicator and change its name to “Iteration.”

     

    9. View the block diagram by selecting Window»Show Block Diagram or pressing .

    10. On the block diagram, drag a while loop around the front panel controls and indicator. Find the while loop at

    Functions»Programming»Structures»While loop.

     

    After selecting the while loop, drag it around the three icons. If you miss one icon, just click and drag it into the loop.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    4/13

    11. Wire the Stop control to the while loop’s stop conditional terminal. Click on the right terminal of the Stop button’s icon,drag to the left input terminal of the stop conditional terminal, and click to complete the wire.

    12. Wire the numeric indicator to the while loop iteration terminal. 

    13. Right-click on the block diagram to open the Functions palette and then navigate to and open the Timing palette, which

    is located at Functions»Programming»Timing.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    5/13

    14. Place the Wait (ms) function, located in the Timing palette, inside the while loop.

    15. Wire the knob control Wait Time (ms) to the input of the Wait (ms) function. The knob value specifies how long, inmilliseconds, the loop waits before running the next iteration.

    16. View the block diagram by selecting Window»Show Front Panel or pressing .

    17. Run the VI.

    18. As the VI runs, change the knob value by clicking and dragging the knob; note that the speed of the loop, as shown bythe iteration indicator, changes accordingly.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    6/13

    19. Stop the VI using the Stop Boolean control.

    Iterative Data Transfer

    When programming with loops, sometimes you need to call data from previous iterations of the loop. In LabVIEW, you canuse shift registers, which are similar to static variables in text-based programming languages, to pass values from one loopiteration to the next.

    Data enters the right shift register and is passed to the left shift register on the next iteration of the loop.

    Tutorial: Using Shift Registers

    The next section of this tutorial guides you through creating and using shift registers in a simple LabVIEW VI.

    1. Create a new LabVIEW VI by navigating to File»New VI.

    2. Place a numeric control on the front panel and change its value to 2.

    3. Double-click on the control’s name and change it to “Initial.” 

    4. Place a numeric indicator on the front panel and name it “Result.”

    5. View the block diagram by selecting Window»Show Block Diagram or pressing .

    6. Place a for loop on the block diagram between the numeric control and indicator. The for loop is located atFunctions»Programming»Structures»For Loop.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    7/13

    7. Right-click on the input of the count terminal of the for loop and select Create Constant. Change the value of thisconstant to 2.

    8. Wire the output of the Initial control to the right edge of the for loop to create a tunnel.9. Right-click on the tunnel that you just created and select Replace with Shift Register.

    10. Wire the output of the right shift register to the Result indicator.

    11. Place a multiply function in the for loop.

    12. Place a numeric constant in the for loop, assign it a value of 3, and connect it to one of the input terminals of themultiply function.

    13. Wire the left shift register to the remaining input of the multiply function, and wire the output of the function to the rightshift register.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    8/13

    14. View the block diagram by selecting Window»Show Front Panel or pressing .

    15. Run the VI. The VI changes the value of the Result indicator to 18.

    Explanation

    Shift registers are integral to this VI. To understand how the VI works, you can step through the code.

    Because the for loop’s counter terminal is wired to a constant of 2, it runs twice. On the first iteration of the for loop, thevalue of Initial, 2, is multiplied by 3. The result is 6, and this value is passed to the right shift register. On the second iterationof the for loop, the left shift register receives the value that was sent to the right shift register on the previous iteration, 6.

    The value of 6 is multiplied by 3, for a result of 18. Because the for loop completed all of its iterations, it stops running andthe value of 18 is sent to the Result indicator on the front panel.

    The mathematical formula for this simple VI looks like this:

    Result = ( ( Initial * 3 ) * 3 )

    If you changed the value of the for loop’s count terminal to 4, the mathematical formula looks like this:

    Result = ( ( ( ( Initial * 3 ) * 3 ) * 3 ) * 3 )

    Case Structures

    When programming in LabVIEW, you may want to choose between multiple sections of code based on an input. Based onthe input it receives on the case selector terminal, the case structure chooses which “case,” or section of code, to execute.The case selector terminal appears as a small question mark (?) on the left side of the case structure.

    If you change the input to the case selector terminal, the section of code that is executed changes. In the figures below, thecase structure executes different code for the input strings “True” and “False.”

     

    Selector Terminal

    The case selector terminal can receive multiple data types. You can use the following data types as inputs to the caseselector terminal:

    • Inte ers

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    9/13

     

    • Boolean values

    • Strings

    • Enumerated type values (Enums)

    A case structure with a Boolean wired to its selector terminal has a maximum of two cases; all other data types allow two ormore cases.

    Tutorial: Programming with a Case Structure

    The following tutorial demonstrates how you can use a case structure to choose between multiple sections of code.

    1. Create a new LabVIEW VI by navigating to File»New VI.

    2. Place two numeric controls and a numeric indicator on the front panel. Name the numeric controls “Input A” and “InputB.” Name the indicator “Result.”

    3. Place an Enum control on the front panel. This control is located in Controls»Modern»Ring & Enum. Rename the

    control “Operation.”

    4. Right-click on the Enum and select Edit Items… to open the Enum Properties.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    10/13

    5. Click the Insert button and add the following Items: Add, Subtract, Multiply, and Divide.

    6. Click the OK  button at the bottom of the Enum Properties box to close it.

    Your front panel should look similar to the following figure.

    7. View the block diagram by selecting Window»Show Block Diagram or pressing .

     

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    11/13

    . , .Functions»Programming»Structures.

    9. Wire the Operation Enum control to the case structure’s selector terminal, located on the left side of the case structure.

    10. Right-click on the border of the case structure and select Add Case for Every Value. The case structure now has a casefor every value of the Enum that is wired to the case selector terminal. This tutorial demonstrates four cases: Add, Subtract,Multiply, and Divide.

    11. Switch to the “Add” case of the case selector terminal by clicking the right or left arrows at the top of the case structure,or by placing your mouse inside the case structure and pressing  while scrolling with the mouse.

    12. Place the add function in the Case Statement when the “Add” case is selected.

    13. Wire the controls Input A and Input B to the inputs of the add function. Wire the output of the add function to the Resultindicator. You can wire through the case structure, which acts much like a loop and creates tunnels automatically.

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    12/13

    14. Switch to the “Subtract” case of the case selector terminal by clicking the right or left arrows at the top of the casestructure, or by placing your mouse inside the case structure and pressing while scrolling with the mouse.

    15. Add the subtract function to the “Subtract” case. Wire the function to the controls and indicators in the same way youwired the add function.

    16. Add the multiply function to the “Multiply” case and the divide function to the “Divide” case. Wire these functions to thecontrols and indicators in the same way you wired the add and subtract functions. Your case structure should now containfour cases similar to the following:

     

    17. When you have successfully wired all the outputs, the tunnel to the Result indicator changes from a hollow square to afilled square, as shown in the following figures.

     

    18. View the block diagram by selecting Window»Show Front Panel or pressing .

    19. Change the value of Input A to 1 and the value of Input B to 2.

    20. Change the value of the Operation Enum to “Add.”

    21. Run the VI, which changes the value of the Result indicator to 3.

    22. Experiment with different values of the Operation Enum and run the VI again.

    Video  Exercise  Timing, Structures, and Storing Data  Modules Home  FIRST CommunityBack to Top

    http://googleweblight.com/?lite_url=http://www.ni.com/academic/students/learn&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFk7UDIoJ102N9NKbgy60wMR-cdAvghttp://googleweblight.com/?lite_url=http://zone.ni.com/devzone/cda/tut/p/id/7529&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkanwW2rQIV7mo86iycnregxbmlHAhttp://googleweblight.com/?lite_url=http://www.ni.com/first&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkaBCGyZvAJu8duSrXFlANxS9MlbAhttp://googleweblight.com/?lite_url=http://zone.ni.com/devzone/cda/tut/p/id/7544&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkrYqOjaA85iQ_eF2uNAtWfUj-9HQhttp://googleweblight.com/?lite_url=http://zone.ni.com/devzone/cda/tut/p/id/7594&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmvfFprGQI1XSXrl8djEUSV13d-Uw

  • 8/20/2019 Timing, Shift Registers, And Case Structures - National Instruments

    13/13

    Bookmark & Share

    Share

    Ratings

    Rate this document

    Select a Rating

    Answered Your Question?

     Yes No

    Submit

    Submit

    Forward Together

    NI equips engineers and scientists to meet the challenges of an increasinglycomplex world.

    Join Us

     

    About National Instruments

    About National Instruments

    Shared Value

    Investor Relations

    Worldwide Offices

    Careers

    Order Information

    Order and Payment Information

    Order by Part Number

    Review Order Status and History

    OEM Solutions

    Support Services

    Request Support from an Engineer

    Submit a Service Request

    View Returns, Warranty and Repair Information

    Find an NI Alliance Partner

    Popular Resources

    Software Updates

    Find Product Downloads and Drivers

    Activate a Product

    Register a Product

    Find Supplier Information

    Site Map

    (800) 531-5066| Privacy | Terms of Use | Other Legal Info | © 2015 National Instruments Corporation. All rights reserved.

    http://googleweblight.com/?lite_url=http://www.ni.com/careers/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFn6EikviDiog7-BgPJN0aGmWB9eSAhttp://googleweblight.com/?lite_url=http://sine.ni.com/myproducts/app/main.xhtml%3Flang%3Den&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkMAzeMyT3XWuvzL3fx_6lDTce6dQhttp://googleweblight.com/?lite_url=http://sine.ni.com/myproducts/app/main.xhtml%3Flang%3Den&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkMAzeMyT3XWuvzL3fx_6lDTce6dQhttp://www.facebook.com/NationalInstrumentshttp://googleweblight.com/?lite_url=http://partners.ni.com/partner_locator/search.aspx&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFny-kWV2vnYV0iTqW_mmcgCTtYHmwhttp://googleweblight.com/?lite_url=http://www.ni.com/company/suppliers/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmlQLNOaliGz_QHGQX-hLBpBorrJQhttps://googleweblight.com/?lite_url=https://sine.ni.com/srm/app/getassistance&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFlSWFfEO0W4Dir0-FjeeGctYR-BkQhttp://www.youtube.com/nationalinstrumentshttp://googleweblight.com/?lite_url=http://www.ni.com/downloads/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmt7bhHKuYMufGCSPdGtGDCLyjOBQhttp://googleweblight.com/?lite_url=http://www.ni.com/help/map.htm&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmvmCs7RxTsFMsu_Qn2Tj68-kv1DAhttp://googleweblight.com/?lite_url=http://www.addthis.com/bookmark.php%3Fv%3D250%26pubid%3Dra-4d9f12be33ab9b64&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFl8DxKRIc6bBwrgT24sbIxPNxfk-Ahttp://googleweblight.com/?lite_url=http://www.ni.com/niglobal/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFnUOCQGVK7iPJuSkXz6AELSGWDZBwhttp://googleweblight.com/?lite_url=http://www.ni.com/legal/termsofuse/unitedstates/us/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFnEOeOmTwMSM_LGaYrf-EsYE1P6jwhttp://googleweblight.com/?lite_url=http://www.ni.com/howtobuy/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmMr5E_9XANvcVr6AL4_EWpp5D0IQhttps://googleweblight.com/?lite_url=https://www.ni.com/status/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFnUj4pAd50Re4BzBMYvaawL56UrcQhttp://googleweblight.com/?lite_url=http://www.ni.com/support/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFlqpVO1XnL26Xd3hcDvNxueQE-9Lghttp://googleweblight.com/?lite_url=http://sine.ni.com/apps/utf8/nios.store%3Faction%3Dpurchase_form&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmWx_PM1QOCcOy-q-cNeOpVqVHsTwhttp://googleweblight.com/?lite_url=http://privacy.truste.com/privacy-seal/National-Instruments-Corporation/validation%3Frid%3Dbc6daa8f-7051-4eea-b7b5-fb24dcd96d95&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmnQI5SJK13FBE3UAJa9kmibltP0Ahttp://googleweblight.com/?lite_url=http://www.ni.com/company/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFm0n4xSX6v363Y6Epmb_05wfbyutQhttp://googleweblight.com/?lite_url=http://www.ni.com/rss/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmwrcFl4UbE_dmgi_W0RWYuXBd31Qhttp://googleweblight.com/?lite_url=http://www.ni.com/oem/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmylcn1Y9HqlDtqvr9eupthVwFY0Qhttp://googleweblight.com/?lite_url=http://www.ni.com/services/warranty_repair_policies.htm&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFn-k5vm2jnuthAp_3aptOnVNdZtNQhttps://googleweblight.com/?lite_url=https://sine.ni.com/srm/app/myServiceRequests&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmGjC63sZP40YG57MpfDkajX1C6VQhttp://twitter.com/niglobalhttp://googleweblight.com/?lite_url=http://www.linkedin.com/company/3433%3Ftrk%3Dtyah&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFl4bAuc-RRJB_fA4uVrm1T7jb6BHQhttps://googleweblight.com/?lite_url=https://decibel.ni.com/content/groups/ni-news-in-real-time/blog/2014/10/23/ni-named-one-of-the-world-s-best-multinational-workplaces-for-fourth-consecutive-year&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmPUSXfNJSfPgDq3hNsoIZhDzVEXwhttp://googleweblight.com/?lite_url=http://www.ni.com/company/shared-value/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmj7osGlN-7DYriUjTCBe1CAZI7fghttp://googleweblight.com/?lite_url=http://investor.ni.com/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkWZR7SAHG20RE4qHh6Hnr1NceXNghttp://googleweblight.com/?lite_url=http://www.ni.com/legal/privacy/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFkDLYKxYQ6N5SdqjrjvsoxNYlThkAhttp://googleweblight.com/?lite_url=http://www.ni.com/legal/&lc=en-IN&s=1&m=193&ts=1443510389&sig=APONPFmlqRZOx5h3qN4w9MyLerTyZh3FAw