KONERU LAKSHMAIAH UNIVERSITY - HostGator · Using Turbo – Assembler – Linker – Debugger...

76
MICROPROCESSORS AND MICROCONTROLLERS LABORATORY (ECE) MANUAL Prepared by S.Sreedhar Babu Assoc.prof[ece], (COURSE COORDINATOR) SCHOOL OF ELECTRICAL SCIENCES KONERU LAKSHMAIAH UNIVERSITY 2012-13.

Transcript of KONERU LAKSHMAIAH UNIVERSITY - HostGator · Using Turbo – Assembler – Linker – Debugger...

MICROPROCESSORS AND MICROCONTROLLERS

LABORATORY (ECE) MANUAL

Prepared by

S.Sreedhar Babu Assoc.prof[ece],

(COURSE COORDINATOR)

SCHOOL OF ELECTRICAL SCIENCES

KONERU LAKSHMAIAH

UNIVERSITY

2012-13.

List of Experiments

Cycle - I: The following Programs/Experiments are to be written for the assembler

Using TASM software.

1. Programs on Data Transfer in forward and reverse direction using 8086.

2. Programs on Arithmetic Instructions with 16-bit data using 8086.

3. Programs on logical Instructions using 8086.

4. Programs on String manipulation using 8086.

5. Programs on Sorting and searching an array using 8086.

6. Programs on Procedures and Macros for BCD to Binary conversion, factorial

using 8086.

7. Programs on Interrupts for using 8086.

Cycle – II: Interfaced with 8051 using Machine language program and some

Programs/Experiments are to be written for the assembler using TOP VIEW SIMULATOR

software for the interfacing.

8. Programs on Arithmetic, Logic& Bit manipulation Instructions 8051.

9. Interfacing 7-Segment Display 8051.

10. LCD Interfacing to 8051.

11. Interfacing of Binary Counters 8051.

12. Interfacing Stepper Motor 8051.

13. Interfacing A/D & D/A to 8051

14. Keyboard Interface 8051.

15. Data Transfer between two PCs using RS.232 C Serial Port.

Note: Minimum of 10 programs to be conducted.

Lab Incharge HOD

INTRODUCTION

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 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 the disk,

where you saved it after editing on the first pass through the source program the assembler determines the

displacement of named data items, the offset of labels and pails this information in 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 is 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 TASM assembler, TASM 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 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 relocatable because it can

be put anywhere in memory to be run. In TASM, TLINK 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 are 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 break point at any point in the program. If you inset a

breakpoint the debugger will run the program upto 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 TASM, td filename is issued 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 which 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 upto 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 F7.

7. We can execute the program completely at a time using F8.

DEBUGGER COMMANDS

ASSEMBLE:

To write assembly language program from the given address.

A starting address <cr>

Eg: a 1000H <cr>

Starts program at an offset of 1000H.

DUMP:

To see the specified memory contents

D memory location first address last address

(While displays the set of values stored in the specified range, which is given above)

Eg: d 0100 0105 <cr>

Display the contents of memory locations from 100 to 105(including).

ENTER:

To enter data into the specified memory locations(s).

E memory location data data data data data …<cr>

Eg: e 1200 10 20 30 40 ….

Enters the above values starting from memory locations 1200 to 1203, by loading 10 into

1200, 20 into 1201 and soon.

GO:

To execute the program

G: one instruction executes (address specified by IP)

G address <cr>: executes from current IP to the address specified

G first address last addresses <cr>: executes a set of instructions specified between the given address.

MOVE:

Moves a set of data from source location to destination location

M first address last address destination address

Eg: m100 104 200

Transfers block of data (from 100 to 104) to destination address 200.

QUIT:

To exit from the debugger.

Q <cr>

REGISTER:

Shows the contents of Registers

R register name

Eg: r ax

Shows the contents of register.

TRACE:

To trace the program instruction by instruction.

T = 0100 <cr>: traces only the current instruction. (Instruction specified by IP)

T = 0100 02 <cr>: Traces instructions from 100 to 101, here the second argument spec ifies the number of

instructions to be traced.

UNASSEMBLE:

To unassembled the program. Shows the opcodes along with the assembly language program.

U 100 <cr>: unassembled 32 instructions starting from 100th location.

U 0100 0109 <cr>: unassembles the lines from 100 to 104.

Using Turbo – Assembler – Linker – Debugger

(TASM, TLINK, TD)

1. Open an MSDOS window.

2. Set the PATH so that the TASM programs are available. The TASM programs are on the C drive; set the

path so that DOS can find them. This only needs to be done once each time you open an MSDOS prompt.

set PATH=%PATH%; C:\TASM\BIN.

3. Use a Text Editor to Edit the .ASM File.

Create your file using one of the following programs:

notepad proj.asm

WordPad proj.asm

edit proj.asm

4. Compile the source code to create an object module .

tasm/z/zi proj.asm

The /z switch causes TASM to display the lines that generate compilation errors. The /zi switch enables

information needed by the debugger to be included in the .OBJ file. Note that you should use "real mode"

assembler, TASM.EXE. Do not use the "protected mode" assembler TASM32.EXE for the assignments that

will be given in class

5. Run Linker TLINK.EXE- generate .EXE file from the .OBJ file

tlink/v proj.

6. Run the Program

Your final program (if there were no errors in the previous step) will have an .EXE ending. To just run it,

type:

proj

If you want to use the debugger to examine the instructions, registers, etc., type:

td proj

This brings up the regular full-screen version of the Turbo debugger.

1. Tracing the Program's Execution

The Turbo debugger first starts, a Module Window which displays the Executable lines of program code,

marked with a bullet in the left column of the window. You can set breakpoints or step to any of these lines

of code. An arrow in the first column of the window indicates the location of the instruction pointer. This

always points to the next statement to be executed. To execute just that instruction use one of the two

methods listed under the Run menu item:

o Trace into (can use F7 key): executes one instruction; traces "into" procedures.

o Step over (can use F8 key): executes one instruction; skips (does not trace into) procedures.

Hitting either of these executes the instruction, and moves the arrow to the next instruction. As each

instruction executes, the effects might be visible in the Registers Window and Watches Window.

2. Setting and Removing Breakpoints

To set a breakpoint, position the cursor on the desired line of source code and press F2. The line containing

the breakpoint will turn red. Pressing F2 again removes the breakpoint. To execute all instructions from the

current instruction pointer up to the next encountered breakpoint, choose Run (can use F9 key) from the Run

menu item.

3. Examining Registers

Another window, the Registers Window, can be opened to examine the current value of the CPU registers

and flags. The View menu can be used to open this Registers Window. The registers and flags might change

as each instruction is executed.

4. Examining Memory

To examine memory, you will need to open an Inspector window. An Inspector window shows the contents

of a data structure (or simple variable) in the program you are debugging. It also allows you to modify the

contents of the data structure or variable. To open an Inspector window, place the cursor on what you want

to inspect and press CTRL-I. After you've examined the data item, press the ESC key to remove the

Inspector window.

5. Viewing the Program's Output

Output written to the screen by a program is not immediately visible, since the main purpose of using a

debugger is to examine the internal operation of the program. To observe what the user would see, press

ALT-F5. The entire screen will change to a user-view showing the program's input and output (and possibly

that of previous programs as well). Press any key to return to the debugger screen.

AN INDRODUCTION TO 8086, SIMPE

PROGRAMS USING 8086 TRAINER KIT

