80 likes | 168 Views
Embedded Programming in C. Presented by: Jered Aasheim Tom Cornelius January 11, 2000. 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.
E N D
Embedded Programming in C Presented by: Jered Aasheim Tom Cornelius January 11, 2000
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.
Number Representation In C, numbers can be expressed in decimal, hexadecimal, or octal:
Bit Manipulation <<, >> - shifts value k bits to the left or right ex. 0x08 >> 3 // 0x01 & - bitwise ANDs two operands together ex. 0x08 & 0xF0 // 0x00 | - bitwise ORs two operands together ex. 0x08 | 0xF0 // 0xF8 ^ - bitwise XORs two operands together ex. 0xAA ^ 0xFF // 0x55
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”
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.
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(;;) { // main program loop } }