You are on page 1of 4

#include<reg51.

h>
void delay(void);
void delay1(void);
void delay2(unsigned int value);

//intialization of variables
sbit sw1=P1^0;
sbit sw2=P1^1;
sbit sw3=P1^2;
sbit sw4=P1^3;
sbit sw5=P1^4;
sbit lr=P2^1;
sbit lg=P2^0;
sbit buz=P2^7;
sbit rr=P2^3;
sbit rg=P2^2;
sbit sg=P2^4;
sbit sy=P2^6;
sbit sr=P2^5;
sbit M=P1^5;

void main()
{
P1=0xff; //......Set P1 as input port
P2=0x00; //......Set P2 as output port
//.....Initial conditions of variables
sw1=1; sw2=1; sw5=1;
lab:
sr=1; sg=0; sy=1;
lr=0; rr=0;
lg=1; rg=1;
buz=1;
while(1) //.... Infinite loop
{
//........switches 1 & 2
if(sw1==0) //.....Loop for the condition- sw1 pressed (Detect train is
coming from left side)
{
sg=1;
sy=0;
buz=0;
delay();
sy=1;
buz=1;
sr=0;
delay1();
goto label;
label:
while(sw3==0) //....checking whether any vehicle is on the track
{
sg=1;
sr=0;
rr=0;
}
while(sw3==1) //....Loop for the condition- no vehicle is on the track
{
rr=1; rg=0;
M=0;
delay2(25);
M=1; //....gate close
goto label1;
label1:
while(sw2==0) //....Train has been passed
{
sr=1; sg=0; rg=1; rr=0;
}
if(sw1==1&&sw2==1)
{
M=0;
delay2(75);
M=1; //......gate open
goto lab;
}
}
}

//...........switches 4 & 5

while(sw4==0) //.....Loop for the condition- sw1 pressed (Detect train


is coming from left side on other track)
{
sg=1;
sy=0;
buz=0;
delay();
sy=1;
buz=1;
sr=0;
delay1();
goto label2;
label2:
while (sw3==0) //....checking whether any vehicle is on the track
{
sg=1;
sr=0;
lr=0;
}
while(sw3==1) //....Loop for the condition- no vehicle is on the track
{
lr=1; lg=0;
M=0;
delay2(25);
M=1; //.....gate close
goto label3;
label3:
while(sw5==0) //....Train has been passed
{
sr=1; sg=0; lg=1; lr=0;
}
if(sw4==1&&sw5==1)
{
M=0;
delay2(75);
M=1; //......gate open
goto lab;
}
}
}

while(sw1==0&&sw4==0)//train coming from opposite sides on each of the track sim


ultaneously
{
sg=1;
sy=0;
buz=0;
delay();
sy=1;
buz=1;
sr=0;
delay1();
goto label4;
label4:
while(sw3==0) //....checking whether any vehicle is on the track
{
sg=1;
sr=0;
rr=0;
lr=0;
}
while(sw3==1) //....Loop for the condition- no vehicle is on the track
{
rr=1; rg=0; lr=1; lg=0;
M=0;
delay2(25);
M=1; //....gate close
goto label5;
label5:
while(sw2==0&&sw5==0) //....Trains passed by
{
sr=1; sg=0; rg=1; rr=0; lg=1; lr=0;
}
if(sw1==1&&sw4==1)
{
M=0;
delay2(75);
M=1; //......gate open
goto lab;
}
}
}
}
}
//.....Functions for delay
void delay(void)
{
int i,x;
for(i=0;i<300;i++)
for(x=0;x<1275;x++)
{
}
}
void delay1(void)
{
int j, k;
for(j=0;j<1200;j++)
for(k=0;k<1275;k++)
{
}
}
void delay2(unsigned int value)
{
int m;
for(m=0;m<value;m++)
{
}
}

You might also like