TechTip: Using GETBIT in a Macro on Weintek HMI

Occasionally, you may need to read a bit or bits from a PLC or controller register if they are not directly accessible via the driver being used – an example of this could be a PLC status word where each bit represents the status of various PLC conditions, for example, “Battery Status” or “CPU Error” etc. Or it may be that you want to replicate some PLC logic that is otherwise inaccessible. The GETBIT macro instruction is very useful in these circumstances, enabling you to retrieve an individual bit and use that in conjunction with further logic such as an “if then else” routine.

The GETBIT syntax is:
GETBIT(source, result, bit_pos)

“source” is the source word(short or int), “result” is the boolean (bool) result and “bit_pos” is the bit position (for 16-bit words, this is 00-15, for 32bit, this is 00-31)

Example: get the status of a bit in “bit_pos” in LW 1 from “source” HMI local word LW-0 and set HMI local bit LB-0 with the “result” value:

macro_command main()

short word_that_we_want_bit_from = 0 // word we're reading is 16-bit
short bit_position = 0 	// bit position is 16bit with value 00 - 15
bool bit_result = 0 // boolean output

GetData(word_that_we_want_bit_from, "Local HMI", LW, 0, 1)	// Get LW 0
GetData(bit_position, "Local HMI", LW, 1, 1) 				// Get value for bit_position from LW 1
															// 00 - 15 for 16bit word

GETBIT(word_that_we_want_bit_from, bit_result, bit_position)// get the value of the bit in "bit_position" in LW 0 
															// note - bit position can also be a constant 

SetData(bit_result, "Local HMI", LB, 0, 1) 	// set LB 0 with the value of the bit in bit_position 
											// from LW 0 

end macro_command
Demo program checking status of bit 7 of the source word – status “0”
Demo program checking status of bit 7 of the source word – status “1”

As usual, we have a demo program for you to download here. This can be run in “Offline Simulation” so it can be tested without a PLC or physical HMI.