M PLC Experiments
M PLC Experiments
M PLC Experiments
1
AIM: - Familiarization with 8051 microcontroller training board and demonstration of
writing and executing a program on KEIL software compiler.
Experiment No. 2
again: ;label
here1: djnz r4, here1 ;dec. r4 and jump to here1 if it is not zero
;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 0003h
here1: djnz r4, here1 ;dec. r4 and jump to here1 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
#include<reg51.h>\\header file
\\function prototype
voidlcdcmd(unsigned char);
voidlcddata(unsigned char);
sbitrw = P3^6;
sbit en = P3^5;
void main () {
while(1)
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);
P2= value;
rs = 0;
rw = 0;
en = 1;
delay(1);
en = 0;
return;
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;
}
}
}
#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);
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);
unsignedinti,j;
for(i=0;i<time;i++)
for(j=0;j<1000;j++);