You are on page 1of 38

EXPERIMENT NO.

1
AIM: - Familiarization with 8051 microcontroller training board and demonstration of
writing and executing a program on KEIL software compiler.
Experiment No. 2

AIM: Program to ADD and MULTIPLY two numbers .

ORG 0000H ; start from memory location 0000h


MOV A,#04h ; move 04(hex data ) into accumulator
MOV R1,#05h ; move 05h into R1
ADD A,R1 ; add contents of accumulator and R1
MOV P0,A ; move result stored in accumulator to port 0
END ; End of the program
Exp:- Program to Multiply Two numbers.
org 0000h ;start from memory location 0000h

mov a,#03h ;move 03h to acc

mov b,#04h ;move 04h to b reg.

mul ab ;multiply acc and b reg.

mov p0,a ;copy acc to port 0

mov p2,b ;copy b reg. to port 2

end ; end of the program


Exp:- Program to Generate Square Wave
on P1.2
org 0000h ;start from the memory location 0000h

again: ;label

setb p1.2 ;set bit 2nd of port 1

acall delay ;give some time

clr p1.2 ;clear bit or make it equal to zero

acall delay ;again call delay sub.

sjmp again ;perform this task again

delay: ;delay sub.

mov r3, #50 ;move 50 in r3

here: mov r4, #255 ;move 255 in r4

here1: djnz r4, here1 ;dec. r4 and jump to here1 if it is not zero

djnz r3, here ;dec. r3 and jump to here if it is not zero

ret ;return from subroutine


Exp:- Program For Blinking the
LED. In this program delay is
produced using timer 1.
org 0000h ;start from the memory location 0000h
start: mov p1,#00h ;send 00h to port 1 or 00000000b
acall delay ;give some time
mov p1,#0ffh ;send 11111111b to port 1
acall delay ;give some delay
sjmp start ;short jump to start label

;delay sub.
delay:mov tmod,#10h ;timer 1 mode 1
mov r3,#05h ;store 05h in r3
again:mov TL1,#08h ;load TL1 with 08h
setb tr1 ;start timer
back: jnb tf1,back ;jump to back if tf1 is not equal to 1
clr tr1 ;stop timer
clr tf1 ;clear the flag
djnz r3,again ;dec r3 and jump to again if it is not equal to zero
ret ;ret from sub.
end ;end of program
Exp:- Program to Study the Interrupt0 of
8051 .
:pin 3.2 is used for int 0

org 0000h ;start fom 0000h

ljmp main ;bypass the int vector table

;int sub routine for int 0

org 0003h

mov p1,#00h ;move 00h to port 1

acall delay ;give some delay

mov p1,#0ffh ;move ffh to port1

acall delay ;give some delay

mov p1,#55h ;move 55h to port1


acall delay ;call delay sub.

mov p1,#0aah ;movaah to port1

acall delay ;call delay sub.

reti ;return from interrupt

main: ;main function

setb p3.2 ;make p3.2 as input pin

mov ie,#0ffh ;activate all interrupts

mov p2,#00h ;mov 00h to port 2

acall delay ;delay sub.

mov p2,#0ffh ;move ffh to port 2

acall delay ;call delay sub.

mov p2,#55h ;move 55h to port 2

acall delay ;call delay sub.

mov p2,#0aah ;move aah to port 2

acall delay ;call delay sub.

sjmp main ;again perform this task


delay: ;delay sub.

mov r3, #255 ;move 50 in r3

here: mov r4, #255 ;move 255 in r4

here1: djnz r4, here1 ;dec. r4 and jump to here1 if it is not zero

djnz r3, here ;dec. r3 and jump to here if it is not zero

ret
Exp:- This program adds two numbers and
displays the result on 16x2 LCD
org 0000h ;start from memory location 0000h
mov a, #38h ;inti. LCD 2 lines ,5x7 matrix
acall command ;call command sub.
acall delay ;give lcd some time

mov a, #0eh ;display on cursor on


acall command ;call command sub.
acall delay ;give lcd some time

mov a, #01h ;clear lcd


acall command ;call command sub.
acall delay ;give lcd some time

mov a, #80h ;cursor at line 1 position 1


acall command ;call command sub.
acall delay ;give some time

mov a, #01 ;store 01 h in acc


