Detailed explanation of the difference and usage of FC and FB

Many beginners often ask, what's the difference between FC and FB, and how should they be used? Actually, it's quite simple—just take a few minutes to read through this explanation carefully!

First, let’s clarify the terms. You can think of FB as a special type of FC that includes a DB (Data Block) background. In other words, FB = FC + DB. The full name of FC is "Function," while DB stands for "Data Block." A Data Block is similar to a database table, storing variables that can be accessed by the function or block.

Now, let’s take a step back and understand what a function really is. A function f(x) is like a black box: you input a value x, and it gives you a unique output f(x). Here, x is the independent variable, and f(x) is the dependent variable. For example, if we want to calculate the area of a circle, the formula is s = π * r². When you provide the radius r, the function returns the area s. This is the basic concept of a function.

Detailed explanation of the difference and usage of FC and FB

In programming, especially in PLCs like TIA Portal, functions (FCs) have different types of parameters. Let’s break them down:

- Input: These are values passed into the function. They are copies of external variables, so changing them inside the function won’t affect the original value outside.

- Output: These parameters allow the function to modify an external variable directly by referencing its address.

- InOut: These act as both input and output. Like Output, they can change the value of external variables.

- Temp: Local variables used only within the function. They don’t have an external interface and must be initialized before use.

- Constant: A fixed value that cannot be changed during runtime. It’s useful for values like π, which are used multiple times in a program.

- Return: Some functions return a single value. If no return is specified, it's considered a procedure instead of a function.

Detailed explanation of the difference and usage of FC and FB

Figure 1: Variable declaration

Detailed explanation of the difference and usage of FC and FB

Figure 2: FC function content

Let’s look at an example. Suppose we have a function that calculates the area of a circle using a constant π. If we hardcode 3.1415 in multiple places, any future changes would require updating each instance. Instead, by declaring π as a constant, we can update it in one place, and all references will automatically reflect the new value.

Another important point is that while both Return and Output can store results, there’s a key difference: Output can have multiple parameters, but Return is limited to a single value. Also, the Return value’s name cannot be changed, unlike Output parameters.

Detailed explanation of the difference and usage of FC and FB

Figure 3: Program running results

Figure 4: DB block value monitoring

When working with FCs, the variables declared in the Input, Output, and InOut interfaces are temporary. Once the function finishes execution, those values are lost. However, with FBs, the DB block retains the data, allowing the function to maintain state between calls. This is especially useful in scenarios where you need to track information over time, such as maximum voltage readings or timestamps.

Detailed explanation of the difference and usage of FC and FB

Figure 5: FB block variable declaration

Detailed explanation of the difference and usage of FC and FB

Figure 6: FB occupies DB background block internal variables

FBs differ from FCs in three main ways: they include a static area for persistent data, they have retentive and accessible variables, and they do not have a Return interface. This means that after an FB call ends, the values stored in the DB block remain intact, unlike FCs, where all internal variables are reset once the function completes.

Figure 7: Timer DB background block structure

The concept of retaining state is similar to how timers work in PLCs. Each timer has a background DB that stores its current value, even after the scan cycle ends. This is exactly what makes FBs powerful—they can keep track of data across multiple function calls.

Figure 8: FB program additions

In the HMI program, you can easily access the variables stored in the DB block of an FB, such as the maximum voltage and the time it was recorded. This makes real-time monitoring much more efficient.

You might wonder, “Can I just create an FC and add a DB manually?” While this is possible for a single instance, it becomes impractical when dealing with multiple similar operations, such as comparing voltage, current, and temperature. Using FBs allows you to manage these scenarios efficiently, as each FB instance has its own DB block, making it easier to handle multiple devices or processes simultaneously.

Rechargeable Battery

Rechargeable Battery,Solar Battery System,Solar System Battery,Portable Battery Bank

Sichuan Liwang New Energy Technology Co. , https://www.myliwang.com

Posted on