Executing multiple tasks at different time intervals
While the great majority of embedded systems are required to run only one pro- gram, they do need to run multiple tasks (implemented as ‘C’ functions in this book): these tasks must, as mentioned earlier, run on a periodic or one-shot basis. These tasks will typically have different durations and will run at different time intervals. For example, we might need to read the input from an ADC every millisec- ond, read one or more switches every 200 milliseconds and update an LCD display every 3 milliseconds.
We can try to run more than one task by extending the technique discussed in Section 13.5. For example, suppose that we have a microcontroller device with (say) three timers available and wanted to use these timers to control the execution of three tasks, by using a separate interrupt service routine to perform each task (Listing 13.5).
Provided we have sufficient timers available, this approach will generally work. However, it would breach some basic software design guidelines.
For example, in Listing 13.5, we have three different timers to manage and – if we had 100 tasks – we would require 100 timers. This would make system maintenance very difficult; for example, 100 changes would be required if we changed the oscilla- tor frequency. It would also be difficult to extend; for example, how can we add another task if there are no further hardware timers available?
In addition to contravening one of the most basic software design guidelines, there is a more specific problem with Listing 13.5. This arises in situations where more than one interrupt occurs simultaneously. As we saw in Chapter 1, having more than one active interrupt in a system can result in unpredictable – and hence, unreliable – pat- terns of behaviour.
Looking back at Listing 13.5 we can see that there will inevitably be occasions when more than one interrupt is generated at the same time. Dealing with this situation is not impossible, but it would add greatly to the complexity of the application.
Overall, as we will see in the next section, use of a scheduler provides a much cleaner solution.