1 / 14

Embedded Programming in C

Embedded Programming in C. Originated by: Jered Aasheim Tom Cornelius Modified by: Bobby Davis Presented January 19, 2006. Infinite Program Loop.

tinahoward
Download Presentation

Embedded Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Embedded Programming in C Originated by: Jered Aasheim Tom Cornelius Modified by: Bobby Davis Presented January 19, 2006

  2. Infinite Program Loop Embedded systems are usually meant to always execute the same program for the duration of the time the device is on. This can be done using a “forever loop”: void main(void) { … for(;;) // you can also use a while(1) loop { // main program loop } }

  3. Why Use C? • C allows the programming low-level access to the machine architecture. • Compiled C programs are memory efficient and can execute very fast. • Higher level language constructs allow designs to be more complicated, reduce the number of bugs, and help get more done per unit of time.

  4. Data Types Note: C does not support the “boolean” data type. Instead, use chars or ints and #define TRUE=1 and FALSE=0.

  5. Number Representation In C, numbers can be expressed in decimal, hexadecimal, or octal:

  6. Boolean Logical Operators • These should be familiar from Java • “True” is any non-zero value • “False” is zero • Ex: 4&&-1 == 1, !(-67) == 0 • Equality operators (“==”, “>=”, etc.) also work the same as in Java

  7. Bit Operators

  8. Reading/Writing Individual Bits The bitwise AND and OR operators can be used to read/write individual bits in a variable: Ex. unsigned char reg = 0xB5; if(reg & 0x08) // Is the 4th bit a “1”? … else // 4th bit is not a “1” … reg = reg | 0x02; // Set the 2nd bit to a “1” reg = reg & 0xFE; // Set the 1st bit to a “0”

  9. Scope There are 3 ways to declare a variable – global, local, static: ex. unsigned char inputStatus; // global ex. void f(void) { int sum; … } // local ex. void g(void) { static char ttlLine; … } // static The static keyword means that the variable will retain its value across function calls; there are situations where this is very useful for retaining state in a subroutine/function.

  10. Preprocessor • The preprocessor responds to preprocessor directives • Output of preprocessor is then compiled • These directives include all commands beginning with the “#” sign, as well as code comments

  11. Preprocessor Commands • #define • This will create constants for your code • The preprocessor will replace all instances of the constant with its numerical value before compiling • Use #define’s LIBERALLY in your code (but define them at the top of your files!) • Ex: #define TRUE 1 // true defined as 1 void function() { int gus = TRUE // gus is 1 }

  12. Preprocessor Commands (2) • #if and #ifdef • These will conditionally compile your code • Can be useful for debugging/testing • Ex: • #define DEBUG • #define TEST 0 • void function() { • #ifdef DEBUG • … // this code will be compiled • #endif • #if TEST • … // this code will not be compiled • #endif • }

  13. Preprocessor Commands (3) • #include • This copies the contents of a header file wherever a #include resides in the code • Conventionally, #include’s should be used only at the top of each source code file • Ex: • #include <dos.h> • #include “myHeader.h” • void whateverFcn() { • … • }

  14. Header Files • Header files will often take on the name of the source code file with which they are most readily associated (ex: lab1.h and lab1.c) • Header files contain #define’s, type definitions, function prototypes, and external variable declarations…but NO executable code! • Standard library headers are #include’d with encompassing <> brackets around the file name, while programmer-defined files use “” quotations (see last slide example)

More Related