0% found this document useful (0 votes)
15 views49 pages

MPMC-Lab-Manual VKTiwari 18 08 2021

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 49

MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING

LABORATORY MANUAL

MICROPROCESSORS AND MICROCONTROLLERS

LABORATORY

For
B.TECH ECE & EIE III YEAR I SEMESTER
And
B.TECH EEE III YEAR II SEMESTER

ACADEMIC YEAR: 2020 - 2021

VIGNAN INSTITUTE OF TECHNOLOGY AND SCIENCE


(Programmes Accredited by NBA and Certified by ISO 9001:2000)
(Approved by AICTE and Affiliated to JNT University, Hyderabad)
Vignan hills, Deshmukhi Village, Pochampally Mandal, Yadadri District - 508284
Sponsored by : Lavu Educational Society

Department of Electronics and Communication Engineering 1


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

COURSE OBJECTIVES AND COURSE OUTCOMES

Course objectives:
 This course is intended to
 Introduce the basic concepts of microprocessor and Microcontrollers.
 Develop in students the assembly language programming skills and real time applications of
Microprocessor as well as microcontroller.
 Understand how different I/O devices can be interfaced to Microprocessors.

Course Outcomes: After undergoing the lab course students will be able to
 Apply the fundamentals of assembly level programming of microprocessors and
microcontrollers.
 Analyze abstract problems and apply a combination of hardware and software to address the
problem
 Contrast how different I/O devices can be interfaced to processor and will explore several
techniques of interfacing.
 Experiment with standard microprocessor interfaces including serial ports, digital-to-analog
converters and analog-to-digital converters.

Department of Electronics and Communication Engineering 2


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

SYLLABUS

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD

EC505PC: MICROPROCESSORS AND MICROCONTROLLERS LAB

B.Tech. ECE and EIE III Year I Semester


B.Tech. EEE III Year II Semester

LTPC 0 0 3 1.5

Cycle 1: Using 8086 Processor Kits and/or Assembler (5 Weeks)


Assembly Language Programs to 8086 to Perform

1. Arithmetic, Logical, String Operations on 16 Bit and 32-Bit Data.


2. Bit level Logical Operations, Rotate, Shift, Swap and Branch Operations.

Cycle 2: Using 8051 Microcontroller Kit (6 weeks)


Introduction to IDE
1. Assembly Language Programs to Perform Arithmetic (Both Signed and Unsigned) 16
Bit Data Operations, Logical Operations (Byte and Bit Level Operations), Rotate, Shift,
Swap and Branch Instructions
2. Time delay Generation Using Timers of 8051.
3. Serial Communication from / to 8051 to / from I/O devices.
4. Program Using Interrupts to Generate Square Wave 10 KHZ Frequency on P2.1 Using
Timer 0 8051 in 8 bit Auto reload Mode and Connect a 1 HZ Pulse to INT1 pin and
Display on Port 0. Assume Crystal Frequency as 11.0592 MHZ

Cycle 3: Interfacing I/O Devices to 8051(5 Weeks)


1. 7 Segment Display to 8051.
2. Matrix Keypad to 8051.
3. Sequence Generator Using Serial Interface in 8051.
4. 8 bit ADC Interface to 8051.
5. Triangular Wave Generator through DAC interfaces to 8051.

TEXT BOOKS:
1. Advanced Microprocessors and Peripherals by A K Ray, Tata McGraw-Hill Education, 2006
2. The 8051 Microcontrollers: Architecture, Programming & Applications by Dr. K. Uma Rao,
Andhe Pallavi, Pearson, 2009.

Department of Electronics and Communication Engineering 3


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

INDEX

S.No. NAME OF THE EXPERIEMNT Page No. Remarks

Cycle 1: Using 8086 Processor Kits and/or Assembler


1. Arithmetic, Logical, String Operations on 16 Bit and 32-Bit Data.
12
16-bit Numbers Addition/Subtraction using Immediate
1A 13
Addressing Mode
16-bit Arithmetic Operations For 8086 Using Direct
1B 14
Addressing Mode And Register Addressing Mode
16-Bit Arithmetic Operations For 8086 Using Indexed
1C 15
Addressing Mode.
16-bit Numbers AND/OR operations using Immediate
1D 16
Addressing Mode.
1E 32-bit Numbers Addition operations 16
Bit level Logical Operations, Rotate, Shift, Swap and Branch
2. Operations. 17

Program to find number of zeros and ones in a given binary


2A 17
number.
Program to find number of positives numbers and negative
2B 18
numbers in a given array of numbers
2C Program Ascending order sorting 19

Cycle 2: Using 8051 Microcontroller Kit


Assembly Language Programs to Perform Arithmetic (Both
1. Signed and Unsigned) 16 Bit Data Operations, Logical
31
Operations (Byte and Bit Level Operations), Rotate, Shift, Swap
and Branch Instructions
1A Programs For Arithmetic Operations 31

1B Programs For Logical And Bit Manipulation Operations 32

2. Time delay Generation Using Timers of 8051. 34

3. Serial Communication from / to 8051 to / from I/O devices. 35


Program Using Interrupts to Generate Square Wave 10 KHZ
4. Frequency on P2.1 Using Timer 0 8051 in 8 bit Auto reload
36
Mode and Connect a 1 HZ Pulse to INT1 pin and Display on Port
0. Assume Crystal Frequency as 11.0592 MHZ

