MC Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

(Permanently Affiliated to University of Mumbai)

Department of Electronics and Telecommunication Engineering

Laboratory Manual

Microcontrollers Lab
Subject code: ECL401
SE, Semester – IV
(R-2019-C-Scheme)

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

LIST OF EXPERIMENTS
Course Name : Microcontroller lab
Course Code : ECL401
Class : SE
Semester : IV (REV- 2019)

Sr. No. Name Of the Experiments Page No

1 Introduction to KEIL µVision 3 Software

2 To Simulate two 8-bit number addition/subtraction using


immediate, direct & indirect addressing.
To Simulate two 8-bit number multiplication/division using
3
immediate, direct & indirect addressing.

To Simulate Logical operation using immediate, direct & indirect


4
addressing.

5 Code Conversion from BCD nos to ASCII

6
Transfer of data bytes between internal & external memory

7 Arrange the given set of data in ascending and descending order

8 To generate square wave using timer

9 Interface LCD with 8051 and display a message on LCD

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

VISION

To create competent technical professionals in the field of Electronics & Telecommunication


Engineering with ethical behavior and environment consciousness.

MISSION

M1 To provide contemporary and cutting-edge technical education to meet global challenges


in the field of Electronics & Telecommunication Engineering.

M2 To provide an ambience which nurtures research ideas and initiate project based learning
in futuristic domains of Electronics & Telecommunication Engineering.

M3 To direct faculties in innovation, research and consultancy / advisory roles.

M4 To establish strong linkages with well-known national and international technical


institutes and industries.

M5 To develop interpersonal skills and to promote a culture of imbibing environmental care


and eco-friendly design.

M6 To become a department of aspiration and choice

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

PROGRAM OUTCOMES(POs)

PO1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering
problems.
PO2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
PO3. Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
PO4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions.
PO5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
PO6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the professional engineering practice.
PO7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
PO8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
PO9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
PO10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend
and write effective reports and design documentation, make effective presentations, and
give and receive clear instructions.
PO11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a
member and leader in a team, to manage projects and in multidisciplinary
environments.

PO12. Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

PROGRAM SPECIFIC OUTCOMES (PSOs)

PSO1: Able to embed the ability to apply the fundamentals of Electronics & Telecommunication
Engineering to various areas like Analog Circuits, Communication systems, Signal
processing,
Data Analytics, Embedded systems, VLSI, RF and Microwave Circuits.

PSO2: Able to design and analyze complex Electronics & Telecommunication Engineering
problems
using modern hardware and software tools to implement optimized solutions for variety of
applications.

PROGRAM EDUCATIONAL OBJECTIVES (PEOs)

PEO-1: To produce Electronics & Telecommunication engineers having strong theoretical


foundation,
good design experience and exposure to research and development.

PEO-2: To produce researcher who have clear thinking, articulation and interest to carry out
theoretical and/or applied research resulting in significant advancement in the field of
specialization.

PEO-3: To develop an ability to identify, formulate and solve Electronics and


Telecommunication
Engineering problems in the latest trends in technology.

PEO-4: To develop the ability among students to synthesize data and technical concepts from
applications to product design with ethical attitude, commitment to social
responsibilities and
environment consciousness.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Course Name : Microcontrollers Laboratory


Course Code : ECL401
Class : SE
Semester : IV (R-2019-C-Scheme)

Course objectives:

1) To understand development tools of microcontroller-based systems.


2) To learn programming for different microcontroller operation & interface to I/O devices.
3) To develop microcontroller-based applications.

Course outcomes:

After successful completion of the course student will be able to

1) Understand different development tools required to develop microcontroller-based


systems. Compare and contrast trucking radio systems.
2) Write assembly language programs for arithmetic and logical operations, code conversion
& data transfer operations.
3) Write assembly language programs for general purpose I/O, Timers & Interrupts.
4) Interface & write programs for Input and Output devices.
5) Develop microcontroller-based Applications.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

General Instructions
1. Use of mobile phones, TAB is strictly prohibited.

2. Keep your bags along with mobile phones in the rack.

3. Photocopy and snap of lab manuals is strictly prohibited.

4. It is the responsibility of students to take care of computers instruments they are

using.

5. Shut down the computers before leaving the laboratory.

6. Use pen drives only to designated computers.

7. Do not change any settings of computers.

8. While performing practical browsing of internet is not allowed.

9. Software installation in any computer laboratory is not allowed without the

permission from the laboratory staff.

10. Rearrange chairs properly before leaving the laboratory.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Suggested List Of Experiments from the University of Mumbai

Suggested Experiment List:


1. Perform Arithmetic and Logical Operations (Using Immediate, Direct and indirect
addressing)
2. Code Conversion
3. Transfer of data bytes between Internal and External Memory
4. Experiments based on General Purpose Input-Output, Timers, Interrupts, Delay, etc
5. Interfacing of Matrix Key board, LED, 7 Segment display, LCD, Stepper Motor, UART
At Least 10 experiment Minimum two from each category of above list must be given during
the Laboratory session batch wise. Computation/simulation based experiments are also
encouraged. Before starting the experiments there should be one session on Study of
development tools like Editor, Assembler-cross Assembler, Compiler-Cross compiler, Linker,
Simulator, emulator etc. Mini project based on 8051 derivatives, PIC, AVR & other 8 bit
microcontrollers using Assembly and/or C language. (Readymade of Arduino & raspberry pi
are not recommended here) Note: Mini Project can be considered as a part of term-work. Term
Work (25-Marks): The experiments should be students centric and attempt should be made to
make experiments more meaningful, interesting and innovative. Application oriented one mini-
project can be conducted for maximum batch of four students. Term work assessment must be
based on the overall performance of the student with every experiments/tutorials and mini-
projects (if included) are graded from time to time. The grades will be converted to marks as
per “Choice Based Credit and Grading System” manual and should be added and averaged.
Based on above scheme grading and term work assessment should be done.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

CERTIFICATE

This is certify that


Mr/Ms____________________________________________________
Of _________________ Class _________________ Roll No. ____________
Exam Seat No. ________________________ has successfully completed the
term work in the subject of
______________________________________________ as prescribed by
University of Mumbai during the year ___________________ in the
department of ___________________________________________________

______________________ __________________
STAFF MEMBER INCHARGE HEAD OF DEPARTMENT

_______________
PRINCIPAL
(SEAL)

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Practical Scheme

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

The 8051 Microcontroller Instruction Set and


Assembly Programming
Objectives
To study the instruction set of the 8051 microcontroller, its addressing modes, and how to write and
execute simple assembly programs.
1. The 8051 Instruction Set
The process of writing program for the microcontroller mainly consists of giving instructions
(commands) in the specific order in which they should be executed in order to carry out a specific task.
All commands are known as INSTRUCTION SET. All microcontrollers compatible with the 8051
have in total of 255 instructions, i.e. 255 different words available for program writing. Depending on
operation they perform, all instructions are divided in several groups:

• Data Transfer Instructions


