Beruflich Dokumente
Kultur Dokumente
AND
APPLICATIONS
(LAB)
Shri Guru Gobind Singhji Institute Of Engineering And Technology
CONTENTS
SNo. Name of the Experiment Page No. Remark
Experiment-1 (Assembly Language Programs for Addition and Subtraction)
1 a) 8-bit addition for unsigned numbers by using different addressing modes 5
2 b) 8-bit subtraction for unsigned numbers by using different addressing modes 7
3 c) Subtract two 16-bit numbers with out using SUBB instruction 9
4 d) Adding two 3 byte BCD numbers in subroutine 10
EXPERIMENT NO. 1
ORG 2000H
START:
MOV A, #20H ; Move 02H to A
MOV B, #10H ; Move 10H to B
ADD A, B ; Add A B
RET
END
ORG 2000H
START:
MOV A, #10H ; Move 10H to A
MOV B, #11H ; Move 11H to B
ADD A, 0F0H ; Add contents in A and 0F0H (B)
RET
END
ORG 2000H
START:
MOV A, #01H ; Move 01H to A
MOV R1, #0F0H ; Move the Address of register B (0F0H) to R1
MOV B, #05H ; Move 05H to B
ADD A, @R1 ; Add A and the content in the Address pointed by R1, i.e., B
RET
END
ORG 2000H
START:
MOV A, #03H ; Move 03H to A
ADD A, #02H ; Add A and the immediate value 02H
RET
END
ORG 2000H
START:
MOV A, #20H ; Move 20H to A
MOV B, #10H ; Move 10H to B
SUBB A, B ; Subtract B from A
RET
END
ORG 2000H
START:
MOV A, #11H ; Move 11H to A
MOV B, #10H ; Move 10H to A
SUBB A, 0F0H ; Subtract contents in A and 0F0H (B)
RET
END
ORG 2000H
START:
MOV A, #05H ; Move 05H to A
MOV R1, #0F0H ; Move the Address of register B (0F0H) to R1
MOV B, #01H ; Move 01H to B
SUBB A, @R1 ; Subtract A and the content in the Address, pointed by R1, i.e., B
RET
END
ORG 2000H
START:
MOV A, #03H ; Move 03H to A
SUBB A, #02H ; Subtract immediate value 02H from A
RET
END
ORG 2000H
START:
MOV A, 23H ; Move lower byte of 2nd number to A
MOV B, 21H ; Move lower byte of 1st number to B
CPL A ; Compliment A
INC A ; Increment A, Now number in A is 2’s compliment of the original
MOV R0, A ; Move the contents in A to R0
MOV A, 22H ; Move the higher byte of 2nd number to A
CPL A ; Compliment A
ADDC A, #00H ; Add A, #00h with carry
MOV R1, A ; Move the contents in A to R1
CLR C ; Clear Carry
MOV A, R0 ; Move the lower byte of the 2’s compliment to A
OUTPUT: 43D9
R3 - #43H R4 - #D9
ORG 2000H
START:
MOV A, 22H ; Move the lower byte of 1st BCD number to A
MOV B, 25H ; Move the lower byte of 2nd BCD number to B
ADD A, B ; Add A B
DA A ; Decimal adjust A
MOV R3, A ; Move the result to R3
MOV A, 21H ; Move the middle byte of 1st BCD number to A
MOV B, 24H ; Move the middle byte of 2nd BCD number to B
ADDC A, B ; Add with carry A and B
DA A ; Decimal adjust A
MOV R2, A ; Move the result to R2
INPUT:
1st Number- 979899, 2nd Number- 878889
20H - #97H 23H - #87H
21H - #98H 24H - #88H
22H - #99H 25H - #89H
OUTPUT: 01858788 H
R0 – #01H
R1 – #85H
R2 - #87h
R3 - #88H
EXPERIMENT NO. 2
ORG 2000H
START:
MOV A, #12H ; Move 12H to A
MOV B, #23H ; Move 23H to B
MUL A B ; Multiply A B
MOV R0, B ; Move the lower byte of the result to R0
MOV R1, A ; Move the higher byte of the result to R1
RET
END
ORG 2000H
START:
MOV A, #A2H ; Move A2H to A
MOV B, #06H ; Move 06H to B
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END
ORG 2000H
START;
MOV A, #02H ; Move 02H to A
MOV B, #09H ; Move 09H to A
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END
ORG 2000H
START:
MOV A, #A1H ; Move A1H to A
MOV B, #00H ; Move 00H to A
DIV A B ; Divide A B, quotient is Stored in A and Remainder is stored in B
RET
END
ORG 2000H
START:
MOV A, 31H ; Move lower byte of 1st number to A
MOV B, 33H ; Move lower byte of 2nd number to B
MUL A B ; Multiply A B
MOV R0, B ; Move higher byte of the result to R0
MOV 13H, A ; Move the lower byte of the result to 13H or R3 of Bank 2
MOV A, 33H ; Move the lower byte of 2nd number to A
MOV B, 30H ; Move higher byte of the 1st number to B
MUL A B ; Multiply A B
MOV R2, B ; Move the higher byte result to R2
MOV R3, A ; Move the lower byte result to R3
MOV A, 32H ; Move the higher byte of 2nd number to A
OUTPUT: 0137DEE8
Bank 2:
R0/10H - #01H
R1/11H - #37H
R2/12H - #DEH
R3/13H - #E8H
d) To set alternate bits in first half addressable memory and set 1 bit then reset 2 bits then set
three bits and so on in remaining half bit addressable memory
MOV R0,#1FH ;addr of ram location just before bit-addr's 1st location
AGAIN:INC R0
DJNZ R6,AGAIN
MOV 28H,#39H
MOV 29H,#7CH
MOV 2AH,#0E0H
MOV 2BH,#0FH
MOV 2DH,#1FH
MOV 2EH,#80H
MOV 2FH,#0FFH
SJMP $
OUTPUT:
RAM AREA
20H=AAH 28H=39H
21H=AAH 29H=7CH
' 2AH=E0H
' 2BH=0FH
' 2CH=F0H
' 2DH=1FH
' 2EH=80H
27H=AAH 2FH=FFH
OUTPUT:
RAM AREA
20H=FFH
21H=FFH
'
'
'
'
2FH=FFH
OUTPUT:
;RAM AREA
20H=00H
21H=00H
'
'
'
'
2FH=00H
EXPERIMENT NO. 3
ORG 2000H
START:
MOV R0, #04H ; Initialize the counter for outer loop by moving 04H to R0
LOOP1: MOV R1, #20H ; initialize the starting index of the array by moving 20H to R1
MOV A, R0 ; Move the contents in R0 to A
MOV R2, A ; Move the contents in A to R2 so as initialize the inner loop counter
LOOP2: MOV A,@R1 ; Move the contents in the location pointed by R1, Ex: 20h
MOV R3, A ; Move contents in A to R3
INC R1 ; Increment R1 for next number
MOV A,@R1 ; Move the contents in the location pointed by R1
SUBB A, R3 ; Subtract contents in A and R3
CONTINUE:
DJNZ R2, LOOP2 ; (inner loop) Decrement R2, if not zero jump to loop2
DJNZ R0, LOOP1 ; (outer Loop) Decrement R0, if not zero jump to loop1
RET
END
INPUT: OUTPUT:
20H - #24H 20H - #02H
21H - #02H 21H - #05H
22H - #05H 22H - #12H
23H - #58H 23H - #24H
24H - #12H 24H - #58H
ORG 2000H
START:
MOV R0, #04H ; Initialize the counter for outer loop by moving 04H to R0
LOOP1: MOV R1, #20H ; Initialize the starting index of the array by moving 20H to R1
MOV A, R0 ; Move the contents in R0 to A
MOV R2, A ; Move the contents in A to R2 so as initialize the inner loop counter
LOOP2: MOV A,@R1 ; Move the contents in the location pointed by R1, Ex: 20h
MOV R3, A ; Move contents in A to R3
INC R1 ; Increment R1 for next number
MOV A,@R1 ; Move the contents in the location pointed by R1
SUBB A, R3 ; Subtract contents in A and R3
CONTINUE:
DJNZ R2, LOOP2 ; (inner loop) Decrement R2, if not zero jump to loop2
DJNZ R0, LOOP1 ; (outer Loop) Decrement R0, if not zero jump to loop1
RET
END
INPUT: OUTPUT:
20H - #02H 20H - #58H
21H - #05H 21H - #12H
22H - #12H 21H - #05H
23H - #01H 23H - #02H
24H - #58H 24H - #01H
END
INPUT: OUTPUT:
00H - #42H 20H - #90H
01H - #67H
02H - #90H
03H - #20H
04H - #01H
END
INPUT: OUTPUT:
00H - #42H 20H - #01H
01H - #67H
02H - #90H
03H - #20H
04H - #01H
e) ALP to find number of EVEN and ODD numbers in an array of n-hexadecimal numbers
OUTPUT:
R1=30H
EXPERIMENT NO. 4
ORG 2000H
START:
MOV A, #26H ; (A+B); Move 26H to A
MOV B, #33H ; Move 35H to B
ADD A, B ; Add A, B (A+B)
MOV R0, A ; Move the result to R0
INPUT:
A=26H
B=35H
C-05H
D=40H
E=08H
OUTPUT: 0000E8
R0 – 00
R1 – 00
R2 – E8
MUL AB ; A=b^2
MUL AB ; A=ac
MUL AB ; A=4ac
CLR C
MOV B,A
MUL AB
MOV B,R0
CJNE A,B,GO
SJMP AHEAD
GO: INC R1
SJMP BACK
CLR C
MOV B,20H
DIV AB
MOV B,#02H
MOV A,R1
ADD A,21H
MOV B,20H
DIV AB
MOV B,#02H
CLR A
CLR C
SJMP $
INPUT:- OUTPUT:-
C) ALP and C-language program to Convert a given BCD number to its ASCII equivalent code
ALP:-
MOV B,#100
DIV AB
MOV A,B
MOV B,#10
DIV AB
INC DPTR
MOV A,B
C code
#include <Intel\8051.h>
void main ()
{
ptr++;
ptr++;
while(1)
{
}
}
EXPERIMENT NO. 5
ALP:-
ORG 2000H
MOV R1 , #07H ;Count of 07 stored in R1
MOV A , #00H ;Move 00 in Accumulator
MOV P1 , A ;Move 00 in port 1 to make it a output port
MOV A , #FEH ;Move FE in accumulator
UP1 : MOV P1 , A ;move to port 1
ACALL DELAY ;call delay of 0.5 sec
ACALL DELAY ;call delay of 0.5 sec
NOTE: The LED’s used are in COMMON ANODE mode. Hence when 0 is sent to output,LED glows.
C-code
#define LED P1
void delay(void);
main()
{
unsigned int c;
unsigned char i;
while(1)
{
i = i << 1;
}
}
}
void delay(void)
{
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}
ORG 2000H
MOV A,#00H ;move 00h in accumulator
MOV P1,A ;move 00h to port 1 to make it an output port
MOV R0,#10H ;store number of counts
UP: MOV A,#00H ;move starting number to accumulator
MOV P1,A ;move this to port 1
ADD A,#01H ;increment data in accumulator by 1
DJNZ R0,UP ;decrement reg 0 till zero
RET
NOTE: The LEDs used are in common cathode mode. The LEDs are connected to lower nibble of port 1.
C-code
#define LED P1
void delay(void);
main()
{
unsigned int c;
unsigned char i;
while(1)
{
i = 0x01 ;
for(c=0;c<15;c++)
{
LED = i ;
delay();
i ++;
}
}
}
void delay(void)
{
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}
EXPERIMENT NO. 6
#include<Intel\8051.h>
#include<stdio.h>
void main ()
{
P1=0x00;
P3=0x00;
while(1)
{
display(0x1c,0xfd,0xfd,0xfd); //displaying E_ _ _
display(0x9c,0x1c,0xfd,0xfd); //displaying EC _ _
delay(5);
delay(5);
delay(5);
display(0xfd,0xfd,0x1c,0x9c); //displaying _ _ EC
delay(5);
display(0xfd,0xfd,0xfd,0x1c); //displaying _ _ _ E
delay(5);
display(0xfd,0xfd,0xfd,0xfd); //displaying _ _ _ _
delay(5);
}
}
delay(2);
P1=b;
delay(2);
P1=c;
P1=d;
delay(2);
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}
EXPERIMENT NO. 7
a) Interface 8 Switches & 1 Seven Segment display with the controller & write the C program
for displaying the number of switch pressed.
#include<Intel\8052.h>
#define IN P1 //initialization
#define OUT P3
void main()
{
IN =0xFF; // p1 as input
OUT =0x00; //p3 as output
while(1)
{
EXPERIMENT NO. 8
a) Interface Stepper motor to 8051 and write C Program to rotate stepper motor 600 CW and 220
CCW if input pin P3.1 is at logic 1 otherwise 450 CCW
#include<reg51.h>
#include<standard.h>
#include<stdio.h>
BIT IN P3^1 //switch connected to port 3.1
Void cw(int); //function for rotating stepper motor in clockwise manner
Void ccw(int); //function for rotating stepper motor in counterclockwise manner
Void delayms(int);
Void main(void)
{
P0=0x00; //configure port zero as an output port
IN=0x01; //configure p3.1 as input
While(1) // continous
{
If(IN==0x01) // if switch is 1
EXPERIMENT NO. 9
AIM: C-PROGRAM FOR INTERFACING KEYPAD TO 8051 AND DISPLAY THE KEY
PRESSED ON SEVEN SEGMENT DISPLAY
#include <Intel\8051.h>
switch(col)
{
case 0xe0:
P1=0x5e; //value to be sent to seven seg display for displaying 'd'
break;
case 0xd0:
P1=0x6f; //value to be sent to seven seg display for displaying '9'
break;
case 0xb0:
P1=0x7d; //value to be sent to seven seg display for displaying '5'
break;
case 0x70:
P1=0x06; //value to be sent to seven seg display for displaying '1'
break;
}
EXPERIMENT NO. 10
a) Interface ADC 0809 to the 8051 microcontroller & write the C program for it.
Theory:
ADC 0809 allows us to monitor up to 8 analog channels using a single chip. It has 8-bit
data bus. The 8 analog input channels are multiplexed and selected according to three address pins A,
B, C. Vref(+) & Vref set the reference voltage. The step size depends upon the voltage applied to the
Vref(+).
SC: Start Conversion
EOC: End of Conversion
ALE: Address latch enable
OE: Output Enable
CLK: 640 KHz along with the Vcc & GND.
Program in C:
void main()
EXPERIMENT NO. 11
a) Interface DAC 0808 to the 8051 microcontroller & write the program for generating the
sine wave.
#include<reg51.h>
Void main ()
{
Unsigned char value[12] = {128,192,238,255,238,192,128,64,17,0,17,64,128};
Unsigned char x;
While(1)
{
for(x=0;x<13:x++)
{
DACDATA = value[x]; // passing values for conversion one by one
}
}
}
EXPERIMENT NO. 12
a) Program to generate Square Wave with 25% Duty Cycle using Timers
#include "Intel\8051.h"
#include "delay.h"
#include"intrins.h"
main()
{
while(1)
{
LED = 0x01 ;
delay25ms();
LED = 0x00;
delay25ms();
delay25ms();
delay25ms();
}
}
void delay50ms(void)
{
TMOD=0x01;
TL0=0x0B0;
TH0=0x3C;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
void delay25ms(void)
{
TMOD=0x01;
TL0=0x58;
TH0=0x9E;
TR0=1;
while(!TF0);
TR0=0;
TF0=0;
}
#include "Intel\8051.h"
#include "delay.h"
#include"intrins.h"
main()
{
IE=0x82;
TMOD=0x09;
TL0=0x58;
TH0=0x9E;
TR0=1;
while(1)
{
}
EXPERIMENT NO. 13
a) Program to transmit data from 1000H memory location on P1.5 serially with start and stop bits
#include<Intel\8051.h>
main()
{
unsigned int c;
unsigned char i;
P1 = 0x00;
f = 0x1000
*f = 0x0ab;
while(1)
{
i=f;
out = 0x01; // start bit
delay();
for(c=0;c<8;c++)
{
out = (i & (1<<c)) ;
delay();
}
out = 0x00; //stop bit
delay();
}
}
void delay(void)
{
unsigned char c,d;
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}
b) Program to receive data and store at 1000H memory location from P1.5 serially with start and stop bits
#include<Intel\8051.h>
main()
{
unsigned int c;
unsigned char i;
P1 = 0xff;
while(1)
{
temp = in;
delay();
while(temp==1)
{
for(c=0;c<8;c++)
{
if(in)
{
i |= (1<<c);
}
delay();
}
temp = in;
delay();
if(temp==0)
{
f=i;
}
}
}
}
void delay(void)
{
unsigned char c,d;
for(c=0;c<=200;c++)
{
for(d=0;d<=200;d++)
{
}
}
}
c)Program to transmit and receive character, string, integer and floating point number using serial
communication of 8051
#include<Intel\8051.h>
#include"uart.h"
main()
{
char *str1;
str1=UART_Read();
InitUART();
UART_str("welcome to SGGS\n\r");
UART_Int(1234);
UART_str("\n\r");
UART_str(str1);
UART_Float(56.78);
while(1);
{
}
}
void InitUART();
void UART_Tx(char);
char UART_Rx();
void UART_str(char *);
void UART_Int(unsigned int);
void UART_Float(float);
char buff;
void InitUART()
{
SCON=0x50;
TMOD=0x20;
TH1=TL1=-3;
TR1=1;
}
void UART_Tx(char c)
{
SBUF=c;
while(!TI);
TI=0;
}
char UART_Rx()
{
while(!RI);
RI=0;
return SBUF;
}
void UART_Float(float f)
{
float x;
int y;
y=f;
UART_Int(y);
UART_Tx('.');
x=(f-y)*100;
y=x;
UART_Int(y);
}
char *UART_Read()
{
char *str;
while(1)
{
while(!RI);
if(RI==1)
{
*str++=SBUF;
RI=0;
}
else
{
EXPERIMENT NO. 14
#include<reg51.h>
#include"delays.h"
#define LCD P0
#define LINE1 0x80
#define LINE2 0xC0
sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;
bit flag;
void InitLCD();
void CmdLCD(char);
void DisplayLCD(char);
void WriteLCD(char);
void StrLcd(char *);
void IntLCD(unsigned int);
void FloatLCD(float);
void InitLCD()
{
CmdLCD(0x28);
CmdLCD(0x0e);
CmdLCD(0x01);
CmdLCD(0x06);
}
void CmdLCD(char c)
{
flag=0;
WriteLCD(c);
}
void DisplayLCD(char c)
{
flag=1;
WriteLCD(c);
}
void WriteLCD(char c)
{
LCD=(c&0xf0);
if(flag==1)
rs=1;
en=1;
DelayMS(1);
en=0;
LCD=((c&0x0f)<<4);
if(flag==1)
rs=1;
en=1;
DelayMS(1);
en=0;
DelayMS(2);
}
main()
{
InitLCD();
CmdLCD(LINE1);
DisplayLCD('v');
CmdLCD(LINE2);
StrLCD("LCD 4 BIT");
while(1)
{
}