The MPLAB Device Blocks for Simulink includes a sophisticated scheduler that enables efficient execution of multi-rate Simulink models on resource-constrained microcontrollers. This guide explains the scheduler concepts, configuration options, and best practices for optimal performance.
Simulink models can be designed with blocks executing at the same rate (single-rate) or at different rates (multi-rate):
| Model Type | Description | Example | Use Case |
|---|---|---|---|
| Single-Rate | All blocks execute at the same sample time | All blocks at 1ms | Simple control loops, data acquisition |
| Multi-Rate | Blocks execute at different sample times | 1ms, 10ms, 100ms tasks | Motor control (fast current loop, slow speed loop, UI) |
For multi-rate models, the MPLAB Device Blocks toolbox offers two execution modes:
In single-tasking mode, all tasks execute sequentially in a single loop. Fast tasks and slow tasks are called in order without preemption.

Figure 1: Single-tasking execution on a 70 MIPS dsPIC33. Tasks execute sequentially: fast 1ms task, then slow 10ms task when scheduled. No preemption occurs.
Advantages:
Disadvantages:

⚠️ Overload Scenario - Single-Tasking On slower chips or with computationally intensive algorithms, single-tasking can cause missed deadlines and timing violations:
Figure 2: Single-tasking overload on a 20 MIPS dsPIC33. The slow task (10ms) execution time extends beyond the 1ms base rate period, causing the fast task to miss its deadline. This results in timing violations and incorrect control behavior.
Consequences of overload:
In multi-tasking mode, tasks execute with priorities based on their execution rate. The scheduler implements the Rate-Monotonic Scheduling algorithm, where higher-rate tasks have higher priority.

Figure 3: Rate-Monotonic Scheduler operation. Key principle: Higher rate tasks (faster execution frequency) have higher priority and can preempt slower tasks.
How it works:

Figure 4: Multi-tasking execution on a 70 MIPS dsPIC33. The fast 1ms task (high priority) preempts the slow 10ms task (low priority) to meet its deadline. After completion, the 10ms task resumes execution.
Advantages:
Disadvantages:

💡 Overload Prevention with Multi-Tasking Multi-tasking mode prevents the overload scenario seen in single-tasking mode:
Figure 5: Multi-tasking on a 20 MIPS dsPIC33. Even on this slower chip, the fast 1ms task executes on time by preempting the slow 10ms task. The system remains stable and all deadlines are met.
Result: Fast control loops maintain correct execution rate, ensuring system stability.
The Rate-Monotonic Scheduling (RMS) algorithm is a mathematically proven optimal fixed-priority scheduling algorithm for periodic tasks.
Tasks are assigned priorities inversely proportional to their period:
| Task | Period (Sample Time) | Rate (Frequency) | Priority |
|---|---|---|---|
| Fast Control Loop | 1 ms | 1000 Hz | Highest (1) |
| Speed Loop | 10 ms | 100 Hz | Medium (2) |
| UI Update | 100 ms | 10 Hz | Low (3) |
| Background Task | 1000 ms | 1 Hz | Lowest (4) |
The RMS algorithm guarantees that all tasks will meet their deadlines if the following condition is met:
U = Σ(Ci / Ti) ≤ n(2^(1/n) - 1)
Where:
U = Total CPU utilization
Ci = Execution time of task i
Ti = Period of task i
n = Number of tasks
For 3 tasks: U ≤ 0.78 (78% CPU utilization)
For ∞ tasks: U ≤ 0.69 (69% CPU utilization - conservative limit)
| Scenario | Single-Tasking | Multi-Tasking | Recommendation |
|---|---|---|---|
| Simple single-rate control loop | ✅ Yes | Optional | Single-tasking for simplicity |
| Motor control (fast current + slow speed) | ⚠️ Maybe | ✅ Yes | Multi-tasking recommended |
| Slow chip (20-40 MIPS) | ❌ No | ✅ Required | Multi-tasking required |
| Fast chip (70+ MIPS) with margin | ✅ OK | ✅ Better | Either works, multi-tasking safer |
| Tight timing requirements | ❌ Risky | ✅ Yes | Multi-tasking for determinism |
| CPU utilization > 50% | ⚠️ Caution | ✅ Yes | Multi-tasking to prevent overload |
The scheduler mode is configured in the MCHP Master block:
1. Double-click the MCHP_Master block in your model
2. Navigate to the "Scheduler Options" tab
3. Locate the "Solver mode" parameter
``****````
| Option | Description | When to Use |
|---|---|---|
| MultiTasking | Rate-monotonic scheduler with preemption | Default and recommended |
| SingleTasking | Sequential execution without preemption | Single-rate models or when preemption is not desired |
| Auto | Automatically selects based on model rates | Let toolbox choose optimal mode |
After building your model, check the generated code to verify the scheduler configuration:
/* In [ModelName].c */
/* Multi-tasking mode: Task functions with priorities */
void Task_1ms(void) /* Priority 1 (highest) */
void Task_10ms(void) /* Priority 2 */
void Task_100ms(void) /* Priority 3 */
/* Scheduler interrupt service routine */
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void)
{
/* Check which tasks need to execute */
if (counter_1ms >= 1) {
counter_1ms = 0;
Task_1ms(); /* Execute highest priority task */
}
if (counter_10ms >= 10) {
counter_10ms = 0;
Task_10ms(); /* May be preempted by Task_1ms */
}
}
The MPLAB Device Blocks toolbox provides several blocks to monitor scheduler performance:
The MCU Load block measures actual CPU utilization:
Usage:
1. Add MCHP_MCU_Load block to your model
2. Connect output to scope or data logging
3. Monitor during testing to ensure load < 80%
The Tasks State block provides detailed task execution information:
See the timing diagrams in Figures 3-5 above - these were captured using the Tasks State block with an oscilloscope.
The MCU Overload block detects timing violations:
| Task Priority | Maximum Execution Time | Guideline |
|---|---|---|
| Highest (fastest rate) | <50% of period | 1ms task should execute in <500μs |
| Medium | <70% of period | 10ms task should execute in <7ms |
| Lowest (background) | <90% of period | 100ms task can use up to 90ms |
Avoid shared resources between tasks of different priorities. If necessary, use:
Possible causes:
Solutions:
In single-tasking mode:
Solution: Switch to multi-tasking mode to enable preemption
In multi-tasking mode:
Solution: Optimize fast task or increase base rate period
If multi-tasking overhead is significant (rare, typically <1%):