Showing posts with label for. Show all posts
Showing posts with label for. Show all posts

Wednesday, August 18, 2010

Programming LabView - Shift Register

The shift register is a key element of LabView programming. It allows looping structures, such as the For and While loop, to carry over values from one iteration into the next. This can be extremely powerful in allowing the developer to build up arrays of information, or retain state information about a program running in a loop.

For starters, let us look at how to create a shift register. Right click on the left edge of a for or while structure and select "Add Shift Register":

Once this is completed, you will see two squares appear on the border on opposite sides of the structure. The square on the left outputs the value of the last iteration, while the square on the right must have something wired to it that is passed around to the next iteration.


Below, a numeric control terminal is wired to the shift register. Whatever value is typed into the control on the front panel will appear as an output on the left square on the next iteration:

Below are some modifications to the loop in progress. An indicator has been wired to the value from the input of the shift register. The control has been disconnected from the right square. A boolean selector will be used in this example. See the menu below to find this routine:

Using the boolean selector, the following code was wired:


This example will retain the previous value of the shift register on the output and to the next iteration if the boolean control is set to false. If it is set true, then the value of the numeric control will be placed onto the shift register. This code will act as a latch, saving the value in the numeric control when the boolean control is on, while retaining the old value and not following the numeric control value when the boolean control is off.

As you move forward with LabView programming, you will see that most design patterns are impossible without the shift register. Anytime you are trying to keep track of something in a loop, you will need to use one.

Exercise 3 – Using Loops

Use a while loop and a waveform chart to build a VI that demonstrates software timing.

Front Panel
1.      Open a new VI.
2.      Build the following front panel.
    1. Select the horizontal pointer slide on the Controls»Numeric Controls palette and place it on the front panel. You will use the slide to change the software timing.
    2. Type millisecond delay inside the label and click outside the label or click the Enter button on the toolbar, shown at left.
    3. Place a Stop Button from the Controls»Buttons palette.
    4. Select a waveform chart on the Controls»Graph Indicators palette and place it on the front panel. The waveform chart will display the data in real time.
    5. Type Value History inside the label and click outside the label or click the Enter button.
    6. The waveform chart legend labels the plot Plot 0. Use the Labeling tool to triple-click Plot 0 in the chart legend, type Value, and click outside the label or click the Enter button to relabel the legend.
    7. The random number generator generates numbers between 0 and 1, in a classroom setting you could replace this with a data acquisition VI. Use the Labeling tool to double-click 10.0 in the y-axis, type 1, and click outside the label or click the Enter button to rescale the chart.
    8. Change –10.0 in the y-axis to 0.
    9. Label the y-axis Value and the x-axis Time (sec).

Block Diagram

1.      Select Window»Show Diagram to display the block diagram.

2.      Enclose the two terminals in a While Loop, as shown in the following block diagram.
a.       Select the While Loop on the Functions»Execution Control palette.
b.      Click and drag a selection rectangle around the two terminals.
c.       Use the Positioning tool to resize the loop, if necessary.

2.      Select the Random Number (0-1) on the Functions»Arithmetic and Comparison»Numeric palette. Alternatively you could use a VI that is gathering data from an external sensor.
3.      Wire the block diagram objects as shown in the previous block diagram.
4.      Save the VI as Use a Loop.vi because you will use this VI later in the course.
5.      Display the front panel by clicking it or by selecting Window»Show Panel.
6.      Run the VI.
The section of the block diagram within the While Loop border executes until the specified condition is TRUE. For example, while the STOP button is not pressed, the VI returns a new number and displays it on the waveform chart.
7.      Click the STOP button to stop the acquisition. The condition is FALSE, and the loop stops executing.
8.      Format and customize the X and Y scales of the waveform chart.
a.       Right-click the chart and select Properties from the shortcut menu. The following dialog box appears.
b.      Click the Scale tab and select different styles for the y-axis. You also can select different mapping modes, grid options, scaling factors, and formats and precisions.  Notice that these will update interactively on the waveform chart
c.       Select the options you desire and click the OK button.
1.      Right-click the waveform chart and select Data Operations»Clear Chart from the shortcut menu to clear the display buffer and reset the waveform chart. If the VI is running, you can select Clear Chart from the shortcut menu.

Adding Timing
When this VI runs, the While Loop executes as quickly as possible. Complete the following steps to take data at certain intervals, such as once every half-second, as shown in the following block diagram. 
a.       Place the Time Delay Express VI located on the Functions»Execution Control palette. In the dialog box that appears, insert 0.5.  This function would make sure that each iteration occurs every half-second (500 ms).
b.      Divide the millisecond delay by 1000 to get time in seconds.  Connect the output of the divide function to the Delay Time (s) input of the Time Delay Express VI.  This will allow you to adjust the speed of the execution from the pointer slide on the front panel.
2.      Save the VI, because you will use this VI later in the course.
3.      Run the VI.
4.      Try different values for the millisecond delay and run the VI again.  Notice how this effects the speed of the number generation and display.
5.      Close the VI.

End of Exercise
Related Posts Plugin for WordPress, Blogger...

Popular Projects

My Blog List

Give support

Give support
Encourage Me through Comments & Followers

Followers