Shift Registers in LabVIEW
Recommended by 0 users
When programming with loops, you often need to access data from previous iterations of the loop. For example, you may have a VI that reads the temperature and displays it on a graph. If you want to display a running average of the temperature as well, you need to use data generated in previous iterations. Two ways of accessing this data include the shift register and the feedback node.
While Loops produce default data when the shift register is not initialized. For Loops produce default data if you wire 0 to the count terminal of the For Loop or if you wire an empty array to the For Loop as an input with auto-indexing enabled. The loop does not execute, and any output tunnel with auto-indexing disabled contains the default value for the tunnel data type. Use shift registers to transfer values through a loop regardless of whether the loop executes.
Shift registers are used with For Loops and While Loops to transfer values from one loop iteration to the next. Shift registers are similar to static variables in text-based programming languages. A shift register appears as a pair of terminals, shown in Figure 4.11, directly opposite each other on the vertical sides of the loop border. The terminal on the right side of the loop contains an up arrow and stores data on the completion of an iteration. LabVIEW transfers the data connected to the right side of the register to the next iteration. After the loop executes, the terminal on the right side of the loop returns the last value stored in the shift register. Create a shift register by right-clicking the left or right border of a loop and selecting the Add shift register from the shortcut menu.
A shift register transfers any data type and automatically changes to the data type of the first object wired to the shift register. The data you wire to the terminals of each shift register must be the same type. You can add more than one shift register to a loop. If you have multiple operations that use previous iteration values within our loop, you can use multiple shift registers to store the data values from those different processes in the structure as shown in Figure 4.12.
Initializing Shift Registers
Initializing the shift register resets the value the shift register passes to the first iteration of the loop when the VI runs. Initialize a shift register by wiring a control or constant to the shift register terminal on the left side of the loop. If you do not initialize the shift register, the loop uses the value written to the shift register when the loop last executed or the default value for the data type if the loop has never executed.
Use a loop with an uninitialized shift register to run a VI repeatedly so that each time the VI runs, the initial output of the shift register is the last value from the previous execution. Use an uninitialized shift register to preserve state information between subsequent executions of a VI. After the loop executes, the last value stored in the shift register remains at the right terminal. If you wire the right terminal outside the loop, the wire transfers the last value stored in the shift register. You can add more than one shift register to a loop.
If you have multiple operations that use previous iteration values within a loop, use multiple shift registers to store the data values from those different processes in the structure as shown in Figure 4.13. The For Loop executes five times. The shift register value is added with the subsequent numbers starting from 1 to 5. After five iterations of the For Loop, the shift register passes the final value 15 to the indicator and the VI stops. Each time you run the VI, the shift register begins with a value of 0. The shift register is used without initialization. The first time you run the VI, the shift register begins with a value of 0, which is the default value for a 32-bit integer. After five iterations of the For Loop, the shift register passes the final value 15 to the indicator and ends. The next time you run the VI, the shift register begins with a value of 15, which was the last value from the previous execution. After five iterations of the For Loop, the shift register passes the final value 30 to the indicator. If you run the VI again, the shift register begins with a value of 30, and so on. Uninitialized shift registers retain the value of the previous iteration until you close the VI.
Stacked Shift Registers
Stacked shift registers let you access data from previous loop iterations. Stacked shift registers remember values from multiple previous iterations and carry those values to the next iterations. This technique is useful for averaging data points. To create a stacked shift register, right-click the left terminal and select Add Element from the shortcut menu.
Stacked shift registers, as shown in Figure 4.14, can only occur on the left side of the loop, because the right terminal only transfers the data generated from the current iteration to the next iteration. If you add one more element to the left terminal, the value from the last two iterations carry over to the next iteration, with the most recent iteration value stored in the top shift register. The second terminal stores the data passed to it from the previous iteration. You can add more than two terminals in the shift register. If you add two more elements to the left terminal, the second terminal stores the data passed to it from the previous iteration and the bottom terminal stores the data passed to it from two iterations ago.
Replacing Tunnels with Shift Registers
Tunnels can be replaced with shift registers wherever necessary. To replace a tunnel into a shift register, right-click the tunnel and select Replace with Shift Register. If no tunnel exists on the loop border opposite of the tunnel you right-clicked, LabVIEW automatically creates a pair of shift register terminals. If one or more than one tunnel exists on the loop border opposite of the tunnel you right-clicked, the mouse pointer will turn to the symbol of a shift register. You can choose the particular tunnel which is to be converted as the shift register by clicking the mouse over that tunnel as shown in Figure 4.15.
Replacing Shift Registers with Tunnels
Replace shift registers with tunnels when you no longer need to transfer values from one loop iteration to the next. To replace a shift register with a tunnel, right-click the shift register and select Replace with Tunnels. If you replace an output shift register terminal with a tunnel on a For Loop, the wire to any node outside the loop breaks because the For Loop enables auto-indexing by default as shown in Figure 4.16. Right click the auto-indexed tunnel and select Disable Indexing on the tunnel to correct the broken wire. This problem does not occur in While Loops because auto-indexing is disabled by default in While Loops.
When the output of a node is connected directly to the input, the feedback node is generated automatically. The feedback node shown in Figure 4.17 appears automatically in a For Loop or While Loop if we wire the output of a node or group of nodes to the input of that node or group of nodes. Like a shift register, the feedback node stores data when the loop completes an iteration, sends that value to the next iteration of the loop, and transfers any data type. Use the feedback node to avoid unnecessarily long wires in loops. The feedback node arrow indicates the direction in which the data flows along the wire. The arrow automatically changes direction if the direction of data flow changes.
You also can select the Feedback Node on the Structures palette and place it inside a For Loop or While Loop. If you place the feedback node on the wire before you branch the wire that connects the data to the tunnel as in Figure 4.18, the feedback node passes each value to the tunnel. If you place the feedback node on the wire after you branch the wire that connects data to the tunnel, the feedback node passes each value back to the input of the VI or function and then passes the last value to the tunnel.
Initializing a Feedback Node
Initializing a feedback node as shown in Figure 4.19 resets the initial value that the feedback node passes for the first time the loop executes when the VI runs. If you do not initialize the feedback node, the feedback node passes the last value written to the node or the default value for the data type if the loop has never executed. If you leave the input of the initializer terminal unwired, each time the VI runs, the initial input of the feedback node is the last value from the previous execution.
To initialize a feedback node, right-click the Feedback Node and select Initializer Terminal from the shortcut menu and add wire a value from outside the loop to the initializer terminal. When you select the Feedback Node on the Functions palette or if you convert an initialized shift register to a feedback node, the loop appears with an initializer terminal.
To replace a feedback node with shift registers, right-click the Feedback Node and select Replace with Shift Register from the shortcut menu.
To replace shift registers with a feedback node, right-click the Shift Registers and select Replace with Feedback Node from the shortcut menu.