(a) AIM: Addition of two 8-bit numbers using IMMEDIATE addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 B0 05 Mov AL,05 Data 05 is stored in reg AL

2002 B3 04 Mov BL,04 Data 04 is stored in reg BL

2004 02 C3 Add AL,BL Data of AL and BL are added and stored in AL

2006 CC Int 03 Stop

RESULT: AL: 09

b) AIM: Subtraction of two 8-bit numbers using immediate addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 B0 05 Mov AL,05 Data 05 is stored in reg AL

2002 B3 04 Mov BL,04 Data 04 is stored in reg BL

2004 2B C3 Sub AL,BL Data of BL is subtracted from AL and stored in AL

2006 CC Int 03 Stop

RESULT: AL: 01

(c) AIM: Multiplication of two 8-bit numbers using immediate addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 B0 05 Mov AL,05 Data 05 is stored in reg AL

2002 B3 04 Mov BL,04 Data 04 is stored in reg BL

2004 F7 E3 Mul BL Data of AL and BL are multiplied and RESULT is stored in AL

2006 CC Int 03 Stop

RESULT : AL

d) AIM: Division of two 8-bit numbers using immediate addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 B0 05 Mov AL,05 Data 05 is stored in reg AL

2002 B3 04 Mov BL,04 Data 04 is stored in reg BL

2004 F6 F3 Div BL Data of AL is divided by BL and RESULT stored in

AL

2006 CC Int 03 Stop

RESULT : AL: 01Remainder AH: 01Quotient

E) AIM: Addition of two 16-bit numbers using direct addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 8B060017 Mov AX,[1700] Move contents of 1700 in reg AX

2004 8B1E0217 Mov BX,[1702] Move contents of 1702 in reg BX

2008 01D8 Add AX,BX Data of AX and BX are added and RESULT stored in AX

200A CC Int 03 Stop

RESULT : AX= Input:-

Location Data

1700 88 1701 00

1702 44

1703 00

f) AIM: Subtraction of two 16-bit numbers using direct addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 8B 06 00 17 Mov AX,[1700] Move contents of 1700

in reg AX

2004 8B 1E 02 17 Mov BX,[1702] Move contents of 1702 in reg BX

2008 29 D8 Sub AX,BX

Data of BX is subtracted from AX

and RESULT stored in AX

200A CC Int 03 Stop

RESULT : AX=

Input:- Location Data Out put :

1700 88

1701 00 1702 44

1703 00

g) AIM: Addition of two 8-bit numbers using indirect addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic comment

2000 BE0017 Mov SI,1700 Move Data from 2000 to SI

2003 8A04 Mov AL,[SI] Move contents of SI to reg AL

2005 46 Inc SI Incrementing SI

2006 8A1C Mov BL,[SI] Moving data from SI to

BL

2008 02C3 Add AL,BL Adding data of AL and BL

200A 89060018 Mov [1800],AL Move contents of AL

to 1800

200E

CC Int 03 Stop

RESULT :

Input:-

Location Data 1700 04

1701 04

Output :- 1800 08

b) AIM: Subtraction of two 8-bit numbers using indirect addressing mode.

APPARATUS: 8086 Trainer kit.

Program:

Offset Address Opcode Label Mnemonic Comment

2000 BE0017 Mov SI,1700 Move Data from 2000 to SI

2003 8A04 Mov AL,[SI] Move contents of SI to

reg AL

2005 46 Inc SI Incrementing SI

2006 8A1C Mov BL,[SI] Moving data from SI to BL

2008 2BC3 Sub AL,BL Subtract data of BL

from data of AL

200A 89060018 Mov [1800],AL Move contents of AL to 1800

200E

CC Int 03 Stop

RESULT :

Input:- Location Data

1700 09

1701 04

Output :- 1800 05

Ex.No.1 DATA TRANSFER Date:

Aim:

A. Write an ALP to transfer the data stored in consecutive memory locations, in the forward direction.

B. Write an ALP to transfer the data stored in consecutive memory locations, in the reverse direction.

C. Write an ALP to transfer the data stored in consecutive memory locations, in the forward direction

overlapping.

Software Used:

Computer system with TASM.

PROGRAM:

A. Forward Direction

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

ARR1 DB 0H,1H,2H,3H,4H,5H,6H,7H,8H,9H

COUNT EQU 10D

ORG 3000H

ARR2 DB 10D DUP (0H)

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV SI, 2000H

MOV DI, 3000H

MOV CX, COUNT

BACK: MOV AH, [SI]

MOV [DI], AH

INC SI

INC DI

LOOP BACK

MOV AH, 4CH

INT21H

CODE ENDS

END START

B.Reverse Direction

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

ARR1 DB 0H,1H,2H,3H,4H,5H,6H,7H,8H,9H

COUNT EQU 10D

ORG 3000H

ARR2 DB 10D DUP (0H)

DATA ENDS

CODE SEGMENT

START: MOV AX, DATA

MOV DS, AX

MOV SI, 2000H

MOV DI, 3000H

MOV CX, COUNT

ADD DI, COUNT-1

BACK: MOV AH, [SI]

MOV [DI], AH

INC SI

DEC DI

LOOP BACK

MOV AH, 4CH

INT21H

CODE ENDS

END START

C.Forward Direction with Overlapping

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

ARR1 DB 0H,1H,2H,3H,4H,5H,6H,7H,8H,9H

COUNT EQU $-ARR1

OVERLAP EQU 06D

ORG 3000H

ARR2 DB 10D DUP (0H)

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV SI, 2000H

MOV DI, 3000H

MOV CX, COUNT-OVERLAP

NXTP: MOV AH, [SI]

MOV [DI], AH

INC SI

INC DI

LOOP NXTP

AGAIN: LEA SI, ARR1

MOV CX, COUNT

MOV AH, [SI]

MOV [DI], AH

INC SI

INC DI

LOOP AGAIN

MOV AH, 4CH

INT21H

CODE ENDS

END START

Result: Data stored in consecutive memory locations is transferred from 2000h memory

location to 3000h memory location (a) in the forward direction (i.e. in the same order), (b) in

the reverse direction and (c) with overlapping in the forward direction.

Ex.No.2 ARITHMETIC OPERATIONS Date:

Aim:

A. Write an ALP to perform addition on 16-bit data stored in consecutive memory locations and store the result from the next location onwards.

B. Write an ALP to perform subtraction on 16-bit data stored in consecutive memory locations and store the result from the next location onwards.

C. Write an ALP to perform multiplication on 16-bit data stored in consecutive memory locations and store the result from the next locations onwards.

D. Write an ALP to perform division on 16-bit data stored in consecutive memory locations and store the result from the next location onwards.

Software Used:

Computer system with TASM.

PROGRAM:

A. Addition

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

ADDEND DW 8765H

ADDER DW 9876H

SUM DW 0H

CARRY DB 0H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV AX, ADDEND

ADD AX, ADDER

JNC SKIP

INC CARRY

SKIP: MOV SUM, AX

MOV AH, 4CH

INT 21H

CODE ENDS

END START

B. Subtraction

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

SUBTRAHEND DW 8765H

SUBTRACTOR DW 9876H

DIFFERENCE DW 0H

BARROW DB 0H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV AX,SUBTRAHEND

SUB AX, SUBTRACTOR

JNC SKIP

INC BARROW

SKIP: MOV DIFFERENCE, AX

MOV AH, 4CH

INT 21H

