You are on page 1of 23

NUST School of Electrical Engineering & Computer Science

Department of Electrical Engineering

Lab Manual

Microprocessor Based Systems


Prepared by: Asst Prof Kamran Zaidi

National University of Sciences & Technology (NUST), Pakistan

LAB No.1 Objective: The aim of the first lab is familiarization with MASM and writing and testing our first program in Assembly Language. INTRODUCTION TO MASM The Microsoft Macro Assembler (MASM) is an x86 assembler for Microsoft Windows that uses the Intel syntax. Assembly language is a great tool to understand how a computer works and with the help of 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 of asm i.e. if you name your file example then it should be saved by going to saveAs and then typing example.asm in the file name and selecting All Files in the file Type. Once you have installed MASM on your PC and written your program then you have to assemble and link your programs before they can be executed. Assemble Link Execute Cycle: The process of editing, assembling, linking, and executing assembly language programs is summarized in Figure below. Following is a detailed description of each step. Step 1: A programmer uses a text editor to create an ASCII text file named the source file. Step 2: The assembler (file ML.exe) reads the source file and produces an object file, a machine-language translation of the program. Optionally, it produces a listing file. If any errors occur, the programmer must return to Step 1 and fix the program. Step 3: The linker (file Link32.exe) reads the object file and checks to see if the program contains any calls to procedures in a link library. The linker copies any required procedures from the link library, combines them with the object file, and produces the executable file. Optionally, the linker can produce a map file. Step 4: The operating system loader utility reads the executable file into memory and branches the CPU to the programs starting address, and the program begins to execute.

Microprocessor Based Systems Lab Manual

At the end the useful files generated are as follows: Example.obj Example.lst Example.exe Example.exe is the executable file 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 after 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 difference between older and later processors is that the pre-386 processors are 16-bit instead of 32-bit. There are 8 32-bit general purpose registers. The first 4, eax, ebx, ecx, and edx can also Microprocessor Based Systems Lab Manual 2

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

Microprocessor Based Systems Lab Manual

Lab No.2 Objective: The aim of 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 of type DWORD and initialized val2 DWORD 40000h val3 DWORD 20000h finalVal DWORD ? .code main PROC mov eax,val1 add eax,val2 sub eax,val3 mov finalVal,eax call DumpRegs exit main ENDP END main

; start with 10000h ; add 40000h ; subtract 20000h ; store the result (30000h) ; display the registers

Microprocessor Based Systems Lab Manual

Lab No.3 Objective: The aim of this lab is to test and observe the result of different addressing mode instructions. Use the call dumpregs function to check the values of the registers after each instruction given below. Also, identify 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 Mov Mov Mov Mov Mov Mov Mov Mov bx, val2 cl, arrayB cl, [arrayB+1] cl, [arrayB+2] ax, arrayW ax, [arrayW+2] bx, [arrayW+4] ecx, arrayD ecx, [arrayD+4]

Exit Main endp End main

Microprocessor Based Systems Lab Manual

Lab No. 4 Objective: The aim of this lab is to test and observe the result of different addressing mode instructions. Use the following instructions in your program and check the register values after each set of instructions by using the call dumpregs function. Also, identify the type of addressing mode used. .data Array1 Array2 Array3

BYTE WORD DWORD

10H, 20H, 30H, 40H, 50H 100h, 200h, 300h, 400h, 500h, 600h 10000H, 20000H, 30000H

.code Main Proc Mov esi, OFFSET Array1 Mov edi, OFFSET Array2 Mov edx, OFFSET Array3 Mov al, [esi] call dumpregs add Mov Mov Mov Add Mov Mov esi, 1 al, [esi] bx, [esi] cx, [edi] edi, 4 cx, [edi] eax, [edx]

Exit Main endp End main

Microprocessor Based Systems Lab Manual

