You are on page 1of 37

SATHYABAMA UNIVERSITY

JEPPIAAR NAGAR, RAJIV GANDHI SALAI,


CHENNAI - 600119

DEPARTMENT OF
ELECTRONICS AND TELECOMMUNICATION
ENGINEERING
LABORATORY MANUAL

SEC4066

MICROCONTROLLERS LAB

(BATCH 2015-2019)
V SEMESTER

NAME: _______________________
ROLL NO: _____________________
REGISTER NO: _________________

Lab Conducted by Department of Electronics and Telecommunication Engineering


SATHYABAMA UNIVERSITY DEPT. OF ETCE

SYLLABUS

Total
SECX4034 MICROCONTROLLERS AND EMBEDDED SYSTEMS LAB L T P Credits
Marks
(Common to ECE & ETCE)
0 0 4 2 100

LIST OF EXPERIMENTS
I. MICROCONTROLLER LAB

1. Study of 8051.
2. 8 Bit Arithmetic Operations: Addition, subtraction, multiplication and division
3. Addition of two sixteen bit numbers.
4. Transferring a Block of data from
(i) Internal to External memory,
(ii) External to External memory and,
(iii) External to Internal memory
5. 8-bit Conversion
(i) ASCII to its equivalent Hexadecimal and
(ii) Hexadecimal to its equivalent ASCII
6. Arrange the given numbers in ascending and descending order
7. Filling External and Internal Memory

II. EMBEDDED SYSTEMS LAB

1. DAC Interfacing
2. Stepper motor Interfacing
3. Servo motor Interfacing
4. ADC Interfacing
5. Elevator Interfacing

MICROCONTROLLERS LABORATORY
2 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

MICROCONTROLLER LAB
LIST OF EXPERIMENTS
INDEX
SL. EXERCISE PAGE
EXPERIMENTS
NO NUMBER NUMBER
1. 1. STUDY OF 8051. 7
2. 2. 8 BIT ARITHMETIC OPERATIONS: ADDITION, SUBTRACTION,
13
MULTIPLICATION AND DIVISION
3. 3. ADDITION OF TWO SIXTEEN BIT NUMBERS. 22
4. 4. TRANSFERRING A BLOCK OF DATA FROM
(I) INTERNAL TO EXTERNAL MEMORY,
25
(II) EXTERNAL TO EXTERNAL MEMORY AND,
(III) EXTERNAL TO INTERNAL MEMORY
5. 5. 8-BIT CONVERSION
(I) ASCII TO ITS EQUIVALENT HEXADECIMAL AND 30
(II) HEXADECIMAL TO ITS EQUIVALENT ASCII
6. 6. ARRANGE THE GIVEN NUMBERS IN ASCENDING AND
33
DESCENDING ORDER
7. 7. FILLING EXTERNAL AND INTERNAL MEMORY 36

MICROCONTROLLERS LABORATORY
3 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

KEYING PROCEDURE FOR 8051 KIT

1) For Assembling a Program

i) Type A followed by starting address then press Enter.


Example:
-A8000
ii) Type the instruction using MNEMONICS.
iii) For keying new line press ENTER and type the instruction.
iv) Press ENTER key twice to exit after keying the program

2) For Checking the program

i) Type D followed by the same starting address then press Enter.


Example:
D8000
ii) For checking next line press SPACEBAR key
iii) Press ENTER key after checking all the instructions

2) For Keying Input

i) Type I / M followed by memory address then press Enter.


Example:
I40 (FOR INTERNAL MEMORY)
M9000 (FOR EXTERNAL MEMORY)

4) For Executing Program

i) Type G followed by same starting address then press Enter.


Example:
-G8000

5) For Viewing Output

i) Type I / M followed by Output memory address then press Enter.


Example:
I60 (FOR INTERNAL MEMORY)
M9100 (FOR EXTERNAL MEMORY)

MICROCONTROLLERS LABORATORY
4 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
LAYOUT OF 8051 MICROCONTROLLER KIT

MICROCONTROLLERS LABORATORY
5 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
RECORD NOTEBOOK / OBSERVATION WRITING FORMAT

LEFT HAND SIDE RIGHT HAND SIDE


(UNRULED) (RULED)
1 FLOWCHART 1 AIM
2 SAMPLE INPUT & OUTPUT 2 APPARATUS REQUIRED
3 ALGORITHM
4 PROGRAM
5 RESULT

PROGRAM:

MEMORY HEX MNEMONICS


LABEL COMMENTS
ADDRESS CODE OPCODE OPERAND

SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
MEMORY MEMORY
DATA DATA
ADDRESS ADDRESS

MICROCONTROLLERS LABORATORY
6 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

8051 MICROCONTROLLER PROGRAMMING


Expt. No: 1
Date:
1. STUDY OF 8051.

AIM:
To study the 8051 Microcontroller

THEORY:

The INTEL 8051 is an 8 bit microcontroller with 128 byte internal RAM and 4K bytes
internal ROM.

The 8051 is a 40 pin IC available in Dual in line package (DIP) and it requires a
single power supply of +5V.

BLOCK DIAGRAM OF 8051 MICROCONTROLLER.

Figure 1.1 8051 Block Diagram

