Вы находитесь на странице: 1из 99

Microprocessors and Interfacing Lab

______________________________________________________

Contents
The 8086 Microprocessor Programs.....................................................5
TASM Editor............................................................................................7
1. Addition of Numbers............................................................................9
2. Move 5 Bytes......................................................................................17
3. Packing of three BCD numbers..........................................................25
4. Convert Fahrenheit to Celsius.............................................................29
5. Factorial of given number...................................................................33
6. Finding Largest number......................................................................34
7. Find the Square Root Of A Given Number........................................37
8. Next number in a Fibonacci Series.....................................................40
9. Arrange The Numbers In Ascending Order........................................41
10. LCM of two given numbers..............................................................44
11. Multiplication by shift and add method............................................48
12. Transfer string from one location to another....................................49
13. Display a string on the screen...........................................................56
14. Identification of Even Or Odd Number Using Procedures...............60
15. Identification of Prime Number Or Not Using Procedures..............66

The 8051 Micro Controller interfacing Programs.............................69


1. LEDs and Switches.............................................................................71
2. Elevator...............................................................................................72
3. Serial Communication........................................................................74
4. Interfacing of LCD..............................................................................76
5. Interfacing of ADC0804.....................................................................80
6. Interfacing of DAC0808.....................................................................84
7. Interfacing of Relay............................................................................86
8. Interfacing of DC Motor.....................................................................88
9. Interfacing of Stepper Motor..............................................................89
10. Interfacing of Keypad and LCD.......................................................92
11. Interfacing of Real time clock..........................................................97
12. Interfacing of EEPROM...................................................................99
13. Interfacing of Temperature sensor..................................................103
14. Interfacing of Buzzer......................................................................107
15. Interfacing of Magnetic sensor.......................................................108

Appendix A..........................................................................................111
GRIET/ECE 2 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Appendix B..........................................................................................112
Appendix C..........................................................................................113

GRIET/ECE 3 of 86
Microprocessors and Interfacing Lab
______________________________________________________

The 8086 Microprocessor Programs

GRIET/ECE 4 of 86
Microprocessors and Interfacing Lab
______________________________________________________

TASM Editor
How to go to TASM editor:

1. Click START - starts window


2. Click RUN - runs window
3. Type cmd - to enter command prompt
4. Type cd.. - to enter drive
5. Type cd tasm- opens tasm
6. Type cd bin – opens bin
7. 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 program


2. Type tasm file name .asm -specifying assembly level language code
3. Type tlink file name .obj –to link to 8086 µP and generate object file
4. Type debug filename.exe - to generate executable file
5. Type t –step by step compilation
6. Type d ds: -displays data in data segment
7. Type e ds: - to enter the data
8. Type go -to compile total program
9. Type t no. of instructions -to compile required number of instructions

GRIET/ECE 5 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 1. Addition of Numbers


Program description Assembly language program to add data located at offset
Address
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Code segment
Assume cs: code
Start: mov ax, 2000h ;initializes DS register with 2000;
mov ds, ax
mov 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 SI
Hlt ;end of program
Code ends
End start

Example:

Input Output
Base Address Offset Address Data
2000 0100 02h 0700 05h
2000 0600 03h

GRIET/ECE 6 of 86
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 7 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 8 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 9 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 3.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 10 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 4.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 11 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 5.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 12 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 2. Move 5 Bytes


Assembly language program to move 5 bytes of data using indirect
Program description
addressing.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
org 2000h
list db 05h, 12h, 99h, 68h, 50h
result db 05h dup (0) ; duplicate 5 locations with 0’s
Data ends
Code 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 13 of 86
Microprocessors and Interfacing Lab
______________________________________________________

2000 0002 12h


2000 0003 99h
2000 0004 68h
2000 0005 50h

GRIET/ECE 14 of 86
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 15 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 16 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 17 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 3.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 18 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 4.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 19 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 5.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 20 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 3. Packing of three BCD numbers.


An assembly language program to perform packing of three BCD
Program description
numbers.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data Segment
Org 2000h
Num1 dw 0305h, 0901h, 0102h
Num2 db ?
Data ends
Code segment
Assume CS: code, DS: data
Start: mov ax, data
mov ds, ax
mov cx, 03
mov si, offset num1
mov di ,offset num2
Back: mov ax, [si]
rol ah, 04
add al, ah
mov [di], al
inc si
inc si
inc di
loop back
Hlt
Code ends
End start