• Arithmetic Instructions
• Branch Instructions
• Logic Instructions
• Bit-oriented Instructions

2. The 8051 Addressing Modes


The CPU can access data in various ways. The data could be in a register, or in memory, or be provided
as an immediate value. These various ways of accessing data are called addressing modes. The 8051
provides a total of six distinct addressing modes. They are as follows:
1. Immediate Addressing Mode
2. Register Addressing Mode
3. Direct Addressing Mode
4. Indirect Addressing Mode
5. External Indirect Addressing Mode
6. Indexed Addressing Mode
2.1 Immediate Addressing Mode
Immediate addressing is so-named because the value to be stored in memory immediately follows the
operation code in memory. That is to say, the instruction itself dictates what value will be stored in
memory.
For example, the instruction:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

MOV A, #20H

This instruction uses Immediate Addressing because the Accumulator will be loaded with the value
that immediately follows; in this case 20 (hexadecimal). Notice that the immediate data must be
preceded by the pound sign, ―#‖. This addressing mode can be used to load information into any of
the registers, including the DPTR register as shown in the following examples:

Notice that we can also use immediate addressing mode to send data to 8051 ports. For example,
―MOV P1, #55H‖ is a valid instruction.
Immediate addressing is very fast since the value to be loaded is included in the instruction. However,
since the value to be loaded is fixed at compile-time it is not very flexible.
2.2 Register Addressing Mode
Register addressing mode involves the use of registers to hold the data to be manipulated. Examples
of register addressing mode are shown below:

It should be noted that the source and destination registers must match in size. In other words, coding
“MOV DPTR, A” will give an error, since the source is an 8-bit register and the destination is a16-bit
register.

Notice that we can move data between the accumulator and Rn (for n = 0 to 7) but movement of data
between "R" registers is not allowed. For example, the instruction “MOV R4, R7″ is invalid

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2.3 Direct Addressing Mode


Direct addressing is so-named because the value to be stored in memory is obtained by directly retrieving it from
another memory location. For example:

MOV A, 30H
This instruction will read the data out of Internal RAM address 30 (hexadecimal) and store it in the
Accumulator. Further examples of the direct addressing mode are shown below:

Direct addressing is generally fast since, although the value to be loaded isn't included in the
instruction, it is quickly accessible since it is stored in the 8051s Internal RAM. It is also much more
flexible than Immediate Addressing Mode since the value to be loaded is whatever is found at the
given address. Also, it is important to note that when using direct addressing any instruction which
refers to an address between 00H and 7FH is referring to Internal Memory. Any instruction which
refers to an address between 80H and FFH is referring to the SFR control registers that control the
8051 microcontroller itself. For example, the instruction MOV 0E0,#30H is equivalent to MOV
A,#30H.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2.4 Indirect Addressing Mode


Indirect addressing is a very powerful addressing mode which in many cases provides an exceptional
level of flexibility. Indirect addressing is also the only way to access the extra 128 bytes of Internal
RAM found on an 8052.
Indirect addressing appears as follows:
MOV A,@R0

This instruction causes the 8051 to analyze the value of the R0 register. The 8051 will then load the
accumulator with the value from Internal RAM which is found at the address indicated by R0.
If the data is inside the on-chip RAM, only registers R0 and R1 are used as pointers. In other words,
R2 – R7 cannot be used to hold the address of an operand located in RAM when using this addressing
mode.
Indirect addressing mode always refers to Internal RAM; it never refers to an SFR.
2.5 External Indirect Addressing Mode
In addition to its code memory, the 8051 family also has 64K bytes of data memory space. In other
words, the 8051 has 128K bytes of address space of which 64K bytes are set aside for program code
and the other 64K bytes are set aside for data. Program space is accessed using the program counter
(PC) to locate and fetch instructions, but the data memory space is accessed using the DPTR register
and an instruction called MOVX, where X stands for external (meaning that the data memory space
must be implemented externally).
This method of addressing is used to access data stored in external data memory. There are only two
commands that use external indirect addressing mode:
MOVX A, @DPTR MOVX @DPTR, A
As you can see, both commands utilize DPTR. In these instructions, DPTR must first be loaded with
the address of external memory that you wish to read or write. Once DPTR holds the correct external
memory address, the first command will move the contents of that external memory address into the
Accumulator. The second command will do the opposite: it will allow you to write the value of the
Accumulator to the external memory address pointed to by DPTR.
2.6 Indexed Addressing Mode

Indexed addressing mode is widely used in accessing data elements of look-up table entries located in
the program ROM space of the 8051. The instruction used for this purpose is:
“MOVC A, @A+DPTR”
The 16-bit register DPTR and register A are used to form the address of the data element stored in on-
chip ROM. Because the data elements are stored in the program (code) space ROM of the 8051, the
instruction MOVC is used instead of MOV. The ―C‖ means code. In this instruction the contents of A
are added to the 16-bit register DPTR to form the 16-bit address of the needed data.
One major difference between the code space and data space is that, unlike code space, the data space
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

3. Some Specific Instructions


