Technical Article

PLC Program Commands: Storing Data Using Arrays

September 08, 2022 by Jon Peterson

Learn how to move data through arrays using LFL, LFU, FFL, and FFU instructions, as well as a technique on cycling data through an array by putting it in the front and letting it drop out the back using CPS instructions.

See our previous articles covering PLC programming commands:

Timers

One-Shots

Up and Down Counters

Math Instructions

Trigonometry Instructions

Comparison Instructions

Move and Copy Instructions

Arrays and Pointers


 

In this article, we will cover a few different strategies for moving data through arrays in Allen-Bradley’s Studio 5000 and RSLogix 5000 Logic Designers. The first four sections will address pre-defined instructions that are used for loading and unloading data from arrays in particular orders. These instructions are the LFL, LFU, FFL, and FFU. The fifth section will demonstrate how to cycle data through an array so that it flows in the front and drops out the back using CPS instructions.

 

FIFO vs LIFO

FIFO is the acronym for First In, First Out. For this method, the first piece of data you put in is the first one to be removed. You can think of this like putting pieces of paper into a file. The first one you put in is at the front and you set the next piece of data behind it. When you remove a sheet, you take the one in the front—the first one you originally put in.

LIFO is an acronym for Last In, First Out. In this case, the last piece of data you put in is the first piece of data to be pulled out. This can be compared to setting pieces of paper onto a pile. The last one you put on is on top and this it becomes the first one you remove as you pull the top paper off.
 

The LFL Instruction - Last In, First Out Load

The LIFO system comes in two instructions. The first instruction loads data into the array and the second instruction pulls data out. To set up the load Instruction, the LFL, we need source data to put into the array, a LIFO array to manipulate, a control tag, and a length. The final entry is the position and is provided by the control tag— though it can be manually manipulated if needed.

In the logic in Figure 1, you can see how I set my source as whatever the current count is on my timer. My array is an array of 20 DINTs. My control tag has a ‘control tag’ in it that I created for this and my length is set to 20 (the size of my array). The instruction has a built-in one-shot, so you can drive it with a single bit like I have at the beginning of the rung.

 

20 DINT array with built-in one-shot instruction

Figure 1. An array of 20 DINTs with a built-in one-shot instruction.

 

When you place data into the array with an LFL instruction, it places the data in the next entry as determined by the control tag. The control tag monitors the current position in the array that the LFL instruction is working with and allows the instruction to advance properly as data is added. 

I added three pieces of data and took screenshots of each instance in the tag monitor. You can see the images all at once in Figure 2. On the left, you can see the number 513 was the last one added to the array. The control bit sees that the last entry was in position number [4], so it places the next number in the middle picture (the number 46) into position number [5]. Once the 46 is placed in position [5], the control tag advances another position and places the next entry (The number 131) into [6]. This is how the loading of a LIFO array is accomplished.

 

Tag monitors showing how the loading of a LIFO array works

Figure 2. Tag monitors showing how the loading of a LIFO array works.

 

LFU - Last In, First Out Unload

Now that we’ve loaded our array, let’s talk about unloading it. This is done by another instruction in another rung. Let’s go through the instruction components first. The LIFO is the array we are working with—I put in the same array we worked with in Figure 2 so we can remove the data we put in. Next, we need a destination. Every time we unload from the array, we need somewhere to put the data. Because it is an array of DINTs, I have put a DINT tag in the destination. 

 

DON’T FORGET THIS STEP!

You must use the same control tag for the unload as you did for the load. If you don’t, the instruction will not know where it is or where the last piece of data was that you put in. I used Control[0] in my loader so I have to also use Control[0] in my unloader. This control bit will fill in our length and position from when we defined those above. Figure 3 is an example of what the unloader looks like in logic.


look at unloader in logic

Figure 3. A look at the unloader in logic. 

 

Figure 4 is another table showing our array. You might remember that the last piece of data we loaded into our array was the number 131 in position [6]. When we hit our unload instruction, this being a LIFO, the last number we put in will be the first one to come out. As you can see below, the number 131 is now unloaded from position [6]. If you hit the instruction two more times, position [5] and then position [4] are unloaded into the destination tag.

 

illustration of using the FIFO unload instruction

Figure 4. A further illustration of using the FIFO unload instruction.

 

FFL - First In, First Out Load

Our next pair of instructions are the Load and Unload for a First In, First Out (FIFO) array. Just like above, we have the Source, FIFO Array, Control, and Length. Once those are set as before, we are ready to load data!

 

set source, FIFO Array, control, and length to load data

Figure 5. Set the Source, FIFO Array, Control, and Length so that you can load data.

 

This system of loading is exactly the same as above, so we won’t spend too much time on it. Send a rising edge bit contact to the instruction, and it stores data in the next spot in the array as directed by the control tag. We have added a few pieces of data as shown in Figure 6 so that we can demonstrate the unloading process.

 

load a FIFO array like a LIFO array

Figure 6. Loading a FIFO array follows the same process as loading a LIFO array.

 

FFU - First In, First Out Unload

The unload instruction is virtually identical in setup to the LFU instruction we set up above: FIFO Array, Dest, Control, Length, and Position.

 

FFU and LFU unload instructions

Figure 7. The FFU and LFU unload instructions have the same setup.

 

Now here’s where the change happens. As described in its title, the FIFO instruction removes the first piece of data that was put in. When we removed the last piece of data before, we simply pulled the last number out of the array. This time, we need to remove position [0] and shift the entire array upward to fill. 

In Figure 8, you can see how I removed three different numbers from the array. First, we remove 495 from position [0] and advance 160 up into it, moving the whole array up behind it. Next, we remove 160 and shift the array up behind it. We do it a final time to remove the 692 and shift everything up again to have the 197 in position to be removed.

 

remove position [0] and shift entire array upward

Figure 8. Remove position [0] and shift the entire array upward.

 

Moving Data Sequentially Through an Array

This last section is not going to cover an instruction, but more of a technique that utilizes two instructions. The method below allows you to flow data through an array, putting it in the front and letting it drop out the back after it cycles through. I learned this technique from a brilliant engineer I worked under several years ago in the sawmill industry.

The first thing we do is move data into position [0] of the advancing array. When the trigger is hit, a one shot allows the controller to scan through the two CPS instructions. The first CPS instruction moves the data forward one space into position [1] of the tracking array where your data is stored. 

 

first step to technique is to move data to position [0]

Figure 9. The first step to this technichnique is to move data into position [0].

 

In Figure 10, you will see how the data flows through the TrackingArray. When we trigger the rung, we see everything in the array advance forward one space with new data moved in. The number 959 moves from position [1] to position [2] and the number 549 is moved into position [1]. This way, you can push data through the array from one end to the other.

 

how data flows through the TrackingArray

Figure 10. A look at how the data flows through the TrackingArray.

 

LIFO, FIFO, and FL Instructions

I hope you found the instructions and technique helpful in your work with arrays! The three different styles, LIFO, FIFO, and Flow-Through are used in a multitude of fields and industries. Learn how to use these instructions and they will serve you well!

 

Featured image used courtesy of Canva

 

If you enjoyed this article, check out the rest of the PLC programming article sequence:

Timers

One-Shots

Up and Down Counters

Math Instructions

Trigonometry Instructions

Comparison Instructions

Move and Copy Instructions

Arrays and Pointers