Chapter 7
Understanding Discrete I/O
7-2
Single Terminal
you need to shift the word value and AND a 1 to mask off the other bits.
Remember to specify terminal numbers in decimal, or use a leading zero if
you’re specifying terminal numbers in octal.
Example: to obtain the value of terminal 12 octal (10 decimal) from link
address 1, group 5, either of these statements works:
bit = input_image_table[4*1+5]>>012 & 1; or
bit = (input_image_table[4*1+5]>>10) & 1;
The first statement as coded depends on the C language’s operator priority:
shifts are done before ANDs. You may feel more comfortable adding
parentheses to emphasize the order of operations, as we did in the second
statement.
If you’re simply going to use a terminal value in an if test, you can shift it
as we did above, or you can use a one bit mask like this:
if ( input_image_table[4*1+5] & (1<<012) ) . . .
This doesn’t look much better than the previous construct, but if you define
symbolic constants (as we recommend) the code becomes clearer:
#define SENS_RACK 1
#define SENS_GRP 5
#define SENS_BIT 012
#define SENS_MASK (1<<SENS_BIT)
. . .
if ( input_image_table [8*SESENS_GRP] & SENS_MASK ) . . .
you simply OR a 1 bit in the appropriate position within the output image
table word. Here’s an example, for terminal 3 of link address 0, group 7:
input_image_table[4*0+7] |=1<<3;
The next time the scanner scans that rack, the new value is transferred to
the scanner’s output image table.
you have to AND a mask that contains 1 bits everywhere except in the bit
position of the terminal to be cleared. The C language operator ~ is made
for this kind of operation:
input_image_table[4*0+7] &= ~(1<<3);
To access a single input terminal,
To set a single output terminal,
To reset (clear) a terminal is a
little trickier;