MICROCONTROLLERS LABORATORY
7 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
The 8051 architecture consists of the following special features.

8 bit CPU with registers A and B


16 bit Program Counter(PC) and Data Pointer(DPTR)
8 bit Program Status Word(PSW)
8 bit Stack Pointer(SP)
Internal ROM or EPROM of 4K bytes
Internal RAM of 128 bytes.
o 4 Register banks , each containing 8 registers
o 16 bytes ,which may be addressed at the bit level
o 80 bytes of general purpose memory
32 input / output pins are arranged as four 8 bit ports : P0-P3
Two 16 bit timer / counters : T0 and T1
Full duplex serial data receiver / transmitter : SBUF
Control registers :TCON,TMOD,SCON,PCON,IP and IE
2 external and 3 internal interrupt sources
Oscillator and Clock circuits.

The 8051 Oscillator and Clock:

The heart of the 8051 is the circuitry that generates the clock pulses
by which all internal operations are synchronized.
Pins XTAL1 and XTAL2 are provided for connecting a resonant
network to form an oscillator.

Program Counter (PC) and Data Pointer (DPTR):

PC and DPTR are used to hold the address of a byte in memory.


PC is the only register that does not have an internal address.
The DPTR register is made up of two 8 bit registers namely DPH
and DPL.
DPTR does not have a single internal address.

A and B CPU Registers:

The 8051 contains 34 general purpose or working registers.


Two of these registers A and B hold results of many instructions,
particularly math and logical operations.

MICROCONTROLLERS LABORATORY
8 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
The other 32 are arranged as part of internal RAM in four banks B0-
B3 each of 8 registers.
The A(accumulator) register is the most versatile of the 2 CPU
registers
The B register is used with A register for Multiplication and Division
operation.
Flags and Program Status Word(PSW):

Flags are 1 bit registers provided to store the results of certain


program instructions.
The 8051 has 4 math flags include Carry(C),Auxiliary
Carry(AC),Overflow(OV) and Parity(P)
The 8051 has 2 User fags namely GF0,GF1
The PSW contains the math flags, user program flag F0 and
register select bits that identify which of the 4 general purpose
register banks is currently in use by the program

Internal Memory:

The 8051 has internal RAM and ROM memory.


Additional memory can be added externally using suitable circuits.

MICROCONTROLLERS LABORATORY
9 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

Figure 1.2 Internal RAM Organization

Internal RAM:
The 128 byte internal RAM address from 00h to 7Fh
32 bytes from 00h to 1Fh - 32 working registers
16 bytes from 20h to 2Fh - 128 addressable bits
from 30h to 7Fh - General purpose

Internal ROM:
An internal ROM occupies code address space 0000h to
0FFFh

Stack and Stack Pointer (SP):

The Stack refers to an area of internal RAM that is used in


conjunction with certain opcodes to store and retrieve data quickly.

MICROCONTROLLERS LABORATORY
10 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
The 8 bit SP register is used to hold an internal RAM address that
is top of the stack.

Special Function Registers (SFR):

The 8051 operations that do not use the internal 128 byte RAM
addresses from 00h to 7Fh are done by a group of special internal
registers each called a Special Function Register(SFR).
Its addresses from 80h to FFh

Interrupts:

5 interrupts are provided in 8051


3 interrupt Timer Flag Interrupts (FF0, TF1) and serial port
interrupts (RI or TI) are generated internally.
2 interrupts INT0 and INT1 are generated externally.

MICROCONTROLLERS LABORATORY
11 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Pin Diagram:
The 8051 is a 40 pin IC available in Dual in line package (DIP)

Figure 1.3 Pin diagram of 8051

RESULT:
Thus the 8051 was studied

MICROCONTROLLERS LABORATORY
12 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 2
Date:

2. 8 BIT ARITHMETIC OPERATIONS:


ADDITION, SUBTRACTION, MULTIPLICATION AND DIVISION

2.1.1. Addition of two 8-bit numbers stored in internal memory


2.1.2. Subtraction of two 8-bit numbers stored in internal memory.
2.1.3. Multiplication of two 8-bit numbers stored in internal memory.
2.1.4. Division of two 8-bit numbers stored in internal memory.

2.2.1. Addition of two 8-bit numbers stored in external memory


2.2.2. Subtraction of two 8-bit numbers stored in external memory.
2.2.3. Multiplication of two 8-bit numbers stored in external memory.
2.2.4. Division of two 8-bit numbers stored in external memory.

Exercise 2.1.1:
Write a program to add two 8-bit numbers stored in internal memory of 8051.
2.1.1 AIM:

To write an ALP for 8051 kit to add two 8-bit numbers stored in its internal memory.

2.1.1 ALGORITHM:
Step 1 : Start.
Step 2 : Load the first data into Register A
Step 3 : Load the second data in register R0
Step 4 : Add the second data with first data
Step 5 : Store the result in destination address
Step 6 : Copy the Carry in Register A and then store it in destination
Step 7 : End program

2.1.1 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV A, 40 Copy first data in Reg A
MOV R0, 41 Copy second data in R0
ADD A, R0 Add First and second data
MOV 42, A Store the Sum in destination
MOV A, #00 Initiate Reg A with 00H
ADDC A, #00 Copy Carry into Reg A
MOV 43, A Store carry in new destination
LCALL 00BB End the program

