A FIFO, or First In, First Out, is a great way of managing an array of values. By loading new data at the front and shuffling all current data up by 1 element, data “Flows” through the array until the most recently added falls off the end.
🎯What’s In This Post?
➡️FIFO Function Block
This example is a FIFO block that I have made myself, and offers the following functionality:
- Enqueue Data – Add data to element 0 and shuffle all other data up by 1 index
- Dequeue Data – Remove the oldest element added to the FIFO
- Flush Data – Flush all data back to the
No_Data_Value
(Initialization value) - First and Last values in the FIFO
- Number of elements in use (Max 100)
- Real Data Type
⚙️How The FIFO Works
The FIFO relies upon an internal array (Array[0..99] of Real
) to keep track of data, with additional functionality wrapped around the array.
⚡Initialization
The data type being used is REAL
, and the default data for a REAL
data type is 0.0
. We may want to change this though, as 0.0
may be a valid value in our FIFO.
In order to provide this functionality, an Initialization
section of code is provided:
This IF Statement
executes the first time the block is scanned. The FILL
command sets every element in the FIFO
array to the No_Data_Value
. It then sets the Initialized
value to True
so that the initialization event cannot occur again.
The No_Data_Value
can be set on the interface of the FIFO, otherwise the default is -1000000000.0
↘️Enqueuing New Data Into The FIFO
By using the Enqueue
input, new data from the Value
input is loaded into the FIFO[0]
whilst all elements are shuffled up by n+1
, where n
is the FIFO element index.
When the ORE[0].Q
value (which is a rising edge from the Enqueue
input) is True
, the MOVE_BLK
shifts all elements starting at element 0
into element 1
.
Once this is complete, FIFO[0]
is updated with the Value
input.
↗️Dequeue Data From FIFO
Dequeuing data from the FIFO removes the oldest value (first in, last in the array) from the FIFO array
This is a little more complicated than the Enqueue
method because we need to find the last value that isn’t the No_Data_Value
.
This is done by running the loop backwards from element 99
to 0
until the first value that isn’t the No_Data_Value
has been found.
We then set the value to the No_Data_Value
, effectively removing it from the FIFO Array.
There’s also a check to see if the current index is 0
. If it is not, then the Last_Value
output is updated with i-1
, where i
is the current index in the loop. If the index is 0
, then we just update Last_Value
with FIFO[0]
as there is no lower element.
⬇️Flush Data From FIFO
We can also flush all data from the FIFO and set everything back to the No_Data_Value
The Fill
instruction simply fills the entire FIFO
array with the No_Data_Value
. The Fill_Result
is not used but must be present for the instruction to be valid.
The Flush is initiated from the Flush
input on the interface of the block
➕Additional Data
The block is also equipped with 3 outputs that state the First_Value
, Last_Value
and total number of Elements_Used
. These can be used to interact with the FIFO for logic purposes in your project.
🔎Example Of Usage – Average FIFO
This example is Enqueuing data into the FIFO every 0.5 seconds. The FIFO Array is then inspected to create an average of the contents
1️⃣ Sample Timer
The Sample-Timer
simply sets the Sample_DN
to True
every 0.5 seconds for 1 scan. This is then used to set a Sample_Data
value to a maximum of 15.0
2️⃣FIFO
The FIFO object’s Enqueue
input is set to True
when the Sample_DN
is True
. This means the a new value is added to the FIFO every 0.5 seconds
3️⃣Average Function
The Average function (written in SCL in a network) adds all of the used elements together, then divides by the number of elements used.
Remember that the Array starts at 0, where as the Elements_Used
counts the number of elements in use, so it needs to be subtracted by 1
in order to index the array correctly.
✅End Result
The end result of these 3 components is an average function that is capable of averaging used elements in the FIFO. When the FIFO becomes full, the oldest value in index location 99
falls off the end and is lost.
Watch the below video and you can see this all in action
📁Example Project Download
🔶TIA Portal V17 Example Project
Disclaimer – This project and any code within it is to be used at your own risk. Liambee.me will not accept any responsibility for issues that arise from the usage of this code. I suggest you ensure it is tested correctly prior to usage within any environment where damage could occur.
The code supplied is for training purposes only
🚫Don’t Have TIA Portal V17?
Here’s two PDF print-outs of the project that will help recreate this functionality in any environment
🔷FIFO Block PDF
🔷FIFO Average Example PDF
Check Out Another Post
Knowledge Base: Analog Scaling
In industrial automation projects, all analog devices, including those connected via Analog Input Cards, necessitate scaling to convert raw input…
How could we use MOVE_BLK on S7300, the triggers too?
I think they are available for S7-300