Cornell University
Electrical Engineering 476
Program Organization for AVR microcontrollers

Introduction

There are many ways to organize realtime programs. By organization, I mean the scheme you use to coordinate the interaction of concurrent program sections (e.g. interrupt service routines) , as well as the usual hierarchical structure of each individual program section. The way you organize a program will depend on may things including:

This document will show some of the ways or organizing your code for the AVR platform and try to address some of the tradeoffs of the various approaches.

Typical programs for desktop systems are not realtime. You write a chunk of code which executes according to the logic you construct and outside events (e.g. mouse input) are under your control. Typical programs for microcontrollers have to consider outside events, as well as timing, and typically operate with at least one, and often several, interrupt sevice routines (ISRs) able to jerk control away from the main code at any time. Many of the most difficult bugs to fix in a design arise from the unintended interaction between programs which are interrupting each others execution.

Approaches to Organization

Here is a list of some organization techniques with their advantages and disadvantages. A simple example is used to illustrate three styles. The example: (1) blinks LED 0 at 2 Hz unless button 0 is pressed. (2) blinks LED 1 at 1 Hz. (3) detects the button press and switches LED 0 to 8 Hz.

You can use any interrupt-driven style you wish in this course, but for the first few lab exercises, I suggest a time-scheduled programming organization. As the projects demand more speed, you may find that a combination of time-scheduled (for the slow stuff) and foreground/background techniques (for the fast stuff) works. See, for example, the next section.

An Example with two ISRs

Another example shows how to handle slow events (button presses) and relatively fast events (audio synthesis). The example program will play a musical scale when any button is pressed.

The example is coded in the three different styles shown above.


Copyright Cornell University Feb 2005