MICROCONTROLLERS LABORATORY
13 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.1.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 42
41 43

2.1.1 RESULT:

An assembly language program using 8051 kit to perform the addition of two 8-bit
numbers stored in internal memory is written and executed

Exercise 2.1.2:
Write a program to subtract two 8-bit numbers stored in internal memory of 8051.
2.1.2 AIM:

To write an ALP for 8051 kit to subtract two 8-bit numbers stored in its internal memory.

2.1.2 ALGORITHM:
Step 1 : Start.
Step 2 : Clear the Carry/ Borrow Flag
Step 3 : Load first data in register A
Step 4 : Load second data in register R0
Step 5 : Subtract the second data from first data
Step 6 : Store the result in destination address
Step 7 : Copy the Borrow in Register A and then store it in destination
Step 8 End program

2.1.2 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 CLR C Clear Carry/Borrow
MOV A, 40 Load first data in Reg A
MOV R0, 41 Load Second data in R0
SUBB A,R0 Subtract second data from First data
MOV 42, A Store the Result
MOV A, #00 Initiate Reg A with 00H
ADDC A, #00 Copy Borrow into Reg A
MOV 43, A Store borrow in new destination
LCALL 00BB End the program
Note: If first number is smaller than second number, compliment of answer is stored and
carry shall be 1.

MICROCONTROLLERS LABORATORY
14 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.1.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 42
41 43

2.1.2 RESULT:

An assembly language program using 8051 kit to perform the subtraction of two 8-bit
numbers stored in internal memory is written and executed

Exercise 2.1.3:
Write a program to multiply two 8-bit numbers stored in internal memory of 8051.
2.1.3 AIM:

To write an ALP for 8051 kit to multiply two 8-bit numbers stored in its internal memory.

2.1.3 ALGORITHM:
Step 1 : Start.
Step 2 : Load first data in register A
Step 3 : Load second data in register B
Step 4 : Multiply both data in A and B
Step 5 : Store the Lower byte result in A to destination address
Step 6 : Store the higher byte in reg B in next destination address through Reg A
Step 7 : End program

2.1.3 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV A, 40 Load first data in A
MOV 0F0, 41 Load second data in B (0F0)
MUL AB Multiply two numbers
MOV 42, A Store Lower byte Product
MOV A, 0F0 Copy higher byte into A
MOV 43, A Store higher byte into the location
LCALL 00BB End the program

2.1.3 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 42
41 43

MICROCONTROLLERS LABORATORY
15 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.1.3 RESULT:

An assembly language program using 8051 kit to perform the multiplication of two 8-
bit numbers stored in internal memory is written and executed

Exercise 2.1.4:
Write a program to divide two 8-bit numbers stored in internal memory of 8051.
2.1.4 AIM:

To write an ALP for 8051 kit to divide two 8-bit numbers stored in its internal memory.

2.1.4 ALGORITHM:
Step 1 : Start.
Step 2 : Load Numerator in Reg A
Step 3 : Load Denominator in Reg B
Step 4 : Divide Numerator by Denominator
Step 5 : Quotient in reg A stored in Destination address
Step 6 : Remainder in reg B stored in Destination address through reg A
Step 7 : End program

2.1.4 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV A, 40 Load Numerator in Reg A
MOV 0F0, 41 Load Denominator in Reg B
DIV AB Divide Reg A by B
MOV 42, A Store Quotient in Destination address
MOV A, 0F0 Copy Remainder in A
MOV 43, A Store Remainder Destination address
LCALL 00BB End the program

2.1.4 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 42
41 43

2.1.4 RESULT:

An assembly language program using 8051 kit to perform the division of two 8-bit
numbers stored in internal memory is written and executed

MICROCONTROLLERS LABORATORY
16 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

Exercise 2.2.1:
Write a program to add two 8-bit numbers stored in external memory of 8051.
2.2.1 AIM:

To write an ALP for 8051 kit to add two 8-bit numbers stored in its external memory.

2.2.1 ALGORITHM:
Step 1 : Start.
Step 2 : Load the first data into Register R0 through Reg A
Step 3 : Load the second data in register A
Step 4 : Add the second data with first data
Step 5 : Store the result in destination address
Step 6 : Copy the Carry in Register A and then store it in destination
Step 7 : End program

2.2.1 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV DPTR, #8000 Initialize the Data Pointer to Source
MOVX A,@DPTR Load the first number into Reg A
MOV R0, A Copy the first number into R0
INC DPTR Increment data pointer
MOVX A,@DPTR Load the second number in reg A
ADD A, R0 Add both numbers
INC DPTR Increment data pointer
MOVX @DPTR, A Store the sum in next location
MOV A, #00 Initialize Reg A to zero
ADDC A,#00 Copy Carry into Reg A
INC DPTR Increment DPTR
MOVX @DPTR,A Store the Carry in Next location
LCALL 00BB End the program

2.2.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8002
8001 8003

2.2.1 RESULT:

An assembly language program using 8051 kit to perform the addition of two 8-bit
numbers stored in external memory is written and executed

