Sie sind auf Seite 1von 23

NUST SchooI of EIectricaI Engineering &

Computer Science

Department of EIectricaI Engineering











Lab Manual
Microprocessor Based Systems


Prepared by:
Asst Prof Kamran Zaidi









National University of Sciences & Technology (NUST),
Pakistan



'Microprocessor Based Systems Lab Manual 1

LAB No.1

Objective: The aim oI the Iirst lab is Iamiliarization with MASM and writing and testing
our Iirst program in Assembly Language.

INTRODUCTION TO MASM

The Microsoft Macro Assembler (MASM) is an x86 assembler Ior MicrosoIt Windows
that uses the Intel syntax. Assembly language is a great tool to understand how a
computer works and with the help oI MASM you will be able to assemble and run your
programs written in Assembly language.


Writing Assembly Language Programs:

You can write Assembly language programs in any text editor e.g. Notepad etc.
However, you have to make sure that you save your programs with an extension oI asm
i.e. iI you name your Iile example then it should be saved by going to saveAs and then
typing example.asm in the Iile name and selecting All Files in the Iile Type.
Once you have installed MASM on your PC and written your program then you have to
assemble and link your programs beIore they can be executed.

Assemble - Link - Execute Cycle:

The process oI editing, assembling, linking, and executing assembly language programs
is summarized in Figure below. Following is a detailed description oI each step.
Step 1: A programmer uses a text editor to create an ASCII text Iile named the source
file.
Step 2: The assembler (Iile ML.exe) reads the source Iile and produces an obfect file, a
machine-language translation oI the program. Optionally, it produces a listing file. II any
errors occur, the programmer must return to Step 1 and Iix the program.
Step 3: The linker (Iile Link32.exe) reads the object Iile and checks to see iI the program
contains any calls to procedures in a link library. The linker copies any required
procedures Irom the link library, combines them with the object Iile, and produces the
executable file. Optionally, the linker can produce a map file.
Step 4: The operating system loader utility reads the executable Iile into memory and
branches the CPU to the program`s starting address, and the program begins to execute.


'Microprocessor Based Systems Lab Manual 2




At the end the useIul Iiles generated are as Iollows:
Example.obj
Example.lst
Example.exe

Example.exe is the executable Iile that can now be run by typing example on the DOS
prompt and pressing enter.

First Assembly Language Program:

TITLE Add two registers (example.asm)
; The comments are given aIter the semi colon on a line
; This program adds 32-bit unsigned
; integers and stores the sum in the ecx register
Include irvine32.inc
.data
;variable declarations go here

.code
Main Proc
;instructions go here
Mov eax, 30 ;Assembly Language is NOT case sensitive
Mov ebx, 20
Add ecx, eax
Add ecx, ebx
Call dumpregs ;displays the result on the screen by displaying all register values

Exit
Main endp
End main

CPU REGISTERS:
Registers are special memory locations on the CPU. One important diIIerence between
older and later processors is that the pre-386 processors are 16-bit instead oI 32-bit.
There are 8 32-bit general purpose registers. The Iirst 4, eax, ebx, ecx, and edx can also
'Microprocessor Based Systems Lab Manual 3

be accessed using 16 or 8-bit names. ax gets the Iirst 16 bits oI eax, al gets the Iirst 8
bits, and ah gets bits 9-16. The Iig below shows all the general purpose and special
purpose registers and their sizes.
























'Microprocessor Based Systems Lab Manual 4

Lab No.2

Objective: The aim oI this lab is to declare and manipulate variables in our assembly
language program and check the output.


Program
TITLE Add and Subtract, (AddSub2.asm)
; This program adds and subtracts 32-bit unsigned
; integers and stores the sum in a variable.
INCLUDE Irvine32.inc
.data
val1 DWORD 10000h ;val1 declared as a variable oI type DWORD and initialized
val2 DWORD 40000h
val3 DWORD 20000h
IinalVal DWORD ?

.code
main PROC
mov eax,val1 ; start with 10000h
add eax,val2 ; add 40000h
sub eax,val3 ; subtract 20000h
mov IinalVal,eax ; store the result (30000h)
call DumpRegs ; display the registers
exit
main ENDP
END main



















'Microprocessor Based Systems Lab Manual 5


Lab No.3

Objective: The aim oI this lab is to test and observe the result oI diIIerent addressing
mode instructions.

Use the call dumpregs Iunction to check the values oI the registers aIter each instruction
given below. Also, identiIy the addressing mode being used.

.data
Val1 WORD 100H
Val2 WORD 200H
arrayB BYTE 10H, 20H, 30H, 40H
arrayW WORD 100h, 200h, 300h, 400h
arrayD DWORD 10000H, 20000H

.code
Main Proc
Mov ax, val1
call dumpregs

Mov bx, val2

Mov cl, arrayB

Mov cl, |arrayB1|

Mov cl, |arrayB2|

Mov ax, arrayW

Mov ax, |arrayW2|

Mov bx, |arrayW4|

Mov ecx, arrayD

Mov ecx, |arrayD4|

Exit
Main endp
End main




'Microprocessor Based Systems Lab Manual 6


Lab No. 4

Objective: The aim oI this lab is to test and observe the result oI diIIerent addressing
mode instructions.

Use the Iollowing instructions in your program and check the register values aIter each
set oI instructions by using the call dumpregs Iunction. Also, identiIy the type oI
addressing mode used.

.data
Array1 BYTE 10H, 20H, 30H, 40H, 50H
Array2 WORD 100h, 200h, 300h, 400h, 500h, 600h
Array3 DWORD 10000H, 20000H, 30000H

.code
Main Proc
Mov esi, OFFSET Array1
Mov edi, OFFSET Array2
Mov edx, OFFSET Array3

Mov al, |esi|
call dumpregs

add esi, 1
Mov al, |esi|

Mov bx, |esi|

Mov cx, |edi|

Add edi, 4
Mov cx, |edi|

Mov eax, |edx|


Exit
Main endp
End main






'Microprocessor Based Systems Lab Manual 7



Lab No. 5

Objective: The aim oI this lab is to check the eIIect oI arithmetic instruction on the Ilags
oI the CPU.

Use the Iollowing instructions in your program and check the register values aIter each
set oI instructions by using the call dumpregs Iunction.

Mov cx, 1
Sub cx, 1

Mov ax, 0FFFFH
Inc ax
Inc ax

Mov ax, 0FFFFH
Add ax, 1

Mov al, 1
Sub al, 2

Mov al, 127
Add al, 1

Mov al, -128
Sub al, 1


















'Microprocessor Based Systems Lab Manual 8


Lab No. 6

Objective: The aim oI this lab is to test the student`s ability to write an Assembly
language program independently.

