Sei sulla pagina 1di 19

MICROCONTROLLER 8051

LABMANUAL

Contents I. PROGRAMMING 1. Data Transfer - Block move, Exchange, Sorting, Finding largest element in an array. 2. Arithmetic Instructions - Addition/subtraction, multiplication and division, square, Cube (16 bits Arithmetic operations bit addressable). 3. Counters. 4. Boolean & Logical Instructions (Bit manipulations). 5. Conditional CALL & RETURN. 6. Code conversion: BCD ASCII; ASCII Decimal; Decimal ASCII; HEX - Decimal and Decimal HEX. 7. Programs to generate delay, Programs using serial port and onChip timer/ counter. II. INTERFACING Write C programs to interface 8051 chip to Interfacing modules to develop single chip solutions. 8. Simple Calculator using 6 digit seven segment display and Hex Keyboard interface to 8051. 9. Alphanumeric LCD panel and Hex keypad input interface to 8051. 10. External ADC and Temperature control interface to 8051. 11. Generate different waveforms Sine, Square, Triangular, Ramp etc. using DAC interface to 8051; change the frequency and amplitude. 12. Stepper and DC motor control interface to 8051. 13. Elevator interface to 8051.

Assembly Programming

1. Write an ALP to move block of data bytes present in internal memory with starting address 10h and ending address 20h to the destination memory with starting address 30h. (Without overlap). Address 9000 Label Mnemonic MOV R1,#10H MOV R2,#20H MOV R0,#30H CLR C MOV A,R2 SUBB A,R1 MOV R2,A MOV A,@R1 MOV @R0,A INC R1 INC R0 DJNZ R2,LOOP LCALL 0003 Comment Starting addr of src Ending addr of src Starting addr of desti Determination of size And stored in R2

LOOP

Copy data byte

2. Write an ALP to move block of data bytes present in internal memory with starting address 10h and ending address 20h to the destination memory with starting address 15h. (With overlap). Address 9000 Label Mnemonic MOV R1,#10H MOV R2,#20H MOV R0,#15H CLR C MOV A,R2 SUBB A,R1 MOV R2,A MOV A,R1 ADD A,R2 MOV R1,A MOV A,R0 ADD A,R2 MOV R0,A INC R2 MOV A,@R1 MOV @R0,A DEC R1 DEC R0 DJNZ R2,LOOP LCALL 0003 Comment Starting addr of src Ending addr of src Starting addr of desti Determination of size And stored in R2

End addr of src

End addr of desti

LOOP

Copy data byte

3. Write an ALP to move block of data bytes present in external memory with starting address 8000h to the destination memory with starting address 9000h and size of array is 10h. Address 8500 Label Mnemonic MOV R0,#10H MOV 82H,#00H MOV 83H,#80H MOVX A,@DPTR MOV 83H,#90H MOV @DPTR,A INC DPTR DJNZ R0,LOOP LCALL 0003H Comment Size of an array DPL=00 DPH=80 Src data to acc DPH=90 Acc to desti

LOOP

4. Write an ALP to exchange block of data bytes present in external memory. Starting address of first is 8000h and starting address of other block 9000h and size of array is 10h. Address 8500 Label Mnemonic MOV R0,#10H MOV 82H,#00H MOV 83H,#80H MOVX A,@DPTR MOV R1,A MOV 83H,#90H MOV A,@DPTR XCH A,R1 MOVX @DPTR,A MOV 83H,#80H MOV A,R1 MOVX @DPTR,A INC DPTR DJNZ R0,LOOP LCALL 0003H Comment Size of an array DPL=00h DPH=80h Src data to acc DPH=90h

LOOP

DPH=80h

5. Write an ALP to sort a given array present in external memory with a starting address 9000h and size of an array is 10h using bubble sort technique. Address 8000 Mnemonic MOV R1,#10H OUTLOOP MOV R0,#10H MOV DPTR,#9000H INLOOP CLR C MOVX A,@DPTR MOV R2,A INC DPTR MOVX A,@DPTR MOV R3,A SUBB A,R3 JNC SKIP XCH A,R2 SKIP MOVX @DPTR,A DEC 82H MOV A,R2 MOVX @DPTR,A INC DPTR DEC R0 CJNE R0,#01H,INLOOP DEC R1 CJNE R1,#01H,OUTLOOP LCALL 0003H Label Comment Outer loop count Inner loop count Carry=0 R2=first no Acc=second no Compare Exchange Big no mem DPL=DPL-1 Small no mem

