Download presentation
Presentation is loading. Please wait.
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 Type Size (bits) Dynamic Range unsigned char 8 0…255
-128…127 unsigned int 16 0…65,535 signed int -32,678…32,677 unsigned long 32 0…4,294,967,295 signed long -2,147,483,647… 2,147,483,647 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: Decimal 2 63 83 Octal 00 02 077 0123 Hexadecimal 0x0 0x2 0x3F 0x53
6
Boolean Logical Operators
C Notation AND && OR || NOT ! 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 Operator C Notation Example AND & 0x08 & 0xF0 // 0x00 OR
| 0x08 | 0xF // 0xF8 Left shift << 0x08 << // 0x10 Right shift >> 0x08 >> // 0x01 XOR ^ 0xAA ^ 0xFF // 0x55 One’s complement ~ ~0x //0xFF
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 }
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)
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.