Example:

Input Output
Memory Location Data Memory Location Data
2000 05h 2006 35h
2001 03h 2007 91h
2002 01h 2008 12h
2003 09h
2004 02h
2005 01h
Exercise:
1. Write an assembly language program to perform un packing of six BCD numbers.

GRIET/ECE 21 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 22 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 4. Convert Fahrenheit to Celsius


Program description An assembly language program to convert Fahrenheit to Celsius.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data Segment
Org 2000h
faren db 91h
Celsius db ?
Data ends
Code segment
Assume CS: code, DS: data
Start: mov ax, data
mov ds, ax
mov bl, faren
mov cl,32
sub bl, cl
mov al,05
mul bl
mov bl, 09
div bl
mov celsius, al
hlt
Code ends
End 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 23 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 24 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 25 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 5. Factorial of given number


An assembly language program to compute factorial of given
Program description
Number.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data Segment
Org 2000h
Num1 dw 0030h
fact db ?
Data ends
Code segment
Assume CS: code, DS: data
Start: mov ax, data
mov ds, ax
mov dx, 00
mov ax, num1
mov bx, ax
mov cx,num1-1
Back: dec bx
mul bx
loop back
mov fact, dx
mov [ fact+1], ax
Hlt

Code ends
End 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 26 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 6. Finding Largest number


Program description An assembly language program to find out the largest number.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086
Data 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 27 of 86
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 28 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 29 of 86
Microprocessors and Interfacing Lab
______________________________________________________

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


An assembly language program to find the square root of a given
Program description
number.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
Org 2000h
n1 dw 25h
res dw ?
Data ends
Code segment
Assume cs: code, ds: data
Start: mov ax, data
mov ds, ax
mov bx, 1h
L1: mov ax, bx
mul bx
mov dx, n1
cmp ax, dx
jz L2
inc bx
loop L1
L2: mov res, bx
Hlt
Code ends
End start

Result:

Input Output
Memory 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 30 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 31 of 86
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 Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
org 1000h
n1 db 5
res db ?
Data ends
Code segment
Assume cs: code, ds: data
Start: mov ax, data
mov ds, ax
mov al, 1h
mov bl, 1h
mov si, offset res
mov [si], al
inc si
mov [si], bl
mov cx, n1
L1: inc si
mov dl, bl
add bl, al
mov al, dl
mov [si], bl
loop l1
Hlt
Code ends
End 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=2
4. Fourth element: 2+1=3
5. Nth element: (N-1)th element+ (N-2)th element
Example:
Input Output
Memory location 1000 1001 1002 1003 1004 1005 1006
Data 01h 01h 02h 03h 05h 08h 0Dh

GRIET/ECE 32 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 9. Arrange The Numbers In Ascending Order.


An assembly language program to arrange the numbers in
Program description
ascending order.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Code segment
Assume cs:code
Start: mov cx, 04h
mov bx, cx
l3: mov cx, 04h
mov si, 2000h
l2: mov al, [si]
cmp al, [si+1]
jng l1
xchg al, [si+1]
mov [si], al
l1: inc si
loop l2
dec bx
cmp bx, 00h
jnz l3
Hlt
Code ends
End start

Example:
Input Output
Memory location 2000 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 33 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 34 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program
Result: Name 10. LCM of two given numbers
Program description An assembly language program to LCM of two given numbers.
Author Input GRIET ECEOutput
Department
Memory Location Data
Date Memory Location Data
2000
Soft ware Platform 15h TASM 2002 75
2001
Microprocessor 25h 8086

Data segment
org 2000h
n1 db 25
n2 db 15
res db ?
Data ends
Code segment
Assume: cs: code, ds: data
Start: mov ax, data
mov ds, ax
mov ah, 00
mov al, n1
mov bl, n2
l1: div bl
cmp ah, 00
jz l2
mov bh, ah
mov al, bl
mov bl, bh
mov ah, 00
loop l1
l2: mov al, n1
mov cl, n2
mul cl
div bl
mov res, al
hlt
Code ends
End start

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