CODE ENDS

END START

C.Multiplication

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

MULTIPLICANT DW 0FFFFH

MULTIPLIER DW 123AH

RES DD 0H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV AX, MULTIPLICANT

MOV BX, MULTIPLIER

MUL BX

MOV WORD PTR [RES], AX

MOV WORD PTR [RES+2], DX

MOV AH, 4CH

INT21H

CODE ENDS

END START

D.Division

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

DIVIDEND DW 8765H

DIVISOR DW 1234H

QUOTIENT DW 0H

REMAINDER DW 0H

DATA ENDS

CODE SEGMENT

START: MOV AX,DATA

MOV DS, AX

MOV AX, DIVIDEND

MOV BX, DIVISOR

DIV BX

MOV QUOTIENT, AX

MOV REMAINDER, DX

MOV AH, 4CH

INT 21H

CODE ENDS

END START

Result:- An ALP is written to perform a) addition, (b) subtraction, (c) multiplication and (d)

division operations using arithmetic instructions and the same is verified.

Ex.No.3 LOGICAL OPERATIONS Date:

Aim:

A. Write an ALP to find number of 1’s in a given word. B. Write an ALP to find the number of even and odd numbers in the given array.

C. Write an ALP to find the number of elements in the array having “1” in their 5th

bit position.

Software Used:

Computer system with TASM.

PROGRAM:

A. Number of 1’s in a word

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

NUM DW 5464

COUNT EQU 16D

BITCOUNT DB 0H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV CL, COUNT

MOV AX, NUM

NXTP: ROR AX, 01H

JNC GO

INC BITCOUNT

GO: DEC CL

JNZ NXTP

MOV AH, 4CH

INT 21H

CODE ENDS

END START

B.Number of even and odd numbers

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

SERIES DW 3456H,4533H,1234H,1567H,0FFFFH,145AH,56D7,4E34H,3421H,

89C5H

COUNT EQU 0AH

ODDCOUNT DB 00H

EVENCOUNT DB 00H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

LEA SI, SERIES

MOV CL, COUNT

NXTP: MOV AX, [SI]

ROR AX, 01H

JC ODD

INC EVENCOUNT

JMP OTHER

ODD: INC ODDCOUNT

OTHER: INC SI

DEC CL

JNZ NXTP

MOV AH, 4CH

INT 21H

CODE ENDS

END START

C.Number of elements having 1’s in their 5th

bit position

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

SERIES DB 21H,54H,05H,34H,32H,14H,18H,17H,53H,58H

COUNT EQU 0AH

BITCOUNT DB 00H

DATA ENDA

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

LEA SI, SERIES

MOV CL, COUNT

NXTP: MOV AX, [SI]

TEST AX, 10H

JZ GO

INC BITCOUNT

GO: INC SI

DEC CL

JNZ NXTP

MOV AH, 4CH

INT 21H

CODE ENDS

END START, Result: Logical operations such as Shift, rotate and test are used to find the a) no. of

1’s in the given byte, b) no. of even and add numbers, c)no. of positive and negative numbers and d)

no. of elements having 1’s in their 5th

bit position.

Ex.No.4 STRING MANIPULATIONS Date:

Aim:

A. Write an ALP to transfer the data in forward direction using string instructions.

B. Write an ALP to transfer the data in reverse direction using string instructions.

Software Used:

Computer system with TASM.

PROGRAM:

A. Forward Direction using String Instructions

ASSUME DS: DATA, CS: CODE, ES: EXTRA

DATA SEGMENT

ORG 2000H

STRING1 DB 'MICROPROCESSOR'

COUNT EQU $-STRING1

DATA ENDS

EXTRA SEGMENT

ORG 3000H

STRING2 DB 14D DUP (00H)

EXTRA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV AX, EXTRA

MOV ES, AX

MOV CX, COUNT

LEA SI, STRING1

LEA DI, STRING2

CLD

REP MOVSB

MOV AH, 4CH

INT 21H

CODE ENDS

END START

B.Reverse Direction using String Instructions

ASSUME DS: DATA, CS: CODE, ES: EXTRA

DATA SEGMENT

ORG 2000H

STRING1 DB 'MICROPROCESSOR'

LENGTH_STRING DW $-STRING1

DATA ENDS

EXTRA SEGMENT

ORG 3000H

STRING2 DB 14D DUP (00H)

EXTRA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV AX, EXTRA

MOV ES, AX

LEA SI, STRING1

LEA DI, STRING2

MOV CX, LENGTH_STRING

ADD DI, CX

DEC DI

BACK: MOVSB

SUB DI, 02H

LOOP BACK

MOV AH, 4CH

INT 21H

CODE ENDS

END START

Result: String manipulations such as a) forward string, b) reverse string.

Ex.No.5 SORTING Date:

Aim:

A. Write an ALP to sort the given array in signed ascending order.

B. Write an ALP to sort the given array in unsigned descending order.

C. Write an ALP to find the maximum and the minimum element in the given array.

Software Used:

Computer system with TASM.

PROGRAM:

A. Signed ascending order

ASSUME DS: DATA, CS: CODE

DATA SEGMENT

ORG 3000H

ARRAY DB 03H,07H,05H,01H,09H,04H,06H,02H,08H

COUNT EQU 09H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV DX, COUNT

DEC DX

BEGIN: MOV CX, DX

LEA SI, ARRAY

BACK: MOV AL, [SI]

CMP AL,[SI+01]

JNZ SKIP

XCHG AL, [SI+01]

XCHG AL, [SI]

SKIP: INC SI

LOOP BACK

DEC DX

JNZ BEGIN

MOV AH, 4CH

INT 21H

CODE ENDS

END START

B.Unsigned descending order

ASSUME DS: DATA, CS: CODE

DATA SEGMENT

ORG 3000H

ARRAY DB 03H,07H,05H,01H,09H,04H,06H,02H,08H

COUNT EQU 09H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV DX, COUNT

DEC DX

BEGIN: MOV CX, DX

LEA SI, ARRAY

BACK: MOV AL, [SI]

CMP AL,[SI+01]

JNB SKIP

XCHG AL, [SI+01]

XCHG AL, [SI]

SKIP: INC SI

LOOP BACK

DEC DX

JNZ BEGIN

MOV AH, 4CH

INT 21H

CODE ENDS

END START

C.Maximum and Minimum elements

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

ARRAY DW 5555H,9999H, 7777H, 2222H, 1111H, 8888H, 6666H

A_LENTH EQU ($-ARRAY)/2

MAX_NO DW 0H

MIN_NO DW 0H

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV CX, A_LENTH-1

LEA SI, ARRAY

MOV AX, [SI]

MOV BX, AX

BACK: INC SI

INC SI

CMP AX, [SI]

JNC SKIP

MOV AX, [SI]

JMP NEXT

SKIP: CMP BX, [SI]

JC NEXT

MOV BX, [SI]

NEXT: LOOP BACK

MOV MAX_NO,AX

MOV MIN_NO, BX

MOV AH, 4CH

INT 21H

CODE ENDS

END START

Result: Sorting the array in a) signed ascending order, b) unsigned descending order and c)

searching for the maximum and minimum elements in the unsigned array, are implemented.

Ex.No.6 PROCEDURE AND MACROS Date:

Aim:

A. Write an ALP to convert Hexadecimal numbers to BCD numbers.

B. Write an ALP to find the factorial of given number.

Software Used:

Computer system with TASM.

