B&R automation studio c programming

Published on May26, 2023 | Category: counter
Share this article:

C programming language developed by Dennis Ritchie at Bell Labs. It is generally used for develop a software applications, operating system, device drivers etc. now a days this programming language also used for develop a plc program. B&R plc support ANSI C language for develop a plc program. ANSI C is standardized version of the C programming language developed by the American National Standards Institute (ANSI).It is also known as C89 or ISO C. latest version of ANSI C is C11.ANSI C allows users to call function blocks and access variables from other IEC languages. This standardized version of C language support function prototypes, void pointers, and library functions, string and memory allocation. C language have low-level memory manipulation capabilities and ability to interface with hardware. C language, including variable declarations, function definitions, and library functions.

Add C language program in B&R automation Project

Open your B&R Automation Studio and create a new project. If you don’t have a physical PLC, use PC-based simulation to make a program. During project setup, select PC-based simulation. To write a program in C for a PLC in B&R Automation Studio, in the menu bar select Insert then Add Object, or right-click on Logical View then select Add Object.

In the Add Object dialog, select Program then choose New Program or Existing Program, then click Next.

img/br-automation-studio/add-object-in-the-br-automation-studio.webp

Enter the name of the program and local data type name, add a description, and click Next.

enter name of program and local data type

Enter the initialization part of the program, select language as ANSI C from the dropdown menu, then click Next.

language as ansi c in program

Select an option to assign the object to a CPU (if required), then click Finish.

select an option if you want to assign object to cpu

The new program appears in the Logical View and has the following files:

br automation c program files

B&R Automation C Program Layout

Open your inserted C program. Below is the Cyclic Program of B&R Automation. A B&R Automation C program has a predefined layout starting with a file header section, followed by the main program section.

br automation c program layout

File Header Section – C Program

This section contains information about the source code file like copyright, module name, file name, author, date, description, and history. This section is purely informational and does not execute during runtime. You can modify it to include revision history, company name, author email, etc.

file header section c program

Main Program Section – C Program

This section contains the executable statements. ANSI C programs start with preprocessor directives, which begin with # and are used to include header files or define macros. The main() function follows. We will discuss preprocessor directives in more detail in another article.

main program section of c program

B&R Automation Studio – C Programming Example

The image below shows a simple C program to check if the entered value is even or odd. The % operator is used to find the remainder. If the remainder is 0, the number is even. The variable enter_value is of integer type, while even and odd are boolean types.

br automation studio c programing example

B&R Automation Studio – Timer Program in C

br automation studio timer program in c

This example demonstrates an On-Delay Timer in a C program. First, declare the instance of the timer. You can declare it in the program file or in the variable file. Here, it is declared in the variable file. Variables start_timer and timer_output are of BOOL data type. The preset and elapsed time are of TIME data type. Download the program into a controller or simulator, set the preset time, and activate start_timer to test the output.