Introduction to C Programming for Embedded Systems - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Introduction to C Programming for Embedded Systems

Description:

C was developed in parallel with UNIX. Martin Richards wrote BCPL in mid 1960s ... int main(int argc, char *argv[]) int i; for (i = 0; i argc; i ) ... – PowerPoint PPT presentation

Number of Views:3123
Avg rating:3.0/5.0
Slides: 40
Provided by: dianet4
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming for Embedded Systems


1
Introduction to C Programming for Embedded Systems
2
History
  • 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

3
History
  • 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!

4
Hello 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

5
Compare 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

6
Functions
  • Equivalent to methods in Java, or
    function/procedure in FORTRAN
  • ReturnType FunctionName (
  • Type Parameter1Name,
  • Type Parameter2Name, )
  • return expression of ReturnType

7
Functions
  • / 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

8
Functions
  • 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)

9
Functions
  • 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()

10
Functions
  • 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)

11
Types, 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

12
Types, 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

13
Types, 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

14
Types, 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

15
Types, Variables, and Constants
16
Types, Variables, and Constants
  • Initialization
  • int n 100
  • float x 20.5
  • char ch X
  • int avg average(n, m)
  • int m n/2, k

17
Types, 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)

18
Types, Variables, and Constants
  • How to use constants?
  • Use C macro
  • define N 1024
  • int arrayN
  • Use constant variable
  • const double Pi 3.1415926

19
Types, 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

20
Types, Variables, and Constants
  • C constant values integer
  • decimal 100
  • hex 0x64
  • octal 0144
  • Hex starts with 0x, octal starts with 0

21
Types, 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)

22
Types, 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

23
Arrays 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?! /

24
Arrays 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

25
Arrays and Pointers
  • Example
  • / This works /
  • nTestArray10 nTestArray20  
  • / This does not work /
  • nTestArray1 nTestArray2

26
Arrays 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

27
Arrays 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 /

28
Array 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

29
Arrays 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

30
Arrays 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

31
Arrays 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

32
Arrays 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

33
Arrays 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! /

34
Structure 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

35
Structure and Union
  • Structure and pointer
  • struct point p1 10, 20
  • struct point ptr p1
  • ptr-gtx 20
  • (ptr).y 50

36
Structure and Union
  • Structure and array
  • struct key
  • char word
  • int count
  • keytabNKEYS
  • keytab0.word auto
  • keytab0.count 5

37
Structure 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.

38
Structure and Union
  • Use of union structure
  • Struct
  • char name
  • int flags
  • int utype
  • union
  • int ival
  • float fval
  • char sval
  • symtabNSYM

39
Object-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
Write a Comment
User Comments (0)
About PowerShow.com