Flow Chart:

PROGRAM

A. Hexadecimal to BCD conversion

ASSUME DS: DATA, CS: CODE

DATA SEGMENT

ORG 2000H

HEXA DB 25H, 57H, 89H, 0A4H

COUNT EQU 04

DECI DB 12 DUP (00H)

BCD DW 04 DUP (00H)

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

MOV SI, OFFSET HEXA

MOV DI, OFFSET DECI

MOV CX, COUNT

BACK: XOR AX, AX

MOV AL, [SI]

MOV BL, 64H

DIV BL

MOV [DI], AL

INC DI

MOV AL, AH

XOR AH, AH

MOV BH, 0AH

DIV BH

MOV [DI], AL

INC DI

MOV [DI], AH

INC SI

INC DI

LOOP BACK

CALL UP2P

MOV AH, 4CH

INT 21H

UP2P PROC NEAR

MOV CH,COUNT

MOV DI,OFFSET DECI

MOV BP,OFFSET BCD

CONTINUE: XOR AX,AX

MOV AH,[DI]

INC DI

MOV AL,[DI]

MOV CL,4

SHL AL,CL

INC DI

ADD AL,[DI]

MOV DS:BP,AX

INC DI

INC BP

INC BP

DEC CH

JNZ CONTINUE

RET

UP2P ENDP

CODE ENDS

END START

B.Factorial of a given number

FACTORIAL MACRO

XOR CX, CX

XOR AX, AX

INC AX

MOV CL, NUMBER

CMP CL, 0

JE GO

REPEAT: MUL CX

LOOP REPEAT

GO: MOV FACT, AX

ENDM

ASSUME CS: CODE, DS: DATA

DATA SEGMENT

ORG 2000H

NUMBER DB 08H

FACT DW 0

DATA ENDS

CODE SEGMENT

START: MOV AX, DATA

MOV DS, AX

FACTORIAL

MOV AH, 4CH

INT 21H

CODE ENDS

END START

Result: Programs on a) Procedure for converting Hexadecimal numbers to packed BCD and b)

Macro for finding the factorial of a given number are implemented.

Ex.No.7 INTERRUPTS Date:

Aim:

A. Write an ALP to find whether the given string is a palindrome or not.

B. Write an ALP to enter the string through keyboard and display it.

Software Used:

Computer system with TASM.

PROGRAM:

A. String is palindrome or not

ASSUME DS: DATA, CS: CODE

DATA SEGMENT

ORG 2000H

STRING DB 'RADAR'

COUNT EQU $-STRING-1

MSG1 DB "STRING IS PALINDROME $"

MSG2 DB "STRING IS NOT PALINDROME $"

DATA ENDS

CODE SEGMENT

ORG 1000H

START: MOV AX, DATA

MOV DS, AX

LEA SI, STRING

LEA DX, MSG2

MOV CX, COUNT

MOV DI, SI

ADD DI, COUNT-1

SHR CX, 1

BACK: MOV AL, [SI]

CMP AL, [DI]

JNZ NEXT

INC SI

DEC DI

LOOP BACK

LEA DX, MSG1

NEXT: MOV AH, 09H

INT 21H

MOV AH, 4CH

INT 21H

CODE ENDS

END START

B.Enter and Display the string

ASSUME DS: DATA, CS: CODE

DATA SEGMENT

ORG 2000H

INPUT DB “ENTER THE STRING”, 0DH, 0AH, “$”

OUTPUT DB “THE ENTERED STRING IS:” 0DH, 0AH, “$”

S_LENTH DB 0

BUFFER DB 80 DUP (0)

DATA ENDS

CODE SEGMENT

START: MOV AX, DATA

MOV DS, AX

XOR CL, CL

LEA DX, INPUT

MOV AH, 09H

INT 21H

LEA BX, BUFFER

REPEAT: MOV AH, 01H

INT 21H

CMP AL, 0DH

JZ EXIT

INC CL

MOV [BX], AL

INC BX

JMP REPEAT

EXIT: MOV S_LENTH, CL

LEA BX, BUFFER

ADD BL, CL

MOV AL,’$’

MOV [BX], AL

LEA DX, OUTPUT

MOV AH, 09H

INT 21H

LEA DX, BUFFER

MOV AH, 09H

INT 21H

CODE ENDS

ENDF START

Result: Interrupts on a) whether the given string is a palindrome or not and b) entering the string through keyboard and displaying the same, are verified.

8051 INTRODUCTION AND

INTERFACING WITH

PERIPHERALDEVICES

USING TOP VIEWIMULATOR

Ex.No.8 Arithmetic, Logic and Bit manipulation Instructions Date:

Aim: Write an ALP to perform Addition, Subtraction, Multiplication, Division, Swap,Reset,

Set, Bit, Byte manipulation Instructions. Software Used:

Computer system with TOPVIEW SIMULATOR.

PROCEDURE:

In Top view simulator:

Select device 89C51 with 12000000Hz frequency.

Open the hex file (Ctrl + O or file->Load program).

Run the program at full speed. ( Run->go)

To stop the execution Run->stop execution.

A.ADDITION

$MOD51

ORG 0000H

MOV DPTR, #0101H

MOVX A,@DPTR

MOV B, A

DEC DPL

MOV A,@DPTR

ADD A, B

INC DPTR

INC DPTR

MOVX @DPTR, A

CLR A

ADDC A, #00H

INC DPTR

MOVX @DPTR, A

STOP: SJMP STOP

END

B.SUBTRACTION

$MOD51

ORG 0000H

MOV DPTR, #0101H

MOVX A,@DPTR

MOV B, A

DEC DPL

MOVX @DPTR, A

CLR C

SUBB A,B

INC DPTR

INC DPTR

MOVX @DPTR, A

CLR A

ADDC A, #00H

INC DPTR.

MOVX @DPTR,A

STOP: SJMP STOP

END

C.MULTIPLICATION

$MOD51

ORG 0000H

MOV DPTR, # 0101H

MOVX A,@DPTR

MOV B, A

DEC DPL

MOVX A, @DPTR

MUL A B

INC DPTR

INC DPTR

MOVX @DPTR, A

MOV A, B

INC DPTR

MOVX @DPTR, A

STOP: SJMP STOP

END

D.DIVISION

$MOD51

ORG 0000H

MOV DPTR, # 0101H

MOV X A,@DPTR

MOV B, A

DEC DPL

MOVX A, @DPTR

DIV AB

INC DPTR

INC DPTR

MOVX @DPTR, A

MOV A, B

INC DPTR

MOVX @DPTR, A

STOP: SJMP STOP

END

D.SWAP

$MOD51

ORG 0000H

MOV DPTR, # 0101H

MOVX A, @ DPTR

SWAP A

INC DPTR

MOVX @ DPTR, A

STOP: SJMP STOP

END

E.RESET

$MOD51

ORG 0000H

MOV DPTR, # 0101H

CLR C

MOV A, ODOH

MOV DPTR, # 0101H

MOVX @ DPTR, A

STOP: SJMP STOP

END

F.SET

$MOD51

ORG 0000H

MOV DPTR, # 0101H

SETB C

MOV A,0D0H

MOV DPTR, # 0101H

MOVX @ DPTR, A

STOP: SJMP STOP

END

G.BIT

$MOD51

ORG 0000H

MOV DPTR, # 0101H

MOV A, ODOH

MOVX @ DPTR, A

CPL C

MOV A, ODOH

