You are on page 1of 5

Shift Registers

Many times you will be creating a program with loops and you need to use data from the previous iteration. As
an example, say you are writing a program that will move the NXT robot counting the number of times a
particular object is found on the ground as you move around the four corners of the room. A for loop going from
0 to 3 works for the 4 corners but you need to retain the counter as the object is found.

In LabVIEW, you can use shift registers, which allows you to pass a value from one to the next iteration. If you
know programming languages such as C++ or JAVA you may be familiar with static variables which are used to
pass values from one loop iteration to the next.

Above is a block diagram showing


what you are going to create in
LabVIEW.

Data comes into the loop by way of the


shift register on the left side of the for
loop and is passed to the next iteration
of the loop through the shift register on the right side of the for loop. Below is another diagram that shows more
detail about the passing of data in the shift register.

Now we are going to write


a LabVIEW program that
will calculate the sum of
the numbers from 1 to 10
using a shift register in a
for loop. If you know C++
or JAVA here is the same
program in C++.
int i, n, sum;
n=11;
sum=0;
for (i=0; i<n; i++)
{
sum = sum + i;
}
cout << sum << endl;

1. Start with a blank VI and add a for loop over in the block diagram.
2. Now we are going to add the shift register to the for loop. Move the cursor to the left border of the for loop. If
you are slow enough when you are moving the cursor the words "for loop" will come up. Right click as soon as
you are on the border. You will know that you are in the right location because a set of operations for the for
loop will be there instead of the function palette. Select "Add Shift Register"

3. The sum must start off at zero and then add 1, then add 2, and so on until we reach 10. To initialized the shift
register you right click on the left shift register and create a constant with zero in it. To do that right click in the
block diagram and select Express --> Arithmetic and Comparison --> Express Numeric and drag the "Num
Constant" icon to the left of the shift register. Wire the constant into the shift register.

4. Remember that for loops in LabVIEW go from 0 to N-1 where N is the value in the count terminal which
looks like this: Count Terminal. Now you want to set the count terminal to 11 because you want the numbers
from 0 to 10. Move the cursor up next to the count terminal, right click and select "create constant". Set the
constant to 11.
Iteration Terminal

5. The iteration terminal of the for loop, which looks like the icon above, provides the current loop count going
from 0 to 10 and will change every time through the loop. We want the value from the shift register, which will
have the previous sum of numbers added to the count terminal giving us the current sum. As an example, say we
have been through the loop 4 times and it has summed 0+1+2+3 which is 6. We are going to add together the 6
plus the current count terminal, which will have the value of 5 in it, giving 11. To do this add an "addition"
function to the block diagram, wire the shift register on the left side of the for loop to one input and the count
terminal to the other input. Wire the output to the shift register on the right side of the for loop.

6. Now we have the for loop calculating the sum of 0..10 but we need to print that sum after the loop has
finished. To do that we add an indicator outside of the for loop to the right of it and wire the shift register on the
right side of the for loop to this indicator.

7. You have a very basic loop with a shift register and you want to make sure that when it is run it produces the
output of 55 since the sum of the numbers from 0..10 is 55. If you want to see what your program should look
like, below is a version of the program. Yours may be different with wired and nodes in different locations.

In one of the exercises


in this tutorial there is
a program to compute
the square root of a
number and that will
use a shift register in a
while loop. The while
loop is used for this
problem because we
will compute a new
estimate of the square
root from the value
computed in the
previous iteration and pass the new value in the shift register.

Square Root Calculator


This will be the final program that you will be doing in the Basic LabVIEW course. It will be calculating the square root of a
number, X, and will be using a shift register to do so. You will have the option of doing the program completely on your
own after the flowchart is shown below.
Let's go over a few items in the
flowchart to better understand
what is being sent through the
shift register. You can then try
to write the LabVIEW code
yourself before the code is
given to you or you can go
straight to the code and enter it
from the step-by-step
instructions.

-The square root is going to be


calculated in a while loop that
has a shift register. The while
loop will have a termination test
and will be covered below.
-The value of X is the number
which we are finding the
square root of and this value
will never change throughout
the program. Therefore, we
don't want to pass it through
the shift register but we do
want read it outside of the loop
and pass the value through the
loop boundary into the loop.
-The value of Y is initially set to
3 and used inside the loop for
calculating a new value, Ynew.
This means that upon each iteration of the loop the newly calculated value of Ynew is sent back to the loop and picked up
as Y. This is probably the most difficult part of a sift register and it may take a little getting used to. Basically you want to
take an initial value, calculate a new value, and send the new value back to through the loop.
-The loop keeps iterating until the absolute value of the difference of Y and Ynew is less than or equal to 0.00001. When
the difference between those two values is less than 0.00001 the square root value is very accurate and the loop should
stop. In LabVIEW we have to wire this condition test into the stop condition.
-After exiting from the while loop we want to print the square root of X and this is stored in Ynew. This means that we run a
wire through the while loop boundary to an indicator outside of the loop. The wire takes the value of Ynew and sends it to
the indicator. The user will see two things on the screen: a control that has the value, X in the flowchart, that they want to
find the square root of and an indicator, Ynew in the flowchart, which has the square root of x.

See if you can write the LabVIEW program for this or you can jump straight to the code. You may find that seeing the code
teaches you more than trying to figure it out yourself.

When you write the square root program it requires you do think about the code structure. One advantage of LabVIEW is
that you get away from the syntax of the programming language and you get to think about the logic of the program. The
most difficult part of LabVIEW programming is that it requires you to know about the dataflow programming but as soon as
you write your first program this gets much easier. Lets start with the front panel of the square root program.

In the front panel


image above, you
can see that there is
one control on the
left which has the
label of "x". Then,
on the right, is one indicator which has the label of "square root of x". Now let's see what the block diagram
looks like.

Here
are the
steps
that
you
need to

construct the block diagram.

1 Start with a blank VI and make sure that you have added the control and the indicator and that the names have been
changed as shown on the front panel diagram above.

2 Add a while loop to your block diagram. You want to make the while loop fairly large so that the code can be added.
Make sure that you don't wire the control or the indicator yet. That will come in a few steps.

3 We need to add a shift register to the while loop and you do that by right clicking on the while loop boundary and select
"add shift register".

4 Now you are going to add the initial value of y. In the previous page we mentioned that Y is the value that enters the
shift register, the value of Ynew is calculated, and the value of Ynew is sent out of the shift register to the next iteration of
the loop. We need to initialize the value of Y to 3 and you can do that by adding a numeric constant outside of the while
loop and you do that by right clicking in the block diagram and selecting Mathematics -- Numeric --> Numeric Constant.
Then make sure that the value in the constant is 3.

5 Wire the constant to the shift register and make sure it looks like the block diagram above.

6 The top condition is taking the previous value of Y, subtracting the new value of Y from it, taking the absolute value, and
comparing that value to 0.00001. The absolute value function is obtained by right clicking on the block diagram and
selecting Mathematics --> Numeric. The less than or equal comparison operator is located at Programming -->
Comparison --> Less than or equal.

7 Add the remaining functions to the block diagram and wire everything like is shown in the block diagram above.

8 Now it is time to run the program. Enter a 2 in the control labeled X and press the run arrow. You should get 1.1421 as
the output.

Congratulations! You have a complete square root calculator program! If you want to do more with this program, in the
next exercise you will modify the square root program to compute the Nth root of a number.

You might also like