add a, #02 ;add 02h into acc.
cjne a, #0ah, next1 ;compare and jump if acc. is not equal to 0ah to next 1 label
next1: ;label
jc next ;jump if carry flag is set
add a , #07h ;add 07h to acc.
next: add a, #30h ;add 030h to acc.
acall data1 ;call data display sub.
acall delay ;give lcd some time

BACK: SJMP BACK ;stay here

delay: ;delay sub.


mov r3, #50 ;move 50 in r3
here: mov r4, #255 ;move 255 in r4
here1: djnz r4, here1 ;dec. r4 and jump to here1 if it is not zero
djnz r3, here ;dec. r3 and jump to here if it is not zero
ret ;return from subroutine

command: ;command sub.


mov p2, a ;move data of acc to port 2
clr p3.7 ;rs=0 for command
clr p3.6 ;r/w =0 for write
setb p3.5 ;e=1 for high pulse
clr p3.5 ;e=0 for high to low pulse
ret ; return from sub.
data1: ;sub for displaying data

mov p2, a ;copy reg a to port 2


setb p3.7 ;RS=1 for data
clr p3.6 ;R/W =0 for write
setb p3.5 ;E=1
clr p3.5 ;E=0 for high to low pulse
ret ;return from sub.
end ;end of the program
Exp:- This Program Display Character on
16x2 LCD.
org 0000h ;start from memory location 0000h
mov a, #38h ;inti. LCD 2 lines ,5x7 matrix
acall command ;call command sub.
acall delay ;give lcd some time

mov a, #0eh ;display on cursor on


acall command ;call command sub.
acall delay ;give lcd some time

mov a, #01h ;clear lcd


acall command ;call command sub.
acall delay ;give lcd some time

mov a, #80h ;cursor at line 1 position 1


acall command ;call command sub.
acall delay ;give some time

mov a,#'H' ;move character H into acc


acall data1 ;call data sub. to display data
acall delay ;give some time

mov a,#'E' ;move character E into acc


acall data1 ;call data sub. to display data
acall delay ;give some time

mov a,#'L' ;move character L into acc


acall data1 ;call data sub. to display data
acall delay ;give some time

mov a,#'L' ;move character L into acc


acall data1 ;call data sub. to display data
acall delay ;give some time

mov a,#'O' ;move character O into acc


acall data1 ;call data sub. to display data
acall delay ;give some time

BACK: SJMP BACK ;stay here

delay: ;delay sub.


mov r3, #50 ;move 50 in r3
here: mov r4, #255 ;move 255 in r4
here1: djnz r4, here1 ;dec. r4 and jump to here1 if it is not zero
djnz r3, here ;dec. r3 and jump to here if it is not zero
ret ;return from subroutine

command: ;command sub.


mov p2, a ;move data of acc to port 2
clr p3.7 ;rs=0 for command
clr p3.6 ;r/w =0 for write
setb p3.5 ;e=1 for high pulse
clr p3.5 ;e=0 for high to low pulse
ret ; return from sub.

data1: ;sub for displaying data


mov p2, a ;copy reg a to port 2
setb p3.7 ;RS=1 for data
clr p3.6 ;R/W =0 for write
setb p3.5 ;E=1
clr p3.5 ;E=0 for high to low pulse
ret ;return from sub.
end ;end of the program
Exp:- Program to Display characters on
16x2 LCD Display using String in C
language

#include<reg51.h>\\header file

\\function prototype

void delay(unsigned int );

voidlcdcmd(unsigned char);

voidlcddata(unsigned char);

sbitrs = P3^7; \\declaretion of control signals

sbitrw = P3^6;

sbit en = P3^5;

