User`s manual

Dynamic C Users Manual digi.com 69
5.7 Timing Considerations
In most instances, costatements and cofunctions are grouped as periodically executed tasks. They can be
part of a real-time task, which executes every n milliseconds as shown below using costatements.
Figure 5-6 Costatement as Part of Real-Time Task
If all goes well, the first costatement will be executed at the periodic rate. The second costatement will,
however, be delayed by the first costatement. The third will be delayed by the second, and so on. The fre-
quency of the routine and the time it takes to execute comprise the granularity of the routine.
If the routine executes every 25 milliseconds and the entire group of costatements executes in 5 to 10 mil-
liseconds, then the granularity is 30 to 35 milliseconds. Therefore, the delay between the occurrence of a
waitfor event and the statement following the waitfor can be as much as the granularity, 30 to 35
ms. The routine may also be interrupted by higher priority tasks or interrupt routines, increasing the varia-
tion in delay.
The consequences of such variations in the time between steps depends on the program’s objective. Sup-
pose that the typical delay between an event and the controllers response to the event is 25 ms, but under
unusual circumstances the delay may reach 50 ms. An occasional slow response may have no conse-
quences whatsoever. If a delay is added between the steps of a process where the time scale is measured in
seconds, then the result may be a very slight reduction in throughput.
If there is a delay between sensing a defective product on a moving belt and activating the reject solenoid
that pushes the object into the reject bin, the delay could be serious. If a critical delay cannot exceed 40
ms, then a system will sometimes fail if its worst-case delay is 50 ms.
5.7.1 waitfor Accuracy Limits
If an idle loop is used to implement a delay, the processor continues to execute statements almost immedi-
ately (within nanoseconds) after the delay has expired. In other words, idle loops give precise delays. Such
precision cannot be achieved with waitfor delays.
A particular application may not need very precise delay timing. Suppose the application requires a 60-
second delay with only 100 ms of delay accuracy; that is, an actual delay of 60.1 seconds is considered
acceptable. Then, if the processor guarantees to check the delay every 50 ms, the delay would be at most
60.05 seconds, and the accuracy requirement is satisfied.