GRIET/ECE 35 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 36 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 11. Multiplication by shift and add method.


An assembly language program to perform multiplication by shift
Program description
and add method.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
org 2000h
n1 db 25
n2 db 13
res dw ?
Data ends
Code segment
Assume cs: code, ds: data
Start: mov ax, data
mov ds, ax
mov ah, 00
mov al, n1
mov bl, n2
mov bh, 00
L1: mov cl, bh
ror bl, 01
jnc L2
shl ax, cl
add dx, ax
L2: inc bh
cmp bh, 08
loopne L1
L3: mov res, dx
Hlt
Code ends
End start

Result:

Input Output
Memory Location Data Memory Location Data
2000 25h 2002 02h
2001 13h 2003 BFh

GRIET/ECE 37 of 86
Microprocessors and Interfacing Lab
______________________________________________________

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


An assembly language program to transfer string from one location
Program description
to another.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
org 2000h
arr db "micro"
res db ?
Data ends
Code segment
Assume cs: code, ds: data
start: mov ax, data
mov ds, ax
mov es, ax
mov si, offset arr
mov di, offset res
mov cx, 05
Cld
rep movsb
Hlt
Code ends
End 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 38 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 39 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 40 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 3.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 41 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 4.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 42 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 5.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 43 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 13. Display a string on the screen


Program description An assembly language program to display a string on the screen
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

Data segment
org 2000h
str1 db 0ah,"micro processors lab",0ah,"$"
Data ends
Code segment
Assume cs: code, ds: data
Start: mov ax, data
mov ds, ax
mov es, ax
mov ah, 09h ;Dos interrupt to display a string
mov dx, offset str1
int 21h
mov ah, 4ch
int 21h
Code ends
End 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 44 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 45 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 46 of 86
Microprocessors and Interfacing Lab
______________________________________________________

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


An assembly language program to find whether a give number is
Program description
even or odd.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 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 47 of 86
Microprocessors and Interfacing Lab
______________________________________________________

jne L2
inc res
L2: pop dx
pop ax
Popf
Ret
Even ends
Code2 ends
Result:

Input Output
Dat Dat
Memory Location Memory Location
a a
2000 24h 2001 00h

GRIET/ECE 48 of 86
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 49 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 1.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 50 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 2.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 51 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Exercise
Program Name 3.

Program description
Author
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 52 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 15. Identification of Prime Number Or Not Using Procedures


Program description An assembly language program to detect whether a given number
is prime or not.
Author GRIET ECE Department
Date
Soft ware Platform TASM
Microprocessor 8086

GRIET/ECE 53 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Data segment
org 2000h
n1 db 27h
org 3000h
n2 db “prime$”
org 4000h
n3 db “not prime$”
Data ends
Code segment
Assume cs: code, ds: data
Start: mov ax,data
mov ds,ax
mov ah,00h
mov al,n1
call prime
Hlt
Prime proc near
Pushf
push bx
push cx
push dx
mov bx,02h
mov ch,00h
div bl
mov cl,al
mov al,n1
L1: div bl
cmp ah,00h
jnz l2
jz l3
L3: mov ah, 09h
GRIET/ECE Mov dx, offset n3 54 of 86
Int 21h
Jmp X
Microprocessors and Interfacing Lab
______________________________________________________

GRIET/ECE 55 of 86
Microprocessors and Interfacing Lab
______________________________________________________

L2: mov ah, 00h


mov al, n1
dec cx
mov bx, cx
loop l1
mov ah, 09h
mov dx, offset n2
Int 21h
X: Popdx
pop cx
pop bx
Popf
ret
endp
Code ends
end start

Example:

Input Output
Memory Location Data Memory Location Data
2000 27h 4000 Not Prime

GRIET/ECE 56 of 86
Microprocessors and Interfacing Lab
______________________________________________________

The 8051 Micro Controller interfacing


Programs

GRIET/ECE 57 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Switch and LED connections on GRIET board:

VCC
5V
VCC
0 7
J19
R1 R8
1.0k 1.0k

0 7 1 2

SW1 S8 LED1 LED8


Key = 1 Key = 8

0 7

GND
0 7
J20

GRIET/ECE 58 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 1. LEDs and Switches


Program description An 8051 C program for LED pattern when switches pressed
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include< REGX51.H >


void main()
{
P0 = 0XFF; //port 0 as input
P1 = 0xFF; //all LEDs off
While(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 59 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 2. Elevator


Program description An 8051 C program for LED pattern as an elevator
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include<reg51.h>
void main( )
{
unsigned char temp;
P0 = 0xff;//P0 as input
P1 = 0xff;//all LEDs off
temp = 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 60 of 86
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 enable
TMOD=0X20; //8-bit auto reload,
TH1=0XFD; //9600 BAUD RATE
TR1=1; //START TIMER1

GRIET/ECE 61 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 3. Serial Communication


Program description An 8051 C program for serial communication with 9600 baud rate
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor 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 62 of 86
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 63 of 86
Microprocessors and Interfacing Lab
______________________________________________________

LCD Interfacing:

P1^1 RS
P1^2 R/W
P1^3 EN

AT89S52 LCD

PORT0 D0-D7 Data Lines

GRIET/ECE 64 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 4. Interfacing of LCD


Program description An 8051 C program to send message "GRIET" on to the LCD
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#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 data
void 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 main
void delay(unsigned char t )
{
int i, j;
for (i=0;i<t;i++)
for (j=0;j<255;j++);
}

GRIET/ECE 65 of 86
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 66 of 86
Microprocessors and Interfacing Lab
______________________________________________________

ADC Interfacing:

P2^4 CS
P2^5 RD
P2^6 WR
P2^7 INTR

AT89S52
ADC0804

PORT1 D0-D7
Data Lines

J13 J14
CS D0

RD D1

WR D2

INTR D3

D4

D5

D6
J12
EXT
D7
AIN
VCC
ONBRD 5V

GND

RD/WR

GRIET/ECE 67 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 5. Interfacing of ADC0804


Program description An 8051 C program for the conversion of Analog data into Digital
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include <REGX51.H>
Sbit CS=P2^4; //CSbar pin of ADC connected to GND
sbit RD1 = P2^5; //port 2 for control lines
sbit WR1 = P2^6;
sbit INTR1 =P2^7;
sfr adcdata = 0x90; //port1 for adc data
void 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 enable
delay(100);
TMOD=0X20;//to select the timer1 in auto reload mode
delay(100);
TH1=0XFD;//to get the 9600 baud rate
delay(100);
TR1=1;//start the timer
delay(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 INPUT
INTR1=1;//TO MAKE INTR PIN ( P2.7) AS INPUT
PIN
RD1=1;//INTIAL STATUS OF RD PIN
while(1)
{
WR1=0;//to start convertion , make wr pin low to high
delay(5);
WR1=1;
while(INTR1==1);//wait till convertion is over
RD1=0;//get the data from adc
delay(100);
value=adcdata;

GRIET/ECE 68 of 86
Microprocessors and Interfacing Lab
______________________________________________________

display(value);
RD1=1;
delay(500);
}//end while
}//end main
void display(unsigned cha ADC_VALUE)
{
unsigned char D1,D2,D3;
D1 = ADC_VALUE / 100;
D2 =( ADC_VALUE % 100)/10;
D3 =( ADC_VALUE % 100)%10
D3+=0x30;
transmit(D1+48);
transmit(D2+48);
transmit(D3+48);
}
void transmit(unsigned char z)
{
SBUF=z; //move the value into serial buffer
while(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 69 of 86
Microprocessors and Interfacing Lab
______________________________________________________

DAC section on GRIET board:

DIGITAL O/P
AT89S52 DAC0808

Port2 D0-D7 INPUT DATA

GRIET/ECE 70 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 6. Interfacing of DAC0808


Program description An 8051 C program for the conversion of Digital data into Analog
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include<reg52.h>
sfr digdata = 0xA0;//port 2
unsigned 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 71 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Relay Interfacing:
Connect +12V DC supply to input

Connect the horn to Output1

12V OUTPUT 1 P1

RL1 N1

N
INPUT
P

12V
OUTPUT 2 P2
RL2
N2

GRIET/ECE 72 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 7. Interfacing of Relay


Program description An 8051 C program to drive a load connected to the Relay
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include<reg51.h>
sbit rel=P1^0;
void delay(unsigned int );
void main()
{
while(1)
{
rel=0; //off
delay(100);
rel=1; //on
delay(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 73 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Motor interfacing:

En=1

Op1
In1 DC Motor
P2^0

Motor control
AT89S52 In2 IC

P2^1
Op2

GRIET/ECE 74 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 8. Interfacing of DC Motor


Program description An 8051 ‘C’ program for running a DC Motor
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include <REGX51.H>
void delay(unsigned int);
sbit in1 = P2^0;
sbit in2 = P2^1;
void main()
{
while (1)
{
in1=0; //Motor stopped
in2=0;
delay(300);
in1=1; //Motor rotates clockwise
delay(300);
in1=0;
in2=0;
delay(300);
in2=1; //Motor rotates anticlockwise
delay(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 75 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 9. Interfacing of Stepper Motor


Program description An 8051 ‘C’ program for running a Stepper Motor
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor 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 76 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Result:

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

GRIET/ECE 77 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Keypad interfacing:

PORT3
ROWS P3.0, .1, .2, .3
PORT0 D0-D7
LCD DATA

AT89S52
KEY PAD
LCD

PORT1
P1.1, 2, 3 RS, RW, EN

COLOUMNS
P2.0,.1, .2,.3, PORT2

GRIET/ECE 78 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 10. Interfacing of Keypad and LCD


An 8051 C program for assigning values to keypad and display on
Program description
LCD
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor 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;//p2
sfr rows=0xb0;//p3
unsigned char col,row,key;
sfr lcd_data=0x80;//p0
sbit 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 79 of 86
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 80 of 86
Microprocessors and Interfacing Lab
______________________________________________________

unsigned char key_pad(void)


{ //1
while(1)
{ //2
colms=0xff;
do
{ //3
row=0x0;
col=colms;
col&=0x07;
} //3ends
while(col!=0x07);
do
{ //4
do
{ //5
del(5);
rows=0x0;
col=colms;
col&=0x07;
} //5ends
while(col==0x07);
del(20);
rows=0x0;
col=colms;
col&=0x07;
}while(col==0x07); //4ends
rows=0xfe;
col=colms;
col&=0x07;
if(col!=0x07)
{ //6
row=0;
break;
}//6ends
rows=0xfd;
col=colms;
col&=0x07;

GRIET/ECE 81 of 86
Microprocessors and Interfacing Lab
______________________________________________________

if(col!=0x07)
{ //7
row=1;
break;
}//7ends
rows=0xfb;
col=colms;
col&=0x07;
if(col!=0x07)
{ //8
row=2;
break;
}//8ends
rows=0xf7;
col=colms;
col&=0x07;
if(col!=0x07)
{ //9
row=3;
break;
}//9ends
}//2ends
if(colms==0xfe)
key=keypad[row][0];
else if(colms==0xfd)
key=keypad[row][1];
elseif(colms==0xfb)
key=keypad[row][2];
else
key=keypad[row][3];
return(key);
}//1ends

Result:

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

GRIET/ECE 82 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 11. Interfacing of Real time clock


Program description An 8051 C program for setting Date, Time and Day
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor 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 port
temp = ReadBYTE(0x00);
temp &= 0x7F;// enable oscillator (bit 7=0)
temp = ReadBYTE(0x08);
if(temp != 0xAA)
{
WriteBYTE(0x08,0xAA);
RTC_ARR[0] = 0x00;-//sec =00
RTC_ARR[1] = 0x10;-//minute = 59
RTC_ARR[2] = 0x12;-//hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x03;-//Day = 1 for sunday; now tuesday
RTC_ARR[4] = 0x24;-//Date = 24
RTC_ARR[5] = 0x11;-//month = november
RTC_ARR[6] = 0x09;-//year = 09 or 2009
WriteRTC(&RTC_ARR[0]);-//Set RTC
}-//end if
while(1)
{
ReadRTC(&RTC_ARR[0]);
putchar(0x0C);-//clear Hyper terminal
printf("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 83 of 86
Microprocessors and Interfacing Lab
______________________________________________________
//delay
#ifndef _DELAY_H
#define _DELAY_H
extern void DelayMs(unsigned int count);
extern void DelayUs(int us);
#endif
//DS1307 driver
#ifndef _DS1307_H
#define _DS1307_H
extern 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_H
extern void InitSerial(void);
#endif

Result:

Clock can be set or reset.

GRIET/ECE 84 of 86
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 Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor 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;//port1
sbit 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 85 of 86
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 main
void 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 86 of 86
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 I2C
void Stop(void)
{
sda = 0;
_nop_();_nop_();
scl= 1;
_nop_();_nop_();
sda = 1;
}

GRIET/ECE 87 of 86
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 88 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 13. Interfacing of Temperature sensor


An 8051 C program for reading temp form ADC0804,convert it to
Program description
decimal
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include <REGX51.H>
Sbit CS=P2^4; //CSbar pin of ADC connected to GND
sbit RD1 = P2^5; //port 2 for control lines
sbit WR1 = P2^6;
sbit INTR1 =P2^7;
sfr adcdata = 0x90;//port1 for adc data
sfr lcddata = 0x80; //port2 for lcd data
sbit 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 INPUT
INTR1=1;//TO MAKE INTR PIN ( P2.7) AS INPUT
PIN
RD1=1;//INTIAL STATUS OF RD PIN

GRIET/ECE 89 of 86
Microprocessors and Interfacing Lab
______________________________________________________

while(1)
{
WR1=0;//to start convertion , make wr pin low to high
delay(5);
WR1=1;
while(INTR1==1);//wait till convertion is over
RD1=0;//get the data from adc
delay(100);
value=adcdata;
display(value);
RD1=1;
delay(500);
}//end while
}//end main
void display(unsigned cha ADC_VALUE)
{
unsigned char D1,D2,D3;
D1 = ADC_VALUE / 100;
D2 =( ADC_VALUE % 100)/10;
D3 =( ADC_VALUE % 100)%10
D3+=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 90 of 86
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 91 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 14. Interfacing of Buzzer


Program description An 8051 C program for buzzer to on and off
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include<reg51.h>
Void delay(unsigned int);
sbit on = P2^0;// buzzer to P2.0
void main( )
{
while(1)
{
0n = 1;//buzzer on
delay(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 92 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Program Name 15. Interfacing of Magnetic sensor


Program description An 8051 C program for magnetic switching operation
Author GRIET ECE Department
Date
Hard ware GRIET Dual board
Soft ware Platform KEIL
Microprocessor AT89S52

#include<reg51.h>
Sbit in = P260;//input
Sbit LED = P2^1;//output
Void main( )
{
LED = 0;
In = 1;//P2^0 as input
While(1)
[
LED = 1;
}
}

Result:

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

GRIET/ECE 93 of 86
Microprocessors and Interfacing Lab
______________________________________________________

Appendix

GRIET/ECE 94 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Appendix A

String operations (using string instructions)

1. REP/ REPE/ REPZ/ REPNE /REPNZ


REP is a prefix which is written before one of the string instructions. These instructions repeat
until specified condition exists.

Instructions Code Condition for Exit


REP CX=0
REPE/ REPZ CX=0 OR ZF=0
REPNE/ REPNZ CX=0 OR ZF=1

2. MOV/ MOVSB/ MOVSW


Move 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 increment
If Direction Flag=1; Index register decrement

3. CMPS/CMPSB/CMPSW
The 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/ SCASW


SCAS 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/LODSW
This 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 95 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Appendix B

DOS Interrupts
Useful DOS interrupt to input information from the keyboard and display it on the
screen.
INT 21h
Function 01 – inputting a single character, with an echo
AH = 01: function number
After the interrupt AL = ASCII code of the input and is echoed to the monitor

Function 02 – setting the cursor to a specific location


AH = 06; function number
DH = row; cursor
DL = column; position

Function 07 – inputting a single character from the keyboard without an echo


AH = 07: function number
Waits 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: string
Function 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 user
AH = 01
Upon execution ZF = 0 if there is a key pressed
Function 00 – keyboard read
AH = 00
Upon execution AL = ASCII character of the pressed key
Note this function must follow function 01

GRIET/ECE 96 of 86
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 97 of 86
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 98 of 86
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 99 of 86

Вам также может понравиться