Detailed explanation of the difference and usage of FC and FB

Many beginners often ask, "What's the difference between FC and FB? How should I use them?" Actually, it's quite straightforward. By carefully reading through the following explanation, you'll be able to understand it easily!

Let's start with the FC block. You can think of it as a basic function block that performs specific tasks. Now, what about FB? The term "FB" stands for Function Block, which is essentially an FC combined with a DB (Data Block). This means that an FB not only has the functionality of an FC but also includes a background DB block. The full name of FC is "Function," while DB stands for "Data Block," similar to a table in a database, where data is stored and accessed.

To better understand how functions work, imagine a function f(x) as a black box. You input a value x, and it gives you a unique output f(x). For example, if you want to calculate the area of a circle, you use the formula s = π * r². Here, r is the independent variable, and s is the dependent variable. It’s a simple yet effective way to represent how functions operate.

Detailed explanation of the difference and usage of FC and FB

Now let's take a look at the variable declarations inside an FC block. There are several types of parameters: Input, Output, InOut, Temp, Constant, and Return. Input parameters act like external variables, and any changes made inside the function do not affect the original value. Output and InOut parameters, on the other hand, directly reference the external variable address, allowing modifications to the original value. Temp variables are local to the function and are used for intermediate calculations. Constants are fixed values that cannot be changed once declared. The Return parameter is used to return a result from the function, though it's optional.

For example, consider a constant like π, which is used multiple times in a program. If you hardcode 3.1415 everywhere, changing it later would be tedious. Instead, declaring π as a constant allows you to update it in one place, and all references will automatically reflect the change. However, in TIA Portal, constants cannot be modified during runtime, so they must be set at declaration.

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

When you drag an FC into your main OB1 block, you'll see the results of the function execution. For instance, if you're converting an analog signal, the function might take an input value, process it, and store the result in an Output or InOut parameter. This approach helps reduce memory usage and keeps the program cleaner.

Detailed explanation of the difference and usage of FC and FB

Figure 3: Program running results

Figure 4: DB block value monitoring

Now, let's move on to the FB block. Unlike FC, an FB is more complex because it includes a Data Block (DB) that stores its internal state. This means that when an FB is called, it retains its previous values even after the function completes. This is especially useful for operations like timers, where the current state needs to be preserved across scan cycles.

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

The key differences between FC and FB include the presence of a static area and retentive variables in FB. These features allow the FB to maintain its state between calls, making it ideal for applications where data persistence is required. Additionally, FB does not have a Return interface, as the Output parameter can be used to pass results back to the caller.

Figure 7: Timer DB background block structure

As you can see, the timer uses a background DB block to store its current value, ensuring that it doesn't reset every time the PLC scans. Similarly, an FB maintains its internal state, making it perfect for real-time applications such as monitoring voltage levels or tracking changes over time.

Figure 8: FB program additions

In summary, FC is a simple function without a persistent data storage, while FB includes a DB block to retain its state. This makes FB more suitable for complex, stateful operations, whereas FC is ideal for simple, one-time calculations. Understanding the difference between these two blocks is essential for efficient PLC programming.

Portable Mobile Power

Portable Mobile Power,Lithium Battery Tester,Rechargeable Portable Lantern,Fast Charging Ups Power

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

Posted on