In order to write simple assembly programs for the 8051 microcontroller, certain widely used
instructions are explored. These instructions are belonging to the different groups of instruction types
illustrated above.
3.1 The MOV Instruction
The MOV instruction is a data transfer instruction, and copies data from one location to another. It has
the following format:
MOV destination, source ; copy source to destination.
This instruction tells the CPU to move (in reality, copy) the source operand to the destination operand.
For example, the instruction ―MOV A, R0‖ copies the contents of register R0 to register A. After this
instruction is executed, register A will have the same value as register R0. The MOV instruction does
not affect the source operand.
When programming the 8051 microcontroller, the following points should be noted:
1. Values can be loaded directly into any of registers A, B, or R0 – R7. However, to indicate that it is
an immediate value it must be preceded with a pound sign (#). For example the instruction MOV
A,#23H and MOV R5,#0F9H. Notice in instruction ―MOV R5 , #0F9H‖ that a 0 is used between
the # and F to indicate that F is a hex number and not a letter. In other words ―MOV R5 , #F9H‖
will cause an error.
2. If values 0 to F are moved into an 8-bit register, the rest of the bits are assumed to be all zeros. For
example, in the instruction ―MOV A, #5″ the result will be A = 05; that is, A = 00000101 in binary.
3. Moving a value that is too large into a register will cause an error. For example the instruction MOV
A,#7F2H is illegal.
3.2 The ADD Instruction
The ADD instruction is an arithmetic instruction. It has the following format: ADD
A, source ; ADD the source operand to the accumulator
The ADD instruction tells the CPU to add the source byte to register A, and put the result in register
A. The source operand can be either a register or immediate data, but the destination must always be
the accumulator (Register A).
For example the following instructions add the contents of register A and register R2 and put the
result in the accumulator.

MOV A,#25H ; load 25H into A


MOV R2,#34H ; load 34H into R2
ADD A,R2 ; add R2 to accumulator

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

3.3 The INC Instruction


The INC instruction is an arithmetic instruction, and is used to increment the contents of a register or
memory location by 1. For example: INC A, INC R0, INC 7H, INC DPTR.
3.4 The DEC Instruction
The DEC instruction is an arithmetic instruction, and is used to decrement the contents of a register or
memory location by 1. For example: DEC A, DEC R1, DEC @R0. The DEC instruction can not be
used to decrement the contents of the data pointer register DPTR.
3.5 The DJNZ Instruction
The DJNZ is an important conditional jump instruction that is widely used for making loops in the
8051 programs. This instruction takes the following syntax:
DJNZ register, label
The DJNZ informs the CPU to decrease the content of the register and jump to a specified location in
the program determined by the label if the register content is not zero. The program jump will be within
–128 to +127 locations relative to the following instruction.
3.6 The SJMP (Short Jump) Instruction
The SJMP instruction is a 2-byte unconditional jump instruction which is used to make jumps in the
program. The jump to the target address will be within -128 to +127 bytes of memory relative to the
address of the current PC (program counter). The syntax of the SJMP instruction is:
SJMP label
The target address is referred to by the label.
3.7 The LJMP (Long Jump) Instruction
The LJMP instruction is a 3-byte unconditional jump instruction which is used to make jumps in the
program. The target address allows a jump to any memory location from 0000 to FFFFH within the
64KB memory address space. The syntax of the LJMP instruction is:
LJMP label
The target address is referred to by the label.

3.8 The JNZ Instruction (Jump if A ≠ 0)


In this instruction the content of register A is checked. If it is not zero, it jumps to the target address.
Notice that the JNZ instruction can be used only for register A. It can only check to see whether the
accumulator is zero or not, and it does not apply to any other register.
3.9 The JZ Instruction (Jump if A = 0)
In this instruction the content of register A is checked. If it is zero, it jumps to the target address. It is
applied only to the accumulator register.
3.10 JNC (jump if no carry)
In this instruction, the carry flag bit in the flag (PSW) register is used to make the decision whether to
jump. In executing ―JNC label‖, the processor looks at the carry flag to see if it is raised (C =1). If
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

it is not, the CPU starts to fetch and execute instructions from the address of the label. If C = 1, it will
not jump but will execute the next instruction below JNC.
3.11 JC (Jump if carry)
In the JC instruction, if C = 1 it jumps to the target address. On the other hand if no carry occurs, the
processor will execute the next instruction below JC.
3.12 The CJNE Instruction
The CJNE (Compare and jump if not equal) instruction is used to compare the content of a general
purpose register with a certain value or direct memory location and jump to a certain address if the two
value are not equal. The syntax of the instruction is:
CJNE destination, source, label
The destination can be register A or the bank registers (R0-R7), and the source may be an immediate
value or a direct memory address.
3.13 The CLR Instruction
The CLR instruction is used to clear the content of the accumulator. It is a one byte logic instruction,
which has the syntax CLR A.
3.14 The CPL Instruction
The CPL (Complement) instruction is a one byte logic instruction that is used to complement (negate)
the bits of register A. After execution of this instruction, bits 1 in register A become 0 and vice versa.
It takes the syntax CPL A. It can also be used as a bitwise instruction to complement a bit.
3.15 The NOP Instruction
The NOP (No operation) instruction does not do anything. It is used only for making desired delays in
the programs.

3.16 The SETB and CLR Instructions


The SETB instruction is a bit oriented instruction that is used to set a certain bit in a register or bit
addressable memory location. The CLR instruction, on the other hand, is used to clear a certain bit in
a register or bit addressable memory location. For example the instruction SETB PSW.3 is used to set
bit 3 (RS0) of the program status word register (The Flag Register). The instruction CLR C (or CLR
PSW.7) is used to clear the carry bit in the flag register.
3.17 The JNB and JB Instructions
The JNB (jump if no bit) and JB (jump if bit = 1) instructions are also widely used single-bit operations.
They allow us to monitor a bit and make a decision depending on whether it is 0 or 1.
4. The Pseudo Instructions
Pseudo instructions are not translated to machine code, and therefore they have no operation code.
They are used by the assembler to organize the source code file (asm file). There are several pseudo
instructions supported by the 8051 microcontroller. Some of the widely used pseudo instructions are
discussed below.
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

4.1 ORG (Origin)


The ORG pseudo instruction is used to indicate the beginning of the address.
The number that comes after ORG can be either in hex or in decimal. If the number is not followed
by H, it is decimal and the assembler will convert it to hex.
4.2 DB (Define Byte)
The DB pseudo instruction is the most widely used data instruction in the assembler. It is used to define
the 8-bit data. When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII
formats. For decimal, the ―D‖ after the decimal number is optional, but using ―B‖ (binary) and
―H‖ (hexadecimal) for the others is required. Regardless of which is used, the assembler will convert
the numbers into hex. To indicate ASCII, simply place the characters in quotation marks (‗like this‘).
The assembler will assign the ASCII code for the numbers or characters automatically. Either single
or double quotation marks can be used around the ASCII strings.

Following are some DB examples:

4.3 EQU (Equate)


This is used to define a constant without occupying a memory location. The EQU pseudo instruction
does not set aside storage for a data item but associates a constant value with a data label so that when
the label appears in the program, its constant value will be substituted for the label. The following uses
EQU for the counter constant and then the constant is used to load the R3 register.

When executing the instruction ―MOV R3, #COUNT‖, the register R3 will be loaded with the value
25 (notice the # sign). What is the advantage of using EQU? Assume that there is a constant (a fixed
value) used in many different places in the program, and the programmer wants to change its value
throughout. By the use of EQU, the programmer can change it once and the assembler will change all
of its occurrences, rather than search the entire program trying to find every occurrence
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

4.4 END
Another important pseudo instruction is the END directive. This indicates to the assembler the end of
the source (asm) file. The END pseudo instruction is the last line of an 8051 program, meaning that in
the source code anything after the END directive is ignored by the assembler.

5. Structure of the 8051 Assembly Language Programs


An Assembly language program consists of, among other things, a series of lines of Assembly language
instructions. An Assembly language instruction consists of a mnemonic, optionally followed by one
or two operands. The operands are the data items being manipulated, and the mnemonics are the
commands to the CPU, telling it what to do with those items.

Sample of an Assembly Language Program

A given Assembly language program (see Figure 1) is a series of statements, or lines, which are either
Assembly language instructions such as ADD and MOV, or statements called pseudo instructions.
While instructions tell the CPU what to do, pseudo-instructions (also called directives) give directions
to the assembler. For example, in the above program while the MOV and ADD instructions are
commands to the CPU, ORG and END are directives to the assembler. ORG tells the assembler to place
the opcode at memory location 0 while END indicates to the assembler the end of the source code.
In other words, one is for the start of the program and the other one for the endof the program.

An Assembly language instruction consists of four fields:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Brackets indicate that a field is optional, and not all lines have them. Brackets should not be typed in.
Regarding the above format, the following points should be noted.
1. The label field allows the program to refer to a line of code by name. The label field cannot
exceed a certain number of characters. Check your assembler for the rule.
2. The Assembly language mnemonic (instruction) and operand(s) fields together perform the real
work of the program and accomplish the tasks for which the program was written. In Assembly
language statements such as
ADD A, R5
MOV A,#0
ADD and MOV are the mnemonics, which produce op-codes; and ―A, R5‖ and ―A, #0″ are the
operands. Instead of a mnemonic and an operand, these two fields could contain assembler pseudo-
instructions. Remember that pseudo-instructions do not generate any machine code (opcode) and are
used only by the assembler, as opposed to instructions that are translated into machine code (opcode)
for the CPU to execute. In Figure 1, the commands ORG (origin) and END are examples of pseudo-
instructions (some 8051 assemblers use .ORG and .END).
3. The comment field begins with a semicolon comment indicator ―;‖. Comments may be at the
end of a line or on a line by themselves. The assembler ignores comments, but they are
indispensable to programmers. Although comments are optional, it is recommended that they
be used to describe the program and make it easier for someone else to read and understand, or
for the programmers to remember what they wrote.
4. Notice the label ―HERE‖ in the label field in Figure 1. Any label referring to an instruction
must be followed by a colon symbol, ―:‖. In the SJMP (short jump instruction), the 8051 is
told to stay in this loop indefinitely. If your system has a monitor program you do not need this
line and it should be deleted from your program.
5.1 Rules for Labels in Assembly Language Programs
By choosing label names that are meaningful, a programmer can make a program much easier to read
and maintain. There are several rules that names must follow. First, each label name must be unique.
The names used for labels in Assembly language programming consist of alphabetic letters in both
uppercase and lowercase, the digits 0 through 9, and the special characters question mark (?), period
(.), at (@), underline (_), and dollar sign ($). The first character of the label must be an alphabetic
character. In other words it cannot be a number. Every assembler has some reserved words that must
not be used as labels in the program. Foremost among the reserved words are the mnemonics for the
instructions. For example, ―MOV‖ and ―ADD‖ are reserved since they are instruction mnemonics. In
addition to the mnemonics there are some other reserved words depending on the specific assembler
to be used.
5.2 Assembling an 8051 Program
The assembler is used to create a ready to run machine code file in HEX format from the entered source
code file. This generated HEX file will be burned into the program ROM of the microcontroller to do
a specific task. There are many commercial 8051 assemblers that can perform

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

the assembly process. The ASEM-51 assembler will be used in our lab for producing the object files
of the written assembly programs. The entered source file (asm file) should be written using an editor
like the MS-DOS EDIT program, or the windows Notepad editor. The ASEM-51 assembler will
generate two output files, the list file and the machine code HEX file as shown in Figure 2. The list
file is very useful to the programmer because it lists all the opcodes and addresses as well as errors that
the assembler may detect. The programmer uses the list file to find syntax errors in order to generate
the required HEX file. This file can be accessed by an editor and displayed on the monitor.

Creating and Assembling 8051Code Files

The source code file is created from the command prompt window after changing the path into the
ASEM51 folder containing the assembler program. To change the path into the directory ASEM51,
type the following command from the root directory:
C:\>CD ASEM51
C:\ASEM51>
In order to print the source file (mycode.asm), type the following command:
C:\ASEM51>notepad mycode.asm
Or you can use the MS-DOS EDIT program as follows:
C:\ASEM51>EDIT mycode.asm
After typing and saving the source code file, it should then be assembled by typing the command
below:
C:\ASEM51>ASEM mycode.asm

If the source file is free of syntax errors, the assembler will generate the HEX file in addition to the
list file. Otherwise, the syntax errors can be detected by displaying the list file as shown below:
C:\ASEM51>EDIT mycode.lst
To list all the created files on the screen, type the following command:
C:\ASEM51>DIR mycode.*

5.3 Placing Code in Program ROM


The assembler program generates two important files from the entered source code file (asm file).
These files are the list file and the machine code file in hexadecimal format (HEX file). The HEX file
is the program code that will be burned into the microcontroller's on-chip ROM.
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

First, we examine the list file of the sample program presented in Figure 1 and how the code is placed
in the ROM of an 8051 chip. As we can see, the opcode and operand for each instruction are listed on
the left side of the list file as shown in Figure

The Contents of the List File Generated by the Assembler

After the program is burned into ROM of an 8051 family member such as 8751 or AT8951 or 87C52,
the opcode and operands are placed in ROM memory locations starting at 0000 as shown in the list
below in Figure

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

ROM Locations for the Assembled Program

The list shows that address 0000 contains 7D, which is the opcode for moving a value into register R5,
and address 0001 contains the operand (in this case 25H) to be moved to R5. Therefore, the
instruction “MOV R5,#25H” has a machine code of “7D25″, where 7D is the opcode and 25 is the
operand. Similarly, the machine code “7F34″ is located in memory locations 0002 and 0003 and
represents the opcode and the operand for the instruction “MOV R7,#34H”. In the same way,
machine code “7400″ is located in memory locations 0004 and 0005 and represents the opcode and
the operand for the instruction “MOV A, #0″. The memory location 0006 has the opcode of 2D, which
is the opcode for the instruction “ADD A, R5″ and memory location 0007 has the content 2F, which is
the opcode for the “ADD A, R7″ instruction. The opcode for the instruction “ADD A, #12H”is located
at address 0008 and the operand 12H at address 0009. The memory location 000A hasthe opcode for
the SJMP instruction and its target address is located in location 000B.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 01
Introduction to 8051 & KEIL µVision
EXPERIMENT NAME Software

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NO. 1
Introduction to 8051 & KEIL µVision Software
INTRODUCTION
Earlier to Microcontrollers, Microprocessors were greatly used for each and every purpose.
Microprocessors were containing ALU, general purpose register, stack pointer, program counter,
clock counter and so many other features which the today‘s Microcontroller also possesses. But
the difference between them exists with respect to the number of instructions, access times, size,
reliability, PCB size and so on.
Microprocessor contains large instruction set called as CISC processor whereas Microcontroller
contains less number of instructions and is called as RISC processor. The access time is less in
case of microcontrollers compared to microprocessors and the PCB size reduces in case of
microcontrollers.
There are many versions of microcontrollers 8051, 80528751, AT8951 from Atmel Corporation
and many more. In this manual we will study about the 8051 architecture, its features,
programming and interfacing.

MCS 8051 is an 8-bit single chip microcontroller with many built-in functions and is the core for
all MCS-51 devices.

The main features of the 8051 core are:


 Operates with single Power Supply +5V.
 8-bit CPU optimized for control applications.
 16-bit program counter (PC) and 16-bit data pointer (DPTR).
 8-bit program status word (PSW).
 8-bit stack pointer (SP).
 4K Bytes of On-Chip Program Memory (Internal ROM or EPROM).
 128 bytes of On-Chip Data Memory (Internal RAM):
 Four Register Banks, each containing 8 registers (R0 to R7) [Total 32 reg]
 16-bytes of bit addressable memory.
 80 bytes of general-purpose data memory (Scratch Pad Area).
 Special Function Registers (SFR) to configure/operate microcontroller.
 32 bit bi-directional I/O Lines (4 ports P0 to P3).
 Two 16-bit timers/counters (T0 and T1).
 Full duplex UART (Universal Asynchronous Receiver/Transmitter).
 On-Chip oscillator and clock circuitry.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

PIN DIAGRAM OF 8051 MICRO-CONTROLLER

PINOUT DESCRIPTION
Pins 1-8: Port 1 Each of these pins can be configured as an input or an output.
Pin 9: RS A logic one on this pin disables the microcontroller and clears the contents of most registers.
In other words, the positive voltage on this pin resets the microcontroller. By applying logiczero to this
pin, the program starts execution from the beginning.
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Pins10-17: Port 3 Similar to port 1, each of these pins can serve as general input or output. Besides,
all of them have alternative functions:
Pin 10: RXD Serial asynchronous communication input or Serial synchronous communication
output.
Pin 11: TXD Serial asynchronous communication output or Serial synchronous communication
clock output.
Pin 12: INT0 Interrupt 0 input.
Pin 13: INT1 Interrupt 1 input.
Pin 14: T0 Counter 0 clock input.
Pin 15: T1 Counter 1 clock input.
Pin 16: WR Write to external (additional) RAM.
Pin 17: RD Read from external RAM.
Pin 18, 19: X2 X1 Internal oscillator input and output. A quartz crystal which specifies operating
frequency is usually connected to these pins. Instead of it, miniature ceramics resonators can also be
used for frequency stability. Later versions of microcontrollers operate at a frequency of 0 Hz up to
over 50 Hz.
Pin 20: GND Ground.
Pin 21-28: Port 2 If there is no intention to use external memory then these port pins are configured
as general inputs/outputs. In case external memory is used, the higher address byte, i.e. addresses A8-
A15 will appear on this port. Even though memory with capacity of 64Kb is not used, which means
that not all eight port bits are used for its addressing, the rest of them are not available as inputs/outputs.

Pin 29: PSEN If external ROM is used for storing program then a logic zero (0) appears on it every
time the microcontroller reads a byte from memory.
Pin 30: ALE Prior to reading from external memory, the microcontroller puts the lower address byte
(A0-A7) on P0 and activates the ALE output. After receiving signal from the ALE pin, the external
register (usually 74HCT373 or 74HCT375 add-on chip) memorizes the state of P0 and uses it as a
memory chip address. Immediately after that, the ALU pin is returned its previous logic state and P0
is now used as a Data Bus. As seen, port data multiplexing is performed by means of only one additional
(and cheap) integrated circuit. In other words, this port is used for both data and address transmission.
Pin 31: EA By applying logic zero to this pin, P2 and P3 are used for data and address transmission
with no regard to whether there is internal memory or not. It means that even there is a program written
to the microcontroller, it will not be executed. Instead, the program written to external ROM will be
executed. By applying logic one to the EA pin, the microcontroller will use both memories, first internal
then external (if exists).
Pin 32-39: Port 0 Similar to P2, if external memory is not used, these pins can be used as general
inputs/outputs. Otherwise, P0 is configured as address output (A0-A7) when the ALE pin is driven
high (1) or as data output (Data Bus) when the ALE pin is driven low (0).
Pin 40: VCC +5V power supply.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

STEPS TO CREATE AND COMPILE Keil μVision-3/4 PROJECT:

1. Double Click on the  icon on the desktop.


2. Close any previous projects that were opened using – Project -> Close.
3. Start Project – New Project, and select the CPU from the device database (Database-
Atmel- AT89C51ED2 or AT89C51RD2 as per the board).On clicking „OK‟, the following
option is displayed. Choose „No‟.

4. Create a source file (using File->New), type in the assembly or C program and save this
(filename.asm/filename.c) and add this source file to the project using either one of the
following two methods. (i) Project->Manage->Components, Environment Books-
>addfiles-> browse to the required file -> OK ―OR‖ ii) right click on the Source Group
in the Project Window and the Add Files to Group option.