Program:
Write a program that asks the user to input 10 integers and stores it in an array oI type
WORD. It then prints all the integers on the screen in diIIerent colors.





































'Microprocessor Based Systems Lab Manual 9


Lab No. 7

Objective: The aim oI this lab is to test the student`s ability to write an Assembly
language program independently.

Program:
Write a program that asks the user to input his Iirst and last name and stores it an array oI
type String. It then counts the number oI characters in the name and displays the result on
the screen.




































'Microprocessor Based Systems Lab Manual 10


Lab No. 8

INTRODUCTION TO MICROCONTROLLERS

Objective: The aim oI this lab is to introduce the student to 89C51 microcontroller. Its
architecture and its programming.

What is a Microcontroller?
A MicroController Unit (or MCU) is a computer-on-a-chip. It is a type oI
microprocessor emphasizing selI-suIIiciency and cost-eIIectiveness, in contrast to a
general-purpose microprocessor (the kind used in a PC). The microcontroller is an IC that
has its own CPU, RAM and non-volatile memory (ROM, EPROM, FLASH EPROM) on
the same chip. Integrating the memory and other peripherals on a single chip and testing
them as a unit increases the cost oI that chip, but oIten results in decreased net cost oI the
embedded system as a whole. As a result oI this integration, the MCU can perIorm stand-
alone Iunctions in a variety oI embedded systems applications.


Applications:
The microcontroller, nowadays, is an indispensable device Ior electrical/electronic
engineers because oI its versatility and its enormous application. Among the applications
oI a microcontroller we can mention industrial automation, mobile telephones, radios,
microwave ovens and VCRs. Besides, the present trend in digital electronics is toward
restricting to microcontrollers and chips that concentrate a great quantity oI logical
circuits, like PLDs (Programmable Logic Devices) and GALs (Gate Array Logic). In
dedicated systems, the microcontroller is the best solution, because it is cheap and easy to
manage.

The 8051 Microcontroller:
The Intel 8051 is a Harvard architecture single chip microcontroller (C) which was
developed by Intel in 1980 Ior use in embedded systems. It was extremely popular in the
1980s and early 1990s, but today it has largely been superseded by a vast range oI
enhanced devices with 8051-compatible processor cores that are manuIactured by more
'Microprocessor Based Systems Lab Manual 11

than 20 independent manuIacturers including Atmel, InIineon Technologies, Maxim IC
(via its Dallas Semiconductor subsidiary), NXP (Iormerly Philips Semiconductor),
Winbond, Silicon Laboratories, Texas Instruments and Cypress Semiconductor. Intel's
oIIicial designation Ior the 8051 Iamily oI Cs is MCS 51.
Intel's original 8051 Iamily was developed using NMOS technology, but later versions,
identiIied by a letter "C" in their name, e.g. 80C51, used CMOS technology and were less
power-hungry than their NMOS predecessors - this made them eminently more suitable
Ior battery-powered devices.
The 8051 is, without doubt, the most popular microcontroller nowadays. The device itselI
is a relatively simple 8-bit microcontroller, but it has wide application. Moreover, the
most important is that there isn't only the IC 8051, but a complete Iamily oI
microcontrollers based upon it. A Iamily oI microcontrollers is a group oI devices that
shares the same basic elements and have the same basic group oI instructions. The 8051's
Iamily is composed oI more than 300 diIIerent ICs. Some oI the popular ones are
O 89C51
O 89C52
O 89C2051
O 89C552

8051 ARCHITECTURE
The AT89C51 (AT ATMEL) is a low-power, high-perIormance CMOS 8-bit
microcomputer with 4K bytes oI Flash Programmable and Erasable Read Only Memory
(PEROM). The device is manuIactured using Atmel`s high density nonvolatile memory
technology and is compatible with the industry standard MCS-51 instruction set and
pinout. The on-chip Flash allows the program memory to be reprogrammed in-system or
by a conventional nonvolatile memory programmer. By combining a versatile 8-bit CPU
with Flash on a monolithic chip, the Atmel AT89C51 is a powerIul microcomputer which
provides a highly Ilexible and cost eIIective solution to many embedded control
applications. Its Ieatures are as Iollows:-
4K Bytes oI In-System Reprogrammable Flash Memory
Endurance: 1,000 Write/Erase Cycles
Fully Static Operation: 0 Hz to 24 MHz
'Microprocessor Based Systems Lab Manual 12

