KL6821 with CX5120
This sample explains how to write a simple PLC program for DALI in TwinCAT and how to link it with the hardware.
A single lamp is to be controlled and switched to the maximum output value or switched off with a push button.
Sample: DALI_Sample_KL6821_CX5120.zip
![]() | 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
- 1x CX5120 Embedded PC
- 1x KL1104 digital 4-channel input terminal (for the switch-on/switch-off function)
- 1x KL6821 DALI terminal
- 1x KL9010 end terminal
Set up the hardware and the DALI components as described in the documentation.
This sample assumes that an On button was connected to the first KL1104 input and an Off button to the second. There is a lamp at DALI address 0.
Software
Creation of the PLC program
Create a new TwinCAT XAE Project and a Standard PLC Project. Add the Tc3_DALI library in the PLC project under References. Generate a global variable list with the name GVL_DALI and create the following variables:
VAR_GLOBAL
bSwitchOn AT %I* : BOOL;
bSwitchOff AT %I* : BOOL;
stKL6821InData AT %I* : ST_KL6821InData;
stKL6821OutData AT %Q* : ST_KL6821OutData;
END_VAR
bSwitchOn: Input variable for the On button.
bSwitchOff: Input variable for the Off button.
stKL6821InData: Input variable for the DALI terminal (see ST_KL6821InData).
stKL6821OutData: Output variable for the DALI terminal (see ST_KL6821OutData).
Create a program (CFC) for the background communication with the DALI terminal. The function block FB_KL6821Communication is called in the program. In the communication block ensure that the structures stInData and stOutData are linked.

Create a MAIN program (CFC) in which the FB_DALI102RecallMaxLevel and FB_DALI102Off function blocks are declared as follows.
PROGRAM MAIN
VAR
fb102RecallMaxLevel : FB_DALI102RecallMaxLevel(Communication.fbKL6821Communication);
fb102Off : FB_DALI102Off(Communication.fbKL6821Communication);
END_VAR
The communication block is specified in the round brackets after the declaration. The reference to the desired DALI terminal is defined via this specification.
For more information see: Transfer of the reference to the communication block.
Call the two instances of the function blocks FB_DALI102RecallMaxLevel and FB_DALI102Off with the following variables.
The input bStart of the function block for switching on a lamp with the maximum output value is linked to the global variable bSwitchOn.
The input bStart of the function block for switching off a lamp is linked to the global variable bSwitchOff.

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 6 ms.

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.

Add the program for the communication to this task. Further information on task configuration can be found in the description of the function block FB_KL6821Communication.

I/O configuration
Select the CX as target system and initiate a search for its hardware.
In the instance (DALI_Sample_KL6821_CX5120 Instance) of the PLC project, you can see that the input and output variables of the PLC program are assigned to the corresponding tasks (PlcCommunication and PlcTask).

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.