microcontroller 8051

85
MICROCONTROLLER 8051 LABMANUAL 1

Upload: hai-akhil

Post on 25-Oct-2014

391 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: MICROCONTROLLER 8051

MICROCONTROLLER 8051

LABMANUAL

1

Page 2: MICROCONTROLLER 8051

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 on-Chip 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.

2

Page 3: MICROCONTROLLER 8051

3

Page 4: MICROCONTROLLER 8051

AssemblyProgramming

4

Page 5: MICROCONTROLLER 8051

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 Label Mnemonic Comment9000 MOV R1,#10H Starting addr of src

MOV R2,#20H Ending addr of src

MOV R0,#30HStarting addr of desti

CLR C

MOV A,R2Determination of size

SUBB A,R1 And stored in R2MOV R2,A

LOOP MOV A,@R1Copy data byte

MOV @R0,AINC R1INC R0DJNZ R2,LOOPLCALL 0003

5

Page 6: MICROCONTROLLER 8051

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 Label Mnemonic Comment9000 MOV R1,#10H Starting addr of src

MOV R2,#20H Ending addr of src

MOV R0,#15HStarting addr of desti

CLR C

MOV A,R2Determination of size

SUBB A,R1And stored in R2

MOV R2,AMOV A,R1ADD A,R2 End addr of srcMOV R1,AMOV A,R0ADD A,R2 End addr of destiMOV R0,AINC R2

LOOP MOV A,@R1Copy data byte

MOV @R0,ADEC R1DEC R0DJNZ R2,LOOPLCALL 0003

6

Page 7: MICROCONTROLLER 8051

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 Label Mnemonic Comment8500 MOV R0,#10H Size of an array

MOV 82H,#00H DPL=00LOOP MOV 83H,#80H DPH=80

MOVX A,@DPTR Src data to accMOV 83H,#90H DPH=90MOV @DPTR,A Acc to destiINC DPTRDJNZ R0,LOOPLCALL 0003H

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 Label Mnemonic Comment8500 MOV R0,#10H Size of an array

MOV 82H,#00H DPL=00hLOOP MOV 83H,#80H DPH=80h

MOVX A,@DPTR Src data to accMOV R1,AMOV 83H,#90H DPH=90hMOV A,@DPTRXCH A,R1MOVX @DPTR,AMOV 83H,#80H DPH=80hMOV A,R1MOVX @DPTR,AINC DPTRDJNZ R0,LOOPLCALL 0003H

7

Page 8: MICROCONTROLLER 8051

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 Label Mnemonic Comment

8000 MOV R1,#10HOuter loop count

OUTLOOP MOV R0,#10HInner loop count

MOV DPTR,#9000HINLOOP CLR C Carry=0

MOVX A,@DPTRMOV R2,A R2=first noINC DPTR

MOVX A,@DPTRAcc=second no

MOV R3,ASUBB A,R3 CompareJNC SKIPXCH A,R2 Exchange

SKIP MOVX @DPTR,A Big nomemDEC 82H DPL=DPL-1MOV A,R2MOVX @DPTR,A Small nomemINC DPTRDEC R0CJNE R0,#01H,INLOOPDEC R1CJNE R1,#01H,OUTLOOPLCALL 0003H

6. Write an ALP to add ‘n’ bytes stored in external RAM (Starting address 9000 and no of bytes is 10 or 0Ah)

Address Label Mnemonic Comment8000 MOV R0,#0A No of bytes

MOV R1,#00 R1=SUM=0MOV DPTR,#9000 DPTR=9000

LOOP MOVX A,@DPTR

ADD A,R1Sum=sum+n[i]

MOV R1,A8

Page 9: MICROCONTROLLER 8051

INC DPTRDJNZ R0,LOOPLCALL 0003

9

Page 10: MICROCONTROLLER 8051

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 Label Mnemonic Comment8000 MOV R0,#10H count

MOV DPTR,#9000HLOOP CLR C Carry=0

MOVX A,@DPTRMOV R2,A R2=first noINC DPTR

MOVX A,@DPTRAcc=second no

