Summary
The article describes implementing a simple Round-Robin multitasking system on an Atmega32 AVR microcontroller, effectively creating a basic real-time operating system (RTOS) demo. It handles 7 independent tasks, each assigned around 300 bytes of RAM for stack space. A timer interrupt triggers context switching, saving CPU registers and status registers for the current task and restoring those of the next, using a stack pointer backup table stored in reserved RAM. This approach enables quasi-parallel execution of tasks, demonstrated by individual LEDs representing each task toggling independently.Parts used in the Round-Robin Multitasking on AVR project:
- Atmega32 microcontroller
- LEDs (7 units for task indication)
- Timer for generating interrupts
- RAM (internal 2KB)
- Programming environment for AVR (e.g., AVR Studio, GCC AVR)
Introduction:
Scheduling algorithm used: Round-robin (RR)
(one of the simplest scheduling algorithm)
Working:
Now, when the task1 is interrupted by the switching timer interrupt, as said above, it push all CPU registers and the status register. Then it checks the ‘task index’ which will be there on the RAMEND. From the task index, we get the task number and thus we can obtain the exact location on the stack backup table on which the stack pointer value is to be stored. Also we can get the next task’s stack pointer value relative to the location where we stored the current one and that is loaded to the stack pointer.
So, we got the stack pointer of the next task. Now, we can pop the status register and all the cpu registers and finally the RETI instruction will enable the global interrupt and then divert (jump to new PC value) the cpu to resume the next task…. This repeats with a time slot in range of micro seconds and thus we feel all the tasks are running in parallel and also purely independent even though they are using the same cpu registers in common.
Here are some pictures which I drawn to make the above explanation more clear. It represent the RAM usage for the stack pointer backup table and the process stack.
For more detail: Multitasking in AVR (A demo to run 7 tasks on an atmega32)