5. Set the Target options using -> Project – Options for Target opens the μvision
Options for Target – Target configuration dialog. Set the Xtal (Crystal frequency)
frequency as 11.0592 MHz, and also the Options for Target – Debug – use either
Simulator / Keil Monitor- 51 driver.

6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select the
COM Port to which the board is connected and select the baud rate as 19200 or 9600

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

(recommended). Enable Serial Interrupt option if the user application is not using on- chip
UART, to stop program execution.

7. Build the project; using Project -> Build Project.  application and links. Any errors
in the code are indicated by – ―Target not created‖ in the Build window, along with the
error line. Debug the errors. After an error free, to build go to Debug mode.

8. Now user can enter into Debug mode with Debug- Start / Stop Debug session dialog.
Or by clicking in the icon.

9. The program is run using the Debug-Run command & halted using Debug-Stop
Running. Also the (reset, run, halt) icons can be used. Additional icons are
(step, step over, and step into, run till cursor).

10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led
status, etc. If it is a part-A program, the appropriate memory window is opened using View
-> memory window (for data RAM & XRAM locations), Watch window (for timer
program), serial window, etc.

11. Note: To access data RAM area type address as D: 0020h. Similarly to access the
DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type in X:
09000H.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Arithmetic Instruction Programming


a. Write an assembly language program to perform the addition of two
16-bit numbers.
mov r0,#34h //lower nibble of No.1
mov r1,#12h //higher nibble of No.1
mov r2,#0dch //lower nibble of No.2
mov r3,#0feh //higher nibble of No.2
clr c
mov a,r0
add a,r2
mov 22h,a
mov a,r1
addc a,r3
mov 21h,a
mov 00h,c
end