6. Write an ALP to add n bytes stored in external RAM (Starting address 9000 and no of bytes is 10 or 0Ah) Address 8000 Label Mnemonic MOV R0,#0A MOV R1,#00 MOV DPTR,#9000 MOVX A,@DPTR ADD A,R1 MOV R1,A INC DPTR DJNZ R0,LOOP LCALL 0003
8

Comment No of bytes R1=SUM=0 DPTR=9000 Sum=sum+n[i]

LOOP

7. Write an ALP to find largest element in a given array present in external memory with a starting address 9000h and size of an array is 10h. Address 8000 Label Mnemonic MOV R0,#10H MOV DPTR,#9000H CLR C MOVX A,@DPTR MOV R2,A INC DPTR MOVX A,@DPTR MOV R3,A SUBB A,R3 JNC SKIP XCH A,R2 MOVX @DPTR,A DEC 82H MOV A,R2 MOVX @DPTR,A INC DPTR DEC R0 CJNE R0,#01H, LOOP INC DPTR MOV A,@DPTR LCALL 0003H Comment count Carry=0 R2=first no Acc=second no Compare Exchange Big no mem DPL=DPL-1 Small no mem

LOOP

SKIP

A=largest no

8. Write an ALP to search a byte in an array of bytes stored in external RAM. Address Label Mnemonic Comment 8000 MOV R0,#0A Array size MOV R1,#10 Search value MOV R2,#00 Count MOV DPTR,#9000 MOVX A,@DPTR CLR C SUBB A,R1 compare INC DPTR JNZ SKIP INC R2 DJNZ R0,LOOP
9

LCALL 0003

10

9. Write an ALP to illustrate addition, subtraction, multiplication and division of two 8 bit numbers. Address 8000 Label Mnemonic MOV R1,#20H MOV R2,#10H MOV A,R1 ADD A,R2 MOV R0,A CLR C MOV A,R1 SUBB A,R2 MOV R3,A MOV A,R1 MOV F0,R2 MUL AB MOV R4,A MOV A,R1 MOV B,R2 DIV AB MOV R5,A LCALL 0003H Comment First no Second no ADDITION R0=R1+R2 SUBTRACTION R3=R1-R2 MULTIPLICATIO N R4=R1xR2

Division R5=R1/R2

10. Write an ALP to illustrate logical operations like AND, OR, NOT and XOR Address 8000 Label Mnemonic MOV R1,#20H MOV R2,#10H MOV A,R1 ANL A,R2 MOV R0,A MOV A,R1 ORL A,R2 MOV R3,A MOV A,R1 CPL A MOV R4,A MOV A,R1 XRL A,R2 MOV R5,A LCALL 0003H
11

Comment First BYTE Second BYTE ANDING R0=R1 AND R2 ORING R3=R1 OR R2 NEGATION R4=~R1 XORING R5=R1 XOR R2

12

11. Write an ALP to add two 2 byte numbers. Address 8000 Label Mnemonic MOV R1,#12 MOV R2,#34 MOV R3,#56 MOV R4,#78 MOV R7,#00 CLR C MOV A,R1 ADD A,R3 MOV R5,A MOV A,R2 ADDC A,R4 MOV R6,A JNC SKIP MOV R7,#01 LCALL 0003 Comment 3412 7856 ------0AC68 3rd byte=0

3rd byte=1

SKIP

12. Write an ALP to subtract 2 byte number from another 2 byte number. Address 8000 Label Mnemonic MOV R1,#56 MOV R2,#78 MOV R3,#12 MOV R4,#34 CLR C MOV A,R1 SUBB A,R3 MOV R5,A MOV A,R2 SUBB A,R4 MOV R6,A LCALL 0003
13

Comment 7856 3412 ------4444

13. Write an ALP to illustrate hexadecimal up counter with a given staring and ending value. Address 8000 Label MAIN LOOP Mnemonic MOV A,#00 MOV F0,#FF MOV R6,A MOV R3,A LCALL 677D MOV R0,#FF MOV R1,#FF LCALL 6850 MOV R0,#FF MOV R1,#FF MOV A,R3 INC A CJNE A,F0,LOOP LJMP MAIN Comment Starting value Ending value