MICROCONTROLLERS LABORATORY
17 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

Exercise 2.2.2:
Write a program to subtract two 8-bit numbers stored in external memory of 8051.
2.2.2 AIM:

To write an ALP for 8051 kit to subtract two 8-bit numbers stored in its external
memory.

2.2.2 ALGORITHM:
Step 1 : Start.
Step 2 : Clear the Carry/ Borrow Flag
Step 3 : Load first data in register A
Step 4 : Load second data in register R0
Step 5 : Subtract the second data from first data
Step 6 : Store the result in destination address
Step 7 : Copy the Carry in Register A and then store it in destination
Step 8 End program

2.2.2 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 CLR C Reset the Carry/ Borrow Flag
MOV DPTR, #8000 Initialize the Data Pointer to Source
MOVX A,@DPTR Load the first number into Reg A
MOV R0, A Copy the first number into R0
INC DPTR Increment data pointer
MOVX A,@DPTR Load the second number in reg A
XCH A, R0 Swap that first number comes in reg A
SUBB A, R0 Subtract second number from first
number
INC DPTR Increment data pointer
MOVX @DPTR, A Store the sum in next location
MOV A, #00 Initialize Reg A to zero
ADDC A,#00 Copy Carry into Reg A
INC DPTR Increment DPTR
MOVX @DPTR,A Store the Carry in Next location
LCALL 00BB End the program

Note: If first number is smaller than second number, compliment of answer is stored and
carry shall be 1.

MICROCONTROLLERS LABORATORY
18 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.2.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8002
8001 8003

2.2.2 RESULT:

An assembly language program using 8051 kit to perform the subtraction of two 8-bit
numbers stored in external memory is written and executed

Exercise 2.2.3:
Write a program to multiply two 8-bit numbers stored in external memory of 8051.
2.2.3 AIM:

To write an ALP for 8051 kit to multiply two 8-bit numbers stored in its external memory.

2.2.3 ALGORITHM:
Step 1 : Start.
Step 2 : Load first data in register A
Step 3 : Load second data in register B
Step 4 : Multiply both data in A and B
Step 5 : Store the Lower byte result in A to destination address
Step 6 : Store the higher byte in reg B in next destination address through Reg A
Step 7 : End program

2.2.3 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 CLR C Reset the Carry/ Borrow Flag
MOV DPTR, #8000 Initialize the Data Pointer to Source address
MOVX A,@DPTR Load the first number into Reg A
MOV 0F0, A Copy the first number into B
INC DPTR Increment data pointer
MOVX A,@DPTR Load the second number in reg A
MUL AB Multiply both the numbers
INC DPTR Increment data pointer
MOVX @DPTR, A Store the product in next location
MOV A, 0F0 Store the Carry in A from B
INC DPTR Increment DPTR
MOVX @DPTR,A Store the Carry in Next location
LCALL 00BB End the program

MICROCONTROLLERS LABORATORY
19 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.2.3 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8002
8001 8003

2.2.3 RESULT:

An assembly language program using 8051 kit to perform the multiplication of two 8-
bit numbers stored in external memory is written and executed

Exercise 2.2.4:
Write a program to divide two 8-bit numbers stored in external memory of 8051.
2.2.4 AIM:

To write an ALP for 8051 kit to divide two 8-bit numbers stored in its external memory.

2.2.4 ALGORITHM:
Step 1 : Start.
Step 2 : Load Numerator in Reg A
Step 3 : Load Denominator in Reg B
Step 4 : Divide Numerator by Denominator
Step 5 : Quotient in reg A stored in Destination address
Step 6 : Remainder in reg B stored in Destination address through reg A
Step 7 : End program

2.2.4 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 CLR C Reset the Carry/ Borrow Flag
MOV DPTR, #8000 Initialize the Data Pointer to Source
MOVX A,@DPTR Load the first number into Reg A
MOV 0F0, A Copy the first number into B
INC DPTR Increment data pointer
MOVX A,@DPTR Load the second number in reg A
XCH A, 0F0 Load Numerator in Reg A
DIV AB Divide first number by second number
INC DPTR Increment data pointer
MOVX @DPTR, A Store the Quotient in next location
MOV A, 0F0 Copy the Remainder in A from B
INC DPTR Increment DPTR
MOVX @DPTR,A Store the Remainder in Next location
LCALL 00BB End the program

MICROCONTROLLERS LABORATORY
20 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
2.2.4 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8002
8001 8003

2.2.4 RESULT:

An assembly language program using 8051 kit to perform the division of two 8-bit
numbers stored in external memory is written and executed

MICROCONTROLLERS LABORATORY
21 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 3
Date:
3. ADDITION OF TWO SIXTEEN BIT NUMBERS.
Exercise 3.1:
Write a program to add two 16-bit numbers stored in internal memory of 8051.
3.1 AIM:

To write an ALP for 8051 kit to add two 16-bit numbers stored in its internal memory.

