User`s manual

54 digi.com Multitasking with Dynamic C
State machines can become quite complicated, involving a large number of state variables and a large
number of states. The advantage of the state machine is that it avoids busy waiting, which is waiting in a
loop until a condition is satisfied. In this way, one big loop can service a large number of state machines,
each performing its own task, and no one is busy waiting.
The cooperative multitasking language extensions added to Dynamic C use the big loop and state machine
concept, but C code is used to implement the state machine rather than C variables. The state of a task is
remembered by a statement pointer that records the place where execution of the block of statements has
been paused to wait for an event.
To multitask using Dynamic C language extensions, most application programs will have some flavor of
this simple structure:
5.2 A Real-Time Problem
The following sequence of events is common in real-time programming.
Start:
1. Wait for a pushbutton to be pressed.
2. Turn on the first device.
3. Wait 60 seconds.
4. Turn on the second device.
5. Wait 60 seconds.
6. Turn off both devices.
7. Go back to the start.
The most rudimentary way to perform this function is to idle (“busy wait”) in a tight loop at each of the
steps where waiting is specified. But most of the computer time will used waiting for the task, leaving no
execution time for other tasks.
main() {
int i;
while(1) { // endless loop for multitasking framework
costate { // task 1
. . . // body of costatement
}
costate { // task 2
... // body of costatement
}
}
}