IAR005 Geting Started With the IAR Embedded Workbench

download IAR005 Geting Started With the IAR Embedded Workbench

of 19

Transcript of IAR005 Geting Started With the IAR Embedded Workbench

  • 7/28/2019 IAR005 Geting Started With the IAR Embedded Workbench

    1/19

    IAR Application Note AVR-005, Sep 99

    1

    ,$5$SSOLFDWLRQ1RWH$95

    *HWWLQJVWDUWHGZLWKWKH,$5

    (PEHGGHG:RUNEHQFK$QGWKH,$5&FRPSLOHUIRU$WPHO$95GHYLFHV

    6800$59

    Digit=0

    end

    init

    Configure

    Port B

    Configure

    Port D

    return

    pollpb

    Read

    If PBpressed

    debounce

    return readpb

    debounce time

    Scale time to ms

    Configure timer

    While timer

  • 7/28/2019 IAR005 Geting Started With the IAR Embedded Workbench

    18/19

    IAR Application Note AVR-005, Sep 99

    18

    Program description - points of interestThe DSDs clearly show the structure of the program and the functions. Each

    function has its own DSD. The main function shows a continuous loop after

    initial setting up of the variables and low level functions. This will be the

    normal feature of most embedded control functions. Loops show up clearly asclockwise lines, with arrows to show the direction. The main function takes

    action only if the pushbutton is pressed. Note how a function is called up

    from within the if statement -

    (if (pollpb==0)), one example of the flexibility of C. The layout of a C

    program is very flexible but it is good practice to keep the braces to show start

    and end of a code block in line with each other. This makes it easier to check

    that you have nested them correctly. Nested loops or decisions should also be

    indented to show them up, and so that you can match the source code with the

    DSD.

    The functions are contrived to show how values can be passed into them,and/or return values from them. This is shown, in the DSD, by the box at the

    side of the appropriate terminator box. For instance the function debounce,

    takes in a 16-bit value (int) and uses it to feed the 16-bit timer of the AVR

    (TCNT1), after scaling to convert it into milliseconds. The function pollpb

    takes nothing in (void) but returns the value of the pushbutton, '1' = not

    pressed, '0' = pressed.

    Hints and Tips

    The list below shows some useful tips which could help produce readable,

    tighter and more efficient code with ICCA90:

    Structure your program using some useful method and use it to dry run theprogram, before coding.

    Separate the program into functional blocks.

    Tab the source code in several times before you start typing. The editorhas no margin control and this will allow a good print output to be

    obtained with margins.

    Comment your program as you progress, because you will never go backand do it later! (I have been there!)

    Keep to local auto variables wherever possible to allow the compiler to

    use and reuse registers instead of SRAM (faster execution and less code). Use char type variables wherever possible to reduce code size.

    Use the increment (e.g. x++) or decrement operator (e.g. x--) instead ofx=x+1 or x=x-1 to allow the compiler to use the MCUs efficient INC and

    DEC instructions.

    Indent code blocks and matching braces to ease readability andmaintenance.

    Define constants at the start of the program to make global changes easier

    later. e.g. #define DELAY (20)

    Use upper case characters for constants to make them show up as

    constants. That way you will not try to change them in the program!

  • 7/28/2019 IAR005 Geting Started With the IAR Embedded Workbench

    19/19

    IAR Application Note AVR-005, Sep 99

    Define the main function as type C_task to stop the compiler preservingand restoring registers on entry and exit, wasting valuable stack space.

    The main function will NEVER be exited in an embedded control

    program.

    Use FLASH ROM for constant storage ( using flash keyword) instead of

    SRAM (there is more of it). Accessing flash is more complicated with theAVR because it has a HARVARD architecture, with ROM and RAM

    accessed by separate address buses. This is covered by another

    application note. (This application example uses the look-up table in

    SRAM to take the easy way out!)

    :KDW\RXJDLQE\XVLQJ(PEHGGHG&Clearly, the low level adaptations that requires many lines of assembly code,

    can be done in C using the IAR C language extensions, thus increasing

    readability and portability. The arduous job of initializing the stack, setting

    up loops and decision branches, looking up addresses and other timeconsuming tasks, are now done by the compiler/linker. The powerful program

    development functions, strong integration with third party tools, and project

    management tools make this a very efficient system to use.

    &RQFOXVLRQThe combination of IAR Embedded Workbench and the Atmel AVR is a

    difficult one to beat in the 8-bit, or even 16-bit field of Embedded

    applications. Using C for Embedded application development considerably

    speeds up the development and software maintenance process, recouping the

    cost of the system usually in one or two projects.

    Contact information

    USA

    IAR Systems Inc.

    One Maritime Plaza

    San Francisco,

    CA 94111

    Tel: +1 415-765-5500

    Fax: +1 415-765-5503

    Email: [email protected]

    SWEDEN

    IAR Systems AB

    P.O. Box 23051

    S-750 23 Uppsala

    Tel: +46 18 16 78 00

    Fax: +46 18 16 78 38

    Email: [email protected]

    GERMANY

    IAR Systems AG

    Posthalterring 5

    D-855 99 Parsdorf

    Tel: +49 89 90 06 90 80

    Fax: +49 89 90 06 90 81

    Email: [email protected]

    UK

    IAR Systems Ltd.

    9 Spice Court,

    Ivory Square

    London SW11 3UE

    Tel: +44 171 924 3334

    Fax: +44 171 924 5341

    Email: [email protected]

    www.iar.com

    Copyright 1999 IAR SystemsIAR and C-SPY is registered trademarks of IAR Systems. IAR Embedded Workbench is a trademark of IAR Systems. Windows is a

    trademark of the Microsoft Corporation. All other products are registered trademarks or trademarks of their respective owners.

    Product features, availability, pricing and other terms and conditions are subject to change by IAR Systems without prior notice.