INC DPTR

MOVX @DPTR, A

STOP: SJMP STOP

END

H.BYTE

$MOD51

ORG 0000H

MOV DPTR, # 0101H

MOVX A @ DPTR

CPL A

INC DPTR

MOVX @ DPTR, A

STOP: SJMP STOP

END

I.COMPLIMENT

$MOD51

ORG 0000H

MOV DPTR,#0101H

MOVX A,@DPTR

CPL A

INC DPTR

MOVX @DPTR,A

STOP: SJMP STOP

END

Result: All the Arithmetic and special instruction operations are performed by using 8051.

Ex. No 9 Interfacing of 7 – Segment Displays Date:

Problem statement:

Interface 7-segment displays (using BCD to 7-seg decoder) to the 8051

microcontroller.

Aim: To display two Digit Decimal counter on 7- segment displays.

Software Used:

Computer system with TOPVIEW SIMULATOR.

PROCEDURE:

In Top view simulator:-

Select device 89C51 with 12000000Hz frequency.

Open the hex file (Ctrl + O or file->Load program).

Open LED module settings to configure the LED connections.( File-> External

modules settings-> LED)

In seven segment display select

Interface selection-> non-multiplexed.

Display type->common cathode.

Data input selection -> BCD.

Click on selection of port line and number of digits.

Select no. of digits as 2

Select control lines and port lines as shown in the below table.

Check with summary window for correct connections.

Open the LED module. (View->External modules->LED).

Run the program at full speed. ( Run->go)

The two digit decimal counter will shown up.

To stop the execution Run->stop execution.

Table for Port line selection

7-Segment Display PIN on 8051

Digit2 A P1.0

Digit2 B P1.1

Digit2 C P1.2

Digit2 D P1.3

Digit2 Dp GND

Digit1 A P1.4

Digit1 B P1.5

Digit1 C P1.6

Digit1 D P1.7

Digit1 Dp GND

Circuit:

Digit2

P1.0 A

P1.1 B

P1.2 C

P1.3 D Digit1

P1.4 A A

P1.5 B B

P1.6 C C

P1.7 D

8051

BCD to 7

SEG

BCD to 7

SEG

7

SEG

7

SEG

|_|

PROGRAM

$MOD51

ORG 0000H

SJMP MAIN

ORG 0030H

MAIN:MOV A,#00H

AGAIN:MOV P1,A

LCALL DELAY

ADD A,#01

DA A

SJMP AGAIN

ORG 0040H

DELAY:MOV R2,0FFH

UP1:MOV R1,0FFH

UP2:NOP

NOP

NOP

DJNZ R1,UP2

DJNZ R2,UP1

RET

END

Simulated output:

Result: Decimal counter is displayed on 7-Segment displays.

Ex.No.10 Interfacing of 2 – line 16 – character LCD display Date:

Problem statement:

Interface LCD controller to the 8051 microcontroller.

Aim: To display strings in two lines on a LCD module.

Software Used:

Computer system with TOPVIEW SIMULATOR.

Procedure:

In Top view simulator

Select device 89C51 with 12000000Hz frequency.

Open the hex file ( Ctrl + O or file->Load program).

Open LCD module settings to configure the LCD connections.( File-> External

modules settings-> LCD) Check with the summary window for correct

connections.

Select LCD as 2lines 16 characters and Data bus width as 8 bit.

In port line selection configure the port lines as per requirement. ( see the table for

port selection).

Open the LCD module. (View->External modules->LCD).

Run the program at full speed. ( Run->go)

The strings are displayed on the LCD.

To stop the execution Run->stop execution.

Table for Port line selection

PIN on 8051 PIN on LCD

P1.0 … P1.7 D0…D7(Data lines)

P2.0 RS

P2.1 RW

P2.2 E

Circuit:

P1.0 D0

P1.7 D7

RS RW E

P2.0

P2.1

P2.2

Flow Chart:

PROGRAM

$MOD51

RS EQU P3.1

EN EQU P3.2

RW EQU P3.3

ORG 0000H

MOV A,#38H

LCALL LCD_COMD

MOV A,#0EH

LCALL LCD_COMD

MOV A,#06H

LCALL LCD_COMD

MOV A,#01H

LCALL LCD_COMD

MOV A,#'E'

LCALL LCD_TEXT

MOV A,#'C'

8051

LCD

controller

LCALL LCD_TEXT

MOV A,#'E'

LCALL LCD_TEXT

LOOP:SJMP LOOP

LCD_COMD:CLR C

LCALL WRITE

RET

LCD_TEXT:SETB C

LCALL WRITE

RET

WRITE:SETB EN

CLR RW

MOV RS,C

MOV P1,A

CLR EN

LCALL DELAY

RET

DELAY:MOV R0,#60

LOOP2:MOV R1,#255

LOOP1:DJNZ R1,LOOP1

DJNZ R0,LOOP2

RET

END

Simulator Output:

Result:

The strings in two lines are displayed on LCD module.

Ex.No.11 LIGHT EMITTING DIODE (LED) Date:

Problem statement:

Interface LED controller to the 8051 microcontroller.

Aim:

Write an ALP for LED using 8051.

Software Used:

Computer system with TOPVIEW SIMULATOR.

PROCEDURE:

In Top view simulator:

Select device 89C51 with 12000000Hz frequency.

Open the hex file (Ctrl + O or file->Load program).

Open LED module settings to configure the LED connections.( File-> External

modules settings-> LED)

In seven segment display select

Interface selection-> non-multiplexed.

Display type->common cathode.

Data input selection -> BCD.

Click on selection of port line and number of digits.

Select no. of digits as 2

Select control lines and port lines as shown in the below table.

Check with summary window for correct connections.

Open the LED module. (View->External modules->LED).

Run the program at full speed. ( Run->go)

The two digit decimal counter will shown up.

To stop the execution Run->stop execution.

Flow Chart:

PROGRAM

$MOD51

CLR A

REPEAT:MOV P1,A

LCALL DELAY

INC A

SJMP REPEAT

DELAY:MOV R2,#0FFH

BACK:MOV R1,#0FFH

UP1:NOP

NOP

DJNZ R1,UP1

DJNZ R2,BACK

RET

END

Simulator Output:

Result: Decimal counter is displayed on LED displays.

Exp.12 Interfacing of Stepper Motor Date:

Aim: Interface the stepper motor to 8051 target system and write program to operate in

different speeds both clock wise and anti clock wise directions.

STEPPER MOTOR CONTROLLER INTERFACING MODULE

CIRCUIT DESCRIPTION

The four winding of the motor is connected with PA0 to PA3 through buffer and driving

circuit. So, the Port A of 8255 will have to initializes in output mode.

HARDWARE INSTALLATION

1. Connect Stepper Motor interfacing module to 8255 – I of 8051 / 8085 Trainer Kit

through 26 pin FRC Cable.

2. Be sure about the direction of the cable i.e. Pin No.1 of module should be

connected to Pin No.1 of 8255 connector.

3. Connect +5V, GND from the trainer kit (+5V & GND signals are available in the

25 & 26 pin of FRC 8255 – I Connector).

4. Connect Motor Supply to +12V from the Trainer Kit.

8255 Port Address:

Port A – FF00H

Port B –FF01H

Port C – FF02H

Control Word – FF03H

Procedure:

1. Create a source file in Assembly language.

2. Assemble the source file using ASM51 cross Assembler and create LST & HEX files.

