Shift Registers in LabVIEW

Gotchas | Main Use Cases | How to implement

Why/when do I need a shift register in LabVIEW?

In LabVIEW, a shift register is used to store data during looping (while and for loops).  The shift register lets you access data and return the value from the previous iteration.  It’s like a local variable inside a loop in a text-based language.

You can use a shift register within a loop to:

  1. Store/update data
  2. Perform calculations from the shift register data
  3. Take actions based on data in the shift register.

What are the main use cases?

Passing data through cases of a state machine or event structure

This is the most common use case.

  1. This simple state machine uses two shift registers – one for the next state to execute and one for the data stored during execution.

While/for loop performing calculations using a previous value or multiple previous values.

  1. This example shows a multi-element shift register. Each time the for loop executes, the new value is stored in the top element of the shift register, the last element in the top is passed to the middle, and the last middle element is passed to the bottom.

Functional global variable – using an uninitialized shift register to hold data.

When a shift register does not contain an initialization value, it’s default value is whatever value was added to the shift register the last time the VI was executed. Not understanding this behavior can lead to some undesirable outcomes since you aren’t controlling what value is used during the first iteration of the while loop. However, this behavior can also be leveraged in what is known as a Functional Global Variable. By expecting the shift register to contain the last value you entered into it, you can store data for the next place that the VI is called. Functional Global Variables are typically used when the user wants to perform complex scaling or calculations on data as it is stored, read or store data in multiple different places throughout an application, or in situations where the user wants to buffer multiple readings. They form the basis for simple OOP-like objects in LabVIEW without needing the full capabilities of OOP. Some people used the term “Action Engine” for these types of VIs.

How do I implement a shift register in LabVIEW?

There are two basic methods.

First:

  1. Create a loop.
  2. Right button on the left or right side of the loop and select the item ‘Add Shift Register’.

Second:

  1. Create a loop (or for while).
  2. Connect a node that you want to turn into a shift register.
  3. Right click on the node and select “Replace with Shift Register”.

What gotchas should I watch out for?

  1. Putting down lots of shift registers instead of creating a single shift register with a cluster that contains all the data.
  2. Not initializing the shift register when it should be. If you aren’t creating a functional global variable, you probably want to initialize the shift register.
  3. While loops always execute. For loops do not.  Careful what the shift register value returns if your for loop doesn’t execute.
  4. If you are using an uninitialized shift register as a Functional Global Variable, make sure you clear the data in the shift register at the start of your application. Otherwise it will contain the data that was added during your last execution.

Have another LabVIEW-related question?

If you work for a US-based manufacturer and you’ve just got a quick technical question, you can reach out here to see if we’ve got a quick answer for you.

Poll – Vote to see how your peers voted!

For those of you in high-mix low-volume manufacturing: What would help you better understand if MES integration with your test systems makes sense for you? (please select up to 3)
This field is for validation purposes and should be left unchanged.

What else might I be interested in?

Whether you’re on the design validation side of things, or manufacturing test, since you’re into LabVIEW you’re probably also interested in automated test systems.  Here’s some additional info that you might find useful:

If you just got a LabVIEW project dropped in your lap and you want some help with it, check out our LabVIEW experience and reach out to us for a LabVIEW consultation if you’d like help.