MOV R3,ASUBB A,R3 CompareJNC SKIPXCH A,R2 Exchange

SKIP MOVX @DPTR,A Big nomemDEC 82H DPL=DPL-1MOV A,R2MOVX @DPTR,A Small nomemINC DPTRDEC R0CJNE R0,#01H, LOOPINC DPTRMOV A,@DPTR A=largest noLCALL 0003H

8. Write an ALP to search a byte in an array of bytes stored in external RAM.

Address Label Mnemonic Comment8000 MOV R0,#0A Array size

MOV R1,#10 Search valueMOV R2,#00 CountMOV DPTR,#9000MOVX A,@DPTRCLR CSUBB A,R1 compareINC DPTRJNZ SKIPINC R2DJNZ R0,LOOP

10

Page 11: MICROCONTROLLER 8051

LCALL 0003

11

Page 12: MICROCONTROLLER 8051

9. Write an ALP to illustrate addition, subtraction, multiplication and division of two 8 bit numbers.

Address Label Mnemonic Comment8000 MOV R1,#20H First no

MOV R2,#10H Second noMOV A,R1 ADDITIONADD A,R2 R0=R1+R2MOV R0,ACLR C SUBTRACTIONMOV A,R1 R3=R1-R2SUBB A,R2MOV R3,A

MOV A,R1MULTIPLICATION

MOV F0,R2 R4=R1xR2MUL ABMOV R4,AMOV A,R1 DivisionMOV B,R2 R5=R1/R2DIV ABMOV R5,ALCALL 0003H

10. Write an ALP to illustrate logical operations like AND, OR, NOT and XOR

Address Label Mnemonic Comment8000 MOV R1,#20H First BYTE

MOV R2,#10H Second BYTEMOV A,R1 ANDINGANL A,R2 R0=R1 AND R2MOV R0,AMOV A,R1 ORINGORL A,R2 R3=R1 OR R2MOV R3,AMOV A,R1 NEGATIONCPL A R4=~R1MOV R4,AMOV A,R1 XORINGXRL A,R2 R5=R1 XOR R2MOV R5,ALCALL 0003H

12

Page 13: MICROCONTROLLER 8051

13

Page 14: MICROCONTROLLER 8051

11. Write an ALP to add two 2 byte numbers.

Address Label Mnemonic Comment8000 MOV R1,#12 3412

MOV R2,#34 7856

MOV R3,#56-------

0AC68MOV R4,#78MOV R7,#00 3rd byte=0CLR CMOV A,R1ADD A,R3MOV R5,AMOV A,R2ADDC A,R4MOV R6,AJNC SKIPMOV R7,#01 3rd byte=1

SKIP LCALL 0003

12. Write an ALP to subtract 2 byte number from another 2 byte number.

Address Label Mnemonic Comment8000 MOV R1,#56 7856

MOV R2,#78 3412

MOV R3,#12-------

4444MOV R4,#34CLR CMOV A,R1SUBB A,R3MOV R5,AMOV A,R2SUBB A,R4MOV R6,ALCALL 0003

14

Page 15: MICROCONTROLLER 8051

13. Write an ALP to illustrate hexadecimal up counter with a given staring and ending value.

Address Label Mnemonic Comment8000 MAIN MOV A,#00 Starting value

MOV F0,#FF Ending valueLOOP MOV R6,A

MOV R3,A

LCALL 677DDisplay R6 data

MOV R0,#FFMOV R1,#FFLCALL 6850 DelayMOV R0,#FFMOV R1,#FFMOV A,R3INC A Next valueCJNE A,F0,LOOPLJMP MAIN

14. Write an ALP to illustrate hexadecimal down counter with a given staring and ending value.

Address Label Mnemonic Comment8000 MAIN MOV A,#FF Starting value

MOV F0,#00 Ending valueLOOP MOV R6,A

MOV R3,A

LCALL 677DDisplay R6 data

MOV R0,#FFMOV R1,#FFLCALL 6850 DelayMOV R0,#FFMOV R1,#FFMOV A,R3DEC A Next valueCJNE A,F0,LOOPLJMP MAIN

