11.07.2015 Views

Imagecraft c compiler and development environment for the atmel avr

Imagecraft c compiler and development environment for the atmel avr

Imagecraft c compiler and development environment for the atmel avr

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ICCV8 <strong>for</strong> AVR – C Compiler <strong>for</strong> Atmel AVRThere are two observations about this process. First, <strong>the</strong> C preprocessor is separatefrom <strong>the</strong> <strong>compiler</strong> proper <strong>and</strong> does textual processing only. There are caveats about#define macros that arise from this. For example, in <strong>the</strong> macro definition, it isadvisable that you put paren<strong>the</strong>ses around <strong>the</strong> macro arguments to preventunintended results:#define mul1(a, b) a * b // bad practice#define mul2(a, b) ((a) * (b)) // good practicemul1(i + j, k);mul2(i + j, k);mul1 produces an unexpected result <strong>for</strong> <strong>the</strong> arguments, whereas mul2 produces <strong>the</strong>correct result (of course, it is not a good idea to #define simple operations such assingle multiplication, but that is ano<strong>the</strong>r subject). Second, C files are translated intoassembler files <strong>and</strong> are <strong>the</strong>n processed by <strong>the</strong> assembler. In fact, C is sometimescalled a high-level assembler, since <strong>the</strong> amount of translation between C <strong>and</strong>assembly is relatively small, compared to <strong>the</strong> more complex languages such as C++,Java, FORTRAN, etc.Source Code Structures; Header Files etc.Your program must contain a function called main. It is a good practice to partitionyour program into separate source files, each one containing functionally relatedfunctions <strong>and</strong> data. In addition, when <strong>the</strong> program is more modular in structure, it isfaster to rebuild a project that has multiple smaller files ra<strong>the</strong>r than one big file. Using<strong>the</strong> CodeBlocks IDE, add each file into <strong>the</strong> Project. Note that if you #include multiplesource files in a main file <strong>and</strong> only add <strong>the</strong> main file in <strong>the</strong> project manager, <strong>the</strong>neffectively you still have just one main file in your project <strong>and</strong> will not be getting <strong>the</strong>benefits stated above.You should put public function prototypes into public header files that are #includeby o<strong>the</strong>r files. Private functions should be declared with <strong>the</strong> static keyword <strong>and</strong> <strong>the</strong>function prototypes should be declared ei<strong>the</strong>r in a private header file or at <strong>the</strong> top of<strong>the</strong> source file where <strong>the</strong>y appear. Public header files should also contain any globalvariable declarations.Recall that a global variable should be defined in only one place but can be declaredin multiple places. A common practice is to put a conditional declaration such as <strong>the</strong>following in a header file:(header.h)#ifndef EXTERN#define EXTERN extern72

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!