Department of Electronics and Communication Engineering 4


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

S.No. NAME OF THE EXPERIEMNT Page No. Remarks

Cycle 3: Interfacing I/O Devices to 8051

1. 7 Segment Display to 8051. 38

2. Matrix Keypad to 8051. 41

3. Sequence Generator Using Serial Interface in 8051. 44

4. 8 bit ADC Interface to 8051. 45

5. Triangular Wave Generator through DAC interfaces to 8051. 47

ADDITIONAL EXPERIEMNTS
1. Program to find number of Even and Odd numbers in a given
48
array.
2. Program to find number of Largest and Smallest numbers in a
49
given array.

Department of Electronics and Communication Engineering 5


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

INTRODUCTION TO 8086 MICROPROCESSOR

8086 ARCHITECTURE:

Department of Electronics and Communication Engineering 6


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PIN DIAGRAM:

8086 INSTRUCTION SET SUMMARY:

The following is a brief summary of the 8086 instruction set:


Data Transfer Instructions
MOV : Move byte or word to register or memory
IN, OUT : Input byte or word from port, output word to port
LEA : Load effective address
LDS, LES : Load pointer using data segment, extra segment
PUSH, POP : Push word onto stack, pop word off stack
XCHG : Exchange byte or word
XLAT : Translate byte using look-up table