3.1 ALGORITHM:
Step 1 : Start.
Step 2 : Copy the Lower byte of first data to register R0
Step 3 : Copy the Higher byte of first data to register R1
Step 4 : Copy the Lower byte of second data to register R2
Step 5 : Copy the Higher byte of second data to register R3
Step 6 : Add the lower bytes of both data in register R0 and R2
Step 7 : Store the lower byte sum in output address
Step 8 : Add the higher bytes of both data in register R1 and R3 with carry generated by
previous addition
Step 9 : Store the higher byte sum in output address
Step 10 : Copy the Carry generated into Reg A and store it in output address
Step 11 : End program

3.1 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV R0,40 Load Lower byte of first data in R0
MOV R1, 41 Load Higher byte of first data in R1
MOV R2, 42 Load Lower byte of Second data in R2
MOV R3, 43 Load Higher byte of second data in R3
MOV A,R0 Copy First Lower byte to Reg A
ADD A,R2 Add Both Lower Bytes
MOV 44,A Store the Lower byte SUM.
MOV A, R1 Copy First the Higher Byte to Reg A
ADDC A, R3 Add Both Higher Bytes with carry
MOV 45, A Store Higher Byte SUM
MOV A,#00 Initiate Reg A
ADDC A, #00 Copy CARRY into Reg A
MOV 46, A Store CARRY
LCALL 00BB End the program
3.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 44
41 45
42 46
43
3.1 RESULT:

MICROCONTROLLERS LABORATORY
22 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

An assembly language program using 8051 kit to perform the addition of two 16-bit
numbers stored in internal memory is written and executed

Exercise 3.2:
Write a program to add two 16-bit numbers stored in external memory of 8051.
3.2 AIM:

To write an ALP for 8051 kit to add two 16-bit numbers stored in its external memory.

3.2 ALGORITHM:
Step 1 : Start.
Step 2 : Initiate first data address with DPTR as pointer
Step 3 : Copy the Lower byte of first data to register R0 through DPTR
Step 4 : Copy the Higher byte of first data to register R1 through DPTR
Step 5 : Copy the Lower byte of second data to register R2 through DPTR
Step 6 : Copy the Higher byte of second data to register R3 through DPTR
Step 7 : Add the lower bytes of both data in register R0 and R2
Step 8 : Store the lower byte sum in output address
Step 9 : Add the lower bytes of both data in register R1 and R3 with carry generated by
previous addition
Step 10 : Store the higher byte sum in output address
Step 11 : Copy the Carry generated into Reg A and store it in output address
Step 12 : End program
3.2 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV DPTR, #8000 Initiate DPTR for first address
MOVX A, @DPTR Load First Lower byte in A
MOV R0, A Copy it to R0
INC DPTR Move DPTR to next address
MOVX A, @DPTR Load First Higher byte in A
MOV R1, A Copy it to R1
INC DPTR Move DPTR to next address
MOVX A, @DPTR Load Second Lower byte in A
MOV R2, A Copy it to R2
INC DPTR Move DPTR to next address
MOVX A, @DPTR Load Second Higher byte in A
MOV R3, A Copy it to R3
MOV A, R0 Copy First Lower byte to A
ADD A, R2 Add two Lower bytes
INC DPTR Move DPTR to Next Location
MOVX @DPTR, A Store Lower byte SUM
MOV A, R1 Copy First Higher byte in A
ADDC A, R3 Add Higher bytes with carry
INC DPTR Move DPTR to next location
MOVX @DPTR, A Store Higher Byte SUM
MOV A, #00 Initiate A
ADDC A, #00 Copy Carry in Reg A
INC DPTR Move DPTR to next location
MOVX @DPTR, A Store CARRY
LCALL 00BB End the program

MICROCONTROLLERS LABORATORY
23 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

3.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8004
8001 8005
8002 8006
8003
3.2 RESULT:

An assembly language program using 8051 kit to perform the addition of two 16-bit
numbers stored in external memory is written and executed

MICROCONTROLLERS LABORATORY
24 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 4
Date:
4. BLOCK DATA TRANSFER
4.1. Transferring a Block of data from internal memory to internal memory
4.2. Transferring a Block of data from internal memory to external memory
4.3. Transferring a Block of data from external memory to external memory
4.4. Transferring a Block of data from external memory to internal memory

Exercise 4.1:
Write a program to transfer a block of data stored in internal memory address
41H-45H to internal memory address 51H to 55 H.
4.1 AIM:

To write an ALP for 8051 kit to transfer a Block of data from internal memory (41H-45H
) to internal memory (51H to 55 H).

4.1 ALGORITHM:
Step 1 : Start.
Step 2 : Load the count of the block data into bank register R2
Step 3 : Load the starting source address into bank register R1
Step 4 : Load the starting destination address into bank register R0
Step 5 : Copy the data from source address by using R1 as pointer into register A
Step 6 : transfer the data from register A into destination address by using R0 as pointer
Step 7 : Increment the content of registers R0 and R1, for choosing next source and
destination for next data
Step 8 : Repeat the steps 5 to 7 until count is zero until all the contents of the block are
transferred.
Step 9 : End program.

4.1 PROGRAM:
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
9000 MOV R2,#05H Size of the array
MOV R1,#41H Starting addr of source
MOV R0,#51H Starting addr of destination
NEXT MOV A,@R1 Copy data byte
MOV R0,A transfer to destination
INC R1 Increment Source address
INC R0 Increment Destination address
DJNZ R2,NEXT Do until Count is zero
LCALL 00BB End Program

