DOC PREVIEW
UNCC ECGR 4101 - Efficient C for 8-Bit Processors f-jones

This preview shows page 1-2-3-4 out of 11 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

etting the best possible performance out ofan eight-bit microcontroller C compilerisn’t always easy. This article concentratesmainly on those microcontrollers that werenever designed to support high-level lan-guages, such as the 8051 family, the 6800family (including the 68HCll), and the PIC line of micro-controllers. Newer eight-bit machines such as the Philips8051XA and the Atmel Atmega series were designed explic-itly to support HLLs, and as such, may not need all the tech-niques I describe here. My emphasis is not on algorithm design, nor does itdepend on a specific microprocessor or compiler. Rather, Idescribe general techniques that are widely applicable. Inmany cases, these techniques work on larger machines,although you may decide that the trade-offs involved aren’tworthwhile.Before jumping into the meat of the article, let’s brieflydigress with a discussion of the philosophy involved. Themicrocontrollers I mentioned are popular for reasons ofsize, price, power consumption, peripheral mix, and so on.Notice that “ease of programming” is conspicuously missingfrom this list. Traditionally, these microcontrollers havebeen programmed in assembly language. In the last fewyears, many vendors have recognized the desire of users toincrease their productivity, and have introduced C compil-ers for these machines—many of which are extremely good.However, it’s important to remember that no matter howgood the compiler, the underlying hardware has severe lim-itations. Thus, to write efficient C for these targets, it’sessential that we be aware of what the compiler can do eas-ily and what requires compiler heroics. In presenting thesetechniques, I have taken the attitude that I wish to solve aproblem by programming a microcontroller, and that the Ccompiler is a tool, no different from an oscilloscope. Inother words, C is a means to an end, and not an end in66 NOVEMBER 1998 Embedded Systems ProgrammingNIGEL JONESfeatureGEfficient C Code forEight-Bit MCUsThe 8051, 68HC11, and PIC are popular MCUs, but they aren’t necessarily easy to program. This article shows how the use of ANSI and compiler-specific constructs can help generate tighter code.RETURNEmbedded Systems Programming NOVEMBER 1998 67itself. As a result, many of my com-ments will seem heretical to the puristsout there. ANSI CThe first step to writing a realistic Cprogram for an eight-bit machine is todispense with the concept of writing100% ANSI code. This concession isnecessary because I don’t believe it’spossible, or even desirable, to write100% ANSI code for any embeddedsystem, particularly for eight-bit sys-tems. Some characteristics of eight-bitsystems that prevent ANSI complianceare:•Embedded systems interact withhardware. ANSI C providesextremely crude tools for address-ing registers at fixed memory loca-tions. Consequently, most compilervendors offer language extensionsto overcome these limitations•All nontrivial systems use inter-rupts. ANSI C doesn’t have a stan-dard way of coding interrupt ser-vice routines•ANSI C has various type promotionrules that are absolute perfor-mance killers to an eight-bitmachine. Unless your system hasabundant CPU cycles, you willquickly learn to defeat the ANSIpromotion rules•Many microcontrollers have multi-ple memory spaces, which have tobe specified in order to correctlyaddress the desired variable. Thus,variable declarations tend to beconsiderably more complex thanon the typical PC application•Many microcontrollers have nohardware support for a C stack.Consequently, some compiler ven-dors dispense with a stack-basedarchitecture, in the process elimi-nating several key features of CThis is not to say that I advocatejunking the entire ANSI standard.Indeed, some of the essential require-ments of the standard, such as func-tion prototyping, are invaluable.Rather, I take the view that one shoulduse standard C as much as possible.However, when it interferes with solv-ing the problem at hand, do not hesi-tate to bypass it. Does this interferewith making code portable andreusable? Absolutely. But portable,reusable code that doesn’t get the jobdone isn’t much use.I’ve also noticed that every compil-er has a switch that strictly enforcesANSI C and disables all compilerextensions. I suspect that this is donepurely so that a vendor can claimANSI compliance, even though thisfeature is practically useless. I havealso observed that vendors who strong-ly emphasize their ANSI complianceoften produce inferior code (perhapsbecause the compiler has a genericfront end that is shared among multi-ple targets) when compared to ven-dors that emphasize their perfor-mance and language extensions.Enough on the ANSI standard—let’s address specific actions that canbe taken to make your code run wellon an eight-bit microcontroller. Themost important, by far, is the choice ofdata types.Data typesKnowledge of the size of the underly-ing data types, together with carefuldata type selection, is essential for writ-ing efficient code on eight-bitmachines. Furthermore, understand-ing how the compiler handles expres-sions involving your data types canmake a considerable difference inyour coding decisions. These topicsare discussed in the following para-graphs.Data type sizeIn the embedded world, knowing theunderlying representation of the vari-ous data types is usually essential. Ihave seen many discussions on thistopic, none of which has been particu-larly satisfactory or portable. My pre-ferred solution is to include a file,<types.h>, an excerpt from whichappears below:#ifndef TYPES_H#define TYPES_H#include <limits.h>/* Assign a built in data type to BOOLEAN. This is compiler specific */#ifdef _C51_typedef bit BOOLEAN#define FALSE 0#define TRUE 1#elsetypedef enum {FALSE=0, TRUE=1}BOOLEAN;#endif/* Assign a built in data type to type CHAR. This is an eight-bit signed variable */#if (SCHAR_MAX == 127)typedef char CHAR;#elif (SCHAR_MAX == 255)/* Implies that by default chars are unsigned */typedef signed char CHAR;#else/* No eight bit data types */#error Warning! Intrinsic data type char is not eight bits!!#endif/* Rest of the file goes here */#endifThe concept is quite simple.Types.h includes the ANSI-requiredfile limits.h. It then explicitly testseach of the predefined data types forthe smallest type that matches signedand unsigned one-, eight-, 16-, and 32-To write efficient C for these targets, it is essential that we be aware of what the compiler can do easily and what


View Full Document

UNCC ECGR 4101 - Efficient C for 8-Bit Processors f-jones

Documents in this Course
Load more
Download Efficient C for 8-Bit Processors f-jones
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Efficient C for 8-Bit Processors f-jones and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Efficient C for 8-Bit Processors f-jones 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?