Display R6 data

Delay

Next value

14. Write an ALP to illustrate hexadecimal down counter with a given staring and ending value. Address 8000 Label MAIN LOOP Mnemonic MOV A,#FF MOV F0,#00 MOV R6,A MOV R3,A LCALL 677D MOV R0,#FF MOV R1,#FF LCALL 6850 MOV R0,#FF MOV R1,#FF MOV A,R3 DEC A CJNE A,F0,LOOP LJMP MAIN Comment Starting value Ending value

Display R6 data

Delay

Next value

14

15. Write an ALP to illustrate decimal up counter with a given staring and ending value. Address 8000 Label MAIN LOOP Mnemonic MOV A,#00 MOV F0,#99 ADD A,#00 DA A MOV R6,A MOV R3,A LCALL 677D MOV R0,#FF MOV R1,#FF LCALL 6850 MOV R0,#FF MOV R1,#FF MOV A,R3 INC A CJNE A,F0,LOOP LJMP MAIN Comment Starting value Ending value

Display R6 data

Delay

Next value

16. Write an ALP to illustrate decimal down counter with a given staring and ending value. Address 8000 Label MAIN LOOP Mnemonic MOV A,#99 MOV F0,#00 ADD A,#00 DA A MOV R6,A MOV R3,A LCALL 677D MOV R0,#FF MOV R1,#FF LCALL 6850 MOV R0,#FF MOV R1,#FF MOV A,R3 DEC A CJNE A,F0,LOOP LJMP MAIN
15

Comment Starting value Ending value

Display R6 data

Delay

Next value

17. Write an ALP to demonstrate call and return instruction using a program to find factorial of a number. Address 8000 Label Mnemonic MOV R0,#05 MOV A,R0 LCALL 9000 LCALL 0003 Comment Input number LCALL FACT CJNE R0,#01,LOOP

9000 9005

FACT LOOP

CJNE R0,#01,9005 RET DEC R0 MOV F0,R0 MUL AB LJMP 9000

LJMP FACT

18. Write an ALP to convert decimal number to its equivalent hexadecimal number. Address 8000 Label Mnemonic MOV R0,#16 MOV A,R0 ANL A,#F0 SWAP A MOV F0,#0A MUL AB MOV R1,A MOV A,R0 ANL A,#0F ADD A,R1 MOV R1,A LCALL 0003 Comment R0=Input byte

R1=Output byte

16

19. Write an ALP to convert hexadecimal number to its equivalent decimal number. Address 8000 Label Mnemonic MOV R0,#FF MOV A,R0 MOV F0,#64 DIV AB MOV R1,A MOV A,B MOV F0,#0A DIV AB MOV R2,A MOV A,B MOV R3,F0 MOV A,R2 SWAP A ADD A,R3 MOV R2,A LCALL 0003 Comment Input no B=64h First dgt B=0Ah Second dgt Third dgt Pack R2 & R3 to R2

20. Write an ALP to convert decimal number to its equivalent ASCII code. Address 8000 Label Mnemonic MOV R1,#0B MOV A,R1 LCALL 9000 LCALL 0003 CLR C SUBB A,#0A MOV A,R1 JC 9009 ADD A,#37 RET ADD A,#30 RET Comment Input char LCALL CONV

9000 9001 9003 9004 9006 9008 9009 900B

CONV

JC DGT ASCII(CHAR) ASCII(NUMBER)

DGT

17

21. Write an ALP to convert ASCII code to its equivalent decimal number. Address 8000 Label Mnemonic MOV R1,#39 MOV A,R1 LCALL 9000 LCALL 0003 CLR C SUBB A,#41 MOV A,R1 JC DGT CLR C SUBB A,#37 RET CLR C SUBB A,#30 RET Comment Input char LCALL CONV

9000

CONV

ASCII CHAR

DGT

ASCII NUMBER

22. Write an ALP to convert BCD to its equivalent ASCII code. Address 8000 Label Mnemonic MOV R0,#23 LCALL 9000 LCALL 0003 MOV A,R0 ANL A,#0F ADD A,#30 MOV R1,A MOV A,R0 ANL A,#F0 SWAP A ADD A,#30 MOV R2,A RET
18

Comment Input char LCALL CONV

9000

CONV

ASCII(FIRST DGT)

ASCII(SECOND DGT)

19

Potrebbero piacerti anche