Lab No. 5 Objective: The aim of this lab is to check the effect of arithmetic instruction on the flags of the CPU. Use the following instructions in your program and check the register values after each set of instructions by using the call dumpregs function. Mov Sub Mov Inc Inc Mov Add Mov Sub Mov Add Mov Sub cx, 1 cx, 1 ax, 0FFFFH ax ax ax, 0FFFFH ax, 1 al, 1 al, 2 al, +127 al, 1 al, -128 al, 1

Microprocessor Based Systems Lab Manual

Lab No. 6 Objective: The aim of this lab is to test the students 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 of type WORD. It then prints all the integers on the screen in different colors.

Microprocessor Based Systems Lab Manual

Lab No. 7 Objective: The aim of this lab is to test the students ability to write an Assembly language program independently. Program: Write a program that asks the user to input his first and last name and stores it an array of type String. It then counts the number of characters in the name and displays the result on the screen.

Microprocessor Based Systems Lab Manual

Lab No. 8 INTRODUCTION TO MICROCONTROLLERS Objective: The aim of 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 of microprocessor emphasizing self-sufficiency and cost-effectiveness, 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 of that chip, but often results in decreased net cost of the embedded system as a whole. As a result of this integration, the MCU can perform standalone functions in a variety of embedded systems applications.

Applications: The microcontroller, nowadays, is an indispensable device for electrical/electronic engineers because of its versatility and its enormous application. Among the applications of 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 of 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 for 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 of enhanced devices with 8051-compatible processor cores that are manufactured by more Microprocessor Based Systems Lab Manual 10

than 20 independent manufacturers including Atmel, Infineon Technologies, Maxim IC (via its Dallas Semiconductor subsidiary), NXP (formerly Philips Semiconductor), Winbond, Silicon Laboratories, Texas Instruments and Cypress Semiconductor. Intel's official designation for the 8051 family of Cs is MCS 51. Intel's original 8051 family was developed using NMOS technology, but later versions, identified 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 for battery-powered devices. The 8051 is, without doubt, the most popular microcontroller nowadays. The device itself 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 family of microcontrollers based upon it. A family of microcontrollers is a group of devices that shares the same basic elements and have the same basic group of instructions. The 8051's family is composed of more than 300 different ICs. Some of the popular ones are

89C51 89C52 89C2051 89C552

8051 ARCHITECTURE The AT89C51 (AT ATMEL) is a low-power, high-performance CMOS 8-bit microcomputer with 4K bytes of Flash Programmable and Erasable Read Only Memory (PEROM). The device is manufactured using Atmels 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 powerful microcomputer which provides a highly flexible and cost effective solution to many embedded control applications. Its features are as follows: 4K Bytes of In-System Reprogrammable Flash Memory Endurance: 1,000 Write/Erase Cycles Fully Static Operation: 0 Hz to 24 MHz Microprocessor Based Systems Lab Manual 11

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 configured 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

12

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

Port 1 Port 1 is an 8-bit bidirectional I/O port with internal pullups. The Port 1 output buffers can sink/source four 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 of the internal pullups. Port 1 also receives the low-order address bytes during Flash programming and verification.

Port 2 Port 2 is an 8-bit bidirectional I/O port with internal pullups. The Port 2 output buffers can sink/source four 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 of the internal pullups. Port 2 emits the high-order address byte during fetches from 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 of the P2 Special Function Register. Port 2 also receives the high-order address bits and some control signals during Flash programming and verification.

Port 3 Port 3 is an 8-bit bidirectional I/O port with internal pullups. The Port 3 output buffers can sink/source four 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 of the pullups.

OSCILLATOR CHARACTERISTICS:

Microprocessor Based Systems Lab Manual

13

XTAL1 and XTAL2 are the input and output, respectively, of an inverting amplifier which can be configured for 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 from an external clock source, XTAL2 should be left unconnected while XTAL1 is driven as shown in Figure 2. There are no requirements on the duty cycle of the external clock signal, since the input to the internal clocking circuitry is through a divide-by-two flip-flop, but minimum and maximum voltage high and low time specifications must be observed.

Microprocessor Based Systems Lab Manual