Three-Level Program Memory Lock
128 x 8-Bit Internal RAM
32 Programmable I/O Lines
Two 16-Bit Timer/Counters
Six Interrupt Sources
Programmable Serial Channel
Low Power Idle and Power Down Modes

PIN CONFIGURATION:













PIN DESCRIPTION:
VCC
Supply voltage.
GND
Ground.

Port 0
Port 0 is an 8-bit open drain bidirectional I/O port. As an output port each pin can sink
eight TTL inputs. When 1s are written to port 0 pins, the pins can be used as
highimpedance inputs. Port 0 may also be conIigured to be the multiplexed loworder
address/data bus during accesses to external program and data memory. In this mode P0
'Microprocessor Based Systems Lab Manual 13

has internal pullups. Port 0 also receives the code bytes during Flash programming, and
outputs the code bytes during program veriIication.
External pullups are required during program veriIication.

Port 1
Port 1 is an 8-bit bidirectional I/O port with internal pullups. The Port 1 output buIIers
can sink/source Iour TTL inputs. When 1s are written to Port 1 pins they are pulled high
by the internal pullups and can be used as inputs. As inputs, Port 1 pins that are externally
being pulled low will source current (IIL) because oI the internal pullups. Port 1 also
receives the low-order address bytes during Flash programming and veriIication.