MICROCONTROLLERS LABORATORY
25 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
4.1 SAMPLE INPUT & OUTPUT:
INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
41 51
42 52
43 53
44 54
45 55

Exercise 4.2:
Write a program to transfer a block of data stored in internal memory address
41H-45H to external memory address 8001H to 8005 H.
4.2 AIM:
To write an ALP for 8051 kit to transfer a Block of data from internal memory (41H-45H
) to external memory (8001H to 8005) H.
4.2 ALGORITHM:
Step 1 : Start.
Step 2 : Load the size of the array into bank register R2
Step 3 : Load the starting source address into bank register R1
Step 4 : Load the starting destination address into bank register DPTR
Step 5 : Copy the data from source address by using R1 as pointer into register A
Step 6 : transfer the data from register A into destination address by using DPTR as
pointer
Step 7 : Increment the content of registers DPTR and R1, for choosing next source and
destination for next data
Step 8 : Repeat the steps 5 to 7 until count is zero until all the contents of the block are
transferred.
Step 9 : End program.

4.2 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R2,#05H Size of the array
MOV R1,#41H Starting addr of source
MOV DPTR,#8001H Starting addr of destination
NEXT MOV A,@R1 Copy data byte
MOVX @DPTR,A transfer to destination
INC R1 Increment Source address
INC DPTR Increment Destination address
DJNZ R2,NEXT Do until Count is zero
LCALL 00BB End Program

MICROCONTROLLERS LABORATORY
26 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
4.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
41 8001
42 8002
43 8003
44 8004
45 8005

Exercise 4.3:
Write a program to transfer a block of data stored in external memory address
8001H to 8005 H to internal memory address 51H-55H.
4.3 AIM:
To write an ALP for 8051 kit to transfer a Block of data from external memory (8001H to
8005 H) to internal memory (51H-55H)
4.3 ALGORITHM:
Step 1 : Start.
Step 2 : Load the size of the array into bank register R2
Step 3 : Load the starting source address into bank register DPTR
Step 4 : Load the starting destination address into bank register R0
Step 5 : Copy the data from source address by using DPTR as pointer into register A
Step 6 : transfer the data from register A into destination address by using R0 as
pointer
Step 7 : Increment the content of registers R0 and DPTR, for choosing next source and
destination for next data
Step 8 : Repeat the steps 5 to 7 until count is zero until all the contents of the block are
transferred.
Step 9 : End program.

4.3 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R2,#05H Size of the array
MOV dptr,#8001H Starting addr of source
MOV R0,#51H Starting addr of destination
NEXT MOV A,@R1 Copy data byte
MOV @R0,A transfer to destination
INC DPTR Increment Source address
INC R0 Increment Destination address
DJNZ R2,NEXT Do until Count is zero
LCALL 00BB End Program

MICROCONTROLLERS LABORATORY
27 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
4.3 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8001 51
8002 52
8003 53
8004 54
8005 55

Exercise 4.4:
Write a program to transfer a block of data stored in external memory address
8001H to 8005 H to external memory address 8101H-8105H.
4.4 AIM:
To write an ALP for 8051 kit to transfer a Block of data from external memory (8001H to
8005 H) to internal memory (8101H-8105H)
4.4 ALGORITHM:
Step 1 : Start.
Step 2 : Load the size of the array into bank register R2
Step 3 : Load the starting source address into bank register DPTR
Step 4 : Load the starting destination address into bank register R0
Step 5 : Copy the data from source address by using DPTR as pointer into register A
Step 6 : transfer the data from register A into destination address by using R0 as pointer
Step 7 : Increment the content of registers R0 and DPTR, for choosing next source and
destination for next data
Step 8 : Repeat the steps 5 to 7 until count is zero until all the contents of the block are
transferred.
Step 9 : End program.

4.4 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R2,#05H Size of the array
MOV 82,#01H Starting addr of source
LOOP MOV 83,#80H Starting addr of destination
MOVX A,@DPTR Copy data byte
MOV 83,#81H transfer to destination
MOVX @DPTR,A Increment Source address
INC DPTR Increment Destination address
DJNZ R2,LOOP Do until Count is zero
LCALL 00BB End Program

MICROCONTROLLERS LABORATORY
28 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
4.4 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8001 8101
8002 8102
8003 8103
8004 8104
8005 8105

4.4 RESULT:

An assembly language program to perform Block Transfer operation between


memories using 8051 is written and executed.

MICROCONTROLLERS LABORATORY
29 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 5
Date:
5. 8-BIT CONVERSION
(i) ASCII to its equivalent hexadecimal and
(ii) Hexadecimal to its equivalent ASCII
Exercise 5.1:
Write a program to convert ASCII to its equivalent Hexadecimal number.
5.1 AIM:
To write an ALP for 8051 kit to transfer an ASCII to its equivalent Hexadecimal number
5.1 ALGORITHM:
Step 1 : Start.
Step 2 : Initialize R1 to input address
Step 3 : Load Reg A with Input ASCII through R1
Step 4 : Increment R1 to next address for output Hexadecimal
Step 5 : Call the Subroutine for R1 to Hexadecimal Conversion
Step 6 : Check for Number 0 to 9. if its more than 9 then do CONV subroutine
Step 7 : ELSE do NUM subroutine
Step 8 Store the Result in Output address through R1.
Step 9 : End program.

