The basics of embedded multitasking on a PIC, part 3In the last of a three-part series, you'll see one way to create preemptive task scheduling on a PIC microchip.
In the last article, I presented a working project of four concurrent display tasks that shared embedded system resources and switched a PIC18F452 MCU equally and willingly among them. I called this kind of a system a cooperative multitasking. Shrinking the tasks' program into a single reentrant program was another accomplishment of that project that helps in fitting complex tasks into the limited size of embedded memory.
In this article, we'll look at the next level of sophistication with my second project as an example. Here, we force tasks to suspend their execution upon periodic system interrupts. We call such an embedded system preemptive. I built this project on macros and work areas used in the previous project. This project demonstrates how modularity and reusability of embedded designs can provide added-value to your projects.
Preemptive task scheduling
This project builds on the cooperative task scheduling project discussed in the previous article. I recommend reading this article first as it will help you understand the changes required to move from cooperative to preemptive multitasking to achieve the policy differences. Preemption does not rely on tasks giving up the PIC control to the scheduler as cooperation did. Instead, the scheduler uses internal or external interrupts to restore system control from user tasks. The project is composed of a group of assembler code and work areas declarations in Microchip's MPLAB IDE (Integrated Development Environment) shown in Figure 1.
Figure 1: Project files window.