14

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 SJMP START ORG 40H START: CLR HERE: JNB P1.2, HERE P1.1

; ; ; ; ;

Starting address of the ROM Jump to the beginning of the program First address above the Interrupt Vector Start Label Turn off the LED to start with

; ;

If the Switch is off then wait here Turn on the LED

SETB P1.1 WAIT: JB P1.2, WAIT

SJMP START END ; End of Program

#include <regx52.h> //header file for 89C51 #include <stdio.h> sbit DIPswitch = P1^2; sbit redLED = P1^1; void main(){ //main function starts while(1){ redLED = 0; while (DIPswitch == 0) {} redLED = 1; while (DIPswitch == 1) {} } } // DIP switch input on Port 1 bit 4 // green LED output on Port 1 bit 5

Microprocessor Based Systems Lab Manual

15

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

ASSEMBLY LANGUAGE PROGRAM:

TITLE

EX2.A51

ORG 0H SJMP START

; ;

Starting address of the ROM Jump to the beginning of the program

ORG 30H START: AGAIN: CLR P1.1

; ;

First address above the Interrupt Vector Start Label

; ; ;

Clears the pin P1.1 Sets pin P1.1 Unconditional Jump to label AGAIN

SETB P1.1 JMP AGAIN

END

End of Program

EXERCISE: Q1. Can you find a single instruction from the instruction set that will alone perform

the function of clearing (CLR) and setting (SETB) a pin. Modify the code above so that the waveform is generated using this instruction. Q2. Q3. Q4. What is the Time Period and Frequency of the waveform generated? Change the code above so that the Time Period of the wave is now doubled Check whether the high time and low time of the pulse are equal? If not why not?

Microprocessor Based Systems Lab Manual

16

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

TITLE

EX3.A51

ORG 0H SJMP START

; ;

Starting address of the ROM Jump to the beginning of the program

ORG 30H START:

; ;

First address above the Interrupt Vector Start Label 8-bit auto reload mode -50 reload value in TH0 Start the Timer0

MOV TMOD, #02H ; MOV TH0, #-50 SETB TR0 LOOP: JNB CLR CPL TF0, LOOP TF0 P1.0 ; ; ; ; ; ;

Wait for timer to overflow Clear the Overflow Flag Toggle port bit Repeat the loop

SJMP LOOP

END

End of Program

Microprocessor Based Systems Lab Manual

17

LAB No.12 Write a Program using Timer1 to create a 1 KHz Square wave on pin P2.0. Modify this program so that after 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

18

LAB No. 13 Objective:

To understand and use the Interrupts of the 89C51 Lab: Write and burn the controller with a program that waits for 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 if the button is pressed again.

Microprocessor Based Systems Lab Manual

19

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 software to execute Interrupt Service Routine where the number of Door open is counted. When the input switch SHOW is pressed, the number of Door open Count is displayed on the 7-Segment LED display. Writing the Program: 1. 2. Declare the inputs and outputs. Initialize the IE (Interrupt Enable Register) according to the Interrupt enabling order. IE.7= Global Enable/Disable, IE.6= Undefined, 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. (IT0=0 for low level activated interrupt). Seven Segment Display: a b g

Microprocessor Based Systems Lab Manual

20

LAB No. 15 Objective: To understand the Serial Port Transmit operation of 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 8th bit.

ASSEMBLY LANGUAGE PROGRAM (SUB ROUTINE):

OUTCHAR: MOV CPL MOV AGAIN: JNB CLR MOV CLR MOV CLR RET TI, AGAIN TI ; ; Check to see if TI empty, if not then again YES, Clear flag Send Character Strip off parity bit C, P C ACC, C ; ; ; Put parity bit in Carry flag Change to odd parity Add to Character code

SBUF, ACC ; ACC.7 SBUF, A ACC.7 ;

EXERCISE: Q. Use the Sub-Routine given above in your program so that all the characters from

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

21

Microprocessor Based Systems Lab Manual

22

You might also like