3.Configure the windows hyper terminal with a baud rate 9600 , 8 data bits, no parity, 1

stop bit and flow control XON / XOFF.

4. Establish a serial communication link between Host and Target system using hyper

terminal.

5. Down load the HEX file from the host to the target system.

6. Connect stepper motor interface module to the target system and run the program.

Program:

This program will move the motor in Clock wise direction. For Anti Clock wise

direction change F9, F5, F6, FA in place of FA, F6, F5, F9.

Address Code Label Mnemonic Operand Comments

2000 90 FF 03 MOV DPTR, #0FF03H

2003 74 80 MOV A,#80H

2005 F0 MOVX @DPTR,A Init ports of 8255 as

all

2006 90 FF 00 START: MOV DPTR,#0FF00H output ports

2009 74 FA MOV A,#0FAH output code for step

1

200B F0 MOVX @DPTR,A

200C 11 1F ACALL

DELAY

delay between two

steps

200E 74 F6 MOV A,#0F6H output code for step

2

2010 F0 MOVX @DPTR,A

2011 11 1F ACALL

DELAY

2013 74 F5 MOV A,#0F5H output code for step

3

2015 F0 MOVX @DPTR,A

2016 11 1F ACALL

DELAY

2018 74 F9 MOV A,#0F9H output code for step

4

201A F0 MOVX @DPTR,A

201B 11 1F ACALL

DELAY

delay between two

steps

201D 80 E7 SJMP START repeat for next

cycle

201F 7F 3F DELAY: MOV R7,#03FH delay count for

controlling speed

2021 7E 3F DELA: MOV R6,#03FH

2023 00 DELA1: NOP

2024 00 NOP

2025 00 NOP

2026 DE FB DJNZ R6,DELA1

2028 DF F7 DJNZ R7,DELA

202A 22 RET

Result:

The Stepper Motor is interfaced to 8051 and operated with different speeds and

directions.

8051 INTERFACING WITH

PERIPHERAL DEVICES

USING ASSEMBLY LANGUAGE

PROGRAM

BINARY COUNTER INTERFACING WITH 8051

AIM:

To interface Binary Counter 8051 parallel port to demonstrate the generation of convert a binary number to

a decimal.

APPARATUS REQUIRED;

8051 Trainer Kit

LED’S

Resistors -330k ohm

THEORY:

Before starting with counters there is some vital information that needs to be understood. The most

important is the fact that since the outputs of a digital chip can only be in one of two states, it must use a different counting system than you are accustomed to. Normally we use a decimal counting system; meaning each digit in a number is represented by one of 10 characters (0-9). In a binary system, there can only be two

characters, 0 and 1. A computer does not recognize 0 or 1. It only works on voltage changes. What we call logic 0 to a

computer is zero volts. What we call logic 1 is +5 volts. When a logic state changes from a zero to a one the voltage at the pin in question goes from zero volts to +5 volts. Likewise, when a logic state changes from a one to a zero the voltage is changing from +5 volts to zero volts.

When counting up in a decimal system, we start with the first digit. When that digit ‘overflows’, i.e. gets

above 9, we set it to 0 and add one to the next digit over. The same goes for a binary system. When the

count goes above 1 we add one to the next digit over and set the first digit to 0. Here is an example

DECIMAL TO BINARY CONVERSION

Decimal Number (base 10) Binary Number (base 2)

0 0

1 1

2 10

3 11

4 100

5 101

6 110

Decimal Number (base 10) Binary Number (base 2)

7 111

8 1000

9 1001

BINARY COUNTING

To convert a binary number to a decimal, we use a simple system. Each digit or ‘bit’ of the binary

number represents a power of two. All you need to do to convert from binary to decimal is add up the

applicable powers of 2. In the example below, we find that the binary number 10110111 is equal to 183. The

diagram also shows that eight bits make up what is called a byte. Nibbles are the upper or lower four bits of

that byte. Referring to nibbles and bytes are useful when dealing with other number systems such as

hexadecimal, which is base 16.

fig1. Interfacing

PROGRAM:

MOV DPTR, # 2023; Control register of 8255.

MOV A, #80; 8255 in I/O mode operation

MOVX @DPTR, A

MOV A, #00H

START: MOV DPTR, # 2020 (port A address )

INC A

MOVX @DPTR, A

LCALL DELAY

CJNE A, #FFH, START

XX: SJMP XX

DELAY: MOV R1, # FFh

CC: MOV R2, # FFh

AA: DJNZ R2, AA

MOV R3, # FFH

BB: DJNZ R3, BB

DJNZ R1, CC

RET

Result:

Seven Segment Display Interfacing With 8051

AIM:

To interface Seven Segment Display to 8051 to generate a digit from 0 to 9 using common anode and

Common cathode technique.

APPARATUS REQUIRED

8051 TRAINER

Common cathode Seven Segment Display

Common anode Seven Segment Display

INTRODUCTION

For the seven segment display you can use the LT-541 or LSD5061-11 chip. Each of the segments of the

display is connected to a pin on the 8051 (the schematic shows how to do this). In order to light up a

segment on the the pin must be set to 0V. To turn a segment off the corresponding pin must be set to 5V.

This is simply done by setting the pins on the 8051 to '1' or '0'.

LED displays are

Power-hungry (10ma per LED)

Pin-hungry (8 pins per 7-seg display)

But they are cheaper than LCD display

7-SEG Display is available in two types -1. Common anode & 2. Common cathode, but command anode

display is most suitable for interfacing with 8051 since 8051 port pins can sink current better than sourcing

it.

CREATING DIGIT PATTERN

For displaying Digit say 7 we need to light segments -a ,b, c. Since we are using Common anode display , to

do so we have to to provide Logic -0 (0 v) at anode of these segments.so need to clear pins- P1.0

,P1.1,P1.2. that is 1 1 1 1 1 0 0 0 -->F8h .

Connection Hex Code

Digit Seg. h Seg. g Seg. f Seg. e Seg. d Seg. c Seg.

b Seg. a HEX

0 1 1 0 0 0 0 0 0 C0

1 0 0 0 0 0 1 1 0 06

2 1 0 1 0 0 1 0 0 A4

3 1 0 1 1 0 0 0 0 B0

4 1 0 0 1 1 0 0 1 99

COMMON ANODE

Digit Seg. h Seg. g Seg. f Seg. e Seg. d Seg. c Seg.

b Seg. a HEX

0 0 0 1 1 1 1 1 1 3f

1 0 0 0 0 0 1 1 0 06

2 0 1 0 1 1 0 1 1 5b

3 0 1 0 0 1 1 1 1 4f

4 0 1 1 0 0 1 1 0 66

COMMON CATHODE

Segment

number 8051 pin number

a P1.0

b P1.1

c P1.2

d P1.3

e P1.4

f P1.5

g p1.6

h(dp) P1.7

Segment

number 8051 pin number

a P1.0

b P1.1

c P1.2

d P1.3

e P1.4

f P1.5

g p1.6

h(dp) P1.7

You can also do this for some characters like A, E. But not for D or B because it will be same

as that of 0 & 8 . So this is one of limitation of 7-seg display.

Since we can enable only one 7-seg display at a time, we need to scan these display at fast rate .The

scanning frequency should be high enough to be flicker- free. At least 30HZ .Therefore – time one digit is

ON is 1/30 seconds

INTERFACING

Note that I am using Common Anode display. So the common Anode pin is tied to 5v .The cathode pins are