Department of Electronics and Communication Engineering 7


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Logical Instructions
NOT : Logical NOT of byte or word (one's complement)
AND : Logical AND of byte or word
OR : Logical OR of byte or word
XOR : Logical exclusive-OR of byte or word
TEST : Test byte or word (AND without storing)

Shift and Rotate Instructions


SHL SHR : Logical shift left, right byte or word by 1 or CL
SAL SAR : Arithmetic shift left, right byte or word by 1 or CL
ROL ROR : Rotate left, right byte or word by 1 or CL
RCL RCR : Rotate left, right through carry byte or word by 1 or CL

Arithmetic Instructions
ADD SUB : Add, subtract byte or word
ADC SBB : Add, subtract byte or word and carry (borrow)
INC DEC : Increment, decrement byte or word
NEG : Negate byte or word (two's complement)
CMP : Compare byte or word (subtract without storing)
MUL DIV : Multiply, divide byte or word (unsigned)
IMUL IDIV : Integer multiply or divide byte or word (signed)
CBW CWD : Convert byte to word, word to double word (useful before
multiply/divide)
AAA,AAS,AAM,AAD: ASCII adjust for addition, subtraction, multiplication,
division (ASCII codes 30-39)
DAA, DAS : Decimal adjust for addition, subtraction (binary coded
decimal numbers)
Transfer Instructions
JMP : Unconditional jump
JA (JNBE) : Jump if above (not below or equal)
JAE (JNB) : Jump if above or equal (not below)
JB (JNAE) : Jump if below (not above or equal)
JBE (JNA) : Jump if below or equal (not above)
JE (JZ) : Jump if equal (zero)
JG (JNLE) : Jump if greater (not less or equal)
JGE (JNL) : Jump if greater or equal (not less)
JL (JNGE) : Jump if less (not greater nor equal)
JLE (JNG) : Jump if less or equal (not greater)

Department of Electronics and Communication Engineering 8


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

INTRODUCTION TO MASM
MS-MASM:
Microsoft‟s Macro Assembler (MASM) is an integrated software package Written by Microsoft
Corporation for professional software developers. it consists of an editor, an assembler, a linker and
a debugger(Code View). The programmer‟s workbench combines these four parts into a user-
friendly programming environment with built in on line help. The following are the steps used if
you are to run MASM from DOS. Summarized basic DOS - Debugger commands.

EDITOR:
An editor is a program, which allows you to create a file containing the assembly language
statements for your program. As you type in your program the editor stores the ASCII codes for the
letters and numbers in successive RAM locations. When you have typed in all of your programs you
then save the file on a floppy of hard disk. This file is called source file. The next step is to process
the source file with an assembler. In the MASM/TASM assembler you should give your source file
name the extension .ASM

ASSEMBLER:
An assembler program is used to translate the assembly language mnemonics for instructions
to the corresponding binary codes. When you run the assembler it reads the source file of your
program from the disk where you saved it after reading on the first pass through the source program,
the assembler determines the displacement of named data items, the offset of labels and this
information is stored on a symbol table. On the second pass through the source program, the assembler
produces the binary code for each instruction and inserts the offset etc that was calculated during the
first pass. The assembler generates two files on floppy or hard disk. The first file called the object
file is given the extension. OBJ. The object file contains the binary codes for the instructions
and information about the addresses of the instructions. The second file generated by the assembler is
called assembler list file. The list file contains your assembly language statements the binary
codes for each instructions and the offset for each instruction. In MASM/TASM assembler
MASM source file name ASM is used to assemble the file. Edit source file name LST is used to
view the list file which is generated, when you assemble the file.

Department of Electronics and Communication Engineering 9


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

LINKER:

A linker is a program used to join several object files into one large object file and convert it to
an exe file. The linker produces a link file, which contains the binary codes for all the combined
modules. The linker however doesn‟t assign absolute addresses to the program, it assigns is said
to be re-locatable because it can be put anywhere in memory to be run. In TASM/MASM LINK
source filename is used to link the file.

DEBUGGER:

A debugger is a program which allows you to load your object code program into system memory,
execute the program and troubleshoot or debug it. The debugger allows you to look at the contents of
registers and memory locations after your program runs. It allows you to change the contents of
register and memory locations after your program runs. It allows you to change the contents of
register and memory locations and return the program. A debugger also allows you to set a break
point at any point in the program. If you inset a breakpoint the debugger will run the program
up to the instruction where the breakpoint is set and stop execution. You can then examine
register and memory contents to see whether the results are correct at that point. In MASM, MD
filename is used to debug the file.

DEBUGGER FUNCTIONS:

1. Debugger allows to look at the contents of registers and memory locations.


2. We can extend 8-bit register to 16-bit register with the help of extended register option.
3. Debugger allows to set breakpoints at any point with the program.
4. The debugger will run the program up to the instruction where the breakpoint is set and then stop
execution of program. At this point, we can examine registry and memory contents at that point.
5. With the help of dump we can view register contents.
6. we can trace the program step by step with the help of T.
7. We can execute the program completely at a time using G.

Department of Electronics and Communication Engineering 10


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

COMMAND SYNTAX

Assemble A [address]
Compare C range address
Dump D [range]
Enter E address [list]
Fill F range list
Go G [=address] [addresses]
Hex H value1 value2
Input I port
Load L[address] [drive][first sector][number]
Move M range address
Name N[pathname][argument list]
Output O port byte
Proceed P [=address][number]
Quit Q [Quit]
Register R[register]
Search S range list
Trace T [=address][value]
Unassembled U [range]
Write W [address}[drive][first sector][number]

MICROPROCESSOR MASM EXECUTION PROCEDURE

STEP1: Opening DOS prompt, Click on start menu button and click on Run and then type CMD at
command prompt DOS window will be appeared.

STEP2: Checking the MASM installation. To know MASM is installed or not simply type MASM
at the command prompt Upon that it replies MASM version vendor (Microsoft), etc., If you get any
error there is no MASM in that PC.

STEP3: Directory changing (create a folder with your branch and not in C drive) Change the
current directory to your own directory suppose your folder in D drive type the following
commands to change the directory at command prompt type D: hit enter, now you are in D drive
type cd folder name hit the enter.
Ex. D: cd MASM/ECEA
Now we are in folder ECEA

STEP4: writing the program. At the command prompt type the edit programname.asm
Ex. Edit add1.asm
Immediately editor window will be opened. Type the program in that window. Save the program
after completion. To save the program, go to file opt in the menu bar and select save opt now your
code is ready to assemble.

STEP5: Assembling, linking and executing the program Go to file opt& click exit opt. Now DOS
prompt will be displayed ,to assemble the program type the following commands at the DOS
prompt MASM program name, program name, program name hit the enter.
Ex. MASM add1.asm;

Department of Electronics and Communication Engineering 11


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

If there are any errors in the program assembler reports all of them at the command prompt with
line no‟s, if there are no bugs you are ready to link the program. To link the program type the
following line at command prompt.
Link program name
Ex. Link add1.Obj;
After linking to execute the program type the following command
Debug programname.exe hit the enter
Ex. Debug add1.exe
To execute the program, instruction by instruction (debugging) first of all press the R key (register)
hit the enter key, it‟ll displays all the registers and their initial values in HEXADECIMAL note
down the values of all the register which are used in the program. To execute the next instruction
press t key (TRACE) hit the enter it‟ll execute that instruction and displays the contents of all the
register. You have to do this until you reach the last instruction of the program. After execution you
have to observe the results (in memory or registers based on what you have written in the program).

STEP6: copying list file (common for all programs):


A list file contains your code starting address and end address along with your program For every
program assembler generates a list file at your folder, Programname.lst (ex. Add.lst) you should
copy this to your lab observation
Opening a list file Edit programname.lst
Ex. Edit add1.lst

Department of Electronics and Communication Engineering 12


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 1: Using 8086 Processor Kits and/or Assembler (5 Weeks)

Assembly Language Programs to 8086 to Perform


Cylce-1 Exp. I. Arithmetic, Logical, String Operations on 16 Bit and 32-Bit Data.
Programs for 16 bit arithmetic operations for 8086 (using Various Addressing Modes).

PROGRAM 1 (A): 16-bit Numbers Addition/Subtraction using Immediate Addressing Mode

ASSUME CS:CODE
CODE SEGMENT
Start:
MOV AX, 1235
ADD AX, 4683 ;SUB for Subtraction
INT 03H
CODE ENDS
END Start

Department of Electronics and Communication Engineering 13


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 1 (B): 16-bit ARITHMETIC OPERATIONS FOR 8086 USING DIRECT


ADDRESSING MODE AND REGISTER ADDRESSING MODE.
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
N1 DW 1525H
N2 DW 2695H
SUM DW ?
DIFF DW ?
MULT1 DW ?
MULT2 DW ?
REM DW ?
QUO DW ?
DATA ENDS

CODE SEGMENT
Start: MOV AX, DATA ;Initialization of data segment
MOV DS, AX
MOV AX, 0000H ;XOR AX, AX
MOV AX, N1
ADD AX, N2 ;Direct Addressing Mode
MOV SUM, AX
MOV AX, N1
SUB AX, N2
MOV DIFF, AX
MOV AX,N1
MOV BX,N2
MUL BX ;Register
MOV MULT 1, AX
MOV MULT 2, DX
MOV AX, N1
MOV BX, N2
XOR DX, DX
DIV BX
MOV REM, DX
MOV QUO, AX
INT 03H
CODE ENDS
END Start

Department of Electronics and Communication Engineering 14


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 1 (C): 16-bit ARITHMETIC OPERATIONS FOR 8086 USING INDEXED


ADDRESSING MODE.

ASSUME CS:CODE, DS:DATA


DATA SEGMENT
ORG 1000H ;Organizing the memory
N DW 1525H, 2695H ;Defining the array in 8086
SUM DW ?
DATA ENDS
CODE SEGMENT
Start:
MOV AX, DATA
MOV DS, AX
LEA SI, N
MOV AX, [SI]
INC SI
INC SI
ADD AX, [SI]
MOV SUM, AX
INT 03H
CODE ENDS
END Start

Department of Electronics and Communication Engineering 15


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 1 (D): 16-bit Numbers AND/OR operations using Immediate Addressing Mode

ASSUME CS:CODE
CODE SEGMENT
Start:
MOV AX, 1235
AND AX, 4683 ;OR for OR Operation
INT 03H
CODE ENDS
END Start

PROGRAM 1 (E): 32-bit Numbers Addition operations

ASSUME CS:CODE
DATA SEGMENT
N1 DD 15258545H
N2 DD 26952565H
SUM DD ?
DATA ENDS

CODE SEGMENT
Start:
MOV AX, DATA
MOV DS, AX
LEA SI, N1
MOV AX, [SI] ;DX AX Contains N1
INC SI
INC SI
MOV DX, [SI]
LEA SI, N2
MOV BX, [SI] ;CX BX Contains N2
INC SI
INC SI
MOV CX, [SI]
ADD AX, BX ;SUB for Subtraction
ADC DX,CX ;SBB for Subtraction with borrow
INT 03H
CODE ENDS
END Start

Department of Electronics and Communication Engineering 16


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cylce-1 Exp. II. Bit level Logical Operations, Rotate, Shift, Swap and Branch Operations

PROGRAM 2 (A): Program to find number of zeros and ones in a given binary number
ASSUME CS:CODE, DS:DATA
DATA SEGMENT.
NUM DB 58H
ZEROS DB ?
ONES DB ?
DATA ENDS

CODE SEGMENT
Start :
MOV AX, DATA
MOV DS, AX ; Initialize Data Segment
MOV AL, NUM ; Get number into Accumulator
MOV BL, 00H ; initially ZERO count = 00H
MOV DL, 00H ; initially ONES count = 00H
MOV CX, 08H ; initialize bit counter = 08 H
UP : ROL AL, 01H ; Rotate left 'NUM'
JC ONE ; If CY = 1 go to label ONE
INC BL ; Increment 0's Count.
JMP NXT :
ONE : INC DL ; Increment 1's Count
NXT : LOOP UP ; Dec cx and if cx = 0 go to up.
MOV ZEROS , BL ; ZEROS = no. of 0's
MOV ONES, DL ; ONES = no. of 1's
INT 03H ; Terminate program by break point.
CODE ENDS
END Start

Department of Electronics and Communication Engineering 17


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 2 (B): Program to find number of positives numbers and negative numbers in a given
array of numbers

ASSUME CS: CODE, DS: DATA


DATA SEGMENT
N1 DB 51H, 20H, 33H, 80H, 19H
POSITIVE DW 1 DUP (00)
NEGATIVE DW 1 DUP (00)
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DX, 0000H
MOV BX, 0000H
MOV CX, 0005H
BACK: MOV AL, [SI]
SAL AL, 01H ;SHL is same as SAL
JC X
INC BX
JMP Y
X: INC DX
Y: INC SI
DEC CX
JNZ BACK
MOV POSITIVE,BX
MOV NEGATIVE,DX
INT 3H
CODE ENDS
END START

Department of Electronics and Communication Engineering 18


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 2 C): Ascending order sorting

ASSUME CS: CODE, DS: DATA


DATA SEGMENT
ORG 2000H
ARRAY1 DW 8342H, 1452H, 3536H, 7741H, 4502H ; Declaring an array
LEN1 EQU ($-ARRAY1)/2 ; EQU is a assembler directive
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV BX,LEN1-1
ABC: MOV CX, BX ; External Loop
LEA SI, ARRAY1
BACK: MOV AX,[SI] ;Internal Loop
CMP AX,[SI+2] ;Comparing
JC XYZ ; Jump if carrying is set
; JNC for Descending order sorting
XCHG AX,[SI+2]
XCHG AX,[SI]
XYZ: INC SI
INC SI
DEC CX
JNZ BACK ; if ZF is not equal then jump
DEC BX
JNZ ABC ; Jump if zero flag ZF=0
INT 03H
CODE ENDS
END START

Department of Electronics and Communication Engineering 19


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 2: Using 8051 Microcontroller Kit (6 weeks)

INTRODUCTION TO IDE

An Introduction to Keil MicroVision


Embedded system means some combination of computer hardware and programmable software
which is specially designed for a particular task like displaying message on LCD. If you are still
wondering about an embedded system, just take a look at these circuit applications using 8051
microcontroller. You can call these applications embedded systems as it involves hardware (8051
microcontroller) and software (the code written in assembly language).
1. Voltmeter using 8051
2. Thermometer using 8051
3. Frequency counters using AVR

Some real life examples of embedded systems may involve ticketing machines, vending machines,
temperature controlling unit in air conditioners etc. Microcontrollers are nothing without a Program
in it.
One of the important part in making an embedded system is loading the software/program we
develop into the microcontroller. Usually it is called “burning software” into the controller. Before
“burning a program” into a controller, we must do certain prerequisite operations with the
program. This includes writing the program in assembly language or C language in a text editor like
notepad, compiling the program in a compiler and finally generating the hex code from the
compiled program. Earlier people used different softwares/applications for all these 3 tasks. Writing
was done in a text editor like notepad/WordPad, compiling was done using separate software
(probably a dedicated compiler for a particular controller like 8051), converting the assembly code
to hex code was done using another software etc. It takes lot of time and work to do all these
separately, especially when the task involves lots of error debugging and reworking on the source
code.
Keil MicroVision is free software which solves many of the pain points for an embedded program
developer. This software is an integrated development environment (IDE), which integrated a text
editor to write programs, a compiler and it will convert your source code to hex files too.
Here is simple guide to start working with Keil uVision which can be used for
 Writing programs in C/C++ or Assembly language
 Compiling and Assembling Programs
 Debugging program
 Creating Hex and Axf file
 Testing your program without Available real Hardware (Simulator Mode)
This is simple guide on Keil uVision 4 though also applicable on previous versions also.

Department of Electronics and Communication Engineering 20


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Steps for Creating an Assembly project in Keil


1) Open the Keil IDE by clicking on its icon on the desktop.
2) Choose new uVision Project from the Project menu.