Result:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

b. Write an assembly language program to perform the subtraction of


two 16-bit numbers.
mov r0,#0dch //lower nibble of No.1
mov r1,#0feh //higher nibble of No.1
mov r2,#34h //lower nibble of No.2
mov r3,#12h //higher nibble of No.2 clr
c //
mov a,r0
subb a,r2
mov 22h,a
mov a,r1
subb a,r3
mov 21h,a
mov 00h,c
end

Result:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 02
To simulate two 8-bit number addition,
subtraction using immediate, direct and
EXPERIMENT NAME
indirect addressing modes.

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Simulation Experiment No. 2


Aim: To simulate two 8-bit number addition, subtraction using immediate
, direct and indirect addressing modes.

Software Required: Keil Software

Theory:

1. Immediate Addressing: In this mode, the operand is specified explicitly within he


instruction itself. For example, MOV A, #0x20 loads the immediate value 0x20 into the
accumulator register A.
2. Direct Addressing: In direct addressing mode, the operand is specified by its memory
address. For example, MOV A, 0x50 moves the contents of memory location 0x50 to the
accumulator register A.
3. Indirect Addressing: In indirect addressing mode, the memory address of the operand is
contained in a register. For example, MOV A, @R0 moves the contents of the memory
location pointed to by register R0 to the accumulator register A.

