microprocessor sys lab manual_revised22nov11

Upload: waleedaslam

Post on 06-Apr-2018

242 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    1/23

    NUST School of Electrical Engineering &

    Computer ScienceDepartment of Electrical Engineering

    Lab ManualMicroprocessor Based Systems

    Prepared by:

    Asst Prof Kamran Zaidi

    National University of Sciences & Technology (NUST),

    Pakistan

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    2/23

    Microprocessor Based Systems Lab Manual 1

    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 asmi.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, amachine-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 programcontains 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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    3/23

    Microprocessor Based Systems Lab Manual 2

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

    .codeMain Proc

    ;instructions go here

    Mov eax, 30 ;Assembly Language is NOT case sensitive

    Mov ebx, 20Add 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

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    4/23

    Microprocessor Based Systems Lab Manual 3

    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 specialpurpose registers and their sizes.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    5/23

    Microprocessor Based Systems Lab Manual 4

    Lab No.2

    Objective: The aim of this lab is to declare and manipulate variables in our assembly

    language program and check the output.

    ProgramTITLE Add and Subtract, (AddSub2.asm)

    ; This program adds and subtracts 32-bit unsigned

    ; integers and stores the sum in a variable.

    INCLUDE Irvine32.inc

    .dataval1 DWORD 10000h ;val1 declared as a variable of type DWORD and initialized

    val2 DWORD 40000h

    val3 DWORD 20000hfinalVal DWORD ?

    .code

    main PROC

    mov eax,val1 ; start with 10000h

    add eax,val2 ; add 40000hsub eax,val3 ; subtract 20000h

    mov finalVal,eax ; store the result (30000h)

    call DumpRegs ; display the registers

    exit

    main ENDPEND main

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    6/23

    Microprocessor Based Systems Lab Manual 5

    Lab No.3

    Objective: The aim of this lab is to test and observe the result of different addressingmode 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 200HarrayB BYTE 10H, 20H, 30H, 40H

    arrayW WORD 100h, 200h, 300h, 400h

    arrayD DWORD 10000H, 20000H

    .codeMain Proc

    Mov ax, val1

    call dumpregs

    Mov bx, val2

    Mov cl, arrayB

    Mov cl, [arrayB+1]

    Mov cl, [arrayB+2]

    Mov ax, arrayW

    Mov ax, [arrayW+2]

    Mov bx, [arrayW+4]

    Mov ecx, arrayD

    Mov ecx, [arrayD+4]

    Exit

    Main endp

    End main

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    7/23

    Microprocessor Based Systems Lab Manual 6

    Lab No. 4

    Objective: The aim of this lab is to test and observe the result of different addressingmode 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 BYTE 10H, 20H, 30H, 40H, 50HArray2 WORD 100h, 200h, 300h, 400h, 500h, 600h

    Array3 DWORD 10000H, 20000H, 30000H

    .code

    Main ProcMov 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, 4Mov cx, [edi]

    Mov eax, [edx]

    Exit

    Main endp

    End main

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    8/23

    Microprocessor Based Systems Lab Manual 7

    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 cx, 1

    Sub cx, 1

    Mov ax, 0FFFFH

    Inc axInc ax

    Mov ax, 0FFFFH

    Add ax, 1

    Mov al, 1Sub al, 2

    Mov al, +127

    Add al, 1

    Mov al, -128

    Sub al, 1

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    9/23

    Microprocessor Based Systems Lab Manual 8

    Lab No. 6

    Objective: The aim of this lab is to test the students ability to write an Assemblylanguage 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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    10/23

    Microprocessor Based Systems Lab Manual 9

    Lab No. 7

    Objective: The aim of this lab is to test the students ability to write an Assemblylanguage 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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    11/23

    Microprocessor Based Systems Lab Manual 10

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

    alone 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

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    12/23

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    13/23

    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 configured to be the multiplexed loworder

    address/data bus during accesses to external program and data memory. In this mode P0

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    14/23

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

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    15/23

    Microprocessor Based Systems Lab Manual 14

    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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    16/23

    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 of the ROM

    SJMP START ; Jump to the beginning of the program

    ORG 40H ; First address above the Interrupt VectorSTART: ; Start Label

    CLR P1.1 ; Turn off the LED to start with

    HERE:

    JNB P1.2, HERE ; If the Switch is off then wait here

    SETB P1.1 ; Turn on the LED

    WAIT:

    JB P1.2, WAIT

    SJMP START

    END ; End of Program

    #include //header file for 89C51

    #include

    sbit DIPswitch = P1^2; // DIP switch input on Port 1 bit 4

    sbit redLED = P1^1; // green LED output on Port 1 bit 5

    void main(){

    //main function starts

    while(1){

    redLED = 0;

    while (DIPswitch == 0)

    { }

    redLED = 1;

    while (DIPswitch == 1)

    { }

    }

    }

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    17/23

    Microprocessor Based Systems Lab Manual 16

    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 ; Starting address of the ROM

    SJMP START ; Jump to the beginning of 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 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. What is the Time Period and Frequency of the waveform generated?

    Q3. Change the code above so that the Time Period of the wave is now doubled

    Q4. Check whether the high time and low time of the pulse are equal? If not why not?

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    18/23

    Microprocessor Based Systems Lab Manual 17

    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 ; Starting address of the ROM

    SJMP START ; Jump to the beginning of 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 for timer to overflow

    CLR TF0 ; Clear the Overflow Flag

    CPL P1.0 ; Toggle port bit

    SJMP LOOP ; Repeat the loop

    END ; End of Program

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    19/23

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    20/23

    Microprocessor Based Systems Lab Manual 19

    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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    21/23

    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 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-SegmentLED 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= 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

    c

    d

    e

    f

    g

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    22/23

    Microprocessor Based Systems Lab Manual 21

    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 C, P ; Put parity bit in Carry flag

    CPL C ; Change to odd parity

    MOV ACC, C ; Add to Character code

    AGAIN:

    JNB TI, AGAIN ; Check to see if TI empty, if not then again

    CLR TI ; YES, Clear flag

    MOV SBUF, ACC ; Send Character

    CLR ACC.7 ; Strip off 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 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.

  • 8/2/2019 Microprocessor Sys Lab Manual_Revised22NOV11

    23/23

    Microprocessor Based Systems Lab Manual 22