3) Create a new folder and Name it OurFirstProject. Type the name ourFirstProject for the project
and click Save.

Department of Electronics and Communication Engineering 21


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

4) In the Data base tree, choose the vendor and then the chip you want to use and then click OK. For
example, if you want to use the LPC2368, click on the NXP and then on the LPC2368 and then
press OK.

5) Click the YES button to add the startup file to the project.

6) Make a new file by clicking on the New Icon (you can make a new file by pressing Ctrl+N or
choosing New from the File menu, as well.)

7) Press Ctrl+S to save the new file. (You can also save the file by choosing Save from the File
menu.)

Department of Electronics and Communication Engineering 22


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

8) Name the file as program.asm and save it in the OurFirstProject directory.

9) Type the following sample program in the file

10) Add the program.asm file to the project. To do so:


a. Right click on Source Group 1 and choose Add Files to Group.

Department of Electronics and Communication Engineering 23


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

b. Then go to the OurFirstProject directory and choose Program.asm, press Add and then Close.

Building

11) To compile click on the Build icon or choose build target from the Project menu.

Department of Electronics and Communication Engineering 24


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

12) If the program is built successfully the following message appears:

Debugging and Tracing

13) To start debugging click on Start/Stop Debug Session icon or choose Start/Stop Debug Session
from the Debug menu. (or simply press Ctrl+F5)