Algorithm 1: Direct addressing mode :

Step1 : Start the program.


Step2 : Load the first 8 bit data into the accumulator
Step 3 : Load the second 8 bit data into the address of memory location 50h Step4 :
perform arithemetic operation
Step5 : store the result in accumulator Step6 : Stop
the program execution

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Program:

ORG 0000H//This directive indicates the start of the program. MOV


A, #05H //load #05h into accumulator
MOV 50H, #03H //Load #03h in address 50h
ADD A,50H //A<—A+50H, the final value is stored in the ACC register//
or SUBB,50H//A<—A-50H, the final value is stored in the ACC register//
END // The END directive is used to indicate the end of the program.
Algorithm 2: Immediate addressing mode:

Step1: Start the program.


Step2: Load the first 8-bit data into the accumulator.
Step3: Perform arithmetic operation with immediate data using immediate
addressing.
Step5: store the result in accumulator
Step6: Stop the program execution

Program:

ORG 00H // This is used to set the register address during assembly. MOV
A, #05H// load #05h into accumulator
ADD A, #03H// A=A+IMMEDIATE DATA
OR SUBB, #03H//A<—A-IMMEDIATE DATA, the final value is stored in the ACC
register//
END// The END directive is used to indicate the end of the program.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Algorithm 3: Indirect addressing mode:


Step1: Start the program.
Step2: Load the first 8-bit data into the accumulator.
Step3: load #10H in register R0 as pointer, here the value of operand is
considered as an address
Step4: perform arithmetic operation using indirect addressing
Step5: store the result in accumulator Step
Step6: Stop the program execution

Program:

ORG 0000H // This is used to set the register address during assembly
MOV A, #30H // moving #30H into accumulator
MOV R0, #10H //moving #10H in register R0 as pointer, here the value of operand
is considered as an address
MOV 10H,#04H //Moving #04H in 10H memory location
ADD A,@R0 //Performing addition operation
SUBB A, @R0// Subtraction operation
END // The END directive is used to indicate the end of the program.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Output of Programs:
1. Direct addressing mode:

2. Immediate Addressing Mode:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

3. Indirect Addressing Mode:

Conclusion:

Through executing the assembly code on the 8051 microcontroller, we explored diverse
addressing modes and arithmetic operations, showcasing its versatility. This hands-on
experience underscored the significance of understanding the microcontroller's architecture
and instruction set for efficient embedded systems development.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 03

To simulate program using two 8-bit


EXPERIMENT NAME
multiplication and division.

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Simulation Experiment No. 3

Aim: To simulate program using two 8-bit multiplication and division.

Software Required: Keil Software

Theory:
Multiplication:

Multiplication is the process of repeated addition. In the context of 8-bit microcontrollers


like the 8051, multiplication of two 8-bit numbers is typically performed using the MUL
instruction. This instruction multiplies the content of the accumulator (A) with the content
of another register (commonly B). The result is stored in the 16-bit register pair A:B, where
A holds the lower 8 bits of the result, and B holds the higher 8 bits.

Division:

Division is the process of repeated subtraction. In the 8051 microcontroller, division of two
8-bit numbers is typically performed using the DIV instruction. This instruction divides the
content of the accumulator (A) by the content of another register (commonly B). After
division, the quotient is stored in the accumulator (A), and the remainder is stored in register
B.

Algorithm:

Step1 : Start the program.


Step2 : Load the first 8 bit data into the accumulator.
Step3 : Load the second 8 bit data into B reg
Step4 : perform multiplication operation
Step5 : store the result that is higher operand in 42h and lower operand in 43h
Step6 : Stop the program execution

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Program:
ORG 0000h // This is used to set the register address during assembly
MOV A,#25H //move #25h into accumalator
MOV B,#12H // move #12h in B
MUL AB // Perform multiplication operation
MOV 42H,A // move higher operand in 42h
MOV 43H,B // move lower operand in 43h
END // end the program
Division:
MOV A,#25H //move #25h into accumalator
MOV B,#12H// move #12h in B
DIV AB //perform division operation
MOV 44F,A //QUOTIENT MOVED TO 44H MEMORY LOCATION MOV
45H,B //REMAINDER MOVED TO 45H MEMORY LOCATION MOV A,
#25H
INC A //Increment accumalator
MOV 46H,A
DEC A // Decrement Accumalator
MOV 47H,A
END

Output of Programs:
1. Multiplication -

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2. Division –

Conclusion:
In conclusion, the provided assembly code exemplifies the fundamental arithmetic operations
of multiplication and division on the 8051 microcontroller. Through instructions like MUL
and DIV, the microcontroller efficiently computes results, utilizing the accumulator and
general-purpose registers.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 04

To simulate using logical operation using


EXPERIMENT NAME
immediate, direct and indirect addressing.
DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Simulation Experiment No. 4

Aim: To simulate using logical operation using immediate, direct and indirect
addressing.

Software Required: Keil

Software Theory:

Logical operations such as AND, OR, and XOR are fundamental bitwise operations that
manipulate individual bits within binary numbers. These operations are commonly used in
digital circuitry and microcontroller programming for tasks like data manipulation, bitwise
masking, and flag setting/clearing.

 AND Operation (ANL): The AND operation sets each bit of the result to 1 if both
corresponding bits of the operands are 1; otherwise, it sets the bit to 0.
 OR Operation (ORL): The OR operation sets each bit of the result to 1 if at least one of
the corresponding bits of the operands is 1; otherwise, it sets the bit to 0.
 XOR Operation (XRL): The XOR operation sets each bit of the result to 1 if the
corresponding bits of the operands are different; otherwise, it sets the bit to 0.

Algorithm:
1.Start the Program
2.Initialise the registers
3. Load accumalator with immediate value.
4. Perform logical operation using different addressing modes
5. Store the result in accumulator.
6. End the program.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Program:

Immediate addressing: ORL, EXOR, ANL:


ORG 0000h //Originate the program
MOV A, #20H /00100000/
ORL A, #03H
//OR// ANL A, #03H //Perform logical AND operation

//OR// XRL A,#03H //perform XOR operation


END // End the program
Direct addressing

org 0000h // Originate the program


MOV A,#20H //
MOV 50H,#03H
ORL A,50H
//OR// ANL A, 50H

//OR// XRL A,50H

END // End the program


INDIRECT ADDRESSING
ORG 0000H // Originate the program
MOV A,#20H
MOV R0, #10H
MOV 10H, # 03H

ORL A,@R0

//OR// ANL A,@ R0


//OR// XRL A,@RO
END // End the program

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Output of Programs:
1. Immediate Addressing -

2. Direct Addressing -

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

3. Indirect Addressing -

Conclusion:

In conclusion, the assembly code exemplifies the application of logical operations (AND,
OR, XOR) utilizing immediate, direct, and indirect addressing modes on the 8051
microcontroller. By employing these operations, developers can manipulate individual bits
within binary numbers, essential for tasks like data filtering, flag manipulation, and signal
processing in embedded systems.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 05