5.1 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R1,#41 Input thro R1
MOV A, @R1 Copy the Input in A
INC R1 Increment R1 to Output address
LCALL CONV LCALL 9100
LCALL 00BB End Program

9100 CONV CLR C Clear Carry/Borrow Register


SUBB A, #41 Subtract 10d from the Input
JC NUM If Borrow is there then Goto NUM
CLR C Copy R0 in A
SUBB A, #37 ASCII TO CHAR
MOV @R1, A Store the Result
RET
NUM CLR C Clear Carry/Borrow
SUBB A, #30
MOV @R1, A ASCII TO NUMBER
RET

5.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
41 42

MICROCONTROLLERS LABORATORY
30 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
5.1 RESULT:

An assembly language program to transfer an ASCII to its equivalent Hexadecimal


number using 8051 is written and executed.

Exercise 5.2:
Write a program to convert Hexadecimal number to its equivalent ASCII.
5.2 AIM:
To write an ALP for 8051 kit to transfer a Hexadecimal number to an ASCII equivalent

5.2 ALGORITHM:
Step 1 : Start.
Step 2 : Initialize R1 to input address
Step 3 : Load Reg A with Input HEXA through R1
Step 4 : Increment R1 to next address for output Hexadecimal
Step 5 : Call the Subroutine for Hexadecimal to ASCII Conversion
Step 6 : Check for Number 0 to 9. if its more than 9 then do CONV subroutine
Step 7 : ELSE do NUM subroutine
Step 8 Store the Result in Output address through R1.
Step 9 : End program.

5.2 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R1,#43 Input through R1
MOV A, @R1 Copy the Input in A
MOV R0, A Copy the input in R0
INC R1 Increment R1 to Output address
LCALL CONV LCALL 9100
LCALL 00BB End Program

9100 CONV CLR C Clear Carry/Borrow Register


SUBB A, #0A Subtract 10d from the Input
JC NUM If Borrow is there then Goto NUM
MOV A,R0 Copy R0 in A
ADD A, #37 CHAR TO ASCII
MOV @R1, A Store the Result
RET
NUM CLR C Clear Carry/Borrow
ADD A, #30 NUMBER TO ASCII
MOV @R1, A Store the Result
RET

5.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
43 44

5.2 RESULT:

MICROCONTROLLERS LABORATORY
31 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

An assembly language program to transfer a Hexadecimal number to an ASCII


equivalent using 8051 is written and executed.

TABLE- ASCII-HEXADECIMAL

ASCII HEXA ASCII HEXA ASCII HEXA ASCII HEXA ASCII HEXA
30 0 41 A 50 P 61 a 70 p
31 1 42 B 51 Q 62 b 71 q
32 2 43 C 52 R 63 c 72 r
33 3 44 D 53 S 64 d 73 s
34 4 45 E 54 T 65 e 74 t
35 5 46 F 55 U 66 f 75 u
36 6 47 G 56 V 67 g 76 v
37 7 48 H 57 W 68 h 77 w
38 8 49 I 58 X 69 i 78 x
39 9 4A J 59 Y 6A j 79 y
4B K 5A Z 6B k 7A z
4C L 6C l
4D M 6D m
4E N 6E n
4F O 6F o

MICROCONTROLLERS LABORATORY
32 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 6
Date:
6. ARRANGE THE GIVEN NUMBERS IN ASCENDING AND
DESCENDING ORDER

Exercise 6.1:
Write a program to arrange the given array of numbers in ascending order.
6.1 AIM:
To write an ALP for 8051 kit to arrange the given array of numbers in ascending order
6.1 ALGORITHM:
Step 1 : Start.
Step 2 : Copy Count of array in two registers
Step 3 : Initialize Input address through DPTR
Step 4 : Load first two numbers in A and another register
Step 5 : Compare both numbers
Step 6 : If First is less than second number, Leave as it is.
Step 7 : If First is greater than second number, swap the places of numbers and
again do it from first
Step 8 Do this until the array count becomes zero
Step 9 End program.

6.1 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R0, #05 Initialize COUNT value to R0
NXT5 MOV R1, R0 Copy COUNT to R1
MOV DPTR, #8000 Initialize DPTR
NXT4 MOVX A, @DPTR Load the first number to A
MOV R2, A Copy First number in R2
INC DPTR Move DPTR to NEXT location
MOVX A, @DPTR Load the second Number in A
CJNE A,R2,NXT1 Compare A & R2 and go to NXT1
when not equal
NXT1 JC NXT2 When A is less than R2 go to NXT2
AJMP NXT3 Go to NXT3
NXT2 XCH A, R2 Exchange A and R2 contents
MOVX @DPTR,A Store Content in A to Destination
DEC 82 Decrement DPL
MOV A, R2 Copy Content of R2 to A
MOVX @DPTR, A Store Content in A to Destination
INC 82 Increment DPL
NXT3 DJNZ R1, NXT4 Decrement R1 and Go to NXT4 if not
Zero
DJNZ R0, NXT5 Decrement R0 and Go to NXT4 if not
Zero
LCALL 00BB End Program