14) If it starts tracing successfully, a cursor appears in front of the next instruction to be executed.

15) Go to the Peripherals menu. It has tools for monitoring different peripherals. We can see values of
registers while tracing the program or change their values by clicking on each bit.

Department of Electronics and Communication Engineering 25


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

16) To trace the program use the Step Over button or click on Step Over from the Debug menu. It
executes the instructions of the program one after another. To trace the program you can use the
Step button, as well. The difference between the Step Over and Step is in executing functions.
While Step goes into the function and executes its instructions one by one, Step Over executes the
function completely and goes to the instruction next to the function. To see the difference between
them, trace the program once with Step Over and then with Step. When you are in the function and
you want the function to be executed completely you can use Step Out. In the case, the instructions
of the function will be executed, it returns from the function, and goes to the instruction which is
next to the function call.

17) To exit from the debugging mode press Start/Stop Debug Session.

Department of Electronics and Communication Engineering 26


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

8051 ARCHITECTURE:

Basic components present internally inside 8051 Microcontroller architecture are:

CPU (Central Processing Unit): CPU act as a mind of any processing machine. It synchronizes
and manages all processes that are carried out in microcontroller. User has no power to control the
functioning of CPU. It interprets the program stored in ROM and carries out from storage and then
performs it projected duty. CPU manage the different types of registers available in 8051
microcontroller.

Interrupts: Interrupts is a sub-routine call that given by the microcontroller when some other
program with high priority is request for acquiring the system buses the n interrupts occur in current
running program.

Interrupts provide a method to postpone or delay the current process, performs a sub-routine task
and then restart the standard program again.

Department of Electronics and Communication Engineering 27


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Types of interrupt in 8051 Microcontroller:

Let's see the five sources of interrupts in 8051 Microcontroller:


 Timer 0 overflow interrupt - TF0
 Timer 1 overflow interrupt - TF1
 External hardware interrupt - INT0
 External hardware interrupt - INT1
 Serial communication interrupt - RI/TI

Memory: For operation Micro-controller required a program. This program guides the
microcontroller to perform the specific tasks. This program installed in microcontroller required
some on chip memory for the storage of the program.

Microcontroller also required memory for storage of data and operands for the short duration. In
microcontroller 8051 there is code or program memory of 4 KB that is it has 4 KB ROM and it also
comprise of data memory (RAM) of 128 bytes.

Bus : Bus is a group of wires which uses as a communication canal or acts as means of data
transfer. The different bus configuration includes 8, 16 or more cables. Therefore, a bus can bear 8
bits, 16 bits all together.

