21
LAB ASSIGNMENT Embedded System Design Student: ____________ ID: ________________ Class: ______________

ASemester2 LAB ASSIGNMENT-EmbededSystemDesign · 2009. 11. 19. · Embedded System Design Lab Page - 7/21 Problem 5: Add the signed numbers found in internal RAM locations 25h, 26h

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • LAB ASSIGNMENT Embedded System Design

    Student: ____________

    ID: ________________

    Class: ______________

  • Embedded System Design Lab Page - 2/21

    CONTENTS ���� 010

    ����

    Problem: Page

    1. Addressing modes ................................................. 3

    2. Double number ..................................................... 4

    3. Xor and compare ................................................... 5

    4. Plus unsigned numbers at 3 memory location ....... 6

    5. Plus unsigned numbers at 3 memory location ........ 7

    6. Plus BCD numbers ........................................... 8

    7. Loop ..................................................................... 9

    8. Multiply .............................................................. 10

    9. Divide ................................................................. 11

    10. Define Byte DB and compare ........................... 12

    11. Sorting values in memory locations .................. 13

    12a. Linear search external memory ....................... 15

    12b. Binary search external memory ....................... 17

    13. Interrupt transmission ....................................... 18

    14. Polling transmisstion ......................................... 19

    15. Serial communication ....................................... 20

    16. Input output ports, COM port communication ... 21

  • Embedded System Design Lab Page - 3/21

    Problem 1:

    Copy the byte in TCON to register R2 using at least four different methods.

    a. Use the direct address for TCON

    b. Use the direct address for TCON and R2

    c. Use R1 as a pointer to R2 and use the address for TCON

    d. Push the content of TCON into direct address

    Code listing a:

    mov 88h, #50h ; TCON address is 88h

    mov R2, 88h ; copy content TCON from its direct address

    Code listing b:

    mov 02h, 88h ; copy direct addrect of TCON to direct address of R2

    Code listing c:

    mov R1, #02h ; R1 contains value 02h which is direct address of R2

    mov @R1, 88h ; copy TCON to direct address that R1 point to

    Code listing d:

    push 88h ; push TCON to stack

    pop 02h ; pop top of stack to direct addrect of R2

  • Embedded System Design Lab Page - 4/21

    Problem 2:

    Double the number in register R2, and put the result in registers R3 (high

    byte) and R4 (low byte).

    Code listing:

    mov R2, #0F2H ; copy R2 content to A

    mov A, R2 ; copy R2 content to A

    mov R3, #0 ; clear R3

    add A, R2 ; double R2

    mov R4, A ; copy low byte result to R4

    jnc over ; if CY = 0 then finish

    mov R3, #1 ; if CY=1 then set 1 to R3 (high byte result)

    over:

    end

    Results from executing the program

  • Embedded System Design Lab Page - 5/21

    Problem 3:

    Find a number that, when XORed to the register A, results in the number

    3Fh in A.

    Code listing:

    mov R1,#0FFh ; copy initial maximum value to R1

    again: mov A,#54h ; copy test value #54h to A

    xrl A, R1 ; xor A with R1

    dec R1 ; decrement R1

    cjne A,#3Fh, again ; test if result = #3Fh

    inc R1 ; finding number contains in R1

    end

    54h = 01010100 6Bh = 01101011 xor = 00111111 = 3Fh

    Results from executing the program

  • Embedded System Design Lab Page - 6/21

    Problem 4:

    Add the unsigned numbers found in internal RAM locations 25h, 26h and

    27h together and put the result in RAM location 31h(MSB) and 30h(LSB).

    Code listing:

    mov 25h,#0FFh ; copy test value #0FFh to address 25h

    mov 26h,#0FAh ; copy test value #0FAh to address 26h

    mov 27h,#0EEh ; copy test value #0EEh to address 27h

    mov R1,#0 ; clear R1

    clr C ; clear Carry

    mov A,25h ; copy value from address 25h to A

    add A,26h ; add value from address 26h to A

    jnc next1 ; if CY=0 then add next value

    inc R1 ; if CY=1 then increase R1

    clr C ; clear Carry

    next1: add A,27h ; add next value from address 27h to A

    jnc next2 ; if CY=0 then report the result

    inc R1 ; if CY=1 then increase R1

    next2: mov 30h,A ; copy LSB to address 30h

    mov 31h,R1 ; copy MSB to address 31h

    end

    Results from executing the program

  • Embedded System Design Lab Page - 7/21

    Problem 5:

    Add the signed numbers found in internal RAM locations 25h, 26h and 27h

    together and put the result in RAM location 31h(MSB) and 30h(LSB).

    Code listing:

    mov 25h,#+20 ; copy test value #+20 to address 25h

    mov 26h,#-30 ; copy test value #-30 to address 26h

    mov 27h,#+70 ; copy test value #+70 to address 27h

    mov R1,#0 ; clear R1

    mov A,25h ; copy value from address 25h to A

    add A,26h ; add value from address 26h to A

    jb PSW.2,over ; if overflow flag OV=1 then cancelled

    jnc next1 ; if CY=0 then add next value

    inc R1 ; if CY=1 then increase R1

    next1: add A,27h ; add next value from address 27h to A

    jb PSW.2,over ; if overflow flag OV=1 then cancelled

    jnc next2 ; if CY=0 then add next value

    inc R1 ; if CY=1 then increase R1

    next2: mov 30h,A ; copy LSB to address 30h

    mov 31h,R1 ; copy MSB to address 31h

    over:

    end

    Results from executing the program

  • Embedded System Design Lab Page - 8/21

    Problem 6:

    Add the BCD numbers found in internal RAM locations 25h, 26h and 27h

    together and put the result in RAM location 31h(MSB) and 30h(LSB).

    Code listing:

    mov 25h,#37h ; copy test value #37h to address 25h

    mov 26h,#48h ; copy test value #48h to address 26h

    mov 27h,#87h ; copy test value #87h to address 27h

    mov R1,#0 ; clear R1

    mov A,25h ; copy value from address 25h to A

    add A,26h ; add value from address 26h to A

    da A ; adjust A to BCD number

    jnc next1 ; if CY=0 then add next value

    inc R1 ; if CY=1 then increase R1

    next1: add A,27h ; add next value from address 27h to A

    da A ; adjust A to BCD number

    jnc next2 ; if CY=0 then add next value

    inc R1 ; if CY=1 then increase R1

    next2: mov 30h,A ; copy LSB to address 30h

    mov 31h,R1 ; copy MSB to address 31h

    end

    Results from executing the program

  • Embedded System Design Lab Page - 9/21

    Problem 7:

    Place any number in internal RAM location 3Ch and increment it until the

    number equals 2Ah.

    Code listing:

    mov 3Ch,#20h ; test value in address 3Ch

    mov R1,#3Ch ; R1 contains value #3Ch

    again: mov A,@R1 ; copy value in address that R1 point to A

    cjne A,#2Ah,next ; compare A with value #2Ah

    sjmp over ; if equal then finish

    next: inc @R1 ; increment value in address 3Ch

    sjmp again ; repeat while not equal

    over:

    end

    Results from executing the program

  • Embedded System Design Lab Page - 10/21

    Problem 8:

    Multiply the data in RAM location 22h by the data in RAM location

    15h.Put the result in RAM Location 19h(LSB) and 1Ah(MSG).

    Code listing:

    mov 15h,#40 ; test value 1 in address 15h

    mov 22h,#7 ; test value 2 in address 22h

    mov A,15h ; copy test value 1 to A

    mov B,22h ; copy test value 2 to B

    mul AB ; multiply A with B

    mov 19h,A ; copy LSB of result to address 19h

    mov 1Ah,B ; copy MSB of result to 1Ah

    end

    Results from executing the program

  • Embedded System Design Lab Page - 11/21

    Problem 9:

    Divide the data in RAM Location 3Eh by the number 12h. Put the quotient

    in R4 and remainer in R5

    Code listing:

    mov 3Eh,#29h ; test value #29h in address 3Eh

    mov A,3Eh ; copy value in address 3Eh to A

    mov B,#12h ; copy #12h to B

    div AB ; divide A to B

    mov R4,A ; copy quotient to R4

    mov R5,B ; copy remainer to R5

    end

    Results from executing the program

  • Embedded System Design Lab Page - 12/21

    Problem 10:

    Define a number in external RAM using ‘DB’. Check whether the number

    is even or odd. If it is even set C to 1, otherwise clear C.

    Code listing:

    org 500h ; at address 500h, define byte value 7h

    data1: DB 7h ; 7h in external memory 500h

    org 0

    mov DPTR, #500h ; DPTR contain external address 500h

    clr A ; clear A

    movc A, @a+DPTR ; copy value from external memory to A

    clr C ; reset Carry

    rrc A ; rotate right A to carry

    jb CY,odd ; if odd

    setb C ; if even

    sjmp over ; finish

    odd:

    clr C

    over:

    end

    Results from executing the program

  • Embedded System Design Lab Page - 13/21

    Problem 11:

    Write a program to sort (ascending/descending) the array which is stored in

    external RAM.

    Code listing:

    mov 30h,#4h ; test values put in address 30h-33h

    mov 31h,#6h

    mov 32h,#5h

    mov 33h,#1h

    mov R1,#30h

    mov R7,#3 ; outer loop counter

    mov R6,#4 ; inner loop counter

    outerloop:

    mov A,R1

    mov R0,A

    mov A,@R1 ;copy first value for each cycle

    dec R6

    mov 03h,R6

    interloop:

    clr c

    inc R0 ; travel inner loop

    mov 40h,@R0 ; use addr 40h for compare value

    cjne A,40h,next ; cy=1 if a less than

    sjmp nextloop

    next: jnc exchange ; if cy=1 then swap

    sjmp nextloop

    exchange: mov 02h,A

    mov A,@R0

    mov @R0,02h

    nextloop: nop

    djnz R6,interloop

    mov @R1,A

    mov R6,03h

    inc R1

    djnz R7,outerloop

    mov R4,30h ; display result in registers R4-R7

    mov R5,31h

    mov R6,32h

    mov R7,33h

    end

  • Embedded System Design Lab Page - 14/21

    Results from executing the program

  • Embedded System Design Lab Page - 15/21

    Problem 12a: Linear Search

    Write a program for searching an element which is stored in external RAM

    location between 0100h and 0200h. Also find out the address of the found

    element. Address of the element will be stored in the register R6 (LSB) and

    R7 (MSB). Return A=1 if element is found otherwise 0. Perform searching

    using at least any two following techniques.

    1. Linear Search

    2. Binary search

    3. even/Odd Search

    Code listing(linear search):

    mov DPTR,#100h ; DPTR point to address 100h external memory

    mov A,#20h ; sample data

    movx @DPTR,A ; copy sample data to 100h

    mov DPTR,#123h ; DPTR point to address 123h

    mov A,#15h ; sample data #15h

    movx @DPTR,A ; copy sample data to 123h

    mov DPTR,#100h ; start address to search

    mov R1,#100 ; loop 100 times from address 100h to 200h

    mov R0,#15h ; value need to find

    again: movx A,@DPTR ; fetch value that DPTR point to

    cjne A,0h,next ; compare content of A with R0

    mov A,#1 ; if found

    sjmp found ; find the address

    next: inc DPTR ; not equal then loop next

    djnz R1,again ; count number of loops

    mov A,#0 ; not found

    sjmp notfound ; finish

    found:

    mov R6,DPl ; identify low byte of found external memory location

    mov R7,DPH ; identify high byte of found external memory location

    notfound:

    end

  • Embedded System Design Lab Page - 16/21

    Results from executing the program

  • Embedded System Design Lab Page - 17/21

    Problem 12b: Binary Search mov A,#100 ; initial value mov R3,#101 mov DPTR,#100 loop:

    movx @DPTR,A ; initiate ;memory location from 100 ;to 200 values from 100-201

    inc A inc DPTR djnz R3,loop ; repeat 101s mov R0,#123 ; find this value

    ; #7Bh(test value) mov DPTR,#100 mov R1,#100 ; each loop divide

    ; 2 and plus to DPTR mov B,#2 mov A,R1 div AB mov R1,A mov A,DPL add A,R1 mov DPL,A ; DPTR point to

    ; middle element mov A,DPH jnc next1 inc A mov DPH,A next1: movx A,@DPTR ; copy value

    ;of middle element to A clr C cjne A,0h,next2; compare

    ;with value #7Bh in R0 mov R6,DPL ; store LSB mov R7,DPH ; store MSB sjmp over ; equal finish next2:

    jnc left ; dest < source then CY=1 mov A,R1 ; DPTR move right mov B,#2 div AB jnz A_e_0 mov A,#1 A_e_0: mov R1,A mov A,DPL ; calculate ; position of middle element add A,R1 mov DPL,A mov A,DPH jnc next3 inc A mov DPH,A next3: sjmp next1 left: clr C ; DPTR move left mov A,R1 mov B,#2 div AB jnz A_e_02 A_e_02: mov A,#1 mov R1,A mov A,DPL ; calculate ; position of middle element subb A,R1 mov DPL,A mov A,DPH jnc next4 inc A mov DPH,A next4: sjmp next1 over: end

    Results from executing the program

  • Embedded System Design Lab Page - 2/21

    Problem 13: Interrupt transmission

    Write a program for transmitting character using following method

    Interrupt Transmission

    Code listing:

    When timer 1 interrupt occurs then increasing A and transmit it to P1

    org 0

    ljmp start

    org 001Bh ; timer 1 interrupt vector table

    inc A ; increment A

    mov P1,A ; copy A to P1

    reti ; return from interrupt service routine

    org 40h

    start: mov TMOD,#20h ; timer 1, mode 2 (8-bit auto-reload)

    mov TH1,#0 ; initial value

    mov IE,#10001000b ; enable timer 1

    setb TR1

    again: sjmp again ; loop forever and wait timer 1 interrupt

    end

    Results from executing the program

  • Embedded System Design Lab Page - 3/21

    Problem 14: Polling transmission

    Write a program for transmitting character using following method Polling

    Transmission

    Code listing:

    mov TMOD,#01h ; timer 0, mode 1 (16-bit timer)

    repeat: mov TH0,#0 ; initial value

    mov TL0,#10 ; iniital value

    setb TR0

    again: jnb TF0, again ; monitor timer flag 0

    clr TR0 ; stop timer 0

    clr TF0 ; clear timer flag 0

    inc A ; increasing A

    mov P0,A ; copy A to port P0

    sjmp repeat ; repeat forever

    end

    Results from executing the program

  • Embedded System Design Lab Page - 4/21

    Problem 15: Serial Communication

    Write a program that takes the character in the A register, transmits it,

    delays for the transmission time, and then returns to the calling program

    with following specifications

    a. Timer 1 must be used to set the baud rate 2400

    b. The delay for one 10-bit character is 1,000/240 or 4.16 milliseconds

    c. The timer 1 should generate baud rate at SBUF

    Code listing:

    Assuming XTAL = 11.0592 MHz

    Machine cycle = 11.0592 MHz / 12 = 921.6 kHz

    Cycle time = 1 / 921.6 kHz = 1.085 µs

    UART frequency = 921.6 kHz / 32 = 28800 Hz

    Use timer 1 mode 2 (8-bit auto-reload)

    To set baud rate 2400:

    28800/2400 = 12 then assign TH1=#-12

    Time delay for one 10-bit character is 4.16 ms (1000/240)

    4160/1.805 = 3834

    a. Timer 1 must be used to set the baud rate 2400

    mov TMOD,#20h ; timer 1, mode 2 (8-bit auto-reload)

    mov TH1,#-12 ; baud rate 2400

    setb TR1 ; start timer 1

    mov SBUF,A ; copy content of A to SBUF for transmit

    wait: jnb TF1, wait ; monitor TF1

    clr TR1 ; stop timer 1

    clr TF1 ; clear timer flag 1

    b. The delay for one 10-bit character is 1,000/240 or 4.16 milliseconds

    65536 – 3834 = 61702 = F106h

    mov TMOD,#10 ; timer 1, mode 1 (16-bit timer)

    mov TH1,#0F1h ; initial high byte value

    mov TL1,#6h ; initial low byte value

    setb TR1 ; start timer 1

    mov SBUF,A ; copy content of A to SBUF for transmit

    wait: jnb TF1,wait ; monotor TF1

    clr TR1

    clr TF1

  • Embedded System Design Lab Page - 5/21

    Problem 16:

    Write a 8051 program to read data from port 1 and write to port 2

    continuously while giving a copy of it to serial COM port to be transferred

    serially. Specification:

    i. Baud rate 9600/2400

    ii. XTAL 11.0592

    iii. write a single Interrupt routine for TI & RI

    Code listing:

    org 0

    ljmp start

    org 0023h ; interrupt vector table for serial

    ljmp serial

    org 30h ; by-pass interrupt vector table

    start: mov P1,#0FFh ; P1 becomes input port

    mov P1,#11001100B ; test value at P1

    mov TMOD,#20h ; timer 1, mode 2 (auto-reload)

    mov TH1,#-3 ; set baud rate 9600

    mov SCON, #192 ; serial mode 1, 8-bit, 1 start bit, 1 stop bit

    mov IE,#10010000B ; enable serial interrupt

    setb TR1 ; start timer 1

    repeat: mov A,P1 ; copy P1 to A

    mov P2,A ; issue A to P2

    mov SBUF,A ; serial transmit A to COM port

    cpl A ; complement A for testing

    mov P1,A ; assign new value for P1

    sjmp repeat ; do it continuously

    serial:jnb TI,serial ; interrupt service routine

    clr TI ; clear TI

    reti

    Results from executing the program