Adding a timer to the super-loop
As mentioned, one of the super-loop’s downsides is that the iterations’ execution time can vary or be uncertain. For a super-loop iteration, how long the iteration takes depends on how long each function takes in the iteration. A slight modification is often made to the super-loop so that each iteration takes the same amount of time, and the iterations start at predictable time intervals:
// Start the super-loop's repeating timer
start_timer();
while(1)
{
func1();
func2();
func3();
// Poll the timer until its current period expires
while( !timer_expired_flag );
}
Much of this super-loop looks just like the previous super-loop. However, after the functions have been executed, there is an inner while
loop that waits for a timer to expire before allowing the functions to execute again. Upon the timer’s expiration, a timer interrupt is generated, and the timer’s ISR is called. The ISR sets a...