MICROCONTROLLERS LABORATORY
33 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
6.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8000
8001 8001
8002 8002
8003 8003
8004 8004
6.1 RESULT:

An assembly language program to arrange the given array of numbers in ascending


order using 8051 is written and executed.

Exercise 6.2:
Write a program to arrange the given array of numbers in descending order.
6.2 AIM:
To write an ALP for 8051 kit to arrange the given array of numbers in descending order
6.2 ALGORITHM:
Step 1 : Start.
Step 2 : Copy Count of array in two registers
Step 3 : Initialize Input address through DPTR
Step 4 : Load first two numbers in A and another register
Step 5 : Compare both numbers
Step 6 : If First is greater than second number, Leave as it is.
Step 7 : If First is less than second number, swap the places of numbers and again
do it from first
Step 8 Do this until the array count becomes zero
Step 9 End program.

6.2 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R0, #05 Initialize COUNT value to R0
NXT5 MOV R1, R0 Copy COUNT to R1
MOV DPTR, #8000 Initialize DPTR
NXT4 MOVX A, @DPTR Load the first number to A
MOV R2, A Copy First number in R2
INC DPTR Move DPTR to NEXT location
MOVX A, @DPTR Load the second Number in A
CJNE A,R2,NXT1 Compare A & R2 and go to NXT1
when not equal
NXT1 JNC NXT2 When A is More than R2 go to NXT2
AJMP NXT3 Go to NXT3
NXT2 XCH A, R2 Exchange A and R2 contents
MOVX @DPTR,A Store Content in A to Destination
DEC 82 Decrement DPL
MOV A, R2 Copy Content of R2 to A
MOVX @DPTR, A Store Content in A to Destination
INC 82 Increment DPL
NXT3 DJNZ R1, NXT4 Decrement R1 and Go to NXT4 if not

MICROCONTROLLERS LABORATORY
34 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Memory Hex Mnemonics
Label Comments
Address code Opcode Operand
Zero
DJNZ R0, NXT5 Decrement R0 and Go to NXT4 if not
Zero
LCALL 00BB End Program

6.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8000 8000
8001 8001
8002 8002
8003 8003
8004 8004
6.2 RESULT:

An assembly language program to arrange the given array of numbers in ascending


order using 8051 is written and executed.

MICROCONTROLLERS LABORATORY
35 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE
Expt. No: 7
Date:
7. FILLING EXTERNAL AND INTERNAL MEMORY
Exercise 7.1:
Write a program to fill the external memory with same data.
7.1 AIM:
To write an ALP for 8051 kit to fill the external memory with same data
7.1 ALGORITHM:
Step 1 : Start.
Step 2 : Initialize R0 to Count address
Step 3 : Initialize DPTR to the input & output address
Step 4 : Copy Data to be filled in Reg A
Step 5 : Increment the external Memory address using DPTR
Step 6 : Copy the data in Reg A to New address using DPTR
Step 7 : Repeat the above step 5 & 6 until Count in R0 is zero
Step 8 End program.

7.1 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R0, #30 Initialize Count in R0
MOV DPTR, #8500 Initialize Input Data thro DPTR
MOVX A, @DPTR Copy Data in Reg A
LOOP INC DPTR Next Address
MOVX @DPTR, A Fill Data in Address
DJNZ R0, LOOP Till Count is zero
LCALL 00BB End Program

7.1 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
8500 34 (DATA) 8501
30 05 (COUNT 8502
8503
8504
8505
7.1 RESULT:

An assembly language program to fill the external memory with same data using 8051
is written and executed.

MICROCONTROLLERS LABORATORY
36 III YEAR (V SEM)
SATHYABAMA UNIVERSITY DEPT. OF ETCE

Exercise 7.2:
Write a program to fill the internal memory with same data.
7.2 AIM:
To write an ALP for 8051 kit to fill the internal memory with same data
7.2 ALGORITHM:
Step 1 : Start.
Step 2 : Initialize R0 to Count address
Step 3 : Initialize R1 to the input & output address
Step 4 : Copy Data to be filled in Reg A
Step 5 : Increment the Internal Memory address using R1
Step 6 : Copy the data in Reg A to New address using R!
Step 7 : Repeat the above step 5 & 6 until Count in R0 is zero
Step 8 End program.

7.2 PROGRAM:

Memory Hex Mnemonics


Label Comments
Address code Opcode Operand
9000 MOV R0, #50 Initialize Count in R0
MOV R1, #40 Initialize Input Data thro R1
MOV A, @R1 Copy Data in Reg A
LOOP INC R1 Next Address
MOV @R1, A Fill Data in Address
DJNZ R0, LOOP Till Count is zero
LCALL 00BB End Program

7.2 SAMPLE INPUT & OUTPUT:

INPUT OUTPUT
ADDRESS DATA ADDRESS DATA
40 34 (DATA) 41
50 05 (COUNT) 42
43
44
45
7. 2 RESULT:

An assembly language program to fill the internal memory with same data using 8051
is written and executed.

MICROCONTROLLERS LABORATORY
37 III YEAR (V SEM)