Академический Документы
Профессиональный Документы
Культура Документы
______________________________________________________
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
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
______________________________________________________
GRIET/ECE 4 of 86
Microprocessors and Interfacing Lab
______________________________________________________
TASM Editor
How to go to TASM editor:
After writing code in TASM editor, the following are the steps to compile, run and to
enter data.
GRIET/ECE 5 of 86
Microprocessors and Interfacing Lab
______________________________________________________
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.
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
______________________________________________________
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
______________________________________________________
GRIET/ECE 14 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Exercises:
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
______________________________________________________
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
______________________________________________________
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:
Exercises:
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
______________________________________________________
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
______________________________________________________
Input Output
GRIET/ECE 27 of 86
Microprocessors and Interfacing Lab
______________________________________________________
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
______________________________________________________
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
______________________________________________________
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
______________________________________________________
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:
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
______________________________________________________
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
______________________________________________________
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.
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
______________________________________________________
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:
Exercises:
1. Write an assembly language program to receive an input string from keyboard.
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
______________________________________________________
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
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
______________________________________________________
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
______________________________________________________
Example:
Input Output
Memory Location Data Memory Location Data
2000 27h 4000 Not Prime
GRIET/ECE 56 of 86
Microprocessors and Interfacing Lab
______________________________________________________
GRIET/ECE 57 of 86
Microprocessors and Interfacing Lab
______________________________________________________
VCC
5V
VCC
0 7
J19
R1 R8
1.0k 1.0k
0 7 1 2
0 7
GND
0 7
J20
GRIET/ECE 58 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Explanation:
Port0 –switches
Port1–LEDS
Output:
GRIET/ECE 59 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
______________________________________________________
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
______________________________________________________
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
GRIET/ECE 64 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
______________________________________________________
Output:
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
______________________________________________________
#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
______________________________________________________
DIGITAL O/P
AT89S52 DAC0808
GRIET/ECE 70 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
12V OUTPUT 1 P1
RL1 N1
N
INPUT
P
12V
OUTPUT 2 P2
RL2
N2
GRIET/ECE 72 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
______________________________________________________
#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:
GRIET/ECE 75 of 86
Microprocessors and Interfacing Lab
______________________________________________________
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
______________________________________________________
#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
______________________________________________________
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:
GRIET/ECE 82 of 86
Microprocessors and Interfacing Lab
______________________________________________________
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:
GRIET/ECE 84 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
______________________________________________________
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:
GRIET/ECE 88 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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
______________________________________________________
#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:
GRIET/ECE 92 of 86
Microprocessors and Interfacing Lab
______________________________________________________
#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:
GRIET/ECE 93 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Appendix
GRIET/ECE 94 of 86
Microprocessors and Interfacing Lab
______________________________________________________
Appendix A
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.
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 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
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