connected to port 1 through 330 Ohm resistance (current limiting).

SOURCE CODE:

8000: MOV DPTR, #2023; Control register of 8255

MOV A, # 80;

MOVX @DPTR, A; select I/O mode in 8255, out at Control register of 8255

MOV R0, # 00;

MOV A, R0;

XX: MOV DPTR, # 8500; Look up table address

MOVC A, @A+DPTR;

MOV DPTR, #2020; Port “A” Address

MOVX @DPTR, A;

LCALL DELAY

INC R0

CJMP R0, # 00FH, XX;

YY: SJMP YY

DELAY: MOV R1, # FF

CC: MOV R2, # FF

AA: DJNZ R2, AA

MOV R3, # FF

BB: DJNZ R3, BB

DJNZ R1, CC

RET

LOOK UP TABLE

8500: C0, 06, A4, B0, 99, -----------------

RESULT: To generate digits from 0 to 9 using common anode and common cathode technique.

INTERFACING DAC WITH 8051

AIM:

To interface DAC with 8051 parallel port to demonstrate the generation of square, saw tooth and

Triangular wave.

APPARATUS REQUIRED;

8051 Trainer Kit

DAC Interface Board

THEORY:

DAC 0809 is an 8 – bit DAC and the output voltage variation is between – 5V and +5V.The output voltage

varies in steps of 10/256 = 0.04 (appx.). The digital data input and the corresponding output voltages are

presented in the Table below

INPUT DATA IN HEX OUTPUT VOLTAGE(V)

00 -5.00

01 -4.96

02 -4.92

7F 00

FD 4.92

FE 4.96

FF 5.00

Table 1.

Referring to Table1, with 00 H as input to DAC, the analog output is – 5V. Similarly, With FF H as input,

the output is +5V. Outputting digital data 00 and FF at regular intervals, to DAC, results in different wave

forms namely square, triangular, etc,

Two methods of creating a DAC: binary weighted and R/2R ladder. The vast majority of integrated

circuit DACs, including the MC1408 (DAC 0809) used in this section use the R/2R method since it can

achieve a much higher degree of precision. The first criterion for judging a DAC is its resolution, which is a

function of the number of binary inputs. The common ones are 8, 10, and 12 bits. The number of data bit

inputs decides the resolution of the DAC since the number of analog output levels is equal to 2n, where n is

the number of data bit inputs. Therefore, an 8-input DAC.

Such as the DAC 0809 provides 256 discrete voltage (or current) levels of output. Similarly, the 12-bit DAC provides 4096 discrete voltage levels. There are also 16-bit DACs, but they are more expensive.

Figure 1. 8051Connection to DAC 0809

ALGORITHM:

(a) Square Wave Generation

1. Move the port address of DAC to DPTR.

2. Load the initial value (00) to Accumulator and move it to DAC.

3. Call the delay program.

4. Load the final value (FF) to accumulator and move it to DAC.

5. Call the delay program.

6. Repeat the steps 2 to 5.

(b) Saw tooth Wave Generation

1. Move the port address of DAC to DPTR.

2. Load the initial value (00) to Accumulator.

3. Move the accumulator content to DAC.

4. Increment the accumulator content by 1.

5. Repeat Steps 3 and 4.

(c) Triangular Wave Generation

1. Move the port address of DAC to DPTR.

2. Load the initial value (00) to Accumulator.

3. Move the accumulator content to DAC

4. Increment the accumulator content by 1.

5. If accumulator content is zero proceed to next step. Else go to step 3.

6. Load value (FF) to Accumulator

7. Move the accumulator content to DAC

8. Decrement the accumulator content by 1.

9. If accumulator content is zero go to step2. Else go to step 7.

PROGRAM: (a) Square Wave Generation

MOV DPTR, # 2023; Control register of 8255.

MOV A, #80; 8255 in I/O mode operation

MOVX @DPTR, A

MOV DPTR, # 2021 (port B address of DAC start)

START: MOV A, #00H

MOVX @DPTR, A

LCALL DELAY

MOV A, #FFH

MOVX @DPTR, A

LCALL DELAY

SJMP START

DELAY: MOV R1, #05H

LOOP: MOV R2, #FFH

HERE: DJNZ R2, HERE

DJNZ R1, LOOP

RET

PROGRAM: (b) Saw Tooth Wave Generation

MOV DPTR, # 2023; Control register of 8255.

MOV A, #80; 8255 in I/O mode operation

MOVX @DPTR,

START: MOV A, #00H

MOV DPTR, # 2021 (port B address of DAC start)

GO: MOVX @DPTR, A

INC A

CJNE A, #FFH, GO

SJMP START

PROGRAM: (c) Triangular Wave Generation

MOV DPTR, # 2023; Control register of 8255.

MOV A, #80; 8255 in I/O mode operation

MOVX @DPTR, A

START: MOV A, #00H

MOV DPTR, # 2021 (port B address of DAC start)

GO: MOVX @DPTR, A

INC A

CJNE A, #FFH, GO

GO1: MOVX @DPTR, A

DEC A

CJNE A, #00H, GO1

SJMP START

Result: Thus the square, triangular and saw tooth wave form were generated by interfacing DAC with

8051 trainer kit.

ADC 0808/0809 INTERFACING WITH 8051

AIM:

To interface ADC with 8051 to generate digital output by giving an analog input voltage.

APPARATUS REQUIRED;

8051 Trainer Kit

ADC Interface Board

THEORY:

One of the most commonly used ADC is ADC0808/0809. ADC 0808/0809 is a Successive

approximation type with 8 channels i.e. it can directly access 8 single ended analog signals.

I/O Pins ADDRESS LINE A, B, and C: The device contains 8-channels. A particular channel is selected by using the

address decoder line. The TABLE 1 shows the input states for address lines to select any channel.

Address Latch Enable ALE: The address is latched on the Low – High transition of ALE.

START: The ADC’s Successive Approximation Register (SAR) is reset on the positive edge i.e. Low- High of the Start Conversion pulse. Whereas the conversion is begun on the falling edge i.e. high – Low of the pulse.

Output Enable: Whenever data has to be read from the ADC, Output Enable pin has to be pulled high thus

enabling the TRI-STATE outputs, allowing data to be read from the data pins D0-D7.

End of Conversion (EOC): This Pin becomes high when the conversion has ended, so the controller comes to know that the data can now be read from the data pins.

Clock: External clock pulses are to be given to the ADC; this can be given either from LM 555 in Astable

mode or the controller can also be used to give the pulses.

ALGORITHM:

1. Start. 2. Select the channel. 3. A Low – High transition on ALE to latch in the address. 4. A Low – High transition on Start to reset the ADC’s SAR. 5. A High – Low transition on ALE. 6. A High – Low

transition on start to start the conversion. 7. Wait for End of cycle (EOC) pin to become high. 8. Make

Output Enable pin High. 9. Take Data from the ADC’s output 10. Make Output Enable pin Low. 11. Stop

The total numbers of lines required are: Datalines:8,ALE:1,START:1,EOC:1,Output Enable:1

I.e. total 12 lines. You can directly connect the OE pin to Vcc. Moreover instead of polling for EOC just put

some delay so instead of 12 lines you will require 10 lines. We Can also provide the clock through the

controller thus eliminating the need of external circuit for clock.

Calculating Step Size

ADC 0808 is an 8 bit ADC i.e. it divides the voltage applied at Vref+ & Vref- into 28 i.e. 256 steps.

