Title: Introduction to C Programming for Embedded Systems
1Introduction to C Programming for Embedded Systems
2History
- C was developed in parallel with UNIX
- Martin Richards wrote BCPL in mid 1960s
- Ken Thompson wrote B in 1970
- Dennis Ritchie designed most creative parts of C
in 1972 - C is used to re-write UNIX in 1973
- Dennis Ritchie and Brian Kernighan wrote The C
Programming Language in 1978 - C was standardized during 1983-1988 by ANSI
3History
- C and its predecessors were designed as system
programming languages - BCPL needs to be compiled on a DEC PDP-7 machine
with 8KB 18-bit words - B was used to write utility programs on a DEC
PDP-11 with 24KB memory running UNIX - C was used to re-write that UNIX on the same
machine - It has to be simple!
4Hello World!
- include ltstdio.hgt
- main()
-
- printf(hello, world\n)
-
- To build and run on a Linux/unix machine
- gcc o helloworld helloworld.c
- ./helloworld
- hello, world
5Compare C and Java/C
- C is a procedural language
- No classes or objects
- Function is the building block
- C philosophy
- As simple as possible
- Uses a minimum set of language constructs
6Functions
- Equivalent to methods in Java, or
function/procedure in FORTRAN - ReturnType FunctionName (
- Type Parameter1Name,
- Type Parameter2Name, )
-
- return expression of ReturnType
-
7Functions
- / calculate the average of x and y /
- int average(int x, int y)
-
- int avg_value
- avg_value (x y) / 2
- return avg_value
-
- / a simpler style /
- int average2(int x, int y)
-
- return (x y)/2
8Functions
- Procedure equivalent function with void return
type - / a procedure that calls three others /
- void do_tasks(int x, int y, int z)
-
- do_task1()
- do_task2(x, y)
- do_task3(z)
9Functions
- main function the starting point of the
program - int main(int argc, char argv)
-
- int i
- for (i 0 i lt argc i)
- fprintf(parameter d is s\n, i, argvi)
-
- Other forms
- int main()
- main()
10Functions
- Prototype declare first, define later
- int callee_func(int x, int y)
- int caller_func()
-
-
- avg callee_func (a, b)
-
-
- int callee_func(int x, int y)
-
-
11Types, Variables, and Constants
- Scalar variables
- int i, j, k
- char c
- float x, y
- double z
- Arrays
- float X100
- float pixel1024768
- Structures and unions
- struct
- int x, y
- float weight
-
- union
- char ch_value
- int int_value
- float float_value
-
-
12Types, Variables, and Constants
- Rules on Variable Names
- What can be used A-Z, a-z, 0-9, _ (underscore)
- The first letter must be A-Z, a-z, or
underscore - Case sensitive MyVariable and myvariable are
different - Variable length as long as wish, but only first
31 may be significant - Convention is to use meaningful names
- int height
- float area
- int array_size
- Another style
- int nHeight
- float fArea
- int nArraySize
13Types, Variables, and Constants
- Scalar type integer, character and floating
point - Integer types
- int normal integer, 2-, 4- or 8-byte
- short short integer, 2-byte
- long long integer, 4- or 8-byte
- Character type
- char single character, one-byte
- Floating point types
- float single-precision floating point, 4-byte
- double double-precision floating point, 8-byte
14Types, Variables, and Constants
- unsigned Modifier
- unsigned short short_value
- unsigned int int_value
- unsigned char char_value
- It changes the effective range of value
- unsigned short is 0 65535 short is -32,768
32,767. - It affects the use of variables in conditions
-
15Types, Variables, and Constants
16Types, Variables, and Constants
- Initialization
- int n 100
- float x 20.5
- char ch X
-
- int avg average(n, m)
- int m n/2, k
17Types, Variables, and Constants
- Type casting convert a value from one type to
another - (type) expression
- pi (double) 3.1415926
- size (int) (3.14 radius radius)
- if ((unsigned) x gt (unsigned) y)
18Types, Variables, and Constants
- How to use constants?
- Use C macro
- define N 1024
- int arrayN
- Use constant variable
- const double Pi 3.1415926
19Types, Variables, and Constants
- Special constants enumerated type
- enum color_type RED, YELLOW, BLUE
- enum color_type wall_color
- Equivalent to
- define RED 0
- define YELLOW 1
- define BLUE 2
20Types, Variables, and Constants
- C constant values integer
- decimal 100
- hex 0x64
- octal 0144
- Hex starts with 0x, octal starts with 0
21Types, Variables, and Constants
- C constant values character
- \0 NULL character
- \n newline
- \r return
- a letter a
- \t tab
- \040 char of ASCII 0408 or 3210 (space)
22Types, Variables, and Constants
- Floating point IEEE 754 standard
- used less frequently in embedded programs
- very expensive versus integer operations
- (Single precision is 32-bit, and double precision
is 64-bit) - Example
- 68HC11 floating point op vs. integer op
- 160 cycles vs. 3 cycles
23Arrays and Pointers
- Type VariableName ArraySize
- Sequence of a specific variable type stored in
memory - Zero-indexed starts at zero and ends at N-1Â
- Â
- int vector8
- int n
-
- vector0 1 / first element /
- n vector7 / last element /
- vector8 -1 / what happens?! /
24Arrays and Pointers
- Be careful of boundaries in C
- No guard to prevent you from accessing beyond
array edge - Write beyond array Potential for disaster
-
- What exactly is an array?
- Not a specific type
- Pointer to a block of memory
- No built-in mechanism for copying arrays
25Arrays and Pointers
- Example
- / This works /
- nTestArray10 nTestArray20 Â
- / This does not work /
- nTestArray1 nTestArray2
26Arrays and Pointers
- A variable is just a block of memory
- Address (starting address) Where the block
starts in the address space - Size How much space it occupies
- Type How to interpret its value
- Pointer is the starting address of a memory block
- Pointer value memory address
- Pointer variable a variable that holds a memory
address
27Arrays and Pointers
- Declare Type VarName
- char pLED
- char pSwitch
- Initializes a pointer variable
- pLED 0x0f001000
- pSwitch 0x0f001004
- Uses (Dereferences) a pointer variable
- pLED 0xff / clear all LED bars /
- ch pSwitch / read switch states /
28Array and Pointers
- int nVal
- int pnVal
- pnVal nVal / let it be 0x20000000 /nVal
10 - pnVal is 0x2000000
- pnVal is 10
- pnVal 5
- pnVal is still 0x2000pnVal is 5nVal is 5
29Arrays and Pointers
- Three key steps when using pointers
- 1. Declare the pointer
- type pName
- Â char pChar
- long pHistory
- 2. Initialize the pointer
- In order to use the pointer, we need to point
it somewhere. - Â pChar (char ) 0x00001800
- pHistory lValue
- Â The (char ) tells the compiler this is a
32-bit memory address, not a 32-bit value. - 3. Access the pointer (Read/Write)
- In order to get the value, we must use a in
front of the name. - Â n pChar 0x80
- if((pHistory 25) gt TOL_HISTORY)
- pHistory TOL_MINIMUM
30Arrays and Pointers
- What does the pointer point to?
- Depends upon the system, may not always be RAM
- Two types of architecture
- Unified Memory - Motorola
- All devices, RAM, etc. share the same address
space - 0x2000 may be memory, a temperature sensor, hard
disk - Split I/O Intel
- Separate addresses for I/O and memory
- Hard disk, PCI cards I/O address space, special
assembly instructions to access
31Arrays and Pointers
- A memory word read always returns the value of
the last write - A device can choose to respond to reads and
writes however it wants. - Thus, a write with bit 7 set may behave
differently than a write with bit 7 clear - Need to understand the devices programming model
or interface
32Arrays and Pointers
- Pointer arithmetic and comparison
- Can add and subtract change by the size of
element - Can test equality or inequality
- int pInt 0x0f000000
- char pChar 0x0f000004
- (pInt 1) points to 0x0f000004
- (pChar1) points to 0x0f000005
- pChar gt pInt is true
- pChar pInt 1 is true
-
33Arrays and Pointers
- An array variable has a pointer value
- int XN
- int pX X
- The following are all equivalent
- X2 100
- (X2) 100
- (pX2) 100
- pX2 100
- An array is not really a variable
- X 0xf0000000/ compiler error! /
34Structure and Union
- To define a complex component
- struct point
- int x
- int y
-
-
- struct point p1 10, 20
- struct point p2, p3
- p2.x 10 p2.y -p1.y
- p3 p2
35Structure and Union
- Structure and pointer
- struct point p1 10, 20
- struct point ptr p1
-
- ptr-gtx 20
- (ptr).y 50
36Structure and Union
- Structure and array
- struct key
- char word
- int count
- keytabNKEYS
- keytab0.word auto
- keytab0.count 5
37Structure and Union
- Union Merge multiple components
- union u_tag
- int ival
- float fval
- char sval
-
- The size of a union variable is the size of its
maximum component.
38Structure and Union
- Use of union structure
- Struct
- char name
- int flags
- int utype
- union
- int ival
- float fval
- char sval
-
- symtabNSYM
39Object-Oriented Programming
- typedef struct
- int x
- int y
- point
- init(point p, int x, int y)
-
- p-gtx x
- p-gty y
-
- add(point p, int x, int y)
-
- p-gtx x
- p-gty y