Chapter 7
Understanding Discrete I/O
7-3
Programming Hint
We suggest that you define constants for the addresses of your I/O modules.
This makes your programs easier to understand, and helps you in debugging.
For example:
#define ALARM_ADDRESS 5
#define ALARM_GRP 0
#define ALARM_TERM 017
. . .
output_image_table [4*ALARM_ALARM_GRP] |= 1<<ALARM_TERM;
In this example, we defined the link address and group of the module that
controls an alarm, and the specific terminal (note the leading zero for octal
notation). To turn on an output terminal, we shifted a 1 bit left by the
terminal number and ORed that into the output image table.
By the way, you may be wondering about efficiency. Most C compilers do
all arithmetic on constants at compile time. So at run time there would be
no difference in execution speed between the above statement and
output_image_table[20] |= 0x8000;
but the first way is certainly easier to understand and maintain.
It’s important to bear in mind that when you update your output image
table, there is not an immediate effect on the outside world. (Though we
talk about output in this section, the same timing applies to input; it takes
as long for a new input value to find its way from the input module to your
program as it does for an output value to follow the reverse path.)
Your program updates the output image table. Sometime later, the scanner
comes to the adapter associated with the changed data and sends the new
information to the adapter.
This sequence of events could take as long as an entire pass through the
scan list. This worst-case event happens when you update an output just
after the scanner has refreshed that adapter. So, it may take one cycle until
the scanner services the adapter and sends the changed output.
How Long is a Cycle?
Multiply 11 milliseconds (7 ms at 115.2K baud) by the number of adapters
in the scan list. Your program has no way of knowing where the scanner is
in the scan list; so, if timing is critical, you must assume that outputs might
not get out to the real world for as long as one complete scan list if each
adapter occurs only once. You can improve the situation by putting a
critical adapter at two points in the scan list; see the scan list command in
chapter 4, “Issuing Scanner Management Commands.”
Timing of Discrete I/O