void main () {
while(1)

unsigned char str[]="GNDEC Electrical Engg. Dpt"; \\string

unsigned char i=0;

lcdcmd(0x38);

delay(50);

lcdcmd(0x0E);

delay(50);

lcdcmd(0x01);

delay(50);

lcdcmd(0x80);

delay(50);

while (str[i]!='\0'){

lcddata(str[i]);

if(i==15) {

lcdcmd(0xC0);
delay(50);

delay(1500);

i++;

delay(5000);

voidlcdcmd(unsigned char value)

P2= value;

rs = 0;

rw = 0;
en = 1;

delay(1);

en = 0;

return;

voidlcddata(unsigned char value)

P2=value;

rs=1;

rw=0;

en=1;

delay(1);

en=0;

}
void delay(unsigned int time)

unsignedinti,j;

for(i=0;i<time;i++)

for(j=0;j<50;j++);

}
Exp:- Program to Control Traffic light.

#include<reg51.h>
sbit led1 =P2^5; //P2.5 WEST(P)[28]
sbit led2 =P1^5; //P1.5 WEST(GREEN R)
sbit led3 =P2^0; //P2.0 WEST(GREEN S)
sbit led4 =P1^4; //P1.4 WEST(GREEN L)
sbit led5 =P1^6; //P1.6 WEST(YELLOW)
sbit led6 =P1^7; //P1.7 WEST(RED)
sbit led7 =P2^4; //P2.4 NORTH(P)[LED 8]
sbit led9 =P1^1; //P1.1 NORTH(GREEN1)
sbit led10 =P2^1; //P2.1 NORTH(GREEN2)
sbit led11 =P1^0; //P1.0 NORTH(GREEN3)
sbit led12 =P1^2; //P1.2 NORTH(YELLOW)
sbit led13 =P1^3; //P1.3 NORTH(RED)
sbit led14 =P2^7; //P2.7 EAST(P)[LED 15]
sbit led16 =P0^5; //P0.5 EAST(GREEN1)
sbit led17 =P2^2; //P2.2 EAST(GREEN2)
sbit led18 =P0^4; //P0.4 EAST(GREEN3)
sbit led19 =P0^6; //P0.6 EAST(YELLOW)
sbit led20 =P0^7; //P0.7 EAST(RED)
sbit led21 =P2^6; //P2.6 SOUTH(P)[LED 22]
sbit led23 =P0^1; //P0.1 SOUTH(GREEN1)
sbit led24 =P2^3; //P2.3 SOUTH(GREEN2)
sbit led25 =P0^0; //P0.0 SOUTH(GREEN3)
sbit led26 =P0^2; //P0.2 SOUTH(YELLOW)
sbit led27 =P0^3; //P0.3 SOUTH(RED)
void delay(unsigned int);
void main(void)
{
while(1)
{
{
led1=1;
led7=1;
led14=1;
led21=1;
led2=0;
led4=0;
led12=0;
led5=0;

led20=1;
led27=1;
led6=1;

led13=0;
led9=1;
led10=1;
led11=1;
led1=0;
delay(1000);
led9=0;
led10=0;
led11=0;
led12=1;
delay(200);
led12=0;
led13=1;
led1=1;
}
{
led20=0;
led16=1;
led17=1;
led18=1;
led7=0;
delay(1000);
led16=0;
led17=0;
led18=0;
led19=1;
delay(200);
led19=0;
led20=1;
led7=1;
}
{
led27=0;
led23=1;
led24=1;
led25=1;
led14=0;
delay(1000);
led23=0;
led24=0;
led25=0;
led26=1;
delay(200);
led26=0;
led27=1;
}
{
led6=0;
led2=1;
led3=1;
led4=1;
led21=0;
delay(1000);
led2=0;
led3=0;
led4=0;
led5=1;
delay(200);
led5=0;
led6=1;
led21=1;
}

}
}

void delay(unsigned int time)


{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<1500;j++);
}
Exp:- Program to Rotate Stepper Motor in
Clock Wise Direction.

#include<reg51.h>

sbit A = P0^2;

sbitBw = P0^3;

sbit C = P0^0;

sbit D = P0^1;

mskdelay(unsigned int);

void main(void)

while(1)

A=0;
Bw=0;

C=1;

D=1;

mskdelay(1);

A=0;

Bw=1;

C=1;

D=0;

mskdelay(1);

A=1;

Bw=1;

C=0;

D=0;

mskdelay(1);

A=1;

Bw=0;

C=0;
D=1;

mskdelay(1);

mskdelay(unsigned int time)

unsignedinti,j;

for(i=0;i<time;i++)

for(j=0;j<1000;j++);

}
Exp:- Program to Rotate Stepper Motor in
Anti Clock Wise Direction.
#include<reg51.h>

sbit A = P0^2;

sbitBw = P0^3;

sbit C = P0^0;

sbit D = P0^1;

mskdelay(unsigned int);

void main(void)

while(1)

{
A=1;

Bw=0;

C=0;

D=1;

mskdelay(50);

A=1;

Bw=1;

C=0;

D=0;

mskdelay(50);

A=0;

Bw=1;

C=1;

D=0;

mskdelay(50);

A=0;

Bw=0;
C=1;

D=1;

mskdelay(50);

mskdelay(unsigned int time)

unsignedinti,j;

for(i=0;i<time;i++)

for(j=0;j<1000;j++);

You might also like