EXPERIMENT NAME Code Conversion from BCD nos to ASCII

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Simulation Experiment No. 5

Aim: Code Conversion from BCD numbers to ASCII.

Software Required: Keil Software

Theory:

Converting BCD numbers to ASCII involves converting each BCD digit to its
corresponding ASCII character. ASCII (American Standard Code for Information
Interchange) is a character encoding standard where each character is represented by a
unique 7-bit binary number.

Here's the basic theory behind converting BCD numbers to ASCII:

1. Extracting BCD Digits: BCD numbers are typically stored in binary format, with each
digit occupying a fixed number of bits. To convert a BCD number to ASCII, you first
need to extract each digit from the BCD representation.
2. Converting to ASCII: Once you have extracted each digit from the BCD representation,
you convert each digit to its corresponding ASCII character. This usually involves
adding an offset value to the digit's binary representation to map it to the ASCII
character set.
3. Storing ASCII Characters: After converting each BCD digit to its ASCII equivalent,
you store the ASCII characters in memory or some other storage medium for further
processing or output.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Algorithm:

1. Start the Program


2. Initialise the registers
3. Load accumalator with BCD.
4. Keep a copy of BCD data in R2
5. Perform and operation on upper nibble and Mask it an ASCII A=35
6. Load (R5=35H ASCII CHAR) and load A=25H,gets the original data
7. Now mask the lower nibble ( A=20H) and perform rotate right operation 4times
8. perform OR operation A=32H and Save ASCIII char in R6
9. End the program.

Program:-

org 0000h //Originate the program


MOV A, #25H //A=25H packed BCD mov
R2,A //Keep a copy of BCD data in R2
ANL A,#0FH //mask the upper nibble(A=05)
ORL A,#30H //Mask it an ASCII A=35
MOV R5 ,A //Save it (R5=35H ASCII CHAR)
MOV A,R2 //A=25H,gets the original data
ANL A,#0F0H //mask the lower nibble ( A=20H)
RRA // Rotate right
RRA // Rotate right
RRA // Rotate right
RRA // Rotate right
ORL A,#30H //A=32H ASCII CHAR
MOV R6,A //Save ASCIII char in R6
END //End the program

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Output of Program:

Conclusion:
In summary, converting Binary-Coded Decimal (BCD) numbers to ASCII in assembly
language for the 8051 microcontroller facilitates displaying numeric data on output devices
such as LCDs or serial monitors. Utilizing bitwise operations and arithmetic instructions,
the 8051 can efficiently extract each BCD digit and map it to its corresponding ASCII
character. This conversion process is essential for generating human-readable output in
embedded systems applications, enabling users to interpret numerical data easily.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 06
Transfer of data bytes between internal and
EXPERIMENT NAME external memory Simulation.

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Experiment No. 6

Aim: Transfer of data bytes between internal and external memory Simulation

Software Required: Keil Software

Theory:

The transfer of data bytes between internal and external memory on the 8051
microcontroller involves understanding memory addressing, data movement instructions,
and memory interface configuration.

Memory Addressing:

 The 8051 microcontroller has both internal and external memory spaces.
 Internal memory, also known as RAM, is accessed directly through the
microcontroller's memory addressing scheme.
 External memory, such as EEPROM or Flash memory, typically requires additional
interfacing components and may have different addressing requirements.

Algorithm:
1. Start the program.
2. Initialize R2 as counter
3. Load R0 and R1 with internal and external memory address.
4. Move the content at memory location into accumulator
5. Store the content of accumulator into memory pointed by R0
6. Increment value of R0and R1
7. DJNZ instruction decrements the byte indicated by the #05h and, if the resulting
value is not zero, branches to back.
8. If R2 not equals to 0, go to step 3
9. End the program

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Program:

ORG 00h
MOV R2,#05H //R2 set as counter for 5
MOV R0,#20H //R0 indicates internal memory
MOV R1,#30H //R1 indicates external memory
BACK:MOV A,@RO // send the value to A
MOV @R1,A // send the value to R1
INC R0 //Increment R0
INC R1 //Increment R1
DJNZ R2,BACK //Repeat if R2 NOT=0
END // End the program

Output of Program:

Conclusion:

In conclusion, the process of transferring data bytes between internal and external memory
on the 8051 microcontroller is a critical aspect of embedded systems development. By
leveraging memory addressing capabilities and data movement instructions, developers can
efficiently move data between different memory spaces.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 07
Arrange the given set of data in ascending
EXPERIMENT NAME and descending order

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Experiment No. 7

Aim: Arrange the given set of data in ascending and descending order

Software Required: Keil Software

Theory:

In the context of the 8051 microcontroller, arranging data in ascending and descending
order involves utilizing sorting algorithms tailored to the microcontroller's architecture and
available resources. Here's the basic theory behind sorting data on the 8051:

Ascending Order:

1. Algorithm Selection: Choose a sorting algorithm suitable for the limited memory and
processing capabilities of the 8051, such as Bubble Sort, Insertion Sort, or Selection
Sort.
2. Implementation: Write assembly code to implement the selected sorting algorithm,
ensuring efficient memory usage and minimizing computational overhead.
3. Data Comparison and Swapping: Perform pairwise comparisons between elements and
swap them if they are out of order, iterating through the data until it is sorted in
ascending order.

Descending Order:

1. Algorithm Modification: Modify the selected sorting algorithm's logic to sort data in
descending order by reversing the comparison and swapping conditions.
2. Implementation: Adapt the assembly code accordingly to implement the modified
sorting algorithm for descending order.
3. Efficiency Consideration: Ensure that the sorting algorithm's efficiency is acceptable
for the size of the dataset and the available memory and processing resources of the 8051
microcontroller.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Algorithm: Ascending order


1. Initialize the register and data pointer.
2. Get first two elements in registers A &B.
3. Compare the two elements of data. If value of B register is high then exchange
A & B data else increment pointer and decrement register R0.
4. Check R3 is zero, and then move the register R2 & R6.
5. Again increment pointer and decrement R0
6. Check R0 is zero. If no repeat the process from step 2.
7. Otherwise end the program.

Program:
ASCENDING ORDER
ORG 00H
MOV R6,#03H
MAIN:MOV R0,#25H
MOV R2,#03H
UP:MOV A, @R0
INC R0
MOV B,@R0
CJNE A,B ,NEXT
NEXT:JC NOEXCHANGE
MOV @R0,A
DEC R0
MOV @R0,B
INC R0
NOEXCHANGE: DJNZ R2,UP
DJNZ R6,MAIN
SJMP EXIT
EXIT:END

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Algorithm: Descending order


1. Initialize the register and data pointer.
2. Get first two elements in registers A &B.
3. Compare the two elements of data. If value of B register is low then exchange A
& B data else increment pointer and decrement register R0.
4. Check R3 is zero, and then move the register R2 & R6.
5. Again increment pointer and decrement R0
6. Check R0 is zero. If no repeat the process from step 2.
7. Otherwise end the program

