MPMC-Lab-Manual VKTiwari 18 08 2021
MPMC-Lab-Manual VKTiwari 18 08 2021
MPMC-Lab-Manual VKTiwari 18 08 2021
LABORATORY MANUAL
LABORATORY
For
B.TECH ECE & EIE III YEAR I SEMESTER
And
B.TECH EEE III YEAR II SEMESTER
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.
SYLLABUS
LTPC 0 0 3 1.5
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.
INDEX
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.
8086 ARCHITECTURE:
PIN DIAGRAM:
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)
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)
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.
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:
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]
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;
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).
ASSUME CS:CODE
CODE SEGMENT
Start:
MOV AX, 1235
ADD AX, 4683 ;SUB for Subtraction
INT 03H
CODE ENDS
END Start
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
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
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
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
PROGRAM 2 (B): Program to find number of positives numbers and negative numbers in a given
array of numbers
INTRODUCTION TO IDE
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.
3) Create a new folder and Name it OurFirstProject. Type the name ourFirstProject for the project
and click Save.
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.)
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.
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.
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.
8051 ARCHITECTURE:
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.
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.
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.
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.
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
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
2. OR LOGIC OPERATION
MOV A,#32H
MOV B,#23H
ORL 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
Cycle 2 Exp III. Serial Communication from / to 8051 to / from I/O devices.
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)$$
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
Connections
PROGRAM 2: Program for detecting a key press on a Matrix Keyboard and send ASCII character
for the key pressed to PORT 0
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
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
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
Connections
PROGRAM 5: Program for generating a triangular wave form by interfacing a DAC to 8051
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
OBJECTIVE: To write an assembly language program to find the even and odd numbers in an
given array.
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
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: