Inhalte

Common Practices: Control/Status Words

Table of Contents

Introduction

Many devices have control and status words. A common practice is to intercept or set the bits by masking: controlWord := (controlWord OR MASK_ACK_ERRORS); or isErrorPending := (statusWord AND MASK_ERROR_IS_PENDING);.
This method of working with the bits works quite well, but is complicated and difficult to read. Especially if you look at the raw data of e.g. controlWord. Therefore, it is mandatory that we use unions for this.

Example

For the example, I take a Siemens G120 frequency converter (shame on me, I use a Siemens device, I know), I only list the control word because with that, it should be clear how it works.

First, we create a data structure for the individual bits. Here it is important that the data type BIT is used because with BOOL it does not work.

Type struct SiemensG120ControlWordBits
TYPE SiemensG120ControlWordBits :
STRUCT
	on									:BIT;
	noOff2								:BIT;
	noOff3								:BIT;
	enableOperation						:BIT;
	enableRampFunctionGenerator			:BIT;
	continueRampFunctionGenerator		:BIT;
	setPointEnable						:BIT;
	acknowledgeFaults					:BIT;
	spare10								:BIT;
	spare11								:BIT;
	controlViaPlc						:BIT;
	directionNotReversal				:BIT;
	spare14								:BIT;
	motorPotentiometerIncrementSetpoint	:BIT;
	motorPotentiometerDecrementSetpoint	:BIT;
	cdsSelection						:BIT;
END_STRUCT
END_TYPE

Next we create the union, in my GSDML file for the Siemens G120 is the control word an UINT.

Union SiemensG120ControlWord
TYPE SiemensG120ControlWord :
UNION
	rawData			:UINT;
	signals			:SiemensG120ControlWordBits;
END_UNION
END_TYPE

The Siemens G120 class, I know there is an interface, but it’s just an empty interface and an empty class to show it.

Class SiemensG120
FUNCTION_BLOCK SiemensG120 IMPLEMENTS IHateSiemens
VAR
	AO_CtrlWord		AT %Q* :SiemensG120ControlWord;
END_VAR

Main

Main Task
PROGRAM MAIN
VAR
	fc01	:SiemensG120;
END_VAR

IO Mapping

Mapping

Set a few bits online for fun

Online view

Summary

It would also work if only our bit data structure is mapped, but only if the matching type is omitted from the mapping. Then you don't have the possibility for word operation like an off method THIS^.AO_CtrlWord.rawData := 0;.