Top Banner
98
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MPI Laboratory Manual
Page 2: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

ContentsContents ........................................................................................................ 2 The 8086 Microprocessor Programs .......................................................... 3 TASM Editor ................................................................................................... 4 1. Addition of Numbers ............................................................................... 5 2. Move 5 Bytes ......................................................................................... 12 3. Packing of three BCD numbers. ......................................................... 20 4. Convert Fahrenheit to Celsius ........................................................... 22 5. Factorial of given number .................................................................... 25 6. Finding Largest number ....................................................................... 26 7. Find the Square Root Of A Given Number. ........................................ 29 8. Next number in a Fibonacci Series. ................................................... 31 9. Arrange The Numbers In Ascending Order. ...................................... 32 10. LCM of two given numbers ................................................................ 34 11. Multiplication by shift and add method. ......................................... 36 12. Transfer string from one location to another. ................................ 37 13. Display a string on the screen .......................................................... 43 14. Identification of Even Or Odd Number Using Procedures ............ 46 15. Identification of Prime Number Or Not Using Procedures ............ 52 The 8051 Micro Controller interfacing Programs .................................. 56 1. LEDs and Switches ................................................................................ 58 2. Elevator ................................................................................................... 59 3. Serial Communication .......................................................................... 61 4. Interfacing of LCD ................................................................................. 64 5. Interfacing of ADC0804 ........................................................................ 67 6. Interfacing of DAC0808 ........................................................................ 70 7. Interfacing of Relay .............................................................................. 72 8. Interfacing of DC Motor ........................................................................ 74 9. Interfacing of Stepper Motor ............................................................... 75 10. Interfacing of Keypad and LCD ........................................................ 78 11. Interfacing of Real time clock ........................................................... 82 12. Interfacing of EEPROM ....................................................................... 84 13. Interfacing of Temperature sensor .................................................. 88 14. Interfacing of Buzzer .......................................................................... 91 15. Interfacing of Magnetic sensor ......................................................... 92 Appendix A .................................................................................................. 94 Appendix B .................................................................................................. 95 Appendix C .................................................................................................. 96

GRIET/ECE 2 of 86

Page 3: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

The 8086 Microprocessor Programs

GRIET/ECE 3 of 86

Page 4: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

TASM EditorHow to go to TASM editor:

1. Click START - starts window2. Click RUN - runs window3. Type cmd - to enter command prompt4. Type cd.. - to enter drive 5. Type cd tasm- opens tasm 6. Type cd bin – opens bin7. Type edit -enters tasm editer

TASM editor commands:

After writing code in TASM editor, the following are the steps to compile, run and to enter data.

1. Click FILE →SAVE - to save the program2. Type tasm file name .asm -specifying assembly level language code 3. Type tlink file name .obj –to link to 8086 µP and generate object file4. Type debug filename.exe - to generate executable file5. Type t –step by step compilation6. Type d ds: -displays data in data segment7. Type e ds: - to enter the data8. Type go -to compile total program9. Type t no. of instructions -to compile required number of instructions

GRIET/ECE 4 of 86

Page 5: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 1. Addition of NumbersProgram description Assembly language program to add data located at offset

AddressAuthor GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Code segmentAssume cs: code

Start: mov ax, 2000h ;initializes DS register with 2000;mov ds, axmov si, 0100h ;moves offset address 0100 into SI;mov al, [si] ;moves contents of SI Register into AL reg.;mov si, 0600h ;moves offset address 0600 into SI;

mov bl, [si];moves contents of SI Register into BL reg.;

add al, bl :adds contents of BL to AL;mov si, 0700h ;moves offset address 0700 into SI;mov [si], ax ;moves AX register into address in SIHlt ;end of program

Code endsEnd start

Example:

Input OutputBase Address Offset Address Data

0700

05h2000 0100 02h2000 0600 03h

GRIET/ECE 5 of 86

Page 6: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercises:

1. Write an assembly language program to add data located at offset 0200H in 2000H segment to another data byte available at 0500H in the same segment and store the result at 0700H in the same segment.

2. Write an assembly language program to add the contents of the memory location 2000H: 0500H to contents of 3000H: 0510H and store the result in 5000H: 0514H.

3. Write an assembly language program to add the immediate byte 05H to the data residing in memory location, whose address is computed using DS=2000H and offset=0600H.Store the result at 0700H.

4. Write an assembly language program for the addition of a series of 8-bit numbers.

5. Write an assembly language program to add two 8-bit BCD numbers.

GRIET/ECE 6 of 86

Page 7: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 7 of 86

Page 8: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 8 of 86

Page 9: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 3.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 9 of 86

Page 10: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 4.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 10 of 86

Page 11: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 5.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 11 of 86

Page 12: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 2. Move 5 Bytes

Program descriptionAssembly language program to move 5 bytes of data using indirect addressing.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentorg 2000h

list db 05h, 12h, 99h, 68h, 50hresult db 05h dup (0) ; duplicate 5 locations with 0’s

Data endsCode segment

Assume cs: code, ds: data

Start: mov ax, data

mov ds, ax

mov cx, 05h

mov si, offset list

mov di, offset result

Back: mov al,[si]

mov [di],al

inc si

inc di

loop back

Hlt

Code ends

End start

Explanation:→ Store 5 numbers from location 2000→ The loop statement continues executing till CX=0; decrement CX is automatic. → Use loop statements to add the 5 numbers.

Output:Base Address Offset Address Data

2000 0001 05h

GRIET/ECE 12 of 86

Page 13: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

2000 0002 12h2000 0003 99h2000 0004 68h2000 0005 50h

GRIET/ECE 13 of 86

Page 14: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercises:

1. Write an assembly language program to move 5 bytes of data using indirect addressing.

2. Write an assembly language program to store the value 99H at 5 consecutive locations.

3. Write an assembly language program to move 5 bytes of data using indexed addressing.

4. Write an assembly language program to find average of five words.

5. Write an assembly language program to combine the least significant bytes of memory locations 2041h and 2043h. Store the result in 2045h.

GRIET/ECE 14 of 86

Page 15: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 15 of 86

Page 16: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 16 of 86

Page 17: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 3.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 17 of 86

Page 18: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 4.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 18 of 86

Page 19: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 5.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 19 of 86

Page 20: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 3. Packing of three BCD numbers.

Program descriptionAn assembly language program to perform packing of three BCD numbers.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data SegmentOrg 2000h

Num1 dw 0305h, 0901h, 0102hNum2 db ?

Data endsCode segmentAssume CS: code, DS: data

Start: mov ax, datamov ds, axmov cx, 03mov si, offset num1mov di ,offset num2

Back: mov ax, [si]rol ah, 04add al, ahmov [di], alinc siinc siinc diloop backHlt

Code endsEnd start

Example:

Input OutputMemory Location Data Memory Location Data

2000 05h 2006 35h2001 03h 2007 91h2002 01h 2008 12h2003 09h2004 02h2005 01h

Exercise:1. Write an assembly language program to perform un packing of six BCD numbers.

GRIET/ECE 20 of 86

Page 21: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 21 of 86

Page 22: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 4. Convert Fahrenheit to CelsiusProgram description An assembly language program to convert Fahrenheit to Celsius. Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data SegmentOrg 2000h

faren db 91hCelsius db ?

Data endsCode segmentAssume CS: code, DS: data

Start: mov ax, datamov ds, axmov bl, farenmov cl,32sub bl, clmov al,05mul blmov bl, 09div bl mov celsius, alhlt

Code endsEnd start

Logic:

The formula for conversion from Fahrenheit to Celsius is:C = (F-32)*5/9

Exercises:

1. Write an assembly language program to convert Celsius to Fahrenheit.

2. Write an assembly language program to convert miles to kilometers.

GRIET/ECE 22 of 86

Page 23: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 23 of 86

Page 24: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 24 of 86

Page 25: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 5. Factorial of given number

Program descriptionAn assembly language program to compute factorial of given Number.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data SegmentOrg 2000h

Num1 dw 0030hfact db ?

Data endsCode segmentAssume CS: code, DS: data

Start: mov ax, datamov ds, axmov dx, 00mov ax, num1mov bx, axmov cx,num1-1

Back: dec bxmul bxloop backmov fact, dxmov [ fact+1], axHlt

Code endsEnd start

Explanation:

Factorial of 4 = 4*3*2*1

Logic:

1! =1;2! =2*1=2;3! =3*2*1=6;4! =4*3*2*1;

GRIET/ECE 25 of 86

Page 26: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 6. Finding Largest numberProgram description An assembly language program to find out the largest number.Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086Data segment

org 2000h

n1 db 94h, 99, 25h,11h,D7h

res db ?

Data ends

Code segment

Assume cs: code, ds: data

Start: mov ax, data

mov ds, ax

mov si, offset n1

mov cx,4

mov al, [si]

L1: cmp al, [si+1]

jg L2

mov al, [si+1]

L2: inc si

loop L1

mov res, al

Hlt

Code ends

End start

Result:

Input Output

GRIET/ECE 26 of 86

Page 27: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Memory Location Data Memory Location Data

2000 94h 2005 D7h

2001 99h

2002 25h

2003 11h

2004 D7h

Exercise:1. Write an assembly language program to find out the smallest number from a given

unordered array of 8-bit numbers, stored in the locations starting from a known address.

GRIET/ECE 27 of 86

Page 28: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 28 of 86

Page 29: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 7. Find the Square Root Of A Given Number.

Program descriptionAn assembly language program to find the square root of a given number.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentOrg 2000h

n1 dw 25hres dw ?

Data endsCode segmentAssume cs: code, ds: data

Start: mov ax, datamov ds, axmov bx, 1h

L1: mov ax, bxmul bxmov dx, n1cmp ax, dxjz L2inc bxloop L1

L2: mov res, bxHlt

Code endsEnd start

Result:

Input OutputMemory Location Data Memory Location Data

2000 25h 2001 05h

Exercise:

1. Write an assembly language program to find the cube root of a given number.(Assume that the number is a perfect cube)

GRIET/ECE 29 of 86

Page 30: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 30 of 86

Page 31: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 8. Next number in a Fibonacci Series.

Program description An assembly language program to generate Fibonacci series.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentorg 1000h

n1 db 5res db ?

Data endsCode segmentAssume cs: code, ds: data

Start: mov ax, datamov ds, axmov al, 1hmov bl, 1hmov si, offset resmov [si], alinc simov [si], blmov cx, n1

L1: inc simov dl, bladd bl, almov al, dlmov [si], blloop l1Hlt

Code endsEnd start

Logic:1. The Fibonacci series is 1, 1, 2, 3, 5, 8, 13…..2. Start the series with the first two elements as 1 and 1.3. Third element: 1+1=24. Fourth element: 2+1=3 5. Nth element: (N-1)th element+ (N-2)th element

Example:Input Output

Memory location1000

1001

1002

1003

1004

1005

1006

Data 01h 01h 02h 03h 05h 08h 0Dh

GRIET/ECE 31 of 86

Page 32: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 9. Arrange The Numbers In Ascending Order.

Program descriptionAn assembly language program to arrange the numbers in ascending order.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Code segmentAssume cs:code

Start: mov cx, 04hmov bx, cx

l3: mov cx, 04hmov si, 2000h

l2: mov al, [si]cmp al, [si+1]jng l1xchg al, [si+1]mov [si], al

l1: inc siloop l2dec bxcmp bx, 00hjnz l3Hlt

Code endsEnd start

Example:Input Output

Memory location2000

2001

2002

2003

2004

2000

2001

2002

2003

2004

Data 88h 54h 76h 00h 09h 00h 09h 54h 76h 88h

Exercise:

1. Write an assembly language program to arrange the given unsigned numbers in descending order.

GRIET/ECE 32 of 86

Page 33: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 33 of 86

Page 34: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 10. LCM of two given numbersProgram description An assembly language program to LCM of two given numbers.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Exercise:1. Write an assembly language program to find GCD of two given numbers

GRIET/ECE 34 of 86

Data segmentorg 2000hn1 db 25n2 db 15res db ?

Data endsCode segmentAssume: cs: code, ds: data

Start: mov ax, datamov ds, axmov ah, 00mov al, n1mov bl, n2

l1: div blcmp ah, 00jz l2mov bh, ahmov al, blmov bl, bhmov ah, 00loop l1

l2: mov al, n1mov cl, n2mul cldiv blmov res, alhlt

Code endsEnd start

Result:

Input OutputMemory Location Data Memory Location Data

2000 15h2002 75

2001 25h

Page 35: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 35 of 86

Page 36: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 11. Multiplication by shift and add method.

Program descriptionAn assembly language program to perform multiplication by shift and add method.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentorg 2000h

n1 db 25n2 db 13res dw ?

Data endsCode segmentAssume cs: code, ds: data

Start: mov ax, datamov ds, axmov ah, 00mov al, n1mov bl, n2mov bh, 00

L1: mov cl, bhror bl, 01jnc L2shl ax, cladd dx, ax

L2: inc bhcmp bh, 08loopne L1

L3: mov res, dxHlt

Code endsEnd start

Result:

Input OutputMemory Location Data Memory Location Data

2000 25h 2002 02h2001 13h 2003 BFh

GRIET/ECE 36 of 86

Page 37: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 12. Transfer string from one location to another.

Program descriptionAn assembly language program to transfer string from one location to another.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentorg 2000harr db "micro"res db ?

Data endsCode segmentAssume cs: code, ds: data

start: mov ax, datamov ds, axmov es, axmov si, offset arrmov di, offset resmov cx, 05Cldrep movsbHlt

Code endsEnd start

Output: Micro

Exercises:1. Write an assembly language program to reverse a 5-bit string using reentrant procedures.

2. Write an assembly language program to concatenate two given strings.

3. Write an assembly language program to check whether the string is palindrome or not.

4. Write an assembly language program to insert a sub string in the main string.

5. Write an assembly language program to scan for the value 66H in a given array of numbers.

GRIET/ECE 37 of 86

Page 38: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 38 of 86

Page 39: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 39 of 86

Page 40: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 3.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 40 of 86

Page 41: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 4.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 41 of 86

Page 42: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 5.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 42 of 86

Page 43: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 13. Display a string on the screenProgram description An assembly language program to display a string on the screenAuthor GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segmentorg 2000hstr1 db 0ah,"micro processors lab",0ah,"$"

Data endsCode segmentAssume cs: code, ds: data

Start: mov ax, datamov ds, axmov es, axmov ah, 09h ;Dos interrupt to display a stringmov dx, offset str1int 21hmov ah, 4chint 21h

Code endsEnd start

Result:

Output: Micro Processors Lab

Exercises:1. Write an assembly language program to receive an input string from keyboard.

2. Write an assembly language program to verify password.

GRIET/ECE 43 of 86

Page 44: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 44 of 86

Page 45: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 45 of 86

Page 46: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 14. Identification of Even Or Odd Number Using Procedures

Program descriptionAn assembly language program to find whether a give number is even or odd.

Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

Data segment

org 2000h

arr dw 24h

res db 01H dup(00h)

Data ends

Code segment

Assume cs: code, ds: data

start: mov ax,data

mov ds,ax

call evn

Hlt

Code ends

End start

Code2 segment

Assume cs: code2

Even proc far

Pushf

push ax

push dx

mov ax, arr

mov dl, 02h

div dl

test ah, 00h

GRIET/ECE 46 of 86

Page 47: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

jne L2

inc res

L2: pop dx

pop ax

Popf

Ret

Even ends

Code2 ends

Result:

Input Output

Memory LocationDat

aMemory Location

Data

2000 24h 2001 00h

GRIET/ECE 47 of 86

Page 48: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercises:

1. Write an alp to perform EX-OR operation with out using XOR instruction

2. Write an assembly language program to generate a delay of 100ms using an 8086 system that runs on 10MHZ frequency.

3. Write an assembly language program to display message on screen using macros.

GRIET/ECE 48 of 86

Page 49: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 1.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 49 of 86

Page 50: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 2.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 50 of 86

Page 51: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Exercise

Program Name 3.

Program description

AuthorDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 51 of 86

Page 52: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 15. Identification of Prime Number Or Not Using ProceduresProgram description An assembly language program to detect whether a given number

is prime or not.Author GRIET ECE DepartmentDateSoft ware Platform TASMMicroprocessor 8086

GRIET/ECE 52 of 86

Page 53: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

GRIET/ECE 53 of 86

Data segmentorg 2000hn1 db 27horg 3000hn2 db “prime$”org 4000hn3 db “not prime$”

Data endsCode segmentAssume cs: code, ds: dataStart: mov ax,data

mov ds,axmov ah,00hmov al,n1call primeHltPrime proc nearPushfpush bxpush cxpush dxmov bx,02hmov ch,00hdiv blmov cl,almov al,n1

L1: div blcmp ah,00hjnz l2jz l3

L3: mov ah, 09hMov dx, offset n3Int 21hJmp X

Page 54: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

GRIET/ECE 54 of 86

Page 55: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

L2: mov ah, 00hmov al, n1dec cxmov bx, cxloop l1mov ah, 09hmov dx, offset n2Int 21h

X: Popdxpop cxpop bxPopfret

endpCode ends

end start

Example:

Input OutputMemory Location Data Memory Location Data

2000 27h 4000 Not Prime

GRIET/ECE 55 of 86

Page 56: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

The 8051 Micro Controller interfacing Programs

GRIET/ECE 56 of 86

Page 57: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Switch and LED connections on GRIET board:

GRIET/ECE 57 of 86

0 7

SW1Key = 1

0

S8Key = 8

7

GND

J19

LED1 LED8

R11.0k?

1

R81.0k?

2

VCC5V

VCC

0

0

7

7

J20

Page 58: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 1. LEDs and SwitchesProgram description An 8051 C program for LED pattern when switches pressedAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include< REGX51.H >void main(){

P0 = 0XFF; //port 0 as inputP1 = 0xFF; //all LEDs offWhile(1){

P1 = P0;

//Data to port 1

} //end while}//end main

Explanation:→ Port0 –switches→ Port1–LEDS

Output:

LEDs glow for corresponding switch pressed.

GRIET/ECE 58 of 86

Page 59: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 2. ElevatorProgram description An 8051 C program for LED pattern as an elevatorAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include<reg51.h>void main( ){

unsigned char temp;P0 = 0xff;//P0 as inputP1 = 0xff;//all LEDs offtemp = P0;while(1){

switch(temp){

case 0xFE:P1 = 0xFE;case 0xFD:P1 = 0xFC;case 0xFB:P1 = 0XF8;case 0XF7:P1 = 0XF0;case 0xEF:P1 = 0XE0;case 0xDF:P1 = 0xC0;case 0xBF:P1 = 0x80;case other:P1 = 0x00;

}}

}

GRIET/ECE 59 of 86

Page 60: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

RS 232 Section on GRIET board:

Explanation:

Xtal = 11.0592MHz;SCON=0X50; //serial mode1,8-bit data,1 start bit, 1 stop bit, this end rxd,txd enableTMOD=0X20; //8-bit auto reload,TH1=0XFD; //9600 BAUD RATETR1=1; //START TIMER1

GRIET/ECE 60 of 86

Page 61: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 3. Serial CommunicationProgram description An 8051 C program for serial communication with 9600 baud rateAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <REGX51.H>

void uartinit( );

void uarttx(unsigned char);

unsigned char uartrx(void);

void main( )

{

unsigned char c;

uartinit( );

while(1)

{

c = uartrx( );

uarttx(c);

}//end while

} //end main

void uartinit( )

{

SCON=0x50;

TMOD=0x20;

TH1=0XFD;

TR1=1;

}

void uarttx(unsigned char c)

GRIET/ECE 61 of 86

Page 62: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

{

SBUF=c;

while(TI==0);

TI=0;

}

unsigned char uartrx(void)

{ unsigned char c;

while(RI==0);

c = SBUF;

RI=0;

return c;

}

Output:The data typed in HYPER TERMINAL is echoed back.

GRIET/ECE 62 of 86

Page 63: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

LCD Interfacing:

GRIET/ECE 63 of 86

P1^1P1^2P1^3

AT89S52

PORT0D0-D7

RSR/WEN

LCD

Data Lines

Page 64: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 4. Interfacing of LCDProgram description An 8051 C program to send message "GRIET" on to the LCDAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

GRIET/ECE 64 of 86

#include<reg51.h>void del(unsigned char);void delay(unsigned char);void lcdcmd(unsigned char);void lcddata(unsigned char);sbit rs=P1^1;sbit rw=P1^2;sbit en=P1^3;sfr lcd _data=0x80;//port0 for lcd datavoid main(){

unsigned char p,msg[]="GRIET:0";delay(0xff);Lcdcmd(0x38);Lcdcmd(0x0E);Lcdcmd(0x01);Lcdcmd(0x06);Lcdcmd(0x80);delay(0xff);for(p=0;msg[p]!='0';p++){Lcddata(msg[p]);}delay(0xff);

}//end mainvoid delay(unsigned char t ){

int i, j;for (i=0;i<t;i++)for (j=0;j<255;j++);

}

Page 65: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

void del(unsigned char t){

unsigned char i;for (i=0;i<t;i++);

}

void lcdcmd(unsigned char value){

lcd_data=value;rs=0;rw=0;en=1;del(5);en=0;

}void lcddata(unsigned char value){

lcd_data=value;rs=1;rw=0;en=1;del(5);en=0;

Output:

The data “GRIET” will be displayed on LCD

GRIET/ECE 65 of 86

Page 66: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

ADC Interfacing:

RD/WR

GRIET/ECE 66 of 86

CSJ13

D0

D3

J14

D4

D6

AIN

J12

GND

VCC

5V

RD

WR

INTR

D1

D2

D5

D7EXT

ONBRD

P2^4P2^5P2^6P2^7

AT89S52

PORT1

CSRDWRINTR

ADC0804

Data LinesD0-D7

Page 67: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 5. Interfacing of ADC0804Program description An 8051 C program for the conversion of Analog data into Digital Author GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <REGX51.H>Sbit CS=P2^4; //CSbar pin of ADC connected to GND sbit RD1 = P2^5; //port 2 for control linessbit WR1 = P2^6;sbit INTR1 =P2^7;sfr adcdata = 0x90; //port1 for adc datavoid display(unsigned char );void delay(int );void transmit(unsigned char);void main(){

unsigned char value;unsigned char i;unsigned char mesg[]="VOLTAGE ACROSS LDR:0";SCON=0X50;//to select the mode2 and receiver enabledelay(100);TMOD=0X20;//to select the timer1 in auto reload modedelay(100);TH1=0XFD;//to get the 9600 baud ratedelay(100);TR1=1;//start the timerdelay(100);for(i=0;mesg[i]!='0';i++)//repeat this loop till the last char {transmit(mesg[i]);}adcdata=0xff;//TO MAKE P1 PORT AS INPUTINTR1=1;//TO MAKE INTR PIN ( P2.7) AS INPUT PINRD1=1;//INTIAL STATUS OF RD PINwhile(1){WR1=0;//to start convertion , make wr pin low to highdelay(5);WR1=1;while(INTR1==1);//wait till convertion is overRD1=0;//get the data from adcdelay(100);value=adcdata;

GRIET/ECE 67 of 86

Page 68: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

display(value);RD1=1;delay(500);}//end while

}//end mainvoid display(unsigned cha ADC_VALUE){

unsigned char D1,D2,D3;D1 = ADC_VALUE / 100;D2 =( ADC_VALUE % 100)/10;D3 =( ADC_VALUE % 100)%10D3+=0x30;transmit(D1+48);transmit(D2+48);transmit(D3+48);

}void transmit(unsigned char z){

SBUF=z; //move the value into serial bufferwhile(TI==0);//wait for transmit one char TI=0;

}void delay(int n){

int x,y;for(x=0;x<n;x++)for(y=0;y<1000;y++);

}

Note: The analog input sources can be on board potentiometer, LDR, temperature sensor.

Output:Digital data will be displayed on Hyper Terminal.

GRIET/ECE 68 of 86

Page 69: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

DAC section on GRIET board:

DIGITAL O/P

GRIET/ECE 69 of 86

AT89S52

Port2DAC0808

INPUT DATA

D0-D7

Page 70: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 6. Interfacing of DAC0808Program description An 8051 C program for the conversion of Digital data into AnalogAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include<reg52.h>sfr digdata = 0xA0;//port 2unsigned int i,j;void main(){

unsigned char temp;temp =0;while(1){digdata = 0;for(j=0; j<255;j+=10){digdata = j;for(i=0;i<54000;i++);//Delay}//end for }//end while

}//end main

Output:Analog output is measured using Multi meter

GRIET/ECE 70 of 86

Page 71: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Relay Interfacing:

GRIET/ECE 71 of 86

RL1

12V

RL2

12V

N

P

INPUT

P1

N1

OUTPUT 1

P2

N2

OUTPUT 2

Connect +12V DC supply to input

Connect the horn to Output1

Page 72: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 7. Interfacing of RelayProgram description An 8051 C program to drive a load connected to the RelayAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include<reg51.h>sbit rel=P1^0;void delay(unsigned int );void main(){

while(1){rel=0; //offdelay(100); rel=1; //ondelay(200);}

}void delay(unsigned int t){

int i,j;for(i=0;i<t;i++)for(j=0;j<300;j++);

}

Result:The load connected to relay will get switched off and on with the given delay.

GRIET/ECE 72 of 86

Page 73: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Motor interfacing:

En=1

Op1In1 DC Motor

In2

Op2

GRIET/ECE 73 of 86

P2^0

AT89S52

P2^1 Motor controlIC

Page 74: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 8. Interfacing of DC MotorProgram description An 8051 ‘C’ program for running a DC MotorAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <REGX51.H>void delay(unsigned int);sbit in1 = P2^0;sbit in2 = P2^1;void main(){

while (1){in1=0; //Motor stoppedin2=0;delay(300);in1=1; //Motor rotates clockwisedelay(300);in1=0;in2=0;delay(300);in2=1; //Motor rotates anticlockwisedelay(300);in2=0;}

}void delay(unsigned int itime){

unsigned int i,j;for(i=0;i<itime;i++)for(j=0;j<1275;j++);

}

Result:

Motors run according to the pattern applied to it.

GRIET/ECE 74 of 86

Page 75: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 9. Interfacing of Stepper MotorProgram description An 8051 ‘C’ program for running a Stepper MotorAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52#include<reg51.h>Sbit SW=P2^7;Void main ( ){

SW=1;While(1){

If(SW==0){

P1=0x66;delay(100);P1=0xcc;delay(100);P1=0x99;delay(100);P1=0x33;delay(100);

}Else{

P1=0x66;delay(100);P1=0x33;delay(100);P1=0x99;delay(100);P1=0xcc;delay(100);

}}

}Void delay (unsigned int k){unsigned int x,y;for(x=0;x<1275;x++)for(y=0;y<k;y++);}

GRIET/ECE 75 of 86

Page 76: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Result:

If switch is set the stepper motor moves clock wise else counterclockwise.

GRIET/ECE 76 of 86

Page 77: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Keypad interfacing:

GRIET/ECE 77 of 86

P3.0, .1, .2, .3

ROWS

KEY PAD

COLOUMNS

PORT3PORT0

AT89S52

PORT1

PORT2

LCD DATA

LCD

RS, RW, ENP2.0,.1, .2,.3,

D0-D7

P1.1, 2, 3

Page 78: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 10. Interfacing of Keypad and LCD

Program descriptionAn 8051 C program for assigning values to keypad and display on LCD

Author GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <reg51.h>#include<stdio.h>

void del(unsigned char);void delay(unsigned char);void lcdcmd(unsigned char);void lcddata(unsigned char);unsigned char key_pad(void);unsigned char keypad[4][4]={'7','8','9','A',’4’,'5','6','B','1','2','3','C',’c’,’0’,’#’,’D’};sfr colms=0xa0;//p2sfr rows=0xb0;//p3unsigned char col,row,key;sfr lcd_data=0x80;//p0sbit rs=P1^1;sbit rw=P1^2;sbit en=P1^3;

void main( ){

unsigned char p,a,msg[]="keypressed:0";delay(0xff);lcdcmd(0x38);lcdcmd(0x0E);lcdcmd(0x01);lcdcmd(0x06);lcdcmd(0x80);delay(0xff);for(p=0;msg[p]!='0';p++){lcddata(msg[p]);}

GRIET/ECE 78 of 86

Page 79: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

delay(0xff);while(1){

a=key_pad();lcddata(a);

} //end while} //end main

void delay(unsigned char t){

int i,j;for(i=0;i<t;i++)for(j=0;j<255;j++);

}void del(unsigned char t){

unsigned char i;for(i=0;i<t;i++);

}void lcdcmd(unsigned char value){

lcd_data=value;rs=0;rw=0;en=1;del(5);en=0;return;

}void lcddata(unsigned char value){

lcd_data=value;rs=1;rw=0;en=1;del(5);en=0;return;

}

GRIET/ECE 79 of 86

Page 80: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

unsigned char key_pad(void){ //1

while(1){ //2

colms=0xff;do{ //3row=0x0;col=colms;col&=0x07;} //3endswhile(col!=0x07);do{ //4do

{ //5del(5);rows=0x0;col=colms;col&=0x07;} //5endswhile(col==0x07);del(20);rows=0x0;col=colms;col&=0x07;

}while(col==0x07); //4endsrows=0xfe;col=colms;col&=0x07;if(col!=0x07){ //6row=0;break;}//6endsrows=0xfd;col=colms;col&=0x07;

GRIET/ECE 80 of 86

Page 81: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

if(col!=0x07){ //7row=1;break;}//7endsrows=0xfb;col=colms;col&=0x07;if(col!=0x07){ //8row=2;break;}//8endsrows=0xf7;col=colms;col&=0x07;if(col!=0x07){ //9row=3;break;}//9ends

}//2endsif(colms==0xfe)key=keypad[row][0];else if(colms==0xfd)key=keypad[row][1];elseif(colms==0xfb)key=keypad[row][2];elsekey=keypad[row][3];return(key);

}//1ends

Result:

The value assigned to the key is displayed on LCD when it is pressed.

GRIET/ECE 81 of 86

Page 82: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 11. Interfacing of Real time clock

Program description An 8051 C program for setting Date, Time and DayAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52#include<reg8252.h>#include<stdio.h>#include<ds1307.h>#include<serial.h>#include<delay.h>unsigned char RTC_ARR[7]; unsigned char p;void main(void){

unsigned char temp;InitSerial();-//Initialize serial porttemp = ReadBYTE(0x00);temp &= 0x7F;// enable oscillator (bit 7=0)temp = ReadBYTE(0x08);if(temp != 0xAA){

WriteBYTE(0x08,0xAA);RTC_ARR[0] = 0x00;-//sec =00RTC_ARR[1] = 0x10;-//minute = 59RTC_ARR[2] = 0x12;-//hour = 05 ,24-hour mode(bit 6=0)RTC_ARR[3] = 0x03;-//Day = 1 for sunday; now tuesdayRTC_ARR[4] = 0x24;-//Date = 24RTC_ARR[5] = 0x11;-//month = novemberRTC_ARR[6] = 0x09;-//year = 09 or 2009WriteRTC(&RTC_ARR[0]);-//Set RTC

}-//end ifwhile(1){

ReadRTC(&RTC_ARR[0]);putchar(0x0C);-//clear Hyper terminalprintf("Day: %s\r\n",Int2Day(RTC_ARR[3]));printf("Time%02bX:%02bX:%02bX\r\n",RTC_ARR[2],RTC_ARR[1],RTC_ARR[0]);printf("Date:%02bX%s20%02bX\r\n",RTC_ARR[4],Int2Month(RTC_ARR[5]),R

TC_ARR[6]);DelayMs(985);-//delay about 1 second

}//end while}end main

GRIET/ECE 82 of 86

Page 83: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

//delay#ifndef _DELAY_H#define _DELAY_Hextern void DelayMs(unsigned int count);extern void DelayUs(int us);#endif//DS1307 driver#ifndef _DS1307_H#define _DS1307_Hextern unsigned char ReadBYTE(unsigned char Addr);extern void WriteBYTE(unsigned char Addr,unsigned char Data);extern void ReadRTC(unsigned char * buff);extern void WriteRTC(unsigned char * buff);extern char * Int2Day(unsigned char day);extern char * Int2Month(unsigned char month);#endif//Serial port driver#ifndef _SERIAL_H#define _SERIAL_Hextern void InitSerial(void);#endif

Result:

Clock can be set or reset.

GRIET/ECE 83 of 86

Page 84: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 12. Interfacing of EEPROM

Program description An 8051 C program for data TX and RX with EEPROM Author GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <reg51.h>#include<intrins.h>typedef unsigned char bit_8;typedef unsigned int bit_16;unsigned char last_rx_byte;unsigned char recv_byte();sbit scl= P0^0;sbit sda = P0^1;sfr lcd_port = 0x90;//port1sbit rs1 = P2^0;sbit rw1 = P2^1;sbit en1 = P2^2;bit_8 bdata a;sbit LSB=a ^ 0;sbit MSB=a ^ 7;unsigned char command[] = {0x38,0x0E,0x01,0x06,0x80,0};unsigned char d,t;unsigned int r;void start();void stop();void send_adr(bit_8 x);void wrt_byte(unsigned char value);void Delay(unsigned int itime);void lcdcmd(unsigned char value);void LCD_INI(void);void recv_data();

GRIET/ECE 84 of 86

Page 85: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

int main(void){

LCD_INI();wrt_byte('a');//fill first 20 bytes with 'A'/*start();send_adr(0xa0);send_adr(0);send_adr('A');stop();*/for(r=0;r<55000;r++);start();send_adr(0xa0);send_adr(0);start();send_adr(0xa1);t=recv_byte();stop();wrt_byte('a');wrt_byte(t); for(;;);

}//end mainvoid send_adr(bit_8 Data){

unsigned char i;for (i=0;i<8;i++){

sda = (Data & 0x80) ? 1:0;scl=1;scl=0;Data<<=1;

}scl = 1; _nop_();_nop_();scl = 0;

}

GRIET/ECE 85 of 86

Page 86: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

unsigned char recv_byte(){

unsigned char i,Data=0;sda = 1;for(i=0;i<8;i++){

scl = 1;Data<<= 1;Data = (Data | sda);scl = 0;_nop_();

}sda = 1; // Send NO ACK_nop_();_nop_();scl = 1;_nop_();_nop_();scl = 0;return Data;

}void Start(void){

sda = 1;scl = 1;_nop_();_nop_();sda = 0;_nop_();_nop_();scl = 0;_nop_();_nop_();

}//stop I2Cvoid Stop(void){

sda = 0;_nop_();_nop_();scl= 1;_nop_();_nop_();sda = 1;

}

GRIET/ECE 86 of 86

Page 87: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

void LCD_INI(void){

unsigned char i;for(i=0;command[i]!=0;i++){lcdcmd(command[i]);}

}void lcdcmd(unsigned char value){

lcd_port = value;rs1 = 0;rw1 = 0;en1 = 1;Delay(5);en1 = 0;return;

}void Delay(unsigned int itime){

unsigned int i,j;for(i=0;i<itime;i++)for(j=0;j<1275;j++);

}void wrt_byte(unsigned char value){

//busy();lcd_port=value;rs1=1;rw1=0;en1=1;Delay(5);en1=0;

}

Result:

Data can be stored and retrieved from EEPROM.

GRIET/ECE 87 of 86

Page 88: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 13. Interfacing of Temperature sensor

Program descriptionAn 8051 C program for reading temp form ADC0804,convert it to decimal

Author GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include <REGX51.H>Sbit CS=P2^4; //CSbar pin of ADC connected to GND sbit RD1 = P2^5; //port 2 for control linessbit WR1 = P2^6;sbit INTR1 =P2^7;sfr adcdata = 0x90;//port1 for adc datasfr lcddata = 0x80; //port2 for lcd datasbit rs = p1^1;sbit rw = p1^2;sbit en =p1^3;void display(unsigned char );void lcdcmd(unsigned char)void lcddata(unsigned char)void delay(int );void main(){

unsigned char value;unsigned char i;unsigned char mesg[]="Temparature:0";lcdcmd(0x38);delay(100);lcdcmd(0x0e);delay(100);lcdcmd(0x01);delay(100);lcdcmd(0x06);for(i=0;mesg[i]!='0';i++)//repeat this loop till the last char {lcddata(mesg[i]);}adcdata=0xff;//TO MAKE P1 PORT AS INPUTINTR1=1;//TO MAKE INTR PIN ( P2.7) AS INPUT PINRD1=1;//INTIAL STATUS OF RD PIN

GRIET/ECE 88 of 86

Page 89: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

while(1){WR1=0;//to start convertion , make wr pin low to highdelay(5);WR1=1;while(INTR1==1);//wait till convertion is overRD1=0;//get the data from adcdelay(100);value=adcdata;display(value);RD1=1;delay(500);}//end while

}//end mainvoid display(unsigned cha ADC_VALUE){

unsigned char D1,D2,D3;D1 = ADC_VALUE / 100;D2 =( ADC_VALUE % 100)/10;D3 =( ADC_VALUE % 100)%10D3+=0x30;lcddata(D1+48);lcddata (D2+48);lcddata (D3+48);

}void lcdcmd(unsigned char value){

lcddata = value;rs = 0;rw = 0;en = 1;delay(100);en = 0;

}void lcddata(unsigned char value){

lcddata = value;rs = 1;rw = 0;en = 1;delay(100);en = 0;

}

GRIET/ECE 89 of 86

Page 90: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

svoid delay(int n){

int x,y;for(x=0;x<n;x++)for(y=0;y<1000;y++);

}

Result:

The temperature read by ADC is converted into decimal and displayed on LCDs.

GRIET/ECE 90 of 86

Page 91: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 14. Interfacing of Buzzer

Program description An 8051 C program for buzzer to on and offAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include<reg51.h>Void delay(unsigned int);sbit on = P2^0;// buzzer to P2.0void main( ){

while(1){

0n = 1;//buzzer ondelay(ff);on = 0;/buzzer off

}}Void delay(unsigned int del){

unsigned int I;for(i=0;i<del;i++);

}

Result:

The buzzer sounds and stops after given delay.

GRIET/ECE 91 of 86

Page 92: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Program Name 15. Interfacing of Magnetic sensor

Program description An 8051 C program for magnetic switching operationAuthor GRIET ECE DepartmentDateHard ware GRIET Dual boardSoft ware Platform KEILMicroprocessor AT89S52

#include<reg51.h>Sbit in = P260;//inputSbit LED = P2^1;//outputVoid main( ){LED = 0;In = 1;//P2^0 as inputWhile(1)

[LED = 1;}

}

Result:

The LED glows when magnet is brought near to REED switch.

GRIET/ECE 92 of 86

Page 93: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Appendix

GRIET/ECE 93 of 86

Page 94: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Appendix A

String operations (using string instructions)

1. REP/ REPE/ REPZ/ REPNE /REPNZREP is a prefix which is written before one of the string instructions. These instructions repeat until specified condition exists.

Instructions Code Condition for ExitREPREPE/ REPZREPNE/ REPNZ

CX=0CX=0 OR ZF=0CX=0 OR ZF=1

2. MOV/ MOVSB/ MOVSWMove byte or words from one string to another .The source and destination addresses are stored in SI and DI register.CLD= Clear direction Flag to auto increment SI and DI.If Direction Flag=0; Index register incrementIf Direction Flag=1; Index register decrement

3. CMPS/CMPSB/CMPSWThe CMPS instruction compares 2 strings of data bytes store at 2 different locations. The length of the string is loaded in CX.

4. SCAS/ SCASB/ SCASWSCAS scans a string for a byte in AL or a word in AX. The instruction affects the flag, but it does not change either the operand in AL or the operand in the string.

5. LODS/LODSB/LODSWThis instruction copies a bytes from a string location pointed to by SI to AL, or a words from a string location pointed to by SI to AX. LODS does not affect any flags. LODSB copies bytes and LODSW copies a word

