mc3 timing and projects

Upload: linus200

Post on 09-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 MC3 Timing and projects

    1/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 108

    Microcontrollers

    Instruction timing

    If the Microcontroller Unit (MCU) is to output results for the user to

    see for example, it usually needs to be slowed down to our speed. A

    simple way to do this is to waste processor cycles. But how many?

    It is useful to have a delay function in multiples of milliseconds for

    example so an accurate 1ms function is useful. To do this you need

    to work out how many instruction cycles to waste then write the

    code to waste them. An instruction cycle takes 4 clock cycles so at8MHz, the instruction cycle time is 0.5uS or 500ns. Dont forget

    that although most instructions take 1 cycle, this is not always the

    case. The debugger stopwatch feature is useful for measuring run-

    time. For example, how long does it take the Wait loop to execute in

    the following simple program?

    #include

    cblock 20hDelay

    endc

    org 0

    movlw .200

    movwf Delay

    Wait

    nop

    nop

    decfsz Delay

    goto Wait

    Here

    goto Here

    end

  • 8/7/2019 MC3 Timing and projects

    2/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 109

    Microcontrollers

    200 iterations

    x 5 cycles

    x 0.5us cycle time

    + 0.5us last decfsz is 2 cycles

    2x0.5us as last goto is not executed

    = 499.5us

  • 8/7/2019 MC3 Timing and projects

    3/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 110

    Microcontrollers

    Common constructs using 16x assembly language

    Here are some examples of typical loops and conditional statements

    with C equivalents.

    Do while (x != 3)

    #include

    ;

    cblock 20h

    X

    endc

    org 00h ; reset vector

    Start

    movlw 5

    movwf X

    DoWhile

    ; ... loop body code here

    decf X,F ; Arbitrary instruction to mod X

    ; ... end loop body code

    movf X,W ; Get X to check for end of loop

    sublw 3 ; If Z=1 must be 3 so skip out of loop

    btfss STATUS,C ; Loop ended so skip out

    goto DoWhile ; Loop not ended so go roud again

    goto Start ; Try DoWhile loop again

    end

  • 8/7/2019 MC3 Timing and projects

    4/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 111

    MicrocontrollersNote: be careful not to use reserved words like else. If the label

    stays blue then it is probably a reserved word so dont use it.

    if (X == 0) y=3;

    This example illustrates an easy way to test for a variable being zero.

    #include

    ; if (X == 0) y=3;

    cblock 20hX

    y

    endc

    org 00h ; reset vector

    Start

    movlw 1 ; Change this value to test

    movwf X

    ; ... body code ...

    ; if (x == 0)

    movf X,F ; move X to itself as it affects Z flag

    btfss STATUS,Z ; Yes its 0 so skip

    goto EndIfPart ; not 0 so skip if part

    movlw 3 ; Do if part

    movwf y

    EndIfPart

    goto Start ; Try If again

    end

  • 8/7/2019 MC3 Timing and projects

    5/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 112

    Microcontrollersif (x == 5) y=0; else y=10;

    #include

    ; if (x == 5) y=0; else y=10;

    cblock 20h

    X

    y

    endc

    org 00h ; reset vector

    Startmovlw 5 ; Change this value to test

    movwf X

    ; ... body code ...

    movf X,W ; get X in w, subtract 5 and test for zero

    sublw 5

    btfss STATUS,Z ; If zero, skip and do if part

    goto ElsePart ; Not zero so do the else part

    clrf y ; If part y = 0

    goto EndIfPart

    ElsePart

    movlw .10 ; y = 10

    movwf y

    EndIfPart

    goto Start ; Try If Else again

    end

  • 8/7/2019 MC3 Timing and projects

    6/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 113

    Microcontrollers

    Accessing data in different modules

    To enable you to develop functions that are reusable without having

    to copy and past code from one program to another, you should

    develop functions as individual modules then link into your program

    the ones you want to use. For example, you may have written a

    good delay routine that is useful to you in many of your programs

    and may be usable to other members of a project team. You would

    develop the delay function in a separate file then link it into your

    main program. The following instructions show you how to create aproject in MPLAB.

    The module from which a function is called (e.g. the main program

    module) needs to declare the called functions EXTERN. This

    informs the assembler that it will come across calls to functions for

    which there is no source code as the source code is EXTERNal to

    this file. For example, in your main program you may call two

    functions called DelayMiliSecs and InitPorts which are declared indifferent files, so use:

    EXTERN DelayMiliSecs

    EXTERN InitPorts

    Conversely, in the files DelayMiliSecs and InitPorts you need to add

    Global directives. This tells the linker that the function is globally

    available to be called from other files. Use:

    GLOBAL DelayMiliSecs in the source file of the Delay routine

    GLOBAL InitPorts in the source file of the Initialisation code

  • 8/7/2019 MC3 Timing and projects

    7/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 114

    MicrocontrollersOnce your main program is written (and the other files for your

    project are available), create a new project file using Project | New.Fill in the project name and browse to the appropriate directory. On

    clicking OK you would expect something to happenbut it doesnt,

    so it looks like your file has not been createdbut it has. Select

    View | Project and the project window displayed.

    Right click on Source files and select Add Files. Browse for the

    files you want in your project and add them.

    The project needs a linker script to let the linker know where to put

    the machine code in the output file. Either right click Linker Scripts

    and point to

    C:\Program Files\MPLAB IDE\MCHIP_Tools\LKR\16f8877A.lkr

    (or wherever your default linker file is for the 16F877) or go to

    Project | Build Options | Project and set the path

    C:\Program Files\MPLAB IDE\MCHIP_Tools\LKR\

    to the linker script path and manually enter 16f877.lkr into the linkerscript dialog box or copy the linker file to your functions directory

    (this is what I have done).

    Build the application and test it.

    The project and source files are illustrated in Figure 24.

    Note that org is no longer used as the code needs to be relocatablei.e. we dont want to have to worry about which addresses the linker

    places each code module. Use the linker script directive STARTUP

    to specify the startup code section then use CODE after that to allow

    the linker to put the remainder of the code where it likes.

  • 8/7/2019 MC3 Timing and projects

    8/8

    DrTony Nicol - University of Central Lancashire Department of Computing. 115

    Microcontrollers

    Figure 24 MPLAB Project

    Note the use of EXTERN and GLOBAL. Note also that the cblock

    is no longer used. This too is unmaintainable in a project as you

    would ned to keep track of all the addresses you allocate your

    variables to. Use the UDATA directive to reserve memory and let

    the linker choose the addresses to allocate.

    If you want to see what the whole program looks like once linked,

    open the .lst file. If you want to see where the linker has put your

    variables, open the .map file.