Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)

Similar presentations


Presentation on theme: "C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)"— Presentation transcript:

1 C Programming for Embedded Systems

2 fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)

3 fig_06_01 From source code to executable program: Compilation: Preprocessing step:

4 fig_06_03 Compiling or assembling:

5 fig_06_04 The entire process:

6 fig_06_05 Example files in the process:

7 fig_06_06 Important features of embedded code: Performance Robustness (response to failures) Ease of change Style—simple, understandable example: flag = x != 0 && ! y/x < 0 how is this evaluated? in embedded systems: use parentheses!!!!!!!

8 C integral types: char short int long (signed or unsigned)

9 fig_06_08 Unsigned integer—range and format

10 fig_06_10 Signed integer (1’s complement)—range & form

11 fig_06_12 Numbers in memory—sign in most sig “nibble”

12 fig_06_14 Character data

13 fig_06_15 Floating point data

14 fig_06_16 Exponent: “excess notation”

15 fig_06_17 Example program:

16 fig_06_18 Variable designations and visibility: scope

17 fig_06_20 Storage classes

18 fig_06_21 Example:

19 fig_06_22 Separate compilations:

20 fig_06_23 Makefiles: example

21 Bitwise Operators Pointers Functions Structs Interrupts (in C)

22 table_07_00 Bitwise operators: useful for dealing with signals of different widths; NOTE: these are LOGICAL operators and variables are declared UNSIGNED— why? (example: homework 1, problem 1) Additional useful reference: http://www.cs.cf.ac.uk/Dave/C/node13.html

23 fig_07_00 Examples of c operations at the byte level

24 fig_07_01 Example of c program—”portShadow” mirrors what is on port; Note use of parentheses to guarantee order of bitlevel operations (even if we are using default order)

25 fig_07_02 Using shift operators (remember these are logical):

26 fig_07_03 Redoing the previous problem with shifts:

27 fig_07_04 Getting data from the port:

28 fig_07_05 Using bitlevel operations for arithmetic; Are there any problems with this approach? C: on signed data, left shift undefined if overflow occurs, right shift is implementation-dependent; Java: all integers are signed

29 fig_07_06 Can use shifts for slightly more complex multiplies and divides; More complex operations (e.g., with 3 1’s in multiplier or divider) are probably not efficient

30 fig_07_07 Pointers: example data

31 fig_07_09 Example instruction:

32 fig_07_10 Example 2 of Instruction execution

33 fig_07_11 Example: what is output?

34 fig_07_12a Pointer arithmetic—not for beginners!

35 fig_07_12b Pointer arithmetic: additional examples

36 fig_07_13 Constant pointers:

37 fig_07_14 Using constants and constant pointers:

38 fig_07_14 Note: pointers can be “generic” or NULL: Generic: Type void: pointer can point to a variable of any type example: 7.3, p. 265 NULL: Pointer needs to be assigned a valid address before dereferencing, otherwise hard-to-find bugs can occur Address 0 is not acceptable Solution: use null pointer address, (void*) 0

39 fig_07_15 C functions:

40 fig_07_16 Example:

41 fig_07_17 Function call: note order of parameters on stack

42 fig_07_18 Using stack to return result: Function body in memory:

43 fig_07_20 Pass by value (default in c):

44 fig_07_21 Pass by reference:

45 fig_07_22 Information hiding: static qualifier prevents visibility outside this file (even to linker):

46 fig_07_23 Function documentation: template for header

47 fig_07_24 We can also define pointers to functions: Dereferencing methods:

48 fig_07_26 Example:

49 fig_07_27 Example 2:

50 fig_07_28 Pointing to add:

51 fig_07_29 Pointing to subtract: pointer does not know functionality, only address

52 fig_07_31 User-defined data structure: struct

53 fig_07_30 User-defined data structure: struct Example:

54 fig_07_34 Can also define a type and use it repeatedly:

55 fig_07_35 Syntax for using struct:

56 fig_07_36 Example: define a rectangle

57 fig_07_37 One struct using another:

58 fig_07_38 C code for this example:

59 fig_07_39 Defining the rectangle type:

60 fig_07_40 Using point and rectangle definitions:

61 fig_07_41 Rectangle functions:

62 fig_07_42 Example program:

63 fig_07_43 Example: passing a struct to a function:

64 fig_07_44 Interrupt service routines: ISR—needs to be SHORT and SIMPLE

65 fig_07_45 How an interrupt occurs: Interrupt may be disabled under certain conditions

66 fig_07_46 Enable / disable control mechanisms: Global Masking method 1: use priorities method 2: use mask register (bitwise masks) Note: interrupts are transient, if we choose to ignore one we may not be able to service it later


Download ppt "C Programming for Embedded Systems. fig_06_00 Computer Layers Low-level hardware to high-level software (4GL: “domain-specific”, report-driven, e.g.)"

Similar presentations


Ads by Google