Multitasking via scheduler

A scheduler may seem too simple and too efficient to qualify as a multitasking operating system. You write all the code, there are no operating system commands to learn, and you can achieve full real-time performance. In one sense a scheduler just makes good use of a real-time clock. Yet the concept is so powerful it rates its own chapter [this post is from chapter13].

What is a scheduler?

Chapter 11 Hardware: Interrupts & Timers went into detail on the production of a regular interrupt—a real-time clock running tasks at specific times is a scheduler. It is an arrangement of software that calls task functions after a specific number of system ticks. Suppose you have a system tick every millisecond. You might have a keyscan task running every 40th tick and a stepper motor pulse task being called every 10th tick (100 steps/S). If the step rate changed, the scheduler might call the step function every 20 ticks (for a 50steps/S rate). All the activities key off counters initialized for a desired delay and then decremented every tick interval. When the counter for a specific task reaches zero, the count is reset, and the given task function is called.

Since scheduler tasks have to fit in between system ticks, you should write the code of any task to execute as quickly as possible and return. Never write while() or for(;;) loops in a task if exiting the loop depends on an outside event. Likewise, if there is a lot of processing to do, set a flag in the scheduler interrupt. Have the main function poll and detect that flag and then handle the processing in the background. Continue reading

Want to buy 3rd edition?

I find I have a box of the 3rd edition (the purple one) still kicking around. It is the version you can download for free courtesy of the publisher who illegally gave permission to Google books…but then they didn’t pay royalties either, so why should I be suprised. If you have an interest email me at Shipping costs plus $5 would satisfy me.

Five Kinds of Functions

Programmers are always looking for shortcuts. If you find the same code in several places, you think, “I shouldn’t have to write this code over and over.” That is where functions come into play. A function might produce steps to drive a stepper motor or convert a binary number to a displayable form. In the main program a function substitutes a descriptive name for a set of program details, making the main program more streamlined and understandable. A function can be called from multiple places so the code of a function can be used over and over. (In other languages you may find a function named a subroutine or a procedure.)

Here are the basics of passing in and returning values for functions in the C programming language:

1: Nothing In, Nothing Out

unsigned char x,y;
void plusfive(void){
void main(void){

In its simplest form a function takes nothing in and returns nothing. You call it by writing its name (without the word call, unlike assembly and some other languages). The program above shows a simple function named plusfive. When called, it takes the value in the global variable y, adds 5 to it, and stores the result in the public variable x. In the next three examples I show the same function in more complicated forms, but here the function works directly on specific external variables.

 Void Function: one that does not return a value

Void This clearly indicates that there is no value returned and no parameters passed in. ANSI C demands this. Void was added to the C language decades ago so the compiler could check whether the programmer was using the function correctly. Earliest versions of C, by default, assumed an integer was returned and would not check what types of parameters were being passed. Continue reading