Port 2
Port 2 is an 8-bit bidirectional I/O port with internal pullups. The Port 2 output buIIers
can sink/source Iour TTL inputs. When 1s are written to Port 2 pins they are pulled high
by the internal pullups and can be used as inputs. As inputs, Port 2 pins that are externally
being pulled low will source current (IIL) because oI the internal pullups. Port 2 emits the
high-order address byte during Ietches Irom external program memory and during
accesses to external data memory that use 16-bit addresses (MOVX DPTR). In this
application it uses strong internal pullups when emitting 1s. During accesses to external
data memory that use 8-bit addresses (MOVX RI), Port 2 emits the contents oI the P2
Special Function Register. Port 2 also receives the high-order address bits and some
control signals during Flash programming and veriIication.

Port 3
Port 3 is an 8-bit bidirectional I/O port with internal pullups. The Port 3 output buIIers
can sink/source Iour TTL inputs. When 1s are written to Port 3 pins they are pulled high
by the internal pullups and can be used as inputs. As inputs, Port 3 pins that are externally
being pulled low will source current (IIL) because oI the pullups.

OSCILLATOR CHARACTERISTICS:
'Microprocessor Based Systems Lab Manual 14

XTAL1 and XTAL2 are the input and output, respectively, oI an inverting ampliIier
which can be conIigured Ior use as an on-chip oscillator, as shown in Figure 1. Either a
quartz crystal or ceramic resonator may be used. To drive the device Irom an external
clock source, XTAL2 should be leIt unconnected while XTAL1 is driven as shown in
Figure 2. There are no requirements on the duty cycle oI the external clock signal, since
the input to the internal clocking circuitry is through a divide-by-two Ilip-Ilop, but
minimum and maximum voltage high and low time speciIications must be observed.
























'Microprocessor Based Systems Lab Manual 15

LAB No.9
Write a Program that turns on an LED (connected to Pin P1.1) on the Trainer based on
whether a switch (connected to Pin P1.2) is ON or OFF.

ASSEMBLY LANGUAGE PROGRAM:
TITLE EX1.A51

ORG 0H ; Starting address oI the ROM
SJMP START ; Jump to the beginning oI the program

ORG 30H ; First address above the Interrupt Vector
START: ; Start Label

CLR P1.1 ; Turn oII the LED to start with
HERE:
JNB P1.2, HERE ; II the Switch is oII then wait here
SETB P1.1 ; Turn on the LED

END ; End oI Program












'Microprocessor Based Systems Lab Manual 16

LAB NO.10
Write a Program that generates a square wave pulse on one oI its pins (P1.1)

ASSEMBLY LANGUAGE PROGRAM:

TITLE EX2.A51

ORG 0H ; Starting address oI the ROM
SJMP START ; Jump to the beginning oI the program

ORG 30H ; First address above the Interrupt Vector
START: ; Start Label
AGAIN:
CLR P1.1 ; Clears the pin P1.1
SETB P1.1 ; Sets pin P1.1
JMP AGAIN ; Unconditional Jump to label 'AGAIN

END ; End oI Program

EXERCISE:
Q1. Can you Iind a single instruction Irom the instruction set that will alone perIorm
the Iunction oI clearing (CLR) and setting (SETB) a pin. ModiIy the code above so that
the waveIorm is generated using this instruction.
Q2. What is the Time Period and Frequency oI the waveIorm generated?
Q3. Change the code above so that the Time Period oI the wave is now doubled
Q4. Check whether the high time and low time oI the pulse are equal? II not why not?





'Microprocessor Based Systems Lab Manual 17