Types of buses in 8051 Microcontroller:

Let's see the two types of bus used in 8051 microcontroller:

Address Bus: 8051 microcontrollers is consisting of 16 bit address bus. It is generally be


used for transferring the data from Central Processing Unit to Memory.
Data bus: 8051 microcontroller is consisting of 8 bits data bus. It is generally be used for
transferring the data from one peripherals position to other peripherals.

Oscillator: As the microcontroller is digital circuit therefore it needs timer for their operation. To
perform timer operation inside microcontroller it required externally connected or on-chip
oscillator. Microcontroller is used inside an embedded system for managing the function of devices.
Therefore, 8051 uses the two 16 bit counters and timers. For the operation of this timers and
counters the oscillator is used inside microcontroller.

Department of Electronics and Communication Engineering 28


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

8051 PIN DIAGRAM:

A Brief Look at 8051 Microcontroller Instructions and Groups


Before going into the details of the 8051 Microcontroller Instruction Set, Types of Instructions and
the Addressing Mode, let us take a brief look at the instructions and the instruction groups of the
8051 Microcontroller Instruction Set (the MCS-51 Instruction Set).

The following table shows the 8051 Instruction Groups and Instructions in each group.

There are 49 Instruction Mnemonics in the 8051 Microcontroller Instruction Set and these 49
Mnemonics are divided into five groups.

Department of Electronics and Communication Engineering 29


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

8051 INSTRUCTION SET


DATA TRANSFER ARITHMETIC LOGICAL BOOLEAN PROGRAM BRANCHING
MOV ADD ANL CLR LJMP
MOVC ADDC ORL SETB AJMP
MOVX SUBB XRL MOV SJMP
PUSH INC CLR JC JZ
POP DEC CPL JNC JNZ
XCH MUL RL JB CJNE
XCHD DIV RLC JNB DJNZ
DA A RR JBC NOP
RRC ANL LCALL
SWAP ORL ACALL
CPL RET
RETI
JMP

Department of Electronics and Communication Engineering 30


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 2 Exp I. Assembly Language Programs to Perform Arithmetic (Both Signed and
Unsigned) 16 Bit Data Operations, Logical Operations (Byte and Bit Level
Operations), Rotate, Shift, Swap and Branch Instructions

PROGRAM 1 (A): PROGRAMS FOR ARITHMETIC OPERATIONS

MOV R0,#50
MOV A,#23H
MOV B,#12H
ADD A,B

MOV @R0,A
MOV A,#25H
MOV B,#16H
SUBB A,B
INC R0
MOV @R0,A

MOV A,#06H
MOV B,#05H
MUL AB
INC R0
MOV @R0,A

MOV A,#06
MOV B,#05
DIV AB
INC R0
MOV @R0,A

INC R0
MOV @R0,B
LCALL 0003
END

Department of Electronics and Communication Engineering 31


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 1 (B): PROGRAMS FOR LOGICAL AND BIT MANIPULATION OPERATIONS


1. AND LOGIC OPERATION
MOV A,#32H
MOV B,#23H
ANL A,B
X: SJMP X
END

2. OR LOGIC OPERATION
MOV A,#32H
MOV B,#23H
ORL A,B
X: SJMP X
END

3. EXCULSIVE OR LOGIC OPERATION


MOV A,#32H
MOV B,#23H
XRL A,B
X: SJMP X
END

4. COMPLIMENT OPERATION
MOV A,#32H
CPL A
X: SJMP X
END

5. SWAPING OPERATION
MOV A,#32H
SWAP A
X: SJMP X
END

6. REGISTER/PORT CLERING OPERATION


MOV A,#32H
CLR A
X: SJMP X
END

7. DATA EXCHANGE OPERATION


MOV A,#32H
MOV B,#23H
XCH A,B
X: SJMP X
END

Department of Electronics and Communication Engineering 32


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

8. BIT SET/RESET OPERATION


MOV P1, 0F0H
SETB P1.2
CLR P1.7
X: SJMP X
END

Department of Electronics and Communication Engineering 33


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 2 Exp II. Time delay Generation Using Timers of 8051.

PROGRAM 2: PROGRAM AND VERIFY TIMER/COUNTER IN 8051


ORG 0000H
RPT:
MOV TMOD,#15H
SETB P3.4
MOV TL0,#00H
MOV TH0,#00H
SETB TR0
MOV R0,#70
AGAIN:
MOV TL1,#00
MOV TH1,#00
SETB TR1
BACK:
JNB TF1,BACK
CLR TF1
CLR TR1
DJNZ R0,AGAIN
CPL P3.0
MOV A,TL0
MOV P2,A
SJMP RPT
END

Department of Electronics and Communication Engineering 34


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 2 Exp III. Serial Communication from / to 8051 to / from I/O devices.

PROGRAM 3: SERIAL COMMUNICATION - UART OPERATION IN 8051


MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
AGAIN:
MOV SBUF, #'M'
HERE:
JNB TI,HERE
CLR TI
MOV SBUF, #'P'
HERE1:
JNB TI,HERE1
CLR TI
MOV SBUF, #'M'
HERE2:
JNB TI,HERE2
CLR TI
MOV SBUF, #'C'
HERE3:
JNB TI,HERE3
CLR TI
X:SJMP X
END

