Efficient Design With Microcontrollers (Part 3)

Efficient software is easy-to-understand software that runs faster and requires less code.

Software Development Strategies

  1. Top-Down Development This is an approach to programming which relates to understandability and the use of functions. Essentially, it is an approach that starts with the broad overview (the “top”) and then moves (“down”) to the details. Using such an approach, you first write the main program (the main() function in C). In doing so, you make liberal use of functions that you have yet to write and assume that later on you will write them and that they will perform in specified ways. For example, if your final application needs a printing device, during development call a (at that moment non-existent) printf() function with a pointer to the desired message. If you need an A-D reading, during early development assume some function will exist to return the reading when called. Once you’ve written the main program, the “down” of “top-down” consists of writing the first level of functions. These in turn may use other functions, which you will also write later. Finally, you write the detailed drivers—the most basic functions. (If you would like to see an example of this 3-level programming, start with Figure 21.1 on page 236 and then follow the code in the rest of that chapter.)
  2. Bottom-Up Development This approach looks good at first glance, but most hardware-oriented projects have details requiring practical experience. As you begin developing projects, it may be better to use a bottom-up approach to software—start by developing the most basic drivers. First, write a function to initialize the display and show a single character. Then write a canned message program. Then write functions to accept varying messages. Then write fixed and scrolling message functions. Then perhaps write messages with constant text and variable values intermixed. Finally, go on with the rest of the project!
  3. Ends-In Development The best solution may be an ends-in approach that maintains an overall perspective while developing the most basic details. That avoids the “Where do I go from here?” sense that comes from the bottom-up approach as well as the “You mean the hardware can’t do it?” problem of the top-down approach. From any direction, there is no substitute for experience in this phase of a project.

Software Development Helps

Easy-to-understand Listings An efficient program includes an easy-to-understand program listing. You include enough commentsmeaningful comments that do not just echo the information directly available from the instructions—so the next programmers (or you several months later) can easily pick up what you did. They can see how the code piece fits the bigger picture and why you programmed in the given way. To help this, you choose variable names that make their purposes apparent. Long blocks of code, even if used only once, should be broken into several individual functions so the user can comprehend the purpose of a given function without having to look over several pages of listing. This method of programming is the opposite of the earliest programming (now almost impossible except in assembly) in which a long string of instructions could involve criss-crossed jumps to unpredictable places up and down the instruction list.

Use Functions Efficient software uses functions rather than straight-line programming. That way, code can be re-used, and individual program functions are in clearly delineated pieces. The process of getting a number out to a display is separated from the calculations that produces the number.

Use Tables Seriously evaluate how much precision you really need. Efficient software will often use tables and interpolation rather than floating-point calculations. Otherwise it chooses pre-simplified calculations and the smallest possible (unsigned integer) variables so code is faster and smaller with fewer math operations.

Use Multitasking & Interrupts Any efficient software will be multitasking oriented. In brief, multitasking software is able to flit from one job to another as needed. It isn’t stuck in the middle of a long calculation where it is unavailable to process a more urgent input from a user or external hardware.

[these programming techniques are discussed in detail in the book from which this is taken, C and the 8051]

Related posts: