4561-4344_pptch_07

Upload: vatsalshah24

Post on 03-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 4561-4344_PPTCH_07

    1/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Chapter - 7

    Programming Examples - I

  • 7/28/2019 4561-4344_PPTCH_07

    2/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 1: Copy Block

    R0 may be used as source pointer and R1 as

    destination pointer.

    R7 may be used as counter.

    Data may be copied from source to destination

    through the accumulator.

    01 OF 33

    Copy a block of twenty bytes of data available

    from address 60H to 73H to the location starting

    from 40H.

  • 7/28/2019 4561-4344_PPTCH_07

    3/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 1 (continued)

    Direct copy from one address to another needsmultiple instructions.

  • 7/28/2019 4561-4344_PPTCH_07

    4/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 1 (continued)

    Algorithm

    Step 1: Initialize R0 as source and R1 as destination

    pointers and load R7 by 20d to serve as the counter.

    Step 2: Copy a byte from source to destination usingR0 and R1 through accumulator (as temporary

    storage). Update pointers after copying.

    Step 3: Decrement counter by one. Continue at Step 2

    if counter is not zero.

    Step 4: Terminate the process.

  • 7/28/2019 4561-4344_PPTCH_07

    5/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 1 (continued)

    Software

    START: MOV R0, #60H ; start source

    MOV R1, #40H ; start destination

    MOV R7, #14H ; counter of 20d

    COPYIT: MOV A, @R0 ; get a byte

    MOV @R1, A ; save it

    INC R0 ; next source

    INC R1 ; next destinationDJNZ R7, COPYIT ; continue

    OVER: SJMP OVER ; done

  • 7/28/2019 4561-4344_PPTCH_07

    6/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 2 : Shift Block

    R0, R1 and R7 may be source and destination

    pointers and counter, respectively.

    Shifting must be started from 57H and not from 50H,

    otherwise data would be destroyed.

    Shift a block of eight bytes of data, presently

    located from 50H to 57H, one byte up, so that

    the data is available from 51H to 58H.

  • 7/28/2019 4561-4344_PPTCH_07

    7/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 2 (continued)

    In shift-block, overlapping exists but not in copy-block.

  • 7/28/2019 4561-4344_PPTCH_07

    8/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 2 (continued)

    Algorithm

    Step 1: Initialize R0 as source and R1 asdestination pointers for highest addresses and

    load R7 by 08H as counter. Step 2: Copy a byte via accumulator from

    source to destination using R0 and R1 andthen decrement both pointers by one.

    Step 3: Decrement counter by one. Continue atStep 2 if counter is not zero.

    Step 4: Terminate the process.

  • 7/28/2019 4561-4344_PPTCH_07

    9/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 2 (continued)

    Software

    START: MOV R0, #57H ; point source

    MOV R1, #58H ; point destination

    MOV R7, #08H ; counter for 8SHIFT: MOV A, @R0 ; get a byte

    MOV @R1, A ; save it

    DEC R0 ; next source

    DEC R1 ; next destination

    DJNZ R7, SHIFT ; continue

    OVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    10/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 3: Count No. of nulls

    In the worst case, there may not be a single nullbyte or all twenty bytes may be containing 00H.

    Twenty bytes of data stored in location from 7FH

    to 6CH of internal RAM. Count the number of

    those bytes, which contain 00H and store thisnumber of null bytes in RAM location 6BH.

  • 7/28/2019 4561-4344_PPTCH_07

    11/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 3 (continued)

    (a) (b) and (c) may be three possible situations.

    (d) Indicates register allotment.

  • 7/28/2019 4561-4344_PPTCH_07

    12/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 3 (continued)

    Algorithm

    Step 1: Initialize R0 as source pointer and R7 as

    byte counter. Clear accumulator.

    Step 2: Compare a byte with zero through pointer.Increment accumulator by one if it is zero.

    Step 3: Decrement both pointer and counter by

    one. Continue at Step 2 if counter is not zero.

    Step 4: Save accumulator content through pointer.

    Terminate the process.

  • 7/28/2019 4561-4344_PPTCH_07

    13/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 3 (continued)

    Software

    START: MOV R0, #7FH ; max address

    MOV R7, #14H ; byte counter

    MOV A, #00H ; null counterMAIN: CJNE @R0, #00H, NOTNUL ; compare

    INC A ; null byte, increment count

    NOTNUL: DEC R0 ; point next location

    DJNZ R7, MAIN ; complete checking

    MOV @R0, A ; save null count at 6BH

    OVER: SJMP OVER ; terminate program

  • 7/28/2019 4561-4344_PPTCH_07

    14/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 4: Find Checksum

    Checksums are generally used to ensure thecorrectness of a set of communicated data.

    Sixteen consecutive bytes starting from 50H are

    having unsigned integers. Develop a program to

    add all these sixteen integers and store the 8-bit

    sum in memory location 60H

  • 7/28/2019 4561-4344_PPTCH_07

    15/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 4 (continued)

    Algorithm

    Step 1: Initialize R0 as source pointer and load R7 by

    16d to serve as the counter. Clear the accumulator to

    calculate the sum.

    Step 2: Add one byte with accumulator as pointed by

    R0. Then increment pointer by one.

    Step 3: Decrement counter by one. Continue at Step 2

    if counter is not zero.

    Step 4: Store accumulator as pointed by R0.

    Terminate the process.

  • 7/28/2019 4561-4344_PPTCH_07

    16/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 4 (continued)

    Software

    START: MOV R0, #50H ; point first entry

    MOV R7, #10H ; counter with 16d

    MOV A, #00H ; clear checksumADDIT: ADD A, @R0 ; add one number

    INC R0 ; point next

    DJNZ R7, ADDIT ; continueMOV @R0, A ; save checksum at 60H

    OVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    17/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 5: Sum of Natural Numbers

    We select bank #0 so that its R6 may

    be address by direct addressing also.

    Write a program to generate and store natural

    numbers starting from 1 up to N terms and also

    find the sum of it. Assume the value of N is storedin location 30H. Store generated natural

    numbers from 40H. Leave the sum in the

    accumulator.

  • 7/28/2019 4561-4344_PPTCH_07

    18/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 5 (continued)

    R7: Term

    counter

    R6: Generated

    natural number

    A: Sum

    R0: Storagepointer

  • 7/28/2019 4561-4344_PPTCH_07

    19/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 5 (continued)

    Algorithm

    Step 1: Select bank #0. Initialize R0 as storage pointer

    of generated natural numbers and load R7 from 30H

    by N to serve as counter. Clear R6 and accumulator.

    Step 2: Increment R6 by one to generate the next

    natural number. Save it through R0 and add it with

    accumulator. Then increment R0 to point next.

    Step 3: Decrement R7 by one and continue at Step 2 ifR7 is not zero.

    Step 4: Terminate the process.

  • 7/28/2019 4561-4344_PPTCH_07

    20/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 5 (continued)

    Software

    START: MOV PSW, #00H ; select bank #0

    MOV A, #00H ; initialize sun

    MOV R6, #00H ; for natural number

    MOV R0, #40H ; storage pointerMOV R7, 30H ; load counter

    MAIN: INC R6 ; next natural number

    MOV @R0, 06H ; save generated number

    ADD A, R6 ; update sumINC R0 ; next saving pointer

    DJNZ R7, MAIN ; continue

    OVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    21/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 6: Sum of a Series

    The odd numbers are positive and

    even numbers are negative.

    Write a program to find the sum of the series

    12 + 34 + up to N terms.

    Assume the non-zero value of N is available in

    location 30H. Store the sum in the accumulator.

  • 7/28/2019 4561-4344_PPTCH_07

    22/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 6 (continued)

    Algorithm

    Step 1: Select bank #0. Load R7 by N from 30H as counter.Clear R6 and accumulator.

    Step 2: Increment R6 by one to generate next (odd) natural

    number. Add it with accumulator. Step 3: Decrement counter R7 by one. Continue at Step 4 if

    counter is not zero. Otherwise go to Step 6.

    Step 4: Increment R6 by one to generate next (even) naturalnumber. Clear carry flag by loading 00H in PSW. Thensubtract the generated number from the accumulator.

    Step 5: Decrement counter R7 by one. Continue at Step 2 if it isnot zero.

    Step 6: Terminate the program.

  • 7/28/2019 4561-4344_PPTCH_07

    23/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 6 (continued)

    Software (part 1)

    START: MOV PSW, #00H ; select bank #0

    MOV A, #00H ; clear sum

    MOV R6, #00H ; term startingMOV R7, 30H ; load counter by N

    MAIN: INC R6 ; get next odd term

    ADD A, R6 ; add odd termDJNZ R7, NEXT ; check for N

  • 7/28/2019 4561-4344_PPTCH_07

    24/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 6 (continued)

    Software (part 2)

    SJMP OVER ; N terms added

    NEXT: INC R6 ; next even term

    MOV PSW, #00H ; clear carry

    SUBB A, R6 ; subtract even term

    DJNZ R7, MAIN ; continueOVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    25/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 7: Fibonacci series

    Fibonacci series

    Generate Fibonacci series up to N terms and

    save from location 50H onwards. Assume N

    being available in location 4FH. Also assume thevalue of N to be greater than 3.

    0 1 1 2 3 5 8

  • 7/28/2019 4561-4344_PPTCH_07

    26/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 7 (continued)

    R7 : counter for N

    R0 : source pointer R1 : destination

    pointer

    A term generator

  • 7/28/2019 4561-4344_PPTCH_07

    27/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 7 (continued)

    Algorithm

    Step 1: Load counter R7 by N from 4FH.

    Step 2: Load R0 by 51H and store 01H (2nd Term).

    Step 3: decrement R0 by 1 and store 00H (1st Term).

    Step 4: Load R1 by 52H (location of 3rd Term). Also decrement

    R7 by 2 as two terms generated.

    Step 5: Load A through R0. Increment R0 by one.

    Step 6: Add the term pointed by 0 with A. Store it through R1.Then increment R1 by 1.

    Step 7: Decrement R7 by one and if not zero then Step 5

    Step 8: terminate program.

  • 7/28/2019 4561-4344_PPTCH_07

    28/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 7 (continued)

    Software (part 1)

    START: MOV R7, 4FH ; load counter by N

    MOV R0, #51H ; point 2nd Term

    MOV @R0, #01H ; store 2nd

    TermDEC R0 ; point 1st Term

    MOV R0, #00H ; store 1st Term

    MOV R1, #52H ; point 3rd

    TermDEC R7

    DEC R7 ; two terms generated

  • 7/28/2019 4561-4344_PPTCH_07

    29/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 7 (continued)

    Software (part 2)

    MAIN: MOV A, @R0 ; get one term

    INC R0 ; point next term

    ADD A, @R0 ; add two termsMOV @R1, A ; save new term

    INC R1 ; point next new term

    DJNZ R7, MAIN ; continue

    OVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    30/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 8: Generate a Series

    This is superimposition of two series.

    Generate and store the following series

    up to N terms. Value of N is available

    in location 30H. The series is presentedin decimal number system.

    1 2 3 11 12 13 21 22 23 31

  • 7/28/2019 4561-4344_PPTCH_07

    31/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 8 (continued)

    Flowchart

  • 7/28/2019 4561-4344_PPTCH_07

    32/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 8 (continued)

    Software (part 1)

    START: MOV R7, 30H ; load counter by N

    MOV A, #01H ; first term in A

    MOV R0, #40H ; storage areapointer

    MAIN: MOV R6, #03H ; term counter

    MAIN1: MOV @R0, A ; save one termDJNZ R7, NEXT ; continue

    SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    33/34

    8051 Microcontroller: Internals, Instructions,Programming and Interfacing by Subrata Ghoshal

    Example 8 (continued)

    Software (part 2)

    NEXT: INC A ; next number

    DJNZ R6, MAIN1 ; max 3 times

    MOV R6, #07H ; count gaps

    NEXT1: INC A ; next number

    DJNZ R6, NEXT1 ; count 7 gaps

    SJMP MAIN ; loop again

    OVER: SJMP OVER ; terminate

  • 7/28/2019 4561-4344_PPTCH_07

    34/34

    8051 Microcontroller: Internals InstructionsP i d I f i b S b Gh h l

    Questions

    ?