GRIET/ECE 94 of 86

Page 95: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Appendix B

DOS Interrupts

Useful DOS interrupt to input information from the keyboard and display it on the screen.INT 21hFunction 01 – inputting a single character, with an echoAH = 01: function numberAfter the interrupt AL = ASCII code of the input and is echoed to the monitor

Function 02 – setting the cursor to a specific locationAH = 06; function numberDH = row; cursorDL = column; position

Function 07 – inputting a single character from the keyboard without an echoAH = 07: function numberWaits for a single character to be entered and provides it in AL

Function 09h - Output character string.IN: DS: DX --> ASCII$ string to print.OUT: stringFunction 4Ch - Terminate with return code. IN: AL Program returns code.OUT: Nothing.

INT 10h / AH = 05h - select active video page.Input: AL = new page number (0….7).The activated page is displayed.

INT16 – Keyboard Programming

Function 01 – check for a key press without waiting for the userAH = 01Upon execution ZF = 0 if there is a key pressedFunction 00 – keyboard readAH = 00Upon execution AL = ASCII character of the pressed keyNote this function must follow function 01

GRIET/ECE 95 of 86

Page 96: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Appendix C

8086 Instruction Set Summary

Data Transfer Instructions

MOV Move byte or word to register or memory

IN, OUT Input byte or word from port, output word to port

LEA Load effective address

LDS, LES Load pointer using data segment, extra segment

PUSH, POP Push word onto stack, pop word off stack

XCHG Exchange byte or word

XLAT Translate byte using look-up table

Logical Instructions

NOT Logical NOT of byte or word (one's complement)

AND Logical AND of byte or word

OR Logical OR of byte or word

XOR Logical exclusive-OR of byte or word

TEST Test byte or word (AND without storing)

Shift and Rotate Instructions

SHL, SHR Logical shift left, right byte or word? by 1 or CL

SAL, SAR Arithmetic shift left, right byte or word? by 1 or CL

ROL, ROR Rotate left, right byte or word? by 1 or CL

RCL, RCR Rotate left, right through carry byte or word? by 1 or CL

Arithmetic Instructions

ADD, SUB Add, subtract byte or word

ADC, SBB Add, subtract byte or word and carry (borrow)

INC, DEC Increment, decrement byte or word

NEG Negate byte or word (two's complement)

CMP Compare byte or word (subtract without storing)

MUL, DIV Multiply, divide byte or word (unsigned)

IMUL, IDIV Integer multiply, divide byte or word (signed)

CBW, CWD Convert byte to word, word to double word (useful before multiply/divide)

GRIET/ECE 96 of 86

Page 97: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Adjustments after arithmetic operations

AAA, AAS, AAM,

AAD

ASCII adjust for addition, subtraction, multiplication, division (ASCII

codes 30-39)

DAA, DAS Decimal adjust for addition, subtraction (binary coded decimal numbers)

Transfer Instructions

JMP Unconditional jump (short ?127/8, near ?32K, far between segments)

Conditional jumps:

JA (JNBE) Jump if above (not below or equal)? +127, -128 range only

JAE (JNB) Jump if above or equal(not below)? +127, -128 range only

JB (JNAE) Jump if below (not above or equal)? +127, -128 range only

JBE (JNA) Jump if below or equal (not above)? +127, -128 range only

JE (JZ) Jump if equal (zero)? +127, -128 range only

JG (JNLE) Jump if greater (not less or equal)? +127, -128 range only

JGE (JNL) Jump if greater or equal (not less)? +127, -128 range only

JL (JNGE) Jump if less (not greater nor equal)? +127, -128 range only

JLE (JNG) Jump if less or equal (not greater)? +127, -128 range only

JC, JNC Jump if carry set, carry not set? +127, -128 range only

JO, JNO Jump if overflow, no overflow? +127, -128 range only

JS, JNS Jump if sign, no sign? +127, -128 range only

JNP (JPO) Jump if no parity (parity odd)? +127, -128 range only

JP (JPE) Jump if parity (parity even)? +127, -128 range only

Loop control:

LOOP Loop unconditional, count in CX, short jump to target address

LOOPE (LOOPZ) Loop if equal (zero), count in CX, short jump to target address

LOOPNE (LOOPNZ) Loop if not equal (not zero), count in CX, short jump to target address

JCXZ Jump if CX equals zero (used to skip code in loop)

GRIET/ECE 97 of 86

Page 98: MPI Laboratory Manual

Microprocessors and Interfacing Lab______________________________________________________

Subroutine and Interrupt Instructions

CALL, RET Call, return from procedure (inside or outside current segment)

INT, INTO Software interrupt, interrupt if overflow

IRET Return from interrupt

String Instructions

2 of 3 9/4/01 5:04 PM

8086 Instruction Set Summary file:///D|/notes/8086inst.html

MOVS Move byte or word string

MOVSB, MOVSW Move byte, word string

CMPS Compare byte or word string

SCAS Scan byte or word string (comparing to A or AX)

LODS, STOS Load, store byte or word string to AL or AX

Repeat instructions (placed in front of other string operations):

REP Repeat

REPE, REPZ Repeat while equal, zero

REPNE, REPNZ Repeat while not equal (zero)

Processor Control Instructions

Flag manipulation:

STC, CLC, CMC Set, clear, complement carry flag

STD, CLD Set, clear direction flag

STI, CLI Set, clear interrupt enable flag

LAHF, SAHF Load AH from flags, store AH into flags

PUSHF, POPF Push flags onto stack, pop flags off stack

Coprocessor, multiprocessor interface:

ESC Escape to external processor interface

LOCK Lock bus during next instruction

Inactive states:

NOP No operation

WAIT Wait for TEST pin activity

HLT Halt processor

GRIET/ECE 98 of 86