Program: DESCENDING ORDER


ORG 00H
MOV R6,#03H
MAIN:MOV R0,#25H
MOV R2,#03H
UP:MOV A, @R0
INC R0
MOV B,@R0
CJNE A,B ,NEXT
NEXT:JNC NOEXCHANGE
MOV @R0,A
DEC R0
MOV @R0,B

INC R0

NOEXCHANGE:DJNZ R2,UP
DJNZ R6,MAIN
SJMP EXIT
EXIT:END

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Output of Programs:

1. Ascending order-

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

2. Descending order-

Conclusion:
Arranging data in ascending and descending order on the 8051 microcontroller requires
adapting sorting algorithms to fit within the constraints of the microcontroller's architecture.
By selecting appropriate algorithms and optimizing their implementation, developers can
effectively manage and manipulate data within embedded systems applications, ensuring
efficient and reliable operation in real-world scenarios.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 08

EXPERIMENT NAME To generate square wave using timer.

DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Experiment No. 8

Aim: To generate square wave using timer.

Software Required: Keil Software

Theory:
Generating a square wave using a timer on the 8051 microcontroller involves configuring
one of the timer/counters to produce periodic interrupts. Here's the basic theory behind
generating a square wave using a timer:

Timer/Counter Modes:
 The 8051 microcontroller typically has multiple timer/counters, each supporting various
modes of operation.
 For generating a square wave, you'll typically use one of the timer/counters in an interval
timing mode, such as mode 1 (16-bit mode) or mode 2 (8-bit auto-reload mode).

Timer Configuration:
 Configure the timer/counter mode using the TMOD (Timer Mode) register.
 Set the initial values for the timer/counter using the THx (Timer High Byte) and TLx
(Timer Low Byte) registers.
 Start the timer by setting the TRx (Timer Run) bit.

Interrupts:
 Configure the microcontroller to generate interrupts when the timer/counter overflows.
 Write an interrupt service routine (ISR) that will be executed each time the timer/counter
overflows.
 Inside the ISR, toggle the output pin to generate the square wave.

Main Loop:
 Implement the main program logic within a loop that continuously executes.
 This loop will continue to run while the timer generates the square wave.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Program:

CLR P1.0

MOV TMOD, #01H


AGAIN: MOV TL0, #0CH
MOV TH0, #0FEH CPL
P1.0
SETB TR0 WAIT:
JNB TF0, WAIT
CLR TR0 CLR TF0
SJMP AGAIN
END

Output of Program:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Conclusion:

In conclusion, generating a square wave using a timer on the 8051 microcontroller is achieved
by configuring the timer/counter and utilizing interrupt-based timing. By setting appropriate
timer modes and initializing timer values, the microcontroller periodically triggers interrupts
upon timer overflow. Within the interrupt service routine, the output pin is toggled, generating
a square wave signal. This method allows for precise control over the square wave frequency
and duty cycle. Understanding timer configurations and interrupt handling is crucial for
efficiently generating square waves in embedded systems applications, enabling various
functionalities such as signal generation, timing, and synchronization.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

EXPERIMENT NUMBER 09

Interface LCD with 8051 and display a


EXPERIMENT NAME
message on LCD
DATE OF PERFORMANCE

DATE OF CORRECTION

SIGN & DATE

GRADE

TIMELY LAB
ATTENDANCE ORAL TOTAL
CORRECTION PERFORMANCE
ALLOTTED 2 2 3 3 10

OBTAINED

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Experiment No. 9

Aim: Interface LCD with 8051 and display a message on LCD

Software Required: Keil Software

Theory:
When interfacing an LCD with an 8051 microcontroller, it's important to understand the
specific features and capabilities of the 8051 architecture. Here's the theory regarding
interfacing an LCD with an 8051 microcontroller:

I/O Ports:
 The 8051 microcontroller features four I/O ports: P0, P1, P2, and P3.
 These ports can be configured as both input and output ports.
 Each port consists of 8 pins, allowing for a total of 32 I/O lines.

Data and Control Lines:


 In LCD interfacing, the data lines (D0-D7) are connected to one of the 8051 ports for
transmitting character data.
 The control lines, including Register Select (RS), Read/Write (RW), and Enable (EN),
are connected to individual pins of another port.

LCD Initialization:
 Before using the LCD, it needs to be initialized to set the display parameters.
 Initialization involves sending specific commands to the LCD controller, typically
through the control lines.
 These commands configure the LCD, such as setting the display mode, cursor position,
and cursor blinking.

Sending Commands and Data:


 To send commands and data to the LCD, the microcontroller sets the appropriate control
signals.
 The RS line is used to select between command and data mode.
 The RW line determines the direction of data flow (write or read).

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

 The EN line enables the LCD to latch the command or data on the falling edge of the
signal.

Timing Considerations:
 Timing is crucial when interfacing with the LCD to ensure that commands and data are
properly processed.
 The microcontroller needs to meet the timing requirements specified by the LCD
datasheet, especially regarding pulse widths and setup/hold times.

Program:
ORG 000H
MOV P1,#00H
MOV A,#38H
ACALL CMND
MOV A,#0EH
ACALL CMND
MOV A,#01H
ACALL CMND
MOV A,#06H
ACALL CMND
MOV A,#84H
ACALL CMND
MOV A,#3CH
ACALL CMND
MOV A,#'W'
ACALL DISP
MOV A,#'E'
ACALL DISP
MOV A,#'L'
ACALL DISP
MOV A,#'C'
ACALL DISP
MOV A,#'O'
ACALL DISP
MOV A,#'M'
ACALL DISP
MOV A,#'E'
LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB
(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

ACALL DISP
AGAIN:SJMP AGAIN
CMND: MOV P1,A
CLR P2.0
CLR P2.1
SETB P2.2
ACALL DELY
CLR P2.2
DISP: MOV P1,A
SETB P2.0
CLR P2.1
SETB P2.2
ACALL DELY
CLR P2.2
RET
DELY: MOV R3,#50
HERE2: MOV R4,#235
HERE: DJNZ R4, HERE
DJNZ R3, HERE2
RET
END

Output of Program:

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB


(Permanently Affiliated to University of Mumbai)
Department of Electronics and Telecommunication Engineering

Conclusion:
In conclusion, interfacing an LCD with an 8051 microcontroller involves leveraging the
microcontroller's I/O ports to control the display through specific commands. Understanding
the 8051 architecture and the commands supported by the LCD is essential for effective
communication and display management. By configuring the I/O ports, sending initialization
commands, and transmitting data, developers can seamlessly integrate LCDs into embedded
systems using assembly language programming. This enables a wide range of applications,
from displaying real-time data to providing user interfaces in embedded devices. Mastery of
LCD interfacing with the 8051 empowers developers to create efficient and interactive
embedded systems solutions, enhancing their functionality and usability in various domains.

LABORATORY: MICROPROCESSOR AND MICROCONTROLLER LAB

You might also like