KL6811 Integration into TwinCAT (CX9020)
This example explains how to write a simple PLC program for DALI in TwinCAT and how to link it with the hardware. The task is to control an individual dimmable lamp via a button.
Hardware
Setting up the components
The following hardware is required:
- 1x Embedded PC CX9020
- 1x digital 2-channel input terminal KL1002 (for the dimming and reset functions)
- 1 DALI terminal KL6811
- 1x end terminal KL9010
Set up the hardware and the DALI components as described in the associated documentation.
This example assumes that a Dim button was connected to the first KL1002 input and a Reset button to the second, and that a dimmable lamp is connected to DALI address 0. Set the initial fade rate of the ballast to 7 to achieve suitable dimming.
Software
Creation of the PLC program
Create a new PLC project for PC-based systems (ARM) and add the TcDALIV2.lib library.
Next, generate the following global variables:
VAR_GLOBAL
bSwitch AT %I* : BOOL;
bReset AT %I* : BOOL;
stDALIInData AT %I* : ST_DALIV2InData;
stDALIOutData AT %Q* : ST_DALIV2OutData;
stCommandBuffer : ST_DALIV2CommandBuffer;
END_VAR
bSwitch: Input variable for the Dim button.
bReset: Input variable for the Reset button.
stDALIInData: Input variable for the DALI terminal.
stDALIOutData: Output variable for the DALI terminal.
stCommandBuffer : Required for the communication with DALI.
Then create a program (CFC) for background communication with DALI. The FB_DALIV2Communication() block is called in this program. Make sure to link the communication block with stDALIInData, stDALIOutData and stCommandBuffer.

Create a MAIN program (CFC) in which the block FB_DALIV2Dimmer1Switch() is called up. Connect the input bSwitchDimm of the dimmer block with the global variable bSwitch and stCommandBuffer with the global variable stCommandBuffer.

![]() | Parameter Make sure that the specified parameters nMinlevelMasterDevice and nMaxLevelMasterDevice match the minimum and maximum values of the device, in order to avoid malfunction. |
Under Task Configuration create a new task for the background communication. Add the communication program to this task. Assign a higher priority (lower number) and shorter interval time to this task than for the standard task. More detailed information can be found in the FB_DALIV2Communication() block description.

Load the project to the CX as the boot project and save it.
Configuration in the System Manager
Create a new TwinCAT System Manager project, select the CX as the target system, and search for the associated hardware.
Add the PLC program created above under PLC configuration. The two tasks are listed when the PLC project is expanded in the tree view. Expand the tasks – all global input and output variables should be allocated to the standard task. However, since the variables stDALIInData and stDALIOutData are to be processed faster, move them to the background communication task via drag & drop.

Now link the global variables of the PLC program with the Bus Terminal inputs and outputs, create the allocations, and activate the configuration. Then start the device in run mode.
Your CX is now ready for use.
The lamp can now be controlled by pressing or holding the dimmer button. Use the Reset button to reset the entries in arrBufferMaximumDemandMeter and arrBufferOverflowCounter.