LAB No.11
Write a program that uses the Timer0 oI the 89C51 to generate a pulse oI 10KHz
Irequency on P1.0 using an Oscilloscope.
ASSEMBLY LANGUAGE PROGRAM:

TITLE EX3.A51

ORG 0H ; Starting address oI the ROM
SJMP START ; Jump to the beginning oI the program

ORG 30H ; First address above the Interrupt Vector
START: ; Start Label
MOV TMOD, #02H ; 8-bit auto reload mode
MOV TH0, #-50 ; -50 reload value in TH0
SETB TR0 ; Start the Timer0
LOOP:
JNB TF0, LOOP ; Wait Ior timer to overIlow
CLR TF0 ; Clear the OverIlow Flag
CPL P1.0 ; Toggle port bit
SJMP LOOP ; Repeat the loop

END ; End oI Program









'Microprocessor Based Systems Lab Manual 18

LAB No.12
Write a Program using Timer1 to create a 1 KHz Square wave on pin P2.0. ModiIy this
program so that aIter 1 minute the timer1 is reinitialized and then it starts generating a 2
KHz Square wave on pin P2.0



























'Microprocessor Based Systems Lab Manual 19

LAB No. 13
Objective:

To understand and use the Interrupts oI the 89C51
Lab:
Write and burn the controller with a program that waits Ior a button to be pressed to turn
on an LED connected to its port pin P1.0. The microcontroller detects this button press on
its EXT0 pin. Once the button is pressed and released the LED should remain ON. At this
stage the LED should turn OFF iI the button is pressed again.
































'Microprocessor Based Systems Lab Manual 20

LAB No. 14
Objective:

To understand the 89C51 Interrupts.
Lab:

Write a program that uses two Ports as 7-segment digit Outputs (Port1 as LSD and Port2
as MSD). One Input SHOW at Port 0.0 which is a latched type switch. The other input
DOOR at P3.2 (External Interrupt0) is a miniature switch attached with a door. Each time
the door is opened the switch DOOR is pressed and causes the soItware to execute
Interrupt Service Routine where the number oI Door open is counted. When the input
switch SHOW is pressed, the number oI Door open Count is displayed on the 7-Segment
LED display.

Writing the Program:

1. Declare the inputs and outputs.
2. Initialize the IE (Interrupt Enable Register) according to the Interrupt enabling
order.
IE.7 Global Enable/Disable,
IE.6 UndeIined,
IE.5 Timer2 Interrupt,
IE.4 Serial port Interrupt,
IE.3 Timer1 Interrupt,
IE.2 External Interrupt1,
IE.1 Timer0 Interrupt,
IE.0 External Interrupt 0.
3. Set the IT0 bit (TCON.0) to declare the External Interrupt0 as Falling Edge
triggered. (IT00 Ior low level activated interrupt).
Seven Segment Display:




a
b
c
d
e
f
g
'Microprocessor Based Systems Lab Manual 21

LAB No. 15
Objective:

To understand the Serial Port Transmit operation oI the 89C51.

Lab:
Write a Sub Routine OUTCHAR to transmit the 7-bit ASCII code in the Accumulator out
the 8951 serial port with odd parity added as the 8
th
bit.

ASSEMBLY LANGUAGE PROGRAM (SUB ROUTINE):

OUTCHAR:
MOV C, P ; Put parity bit in Carry Ilag
CPL C ; Change to odd parity
MOV ACC, C ; Add to Character code
AGAIN:
JNB TI, AGAIN ; Check to see iI TI empty, iI not then again
CLR TI ; YES, Clear Ilag
MOV SBUF, ACC ; Send Character
CLR ACC.7 ; Strip oII parity bit
MOV SBUF, A
CLR ACC.7
RET


EXERCISE:
Q. Use the Sub-Routine given above in your program so that all the characters Irom
A to Z are transmitted to the PC by serial Port and printed on the screen. For this you will
have to use Hyper Terminal Utility in Windows.




'Microprocessor Based Systems Lab Manual 22

Das könnte Ihnen auch gefallen