Step Size = (Vref+ - Vref-)/256

Suppose Vref+ is connected to Vcc i.e. 5V & Vref- is connected to the Gnd then the step size will be

Step size= (5 - 0)/256= 19.53 mv.

Calculating Dout.

The data we get at the D0 - D7 depends upon the step size & the Input voltage i.e. Vin.

Dout = Vin /step Size.

If you want to interface sensors like LM35 which has output 10mv/°C then I would suggest that you set the

Vref+ to 2.56v so that the step size will be

Step size= (2.56 - 0)/256= 10 mv.

So now whatever reading that you get from the ADC will be equal to the actual temperature.

Here is a program for interfacing the ADC to microcontroller, as stated above have assumed that the

OE pin is connected to Vcc & the clock is given by the controller.

This program selects channel 0 as input channel reads from it & saves in the accumulator.

NOTE: In the LAB, ADC is interfaced with 8051 Microcontroller through 8255 PPI.

Port 2 of 8051 is connected to Port C of 8255, Port 1 of 8051 is connected to Port A of 8255,

Port C is for interfacing signals and Port A is for Reading data from ADC.

Fig.1 interfacing circu it

PROGRAM:

MOV DPTR, #2023

MOV A, #90: PA-I/P PORT, PORT B, C-O/P PORTS

MOVX @DPTR, A

BEGIN: MOV DPTR, #2022; Select CH-0

MOV A, #00

MOVX @DPTR, A

MOV A, #0DH; Set PC6 (OE)

MOV DPTR, #2023

MOVX @DPTR, A

MOV A, #0FH; SET SOC

MOVX @DPTR, A

LCALL DELAY

MOV A, #0EH

MOVX @DPTR, A

MOV A, #0CH

MOVX @DPTR, A

MOV DPTR, #2020

BACK: MOVX A,@DPTR

JB 0E7, BACK

MOV DPTR, #2020

REP: MOVX A,@DPTR

ANL A, #80H

JNB 0E7, REP

MOV A, #0DH

MOV DPTR, #2023

MOVX @DPTR, A

MOV DPTR, #2020

MOVX A,@DPTR

MOV DPTR, #9000H; Read the digital output from this address

MOVX @DPTR, A

SJMP BEGIN

DELAY: MOV R3, #30H

AGAIN: MOV R4, #FFH

BACK: NOP

NOP

DJNZ R4, BACK

DJNZ R3, AGAIN

RET

RESULT: Given input voltage varying from 5v to 0v at Channel 0 and observe the digital output value at

the address at 9000h, which is given in code.

STEPPER MOTOR INTERFACING WITH 8051

AIM:

To interface a stepper motor with 8051 microcontroller and operate it.

APPARATUS:

8051

Stepper motor interfacing board

THEORY:

A motor in which the rotor is able to assume only discrete stationary angular position is a stepper

motor. The rotary motion occurs in a step-wise manner from one equilibrium position to the next. Stepper

Motors are used very wisely in position control systems like printers, disk drives, process control machine

tools, etc.

The basic two-phase stepper motor consists of two pairs of stator poles. Each of the four poles has its

own winding. The excitation of any one winding generates a North Pole. A South Pole gets induced at the

diametrically opposite side. The rotor magnetic system has two end faces. It is a permanent magnet with one

face as South Pole and the other as North Pole.

The Stepper Motor windings A1, A2, B1, B2 are cyclically excited with a DC current to run the

motor in clockwise direction. By reversing the phase sequence as A1, B2, A2, B1, anticlockwise stepping

can be obtained.

8051 Interfacing with stepper motor

Stepper motors can be driven in two different patterns or sqeunces. Namely,

1. Full Step Sequence 2. Half Step Sequence

A normal 4 step sequence is like below.

Step # Winding A Winding B Winding C Winding D

1 1 0 0 1

2 1 1 0 0

3 0 1 1 0

4 0 0 1 1

Going from step 1 to 4 we rotate motor clockwise, going from step 4 to 1 we rotate motor counter clockwise. The stepper motor discussed here has total 6 leads. 4 leads representing 4 windings and 2 commons for centre tapped leads. Here we have used 2 phase 4 step sequence. It must also be noted we can start from any step. Be it step 1, 2, 3 or 4.

But we must continue in proper order for proper rotation.

In programs on blog I have mostly begun from step 3. Other step sequences are as below

Half Step 8 Step Sequence

Step # Winding A Winding B Winding C Winding D

1 1 0 0 1

2 1 0 0 0

3 1 1 0 0

4 0 1 0 0

5 0 1 1 0

6 0 0 1 0

7 0 0 1 1

8 0 0 0 1

Wave drive 4 step sequence

Step # Winding A Winding B Winding C Winding D

1 1 0 0 0

2 0 1 0 0

3 0 0 1 0

4 0 0 0 1

The Movement is associated with a step angle:

Step angle Steps per revolution

.72 500

1.8 200

2 180

2.5 144

5 72

7.5 48

15 24

4 Pins Of Stepper Motor Controlled By 4 Bits Of 8051 P1.0 - P1.3; It Is Interfaced Using Darlington

Arrays Such As Uln2003 As; 8051 Lacks Current To Run The Motor. So Uln2003 Is Used For

Energizing Stator.; Program Below Shows Just Rotating The Motor Step Wise; But Step Width Is

Unknown

MOV A, #66H ; Load The Step Sequence

BACK :MOV P1, A ; Load Sequence To Port

RR A ; Change Sequence Rotate Clockwise

ACALL DELAY : Wait For It

SJMP BACK ; Now Keep Going

DELAY :MOV R2, #100

H1 :MOV R3, #255

H2 :DJNZ R3, H2

DJNZ R2, H1

RET

Step Angle:

Step angle of the stepper motor is defined as the angle traversed by the motor in one step. To

calculate step angle, simply divide 360 by number of steps a motor takes to complete one revolution. As

we have seen that in half mode, the number of steps taken by the motor to complete one revolution gets

doubled, so step angle reduces to half.

As in above examples, Stepper Motor rotating in full mode takes 4 steps to complete a revolution, So

step angle can be calculated as...Step Angle ø = 360° / 4 = 90°.And in case of half mode step angle gets half

so 45°.So this way we can calculate step angle for any stepper motor. Usually step angle is given in the spec sheet

of the stepper motor you are using.

Knowing stepper motor's step angle helps you calibrate the rotation of motor also to helps you move the motor

to correct angular position.

; This Other Program Code How In Same Interfacing To Rotate A Stepper Motor

; 64 Degrees In Clockwise Direction Using 4 Step Sequence

; It Takes Some Calculations

; Consider A Motor With Step Angle 2 Degree

; So Steps Per Revolution = 180

; No Of Rotor Teeth = 45

; Movement Per 4 Step Sequence is 8 Degrees

; For 64 Degree Movement We Need To Send 8 Consecutive 4 Step Sequence

; That Is It Will Cover 32 Steps

ORG 0000H

MOV A, #66H

MOV R1, #32H ; 32 Steps To Be Taken

BACK :RR A ; Rotate Clockwise

MOV P1, A ;

ACALL DELAY ;

DJNZ R0, BACK ;

END

DELAY: MOV R2, #100

H1: MOV R3, #255

H2: DJNZ R3, H2

DJNZ R2, H1

RET

Result: By applying step sequence in program rotate the motor in clock wise and anti clock wise direction.