15

Page 16: MICROCONTROLLER 8051

15. Write an ALP to illustrate decimal up counter with a given staring and ending value.

Address Label Mnemonic Comment8000 MAIN MOV A,#00 Starting value

MOV F0,#99 Ending valueLOOP ADD A,#00

DA AMOV R6,AMOV R3,A

LCALL 677DDisplay R6 data

MOV R0,#FFMOV R1,#FFLCALL 6850 DelayMOV R0,#FFMOV R1,#FFMOV A,R3INC A Next valueCJNE A,F0,LOOPLJMP MAIN

16. Write an ALP to illustrate decimal down counter with a given staring and ending value.

Address Label Mnemonic Comment8000 MAIN MOV A,#99 Starting value

MOV F0,#00 Ending valueLOOP ADD A,#00

DA AMOV R6,AMOV R3,A

LCALL 677DDisplay R6 data

MOV R0,#FFMOV R1,#FFLCALL 6850 DelayMOV R0,#FFMOV R1,#FFMOV A,R3DEC A Next valueCJNE A,F0,LOOP

16

Page 17: MICROCONTROLLER 8051

LJMP MAIN

17

Page 18: MICROCONTROLLER 8051

17. Write an ALP to demonstrate call and return instruction using a program to find factorial of a number.

Address Label Mnemonic Comment8000 MOV R0,#05 Input number

MOV A,R0LCALL 9000 LCALL FACTLCALL 0003

9000 FACT CJNE R0,#01,9005

CJNE R0,#01,LOOP

RET9005 LOOP DEC R0

MOV F0,R0MUL ABLJMP 9000 LJMP FACT

18. Write an ALP to convert decimal number to its equivalent hexadecimal number.

Address Label Mnemonic Comment8000 MOV R0,#16 R0=Input byte

MOV A,R0ANL A,#F0SWAP AMOV F0,#0AMUL ABMOV R1,AMOV A,R0ANL A,#0FADD A,R1

MOV R1,AR1=Output byte

LCALL 0003

18

Page 19: MICROCONTROLLER 8051

19. Write an ALP to convert hexadecimal number to its equivalent decimal number.

Address Label Mnemonic Comment8000 MOV R0,#FF Input no

MOV A,R0MOV F0,#64 B=64hDIV ABMOV R1,A First dgtMOV A,BMOV F0,#0A B=0AhDIV ABMOV R2,A Second dgtMOV A,BMOV R3,F0 Third dgtMOV A,R2

SWAP APack R2 & R3 to

ADD A,R3R2

MOV R2,ALCALL 0003

20. Write an ALP to convert decimal number to its equivalent ASCII code.

Address Label Mnemonic Comment8000 MOV R1,#0B Input char

MOV A,R1LCALL 9000 LCALL CONVLCALL 0003

9000 CONV CLR C9001 SUBB A,#0A9003 MOV A,R19004 JC 9009 JC DGT9006 ADD A,#37 ASCII(CHAR)9008 RET

9009 DGT ADD A,#30ASCII(NUMBER)

900B RET

19

Page 20: MICROCONTROLLER 8051

21. Write an ALP to convert ASCII code to its equivalent decimal number.

Address Label Mnemonic Comment8000 MOV R1,#39 Input char

MOV A,R1LCALL 9000 LCALL CONVLCALL 0003

9000 CONV CLR CSUBB A,#41MOV A,R1JC DGTCLR CSUBB A,#37 ASCIICHARRET

DGT CLR CSUBB A,#30 ASCIINUMBERRET

22. Write an ALP to convert BCD to its equivalent ASCII code.

Address Label Mnemonic Comment8000 MOV R0,#23 Input char

LCALL 9000 LCALL CONVLCALL 0003

9000 CONV MOV A,R0ANL A,#0FADD A,#30

MOV R1,A

ASCII(FIRST DGT)

MOV A,R0ANL A,#F0SWAP AADD A,#30

MOV R2,A

ASCII(SECOND DGT)

RET

20

Page 21: MICROCONTROLLER 8051

21