s3 c Programming

Embed Size (px)

Citation preview

  • 8/9/2019 s3 c Programming

    1/23

    1

    Programming The

    Encore using ANSI C

  • 8/9/2019 s3 c Programming

    2/23

    2

    Outline

    1. Project structure: Source files, include files

    2. C data types and derived types

    3. Looping (while, for, do-while)4. Viewing variables in the debugger

    5. Conditional flow (if-then and switch)

    6. Shorthand

    7. Functions and parameter-passing8. Debugging functions

    9. Multi-file projects

  • 8/9/2019 s3 c Programming

    3/23

    3

    Project structure Project

    uC selection

    Editor settings

    Debugger settings

    Source files

    External dependencies Include files

    Object code libraries

  • 8/9/2019 s3 c Programming

    4/23

    4

    Source files Typically main.c

    contains the main()

    function, where executionstarts

    Split program up into

    several functions

    Often one function for each

    source file

  • 8/9/2019 s3 c Programming

    5/23

    5

    External dependencies Declared by #include

    Include files that declare std C functions, e.g. stdio.h (standard I/O, e.g. printf, scanf) math.h (e.g. sin, exp, rand)

    Include files for uC-specific info eZ8.h

    Declared by #include filename

    Include files for user-defined functions

    User-created object files

  • 8/9/2019 s3 c Programming

    6/23

    6

    C Data Types Character (8-bits)

    char c1, c2;

    c1 = a; c2 = \n; Integer (16-bits)1

    int i;

    i = -20; // a decimal number

    Floating point (32-bits)float f1, f2;

    f1 = 2.3; f2 = 1.5e3;

    1by default, short is the same as int

  • 8/9/2019 s3 c Programming

    7/23

    7

    C Data Types Byte (8-bits)

    unsigned char a,b;

    a = 0xaf; b = 128; Word (16-bits)

    unsigned int i;

    i = 0xffff; // hexadecimal FFFFh

    Long (32-bits)long i;

    i = -100000;

  • 8/9/2019 s3 c Programming

    8/23

    8

    C Derived TypesArray

    int tbl[5];

    tbl[0] = 3; tbl[4] = -1;

    String

    char * name = Encore;

    name[0] = I;

    (name will now contain Incore)

  • 8/9/2019 s3 c Programming

    9/23

    9

    Conditionalif

    if (i==1)

    ch = y;

    else

    ch = n;

    if (i>0) f = f+1;

    if (i>=j) {temp = i; i = j; j = temp;

    }

  • 8/9/2019 s3 c Programming

    10/23

    10

    Conditionalswitch

    switch (val) {

    case 1:

    ch = q; break;

    case 2:

    ch = z; break;

    default:ch = x; break;

    }

  • 8/9/2019 s3 c Programming

    11/23

    11

    whileLoops

    int tbl[5]={3,2,-1,4,6}, sum, i;

    sum = 0;

    i = 0;

    while (i

  • 8/9/2019 s3 c Programming

    12/23

    12

    forand

    do-whileloops

    for loopssum = 0;

    for (i=0; i

  • 8/9/2019 s3 c Programming

    13/23

    13

    Other operations

    Math +, -, *, /, % (remainder)

    Logical ! (not), && (and), || (or)

    Comparison

    =, == (equal), != (not equal) Bitwise

    ~ (not), & (and), | (or), ^ (xor),>> (right shift),

  • 8/9/2019 s3 c Programming

    14/23

    14

    Logical and bitwise opsint a=0xf0, b=0x21, c, d, e;

    c = a & b;

    d = a && b;

    e = b

  • 8/9/2019 s3 c Programming

    15/23

    15

    Shorthand Increment/Decrement

    int i,j,k;

    i = 3;

    j = i++;

    k = ++i;

    Accumulator operations

    i += 4;

    PAOUT |= 0x0f;

    PAOUT &= 0xfb;

    j will be 3; i will be 4

    k will be 5; i will be 5

    Sets lower nibble of PAOUT high

    Sets bit 2 (3rd bit) of PAOUT low

    Equivalent to i=i+4; i will be 9

  • 8/9/2019 s3 c Programming

    16/23

    16

    Functionsdelay() {

    }

    main() {

    while (1) {

    delay();

    }

    }

  • 8/9/2019 s3 c Programming

    17/23

    17

    Passing argumentsdelay(int imax) {

    int i,j;

    for (i=0; i

  • 8/9/2019 s3 c Programming

    18/23

    18

    Returning argumentsfloat getfreq(int ch) {

    return clkfreq/count;

    }

    main() {

    float freq;freq = getfreq(1);

    }

  • 8/9/2019 s3 c Programming

    19/23

    19

    Why use functions?

    Re-use code

    Keep size of each function small,especially main() Rule of thumb: < 100 lines

    Can repeat variable names for local use

    If placed in separate files, functions can becompiled separately Speeds up overall compilation Faster debugging cycle

    Necessary for vectored interrupts

  • 8/9/2019 s3 c Programming

    20/23

    20

    Debugging functionsStep into:Enter and

    debug thesubroutine

    Step over:Execute thesubroutine

    Step into (F11)

    Step over (F10)

  • 8/9/2019 s3 c Programming

    21/23

    21

    BreakpointsRight-click on the line of interest,

    Select Insert Breakpoint

    Breakpoint is set

    Execution will stop at thebreakpoint

  • 8/9/2019 s3 c Programming

    22/23

    22

    Placing functions

    in separate files The clean way (easier to debug)

    Put functions in a separate .c file in your project

    folder Put function prototypes in a separate .h file

    Function header w/o the body

    E.g. void delay(int imax);

    Add the .c file to your project files list Put #include statements

    In source code files that use your function

    In the file where your function is completely written

  • 8/9/2019 s3 c Programming

    23/23

    23

    Placing functions

    in separate files

    #include looks for xx in a special ZDS directory#include xx looks for xx in your project folder