Академический Документы
Профессиональный Документы
Культура Документы
College of Engineering
Bangalore - 59
(Autonomous Institution affiliated to VTU, Belgaum)
Laboratory Manual
( 2016 SCHEME )
Microprocessor And Microcontroller Lab
(12EE63)
List of Experiments
Program
Week Programmes
No
Data Transfer Programs: Block Moves (With & Without Overlap)
1
1 with &without String Instructions
A,B,C,D,E
& Data Exchange
2 Arithmetic Operations: Addition, Subtraction, Multiplication &
2
A,B,C,D Division on 32-Bit Data.
a) Write 8086 Assembly Language Program to transfer block of data using string
instruction without overlap.
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred to array2
Step 2: Find the length of defined array1
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 to counter register
Step 5: Clear the direction flag, so that SI and DI are incremented by one automatically
Step 6: Move the contents of array1 to array2 until counter=0 using string instruction
Step 7: Terminate the program.
Program:
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $ - ARRAY1 ; finding length of array1
ARRAY2 DB 4 DUP (?) ; allocating memory for array2
. CODE
MOV AX, @ DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2
MOV CX, LEN ; taking length of array1 to CX register
CLD ; clear direction flag
REP MOVSB ; move string of bytes repeatedly from SI
;to DI location until CX=0
MOV AH, 4CH ; terminate a program
INT 21H
END
Output:
Run 1:
Keep on executing the program till the instruction given in italics through single
stepping (F8 key). Observe the contents of SI, DI and CX registers. The SI will be
initialized to starting address of array1. Similarly, DI will be initialized to starting
address of array2.The CX will be initialized to number elements of the array1.
To observe data transfer operation, examine the contents of data segment by
displaying its locations consisting of array1 and array2.
For example if SI=0000h, then DI=0007H
To display contents of data segment type command
After execution of remaining instructions of program, examine the contents of data
Segment to observe the contents array2 same as array1.
d ds: 00 15 (press enter key)
4AE1:0000 02 04 06 08 0A 05 00 02-04 06 08 0A 00 00 00 00
Run 2:
b) Write 8086 ALP to transfer block of data without using string instruction without
overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred to
array2
Step 2: Find the length of defined array1
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 to count register
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by destination
index
register
Step 6: Increment the index registers to point to next location
Step 7: Move the contents of array1 to array2 until count=0 using string instruction
Step 8: Terminate the program.
Program:
.MODEL SMALL
.DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $ - ARRAY1 ; finding length of array1
ARRAY2 DB 4 DUP (?) ; allocating memory for array2
.CODE
MOV AX, @ DATA ; Initialization of data segment
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2
Output:
c) Write an ALP to transfer block of data without using string instruction with
overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred.
Step 2: Find the length of defined array1
Step 3: Move the length of array1 to count register
Step 4: Use index registers to point to last element of array1 and to a location above
the last element of the array1 by overlap amount (The program below
is written for overlap amount of 2)
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by
destination index register
Step 6: Decrement the index registers to point to next location
Step 7: Move the contents of array1 until count=0.
Step 8: Terminate the program.
Program
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
LEN DW $-ARRAY1 ; finding length of array1
.CODE
MOV AX, @DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES,AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV CX, LEN ; taking count to CX register
ADD SI, CX
DEC SI
MOV DI, SI
ADD DI, 02 ; overlapping occurs at second
; position in array1
BACK: MOV AL, [SI] ; exchanging the content of SI and DI
; Until CX=0 repeat loop back
MOV [DI], AL
DEC SI
DEC DI
LOOP BACK
MOV AH, 4CH ; to terminate a program
INT 21H
END
Output:
d) Write an ALP to transfer block of data using string instruction with overlap
Algorithm
Step 1: Initialize an array1 of N bytes in data segment which is to be transferred.
Step 2: Find the length of defined array1
Step 3: Move the length of array1 to count register
Step 4: Use index registers to point to last element of array1 and to a location above
the last element of the array1 by overlap amount (The program below
is written for overlap amount of 2)
Step 5: Move the contents of source index register to 8-bit register and then the
contents of this 8-Bit register move to the location pointed by
destination index register using string instruction.
Step 6: Terminate the program.
Program:
. MODEL SMALL
. DATA
ARRAY1 DB 02H, 04H, 06H, 08H, 0AH ; declaring an array1 of N bytes
COUNT DW $-ARRAY1 ; finding length of array1
. CODE
MOV AX, @DATA ; Initialization of DS & ES
MOV DS, AX
MOV ES,AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV CX, COUNT ; taking count to CX register
ADD SI, CX
DEC SI
MOV DI, SI
ADD DI, 02 ; overlapping occurs at second position
; in array1
STD ; set direction flag
REP MOVSB ; move string of bytes repeatedly from SI
; to DI location until CX=0
MOV AH, 4CH ; to terminate a program
INT 21H
END
Output:
e) Write an ALP to exchange block of data between locations of data segment.
Algorithm
Step 1: Initialize an array1 and array2 of N bytes in data segment
Step 2: Find the lengths of defined array1 and array2
Step 3: Use index registers to point array1 and array2 respectively
Step 4: Move the length of array1 or array2 which is greater to count register
Step 5: Move the contents of source index register to 8-bit register and the contents of
destination Index register to another 8-bit register
Step 6: Exchange the contents of the specified two 8-bit registers each other
Step 7: Increment the index registers to point to next location
Step 8: Move the contents of array1 to array2 until count=0 using string instruction
Step 9: Terminate the program.
Program:
. MODEL SMALL
. DATA
ARRAY1 DB 01H,02H, 03H, 04H, 05H, 06H, 07H, 08H, 09H ;
LEN1 DW $- ARRAY1 ; finding length of array1
ARRAY2 DB 11H, 12H, 13H, 14H, 15H, 16H, 17H, 18H,19H ;array2 of 9
Elements
. CODE
MOV AX, @ DATA ; Initialization of DS and ES
MOV DS, AX
MOV ES, AX
MOV SI, OFFSET ARRAY1 ; make SI to point array1
MOV DI, OFFSET ARRAY2 ; make DI to point array2
MOV CX, LEN1 ; taking count to CX register
REPT: MOV AL, [SI] ; move data pointed by SI to AL
MOV AH, [DI] ; move data pointed by DI to AH
MOV [SI], AH ; exchanging the data until CX=0
MOV [DI], AL
INC SI
INC DI
LOOP REPT
MOV AH, 4CH
INT 21H
END
Output:
Program : 2(WEEK 2)
MOV SI,OFFSET N1
MOV DI,OFFSET N2
MOV BP,OFFSET RESULT
Program : 3
Program:
.MODEL SMALL
.DATA
MSG1 DB 0DH, 0AH,"ENTER THE INPUT TO BE CONVERTED :( 0H-FH)",'$'
MSG2 DB 0DH, 0AH,"BCD EQUIVALENT OF BINARY IS:",'$'
MSG3 DB 0DH, 0AH,"DO U WANT TO CONTINUE: Y/N",'$'
LK_UP DB
00H,01H,02H,03H,04H,05H,06H,07H,08H,09H,10H,11H,12H,13H,14H,15H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
b) Write an ALP to convert Binary numbers into its equivalent ASCII value
Algorithm:
Step 1: Display the message on the console which asks the input value
Step 2: Accept the input values entered through keyboard
Step 3: Convert the input and translate the input into its ASCII equivalent using lookup
table
Step 4: display the output ASCII value (MS and LS nibble output) on the VDU(Video
Display Unit-SCREEN)
Step 5: Prompt the user to enter Y/N to continue or terminate.
Step 6: Accept the user decision and continue conversion or terminate the program
Output:
c) Write an ALP to convert Binary numbers into its equivalent gray value.
Algorithm
Step 1: Display the message on the console to prompt user to enter the binary value.
Step 2: Accept the input values entered through keyboard.
Step 3: Convert the input and translate the input into its gray equivalent using lookup
table
Step 4: Display a message and display the output GRAY value (MS and LS nibble
output) on the VDU
Step 5: Ask the user to terminate or to continue conversion.
Step 6: Accept the user decision and continue conversion or terminate the program.
Program:
.MODEL SMALL
.STACK 64 ; initialize the stack segment
.DATA
MSG1 DB 0DH,0AH,"ENTER THE INPUT TO BE CONVERTED:(00H-FH)",'$'
MSG2 DB 0DH,0AH,"GRAY EQUIVALENT OF BINARY IS:",'$'
MSG3 DB 0DH,0AH,"DO U WANT TO REPEAT: Y/N",'$'
LK_UP DB
00H,01H,03H,02H,06H,07H,05H,04H,12H,13H,15H,14H,10H,11H,09H,08H
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
Program : 4
Algorithm
Step 1: Initialize the SEARCHKEY.
Step 2: Compare the search key with the first element of the array
Step 3: Stop the search and go to step7 if the search key is found else go to step4
Step 4: If all the elements of the array are not searched then go to step5 else go to step8
Step 5: Compare the next element of the array & if search key is found goto step 7.
Step 6: Repeat step4 if the search key is not found
Step 7: Display the Success message
Step 8: Display the Fail message
Program:
.MODEL SMALL
.DATA
ARRAY DB 55H,33H,44H,66H,22H
LENTH DW $-ARRAY
SRCHKEY EQU 55H
SUCMSG DB 'ELEMENT 55 FOUND AT POSITION:'
RESULT DB ? ,'$'
FAILMSG DB 'ELEMENT 55 NOT FOUND','$'
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
CLD
MOV DI,OFFSET ARRAY
MOV BX,OFFSET ARRAY
MOV AL,SRCHKEY
MOV CX,LENTH
REPNE SCASB
JE SUCCESS
LEA DX, FAILMSG
JMP DISPLAY
SUCCESS:
MOV AX,DI
SUB AX,BX
ADD AL,'0'
MOV RESULT,AL
LEA DX,SUCMSG
DISPLAY:
MOV AH,09H
INT 21H
MOV AH,4CH
INT 21H
END
Output & Efficiency:
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
MOV BX,1
MOV DX,LENTH
MOV CX,SRCHKEY
MOV AX,BX
ADD AX,DX
SHR AX,1
CMP CX,ARRAY[SI]
JAE BIGER
DEC AX
MOV DX,AX
JMP AGAIN
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX ; Initialize the segment registers
NO_SWAP: INC SI
LOOP INLOOP
DEC BX
JNZ OUTLOOP
MOV AH,4CH
INT 21H
END ; Terminate the program
Output & Efficiency:
.CODE
MOV AX,@DATA ;initialize Data Segment
MOV DS,AX
MOV DX,LEN
DEC DX
MOV AH,4CH
INT 21H
END
Note:2F Indicates the result is negative, when larger number is subtracted from the smaller
number
MOV AL, N1
MUL N2
AAM
ADD AL,DH
DAA
ADD AL, CH
DAA
MOV DL,AL
AND AL,0FH
MOV [DI+2], AL
MOV CL, 04
MOV AL,DL
AND AL,0F0H
SHR AL,CL
ADD AL,AH
MOV [DI+3],AL
MOV AH,4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:
d) Write a program for ASCII division of 16-Bit data by 8 Bit data
Program
.MODEL SMALL
.DATA
N1 DW 0608H
N2 DB 09H
QUO_REM DW ?
.CODE
MOV AX,@DATA
MOV ES,AX
MOV DS,AX
MOV DI,OFFSET QUO_REM
MOV AX, N1
AAD
DIV N2
MOV [DI],AX
MOV AH,4CH
INT 21H
END
Output:
Before Execution:
d ds: 00 10 (press enter key)
4AE1:0000 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00
After Execution:
8051 Microcontroller
Program : 6
6. BCD UP/DOWN counter program in C. To display the values with certain delay
on the onboard 7 – segment display unit of flash 8051 (Philips 89C61X2) board.
// Follow indentation in every C program when practicing, writing programs in the record and
also on the data sheet.
#include<reg51.h> // to access ports, timer registers etc.,
Program : 7
//clockwise or counter clockwise depending upon switch status read through P1.6
#include <reg51.h>
unsigned char x1[] = {0x03,0x09,0x0C,0x06},temp;
//unsigned char x2[] = {0x03,0x01,0x09,0x08,0x0C,0x04,0x06, 0x02};
// x2 array for alternate phase excitation
void main()
{
voidclk_wise();
voidcclk_wise();
void delay(unsigned int);
P2 = 0x00; // configuring P2 as out
while(1)
{
temp = P1 & 0x40;
//delay(100);
if(temp!= 0)
clk_wise();
else
cclk_wise();
}
//while(1);
P2 = x1[i];
delay(5000);
}
}
void cclk_wise(void)
{
unsigned int i;
for(i=0;i<4;i++)
{
P2 = x1[3-i];
delay(5000);
}
}
Program : 8
8. To generate Square, triangular, ramp, staircase, and sine waveforms using a DAC
card interfaced to the flash board.
void main(void)
{
void delay(int);
unsigned char ch =0x00;
P0 = 0x00; P1 = 0x00; // ports P0, P1 are configured as OUT
while(1)
{
P0= ch;
P1= ch; // required only if Dual DAC card is used
delay(50);
P0 =~ch;
P1= ch;
delay(50);
}
}
void delay(int n)
{
int i;
for(i=0;i<n;i++)
;
}
Ramp / Triangular
#include<reg51.h>
void main(void)
{
unsigned char chu =0x00, chd = 0xff;
//unsigned flag= 0xFF;
while(1)
{
/*while(1) // commented to observe negative ramp, check initial value of chu
{
P0= chu;
chu++;
if(chu == 0xff)
break;
else
;
}*/
while(1)
{
P0= chd;
chd--;
if (chd == 0x00)
break;
else
;
}
} // end of forever while
} // end of main
// staircase signal
#include<reg51.h>
void main(void)
{
void delay(int);
unsigned char ch[] = {0x00, 0x33,0x66,0x99,0xCC,0xFF}; // corresponds to 0, 1, 2 , …5 V
Modify the program to get staircase signal in either direction.
unsigned char i =0;
while(1) // Can it be implemented with one loop?
{ // Don’t think of complex logic, use loops 2 or 3?
P0= ch[i];
//P1= ch[i];
delay (10);
i++;
if(i>6)
i=1;
else
;
}
}
// sine signal For 5 V output signal range, let V = 5 sin be the required signal. Calculate
voltage values for every 30 angle between 0 & 360. For 256 steps, steps per volt = 256/5 =
51.2. Multiply the voltage values by 51.2 to get integer values of the sine function. Replace
the char array by these values and change index, according to the number of samples, to view
crude sine wave.
9.Interfacing a hex keypad and seven segment display unit to 8051 flash board and
to realize simple calculator operations.
#include<reg51.h>
#include<kdata.h>
#include <kpdrd.h>
#include<del.h>
#include<kdisp.h>
void delay(int n);
void main(void)
{
unsigned int k,i1,i2,n1,n2,op, n, nxt;
do
{
nxt =14;
disp_op(nxt); // display E on 4th LED as prompt to enter number
k =11;
// read 1 st number
do
{
k = key_rd();
i1=k;
} while(k>10); // read a digit between 0 & 9 MSD
delay(65535); // come down to user speed to feed number
k =12;
do
{
k = key_rd();
i2 =k;
} while(k>11);// reads LSD
delay(65535);
n1 = i1*10+i2; // construct the number, 2 - digit
digit_disp(n1); // displayed on right corner
// read the operator
nxt = 14;
disp_op(nxt); // prompt to enter operator
op=6;
do
{
k = key_rd();
op =k;
}while(op<10);
delay(65535);
// read 2nd number
nxt =14;
disp_op(nxt);
k =11;
while(k>10)
{
k = key_rd();
i1 = k; // read MSD
}
delay(65535);
k =12;
while(k>11)
{
k = key_rd(); // read LSD
i2 = k;
}
delay(65535);
n2 = i1*10+i2;
digit_disp(n2);
switch(op)
{
case 10: n =n1+ n2;
break;
case 11: n =n1- n2;
break;
case 12: n = n1*n2;
break;
case 13: if (n2!=0)
n= n1/n2;
else
n = 0x00;
break;
default: n= n1%n2;
break;
}
disp_op(op);
digit_disp(n);
op =6; // to wait for proper op
}while(1);
}
10.Interfacing an elevator model to the 8051 flash board and to study the elevator
operations.
#include <reg51.h>
#include<eledata.h>
#include<del.h>
main()
{
unsigned char reqflr,curflr = 0x01,i,j; // note all var’s are unsigned char
P0 = 0xf0;
while(1)
{
P1 = 0x0f; // lower nibble of P1 made input port
reqflr = P1 | 0xf0; // read floor request & append f (for upper nibble)
// loop until request appears
while (reqflr == 0xff)
reqflr = P1 | 0xf0; // Read Request Floor from P1
Program : 11Interfacing the given external LCD unit to the 8051 flash board and to
display the given string on line 1 or line 2.
#include<reg51.h>
sbit rs = P2^4;
sbit rw = P2^5;
sbit en = P2^6;
void main()
{
void delay(unsigned int n);
void lcdcmd(unsigned char cmd); // command function
void lcddata(unsigned char d); // write data function
unsigned char x[] = {"Hello RV"}; // string to be displayed
unsigned char i = 0;
lcdcmd(0x3C); // command 0x38 for 1 line & 5X7 dots ;
delay(100); // write time required for LCD to set the function (40 s)
lcdcmd(0x0e);
lcdcmd(0x01); // clears display screen
delay(100);
lcdcmd(0x06); // shift display to right after each char dispaly
delay(100);
//lcdcmd(0x0C); // cursor off & display on
//delay(100);
lcdcmd(0x80); // cursor off & display on
delay(100);
for(i=0;i< 8;i++) // time for sending data
{
lcddata(x[i]); // display the string of length 8
delay(1000);
}
Program : 12-aInterfacing the given ADC with temperature sensor to the 8051 flash
board and to display temperature suitably.
adc_data.h
//adc.data
Unsigned char adc_table[55]=
{ 0x00, 0x03,0x05,0x08, 0x0a,0x0d,0x0f, 0x11,0x14,0x16,
0x19,0x1c,0x1e, 0x21,0x24,0x27, 0x2a,0x2c,0x2e, 0x32,
0x34, 0x36, 0x39, 0x3c, 0x3f, 0x42, 0x45, 0x48, 0x4a, 0x4c,
0x4e, 0x50, 0x52,0x54, 0x56,0x58,0x5b, 0x61, 0x64, 0x67,
0x6a, 0x6d,0x70, 0x72,0x74,0x77, 0x7a,0x7d,0x7f, 0x82,
0x85,0x87,0x8a,0x8d,0x90}
#include <reg51.h>
#include<adc_data.h>
#include<del.h>
sbit start = P3^5;
sbitch_sel = P3^6;
sbiteoc = P3^3;
void main ()
{
idata unsigned char adc_val,temperature,i;
P2 = 0xff; // P2 as input
ch_sel = 0x00; // channel 1 selection Wr = 1 setb p3.6
start = 0; // clr p3.5
delay (200);
while (1)
{
start = 1;
delay(200);
start = 0; // start of conversion initiated
while (eoc != 1) ; // wait for eoc = 0
delay(200);// after eoc, read the adc data from P2
Program : 12-bTo toggle all bits of P2 continuously every 500 ms, using timer 1 in mode
#include<reg51.h>
void main(void)
{
void timer delay(void);
unsigned char x;
P2 = 0x55;
while(1)
{
P2 =~ P2;
for(x =0;x<20;x++)
timer_delay();
}
}
void timer_delay(void)
{
TMOD = 0x10; // timer 1 in mode 1
TL1 = 0xFe; // loading values
//A5FE = 42494; 65536 - 42494 = 23042 X 1-085 = 20 ms.
TH1 = 0xA5;
TR1 =1;
while(TF1==0); // wait for flag to roll over
TR1 =0; // turn off timer1
TF1 =0; // clear flag
}