B&R Automation Studio Limit Instruction example in ANSI C

Published on Jun02, 2023 | Category: C Programming
Share this article:

Limit Instruction in a PLC limit input value to specified maximum and minimum range. we can LIMIT instructions in ladder. in ANSI we create a limit function because ANSI Don\'t have limit instructions. so i am creating a simple limit instructions example. we can discuss more what is function in ANSI. we create a limit function with the help of conditional statement (if else). limit function that limits a value between a minimum and maximum range so first we create a Limit function.

Create a Limit Function in ANSI C

In the example above, a custom limit function is defined in ANSI C to restrict a value within a specified range. The function takes three input parameters:

The logic inside the function uses simple conditional (if-else) statements. If the input value is below the minimum threshold, it returns the minimum value. If the input exceeds the maximum, it returns the maximum value. Otherwise, the original input value is returned. This ensures the result always stays within a safe and predictable range.

Call Limit Function in the Main Program

img/limit-function-in-c/call-limit-function-in-a-main-program.webp

Once the limit function is created, it can be easily called from within the main() program. You can pass either variables or constant values to the function. In this example, real (floating-point) numbers are used. Suppose the minimum range is set to 1.0 and the maximum is 12.30:

This method is useful in various automation and control systems to avoid unsafe values or overflow issues in calculations.

Limit Function with Constant Values

The beauty of this limit function lies in its reusability. It can be called multiple times in different contexts. In the second example, the function is used with constant values directly passed as arguments:

Here, 12.0 is the minimum range, 98.0 is the maximum limit, and 13.0 is the input value. Since 13.0 lies between the specified limits, the function simply returns 13.0. If the input were 100.0, it would return 98.0 instead.
This simple yet powerful function is a practical solution in embedded systems, PLCs, and real-time control environments where bounded values are crucial for stability and safety.