Department of Electronics and Communication Engineering 35


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 2 Exp IV. Program Using Interrupts to Generate Square Wave 10 KHZ Frequency on
P2.1 Using Timer 0 8051 in 8 bit Auto reload Mode and Connect a 1 HZ
Pulse to INT1 pin and Display on Port 0. Assume Crystal Frequency as
11.0592 MHZ

Timer Mode2
The timer in Mode-2 can be used as an 8-bit timer to count from 00 to 0FFH. The timer value for
the required delay needs to be loaded into Timer Count Registers TH (which is copied to TL). After
loading the values to the register, the timers must be started. Now the Timer starts incrementing TL
and once it reaches the max value (0xff), it rolls back to zero setting the overflow flag and reloads
the value from TH.

Timer Calculation
8051 Oscillator frequency is divided by 12 and then fed to the controller, Time to increment the
Timer count by one(timer tick) can be determined as below.
tick = (1/(Fosc/12)
$$tick = 12/Fosc$$ For Fosc == 11.0592Mhz, the tick time will be
tick = 12/11.0592M = 1.085069444µs = 1.085µs
Now the Timer value for the required delay can be calculated as below.
Delay = TimerCount * tick
Count = (Delay/tick)
RegValue = TimerMax- Count
RegValue = TimerMax-(Delay/tick) = TimerMax - (Delay/1.085 µs)
$$RegValue = TimerMax-((Delay/1.085) * 10^6)$$

Required frequency  f =10KHz


Time Period  T= 1/f  1/10kHz = 0.1ms = 100µs

For Square wave of 50% duty cycle

Department of Electronics and Communication Engineering 36


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

ON time = OFF time = T/2 = 50 µs

Timer Calculation for 50µs delay


Fosc = 11.0592Mhz
Delay = 50µs
RegValue = TimerMax-((Delay/1.085) * 10^6)

RegValue = 256 - (50µs/1.085)*10^6 = 256 – 46.08 ~ 210 = 0xD2

PROGRAM 4: Program to Generate Square Wave 10 KHZ Frequency on P2.1 Using Timer 0
8051 in 8 bit Auto reload Mode.
ORG 0000H
LJMP MAIN

ORG 000BH
CPL P2.1
CLR TR0
RETI

ORG 0100H
MAIN:
MOV IE,#83H
MOV IE, #02H
MOV TL0, #00H
AGAIN:
MOV TH0, #0D2H
SEIB TR0
SETB P3.3
MOV P0,P3
SJMP AGAIN
END

Department of Electronics and Communication Engineering 37


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 3: Interfacing I/O Devices to 8051(5 Weeks)


Cycle 3 Exp I. 7 Segment Display to 8051
7- Segment Display

Hex codes for Characters 0 to 9

Department of Electronics and Communication Engineering 38


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Connections

PROGRAM 1: Program for displaying digits 0 to 9 on a 7-segment display connected at PORT 2


ORG 4000H
DB 3FH, 06H, 5BH, 4FH, 66H, 6DH, 7DH, 07H, 7FH, 6FH ;Lookup table for digits 0 to 9
ORG 0000H
MAIN: MOV DPTR, #4000H
REPEAT: CLR A
MOVC A, @A+DPTR ; Copy data from external location to accumulator
MOV P2, A ; Move the pattern of the digit into port P2
ACALL DELAY ; Call a delay to so that the transition is visible
INC DPTR ; Point to the next pattern
CJNE A, 0, REPEAT ; Repeat till 0 (Stop bit) is received
SJMP MAIN ; Run this forever till externally stopped

; generate a decent enough delay between transitions


DELAY:
MOV R0, #08H

Department of Electronics and Communication Engineering 39


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

LP2: MOV R1, #0FFH


LP1: MOV R2, #0FFH
LP3: DJNZ R2, LP3
DJNZ R1, LP1
DJNZ R0, LP2
RET
END

Department of Electronics and Communication Engineering 40


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 3 Exp II. Matrix Keypad to 8051


Connections

Department of Electronics and Communication Engineering 41


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

PROGRAM 2: Program for detecting a key press on a Matrix Keyboard and send ASCII character
for the key pressed to PORT 0

MOV P2, #0FFH ;P1.0-P1.3 connected to rows


K1: ;P2.0-P2.3 connected to columns
MOV P1, #0
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, K1

K2:
ACALL DELAY
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, OVER
SJMP K2

OVER:
ACALL DELAY
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, OVER1
SJMP K2

OVER1:
MOV P1, #11111110B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_0

MOV P1, #11111101B


MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_1

MOV P1, #11111011B


MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_2

MOV P1, #11110111B


MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_3
LJMP K2

Department of Electronics and Communication Engineering 42


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

ROW_0:
MOV DPTR, #KCODE0
SJMP FIND
ROW_1:
MOV DPTR, #KCODE1
SJMP FIND
ROW_2:
MOV DPTR, #KCODE2
SJMP FIND
ROW_3:
MOV DPTR, #KCODE3

FIND:
RRC A
JNC MATCH
INC DPTR
SJMP FIND

MATCH:
CLR A
MOVC A,@A+DPTR
MOV P0,A
LJMP K1

; ASCII look-up table for each row


KCODE0: DB „0‟, „1‟, „2‟, „3‟ ;ROW 0
KCODE1: DB „4‟, „5‟, „6‟, „7‟ ;ROW 1
KCODE2: DB „8‟, „9‟, „A‟, „B‟ ;ROW 2
KCODE3: DB „C‟, „D‟, „E‟, „F‟ ;ROW 3
END

;// Delay Subroutine


DELAY:
MOV R3,#50
HERE2: MOV R4,#255
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
RET
END

Department of Electronics and Communication Engineering 43


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 3 Exp III. Sequence Generator Using Serial Interface in 8051

PROGRAM 3: Program for generating a sequence of numbers 0 to 9 using serial interface.


MOV TMOD, #20H
MOV TH1, #-3
MOV SCON, #50H
SETB TR1
REPEAT:
MOV A, #30H
MOV B, #3AH
AGAIN:
MOV SBUF, A
HERE:
JNB TI,HERE
CLR TI
INC A
CJNE A,B, AGAIN
MOV SBUF, #0AH
HERE1:
JNB TI, HERE1
CLR TI
SJMP REPEAT
END

Department of Electronics and Communication Engineering 44


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 3 Exp IV. 8-bit ADC Interface to 8051


Connections

PROGRAM 4: Program for converting Analog value to Digital value by an ADC interface to 8051
;// A to D Converter interface to 8051
;// Initializing
ORG 0000H ; Starting address
MOV P1,#0FFH ; Makes port 1 input port
SETB P2.7 ; Makes EOC pin high
CLR P2.4 ; Clears ALE pin
CLR P2.6 ; Clears Start pin
CLR P2.5 ; Clears OE pin

;// Selecting Analog Input channel through Address lines A, B and C


BACK: CLR P2.2 ; Clears ADD C
SETB P2.1 ; Sets ADD B
CLR P2.0 ; Clears ADD A
(this selects the second address line)
;// Converting Analog to Digital
ACALL DELAY
SETB P2.4 ; Sets ALE high
ACALL DELAY
SETB P2.6 ; sends a command to start conversion
ACALL DELAY

Department of Electronics and Communication Engineering 45


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

CLR P2.4 ; makes ALE low


CLR P2.6 ; makes Start pin low
HERE:
JB P2.7,HERE ; waits for low pulse at EOC
HERE1:
JNB P2.7,HERE1 ; waits for low pulse to finish

;// Reading the converted Digital Data


SETB P2.5 ; enables OE pin to extract data from ADC
ACALL DELAY
MOV A,P1 ; moves acquired data to accumulator
CLR P2.5 ; clears OE
SJMP BACK ; repeatedly gets data from ADC

;// Delay Subroutine


DELAY:
MOV R3,#50
HERE2: MOV R4,#255
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
RET
END

Department of Electronics and Communication Engineering 46


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Cycle 3 Exp V. Triangular Wave Generator through DAC interfaces to 8051

Connections

PROGRAM 5: Program for generating a triangular wave form by interfacing a DAC to 8051

;// D to A Converter interface to 8051 to Generate Triangular wave


;// Initializing

ORG 0000H ; Starting address


MOV P1,#00H

REPEAT:
ACALL TRIWAVE ; Generate triangular wave
SJMP REPEAT

TRIWAVE:
MOV A,#00H

RISING:MOV P1,A
INC A
CJNE A,#0FFH,RISING
FALLING:MOV P1,A
DEC A
CJNE A,#00H,FALLING
RET
END

Department of Electronics and Communication Engineering 47


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Additional Exp I. FINDING EVEN AND ODD NUMBERS IN A GIVEN ARRAY

OBJECTIVE: To write an assembly language program to find the even and odd numbers in an
given array.

TOOLS REQUIRED: PC With installed Windows 7, DOSBOX and MASM.

PROGRAM:
ASSUME CS: CODE, DS: DATA
DATA SEGMENT
N1 DB 56H, 49H, 33H
EVN DW 1 DUP (00)
ODD DW 1 DUP (00)
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DX, 0000H
MOV BX, 0000H
MOV CX, 0003H
BACK: MOV AL, [SI]
ROR AL, 01H
JC X
INC BX
JMP Y
X: INC DX
Y: INC SI
DEC CX
JNZ BACK
MOV EVN,BX
MOV ODD,DX
INT 3H
CODE ENDS
END START

Department of Electronics and Communication Engineering 48


MICROPROCESSORS AND MICROCONTROLLERS LABORATORY

Additional Exp II. FINDING THE LARGSET AND SMALLEST NUMBERS IN AN GIVEN
ARRAY

OBJECTIVE: To write an assembly language program to find the Largest and Smallest numbers
in an given array..
TOOLS REQUIRED: PC With installed Windows 7, DOSBOX and MASM.

PROGRAM:

ASSUME CS:CODE, DS:DATA


DATA SEGMENT
N1 DB 20H, 33H,08H,19H,77h
LARGEST DB 1 DUP(00)
SMALLEST DB 1 DUP(00)
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DX, 0000H
MOV BX, 0000H
MOV CX, 0004H
MOV AL, [SI]
BACK: CMP AL,[SI+1]
JG X
MOV AL,[SI+1]
X: INC SI
LOOP BACK
MOV LARGEST,AL
MOV CX,04H
MOV SI,OFFSET N1
MOV AL, [SI]
BACK1: CMP AL,[SI+1]
JL Y
MOV AL,[SI+1]
Y: INC SI
LOOP BACK1
MOV SMALLEST,AL
INT 3H
CODE ENDS
END START

Department of Electronics and Communication Engineering 49

You might also like