| Getting Started with M68K Ada: Ada 95 Compilation System for the Motorola M68000 Family | ||
|---|---|---|
| Prev | Chapter 3. Real-Time Programs | Next |
The general form of a periodic task is given in the following example. You should note that tasks and protected objects must be declared in a library package, and not in a subprogram.
The task's three scheduling parameters are declared as constants, giving the example task a frequency of 100 Hz, and a phase lag of 3 milliseconds, and a priority of 3. You will have computed these parameters by hand, or using a commercial scheduling tool.
Example 3-3. A Periodic Task
T0 : constant Time := Clock;
-- Gets set at elaboration time and used by all periodic tasks
Task1_Priority : constant System.Priority := 3;
Task1_Period : constant Time_Span := To_Time_Span (0.010);
Task1_Offset : constant Time_Span := To_Time_Span (0.003);
task Task1 is
pragma Priority (Task1_Priority);
end Task1;
task body Task1 is
Next_Time : Time := T0 + Task1_Offset;
begin
loop
Next_Time := Next_Time + Task1_Period;
delay until Next_Time;
-- Do something
end loop;
end Task1;
The task must have an outer loop the runs for ever. The periodic running of the task is controlled by the delay statement, which gives the task a time slot defined by Offset, Period, and the execution time of the rest of the body.
The value of Task1_Period should be a whole number of microseconds, otherwise, through the accumulation of rounding errors, you may experience a gradual change in phase that may invalidate the scheduling analysis you did earlier.