KL6831 with CX5120

This sample describes how to write a simple PLC program for SMI in TwinCAT and how to link it with the hardware.

A motor is controlled stepwise with a button. One button sends the Up command, the other the Down command.

Sample: SMI_Sample_KL6831_CX5120.zip

KL6831 with CX5120 1:

The TwinCAT project is available for download as *.zip file. This must first be unpacked locally so that the archive (*.tnzip file) is available for import into the TwinCAT project.

Hardware

Setting up the components

Set up the hardware and the SMI components as described in the documentation.

The sample assumes that a Reset button has been connected to the first input of the KL1104, an Up button to the second input and a Down button to the third input. There is a drive at the SMI device address 1.

Software

Creation of the PLC program

Create a new "TwinCAT XAE Project" and a "Standard PLC Project".

Add the library Tc2_SMI in the PLC project under "References".

Create the following global variables:

VAR_GLOBAL
    bReset              AT %I* : BOOL;
    bUp                 AT %I* : BOOL;
    bDown               AT %I* : BOOL;
    stKL6831InData      AT %I* : ST_KL6831KL6841InData;
    stKL6831OutData     AT %Q* : ST_KL6831KL6841OutData;
    stCommandBuffer            : ST_SMICommandBuffer;
END_VAR

Name

Type

Description

bReset

BOOL

Input variable for the Reset button

bUp

BOOL

Input variable for the Up button

bDown

BOOL

Input variable for the Down button

stKL6831InData

ST_KL6831KL6841InData

Input variable for the SMI terminal

stKL6831OutData

ST_KL6831KL6841OutData

Output variable for the SMI terminal

stCommandBuffer

ST_SMICommandBuffer

Required for communication with SMI

Create a program (CFC) for the background communication with SMI. The function block FB_KL6831KL6841Communication is called in the program. With the communication block, ensure that the structures stKL6381InData and stKL6831OutData and stCommandBuffer are linked.

KL6831 with CX5120 2:

Create a MAIN program (CFC) in which the function blocks FB_SMIUpStep and FB_SMIDownStep are called.

The input bStart of the function block for sending the Up command is linked to the global variable bUp.

The input bStart of the function block for sending the Down command is linked to the global variable bDown.

KL6831 with CX5120 3:

Navigate to the task configuration section and configure the PlcTask. By way of example, the task is assigned priority 16 and a cycle time of 10 ms.

KL6831 with CX5120 4:

Create a further task for the background communication. Assign a higher priority (smaller number) and a lower interval time to this task than the PlcTask.

KL6831 with CX5120 5:

Add the program for the communication to this task. More precise information on the task configuration can be found in the function block description.

KL6831 with CX5120 6:

I/O configuration

Select the CX as target system and initiate a search for its hardware. In the project instance within the PLC section, you can see that the input and output variables are assigned to the corresponding tasks.

KL6831 with CX5120 7:

Now link the global variables of PLC program with the inputs and outputs of the bus terminals. Create the Solution and enable the configuration.

The lamp with the maximum brightness value is switched on by pressing the first push button. The second push button can be used to switch it off again.

You can use the Reset button to reset the inputs in arrBufferMaximumDemandMeter and arrBufferOverflowCounter.