Discussion in "Project Help" started by    CitronsG6    Jun 4, 2017.
Sun Jun 04 2017, 04:06 PM
#1
Good Day,
Need some help with my code, i am working on a code for a density based traffic system and i am trying to understand how to write a loop for when my some of my inputs are on but i am having difficult in getting results. i am a beginner at c programming, so in case i am make very obvious errors please take it easy on me. below is my the code i am working on.
#include<reg52.h>


sbit in1_na=P2^0; 	//sensor1	
sbit in2_nb=P2^1;  	//sensor2
sbit in1_wa=P2^2;  	//sensor3
sbit in2_wb=P2^3;   //sensor4
sbit in1_sa=P2^4;   //sensor5
sbit in2_sb=P2^5;		//sensor6
sbit in1_ea=P2^6;		//sensor7	
sbit in2_eb=P2^7;		//sensor8

sbit out1_ng=P0^0; //north light green
sbit out2_ny=P0^1; //north light yellow
sbit out3_nr=P0^2; //north light red

sbit out1_wr=P0^3; //west light red
sbit out2_wy=P0^4; //west light yellow
sbit out3_wg=P0^5; //west light green

sbit out1_eg=P1^0; //east light green
sbit out2_ey=P1^1; //east light yellow
sbit out3_er=P1^2; //east light red

sbit out1_sr=P1^3; //south light red
sbit out2_sy=P1^4; //south light yellow
sbit out3_sg=P1^5; //south light green

unsigned int i,j,k=0,v1=0,w1=0,x1=0,y1=0,t1=0;

void delay_2sec(); 
void delay_5sec();
void delay_12sec();

void main()
{
	
out1_wr=1; //led on
out2_wy=0; //led off
out3_wg=0; //led off

out1_sr=1; //led on
out2_sy=0; //led off
out3_sg=0; //led off

out1_eg=0;  //led off
out2_ey=0;  //led off
out3_er=1;  //led on

out1_ng=1; //led on
out2_ny=0; //led off
out3_nr=0; //led off
	
delay_5sec();  //delay for 5sec

out3_er=1;					//led off
out2_ey=0;          //led off
out1_eg=0;          //led on
out1_ng=0;					//led off
out2_ny=1;           //led on
out3_nr=0;           //led off

delay_2sec(); 	//delay for 2sec

out1_ng=0;          //led off
out2_ny=0;          //led off
out3_nr=1;          //led on

out1_eg=1;           //led on
out2_ey=0;           //led off
out3_er=0;           //led off

out1_sr=1;          //led off
out2_sy=0;          //led on
out3_sg=0;          //led off

out1_wr=1;         //led on
out2_wy=0;         //led off
out3_wg=0;         //led off

delay_5sec();  //delay for 5sec

out3_er=0;					//led off
out2_ey=1;          //led off
out1_eg=0;          //led on
out1_sr=1;					//led off
out2_sy=0;           //led on
out3_sg=0;           //led off

delay_2sec(); 	//delay for 2sec

out1_ng=0;          //led off
out2_ny=0;          //led off
out3_nr=1;          //led on

out1_eg=0;           //led on
out2_ey=0;           //led off
out3_er=1;           //led off

out1_sr=0;          //led off
out2_sy=0;          //led on
out3_sg=1;          //led off

out1_wr=1;         //led on
out2_wy=0;         //led off
out3_wg=0;         //led off

delay_5sec();  //delay for 5sec

out1_sr=0;					//led off
out2_sy=1;           //led on
out3_sg=0;           //led off
out1_wr=1;         //led on
out2_wy=0;         //led off
out3_wg=0;         //led off

delay_2sec(); 	//delay for 2sec

out1_ng=0;          //led off
out2_ny=0;          //led off
out3_nr=1;          //led on

out1_eg=0;           //led on
out2_ey=0;           //led off
out3_er=1;           //led off

out1_sr=1;          //led off
out2_sy=0;          //led on
out3_sg=0;          //led off

out1_wr=0;         //led on
out2_wy=0;         //led off
out3_wg=1;         //led off


while(1)
{
v1=0;																//clear v1
w1=0;																//clear w1
x1=0;                               //clear xl 
y1=0;                               //clear y1

if(in1_na==1)                         //if sensor1 is blocked
{
	v1=1;
	break;
	}
	
if((in1_na==1)&&(in2_nb==1))  //if sensorl & sensor2 are blocked
{
v1=2;
	break;
}

if(in1_wa==1)                     //if sensor3 is blocked
{
w1=1;
	break;
}	

if((in1_wa==1)&&(in2_wb==1))		//if sensor3 and sensor4 are blocked
{
w1=2;
	break;
}

if(in1_sa==1)                         //if sensor5 is blocked
{
x1=1;
	break;
}
if((in1_sa==1)&&(in2_sb==1))  //if sensor5 & sensor6 are blocked
{
x1=2;
	break;
}

if(in1_wa==1)                     //if sensor7 is blocked
{
y1=1;
	break;
}	

if((in1_wa==1)&&(in2_wb==1))		//if sensor7 and sensor8 are blocked
{
y1=2;
	break;
}

for(k=0;k<=v1;k++)
{
delay_2sec();
}
out3_wg=1;

for(k=0;k<=w1;k++)
{
delay_5sec(); //delay for 5sec
}
out2_ny=1;         //led on
out3_nr=1;          //led off
out2_sy=1;           //led on
out3_sg=1;           //led off

delay_2sec();

out1_eg=1;          //led off
out2_ey=1;          //led off
out3_er=1;          //led on

out1_sr=1;         //led on
out2_sy=1;           //led off
out3_sg=1;           //led off

out1_ng=0;         //led on
out2_ny=1;         //led off
out3_nr=1;        //led off

out1_wr=1;           //led off
out2_wy=1;           //led off
out3_wg=0;           //led on

delay_2sec();
out1_ng=1;
}
}

/*******************12s delay*****************/

void delay_12sec()
{
for(j=0;j<12;j++)
{
for(i=0;i<1000;i++)
{

TMOD=0X01;          // SET THE TIMER IN MODE 1 MODE
TL0=0XFE;           // LOAD THE THO VALUE
TH0=0XFB;           // LOAD THE TL0 VALUE
TR0=1;              // START THE TIMER0
TF0=0;
while(TF0==0); // WAIT FOR TIMER OVERFLOWS
TF0=0;              // CLAEAR THE TIMER0 OVER FLOW FLAG
TR0=0;      //STOP THE TIMER
}
}
}

/*******************5s delay*****************/

void delay_5sec()
{
for(j=0;j<5;j++)
{
for(i=0;i<1000;i++)
{

TMOD=0X01;          // SET THE TIMER IN MODE 1 MODE
TL0=0XFE;           // LOAD THE THO VALUE
TH0=0XFC;           // LOAD THE TL0 VALUE
TR0=1;              // START THE TIMER0
TF0=0;
while(TF0==0); // WAIT FOR TIMER OVERFLOWS
TF0=0;              // CLAEAR THE TIMER0 OVER FLOW FLAG
TR0=0;      //STOP THE TIMER
}
}
}

/****************** 2s delay ********************/	

void delay_2sec()
{
for(j=0;j<2;j++)
{
for( i=0;i<1000;i++)
{
TMOD=0X01;  //SET THE TIMER0 IN MODE 1 MODE
TL0=0XFE;  //LOAD THE TH0 VALUE
TH0=0XFD;  //LOAD THE TL0 VALUE
TR0=1;     //START THE TIMER0
TF0=0;
while(TF0==0); //WAIT FOR TIMER0 OVERFLOWS
TF0=0;      //CLEAR THE TIMER0 OVER FLOW FLAG
TR0=0;      // STOP THE TIMER

}
}
}



Mon Jun 05 2017, 08:47 PM
#2


i am a beginner at c programming, so in case i am make very obvious errors please take it easy on me. below is my the code i am working on.


No problem, we are here to help beginners.

I have attached a re-worked version of your code to help to see what it does.

The main problem is the break statements in your while(1) loop.
Every "break" statement causes your code to exit the while(1) loop.
if(in1_na==1)                         //if sensor1 is blocked
{
	v1=1;
	break;  //"break" out of while loop
	}


In this case there is nowhere to go outside the loop, so the program will crash.



Attachment


[ Edited Fri Jun 09 2017, 05:50 AM ]

Get Social

Information

Powered by e107 Forum System

Conversation

ExperimenterUK
Wed Jul 26 2017, 07:51 PM
Please post questions in the forum, the chat box is for chat
msd007
Wed Jul 26 2017, 11:27 AM
i need code if any one have aduc706 interfacing lcd 16*2
msd007
Wed Jul 26 2017, 11:26 AM
pl help me how to write code of interfacing ADuc7026 controlleo lcd pl any one help me hoe to initializend configure the direction
rammichael
Wed Jul 26 2017, 06:04 AM
can you explain what is timer and how to use their modes ...
rammichael
Wed Jul 26 2017, 06:03 AM
ExperimenterUk thank you for your quick reply okay
ExperimenterUK
Wed Jul 26 2017, 01:43 AM
rammichael & Sarvn We will help,but mainly by answering questions about problems.
rammichael
Tue Jul 25 2017, 10:59 AM
i want few tips to learn to become an expert in embedded systems ...?anybody is there to help me?
Sharmi
Sun Jul 23 2017, 04:13 AM
Sir, I wants to make a digital clock with RTC using AT89C51 or AT89C2051 and seven segment led. Sir will u help me?
Sarvn
Sat Jul 22 2017, 11:42 AM
I wnt to learn embedded could anyone help me
Sarvn
Sat Jul 22 2017, 11:41 AM
Hi

Downloads

Comments

msd007
Wed Jul 26 2017, 11:23 AM
hmn16
Wed Jul 26 2017, 01:04 AM
rammichael
Tue Jul 25 2017, 07:12 AM
Rohanmore26
Tue Jul 25 2017, 04:26 AM
AlFaller
Mon Jul 24 2017, 02:31 PM
Sarvn
Sat Jul 22 2017, 11:34 AM
diablom
Fri Jul 21 2017, 01:44 PM
Alihaider.007
Thu Jul 20 2017, 03:07 PM

Online

Guests: 49, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37866
Newest member: msd007
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Jul 26 2017, 07:35 PM
A byte such as 0xfa will not pass through the sys...
hmn16 Avatar
Posted By hmn16
Wed Jul 26 2017, 01:08 AM
Hello, I am current;y trying to send a hexadecimal...
Rohanmore26 Avatar
Posted By Rohanmore26
Tue Jul 25 2017, 04:29 AM
Hello I want to know How 3D stitching of 2 3D imag...
AlFaller Avatar
Posted By AlFaller
Mon Jul 24 2017, 02:40 PM
Here is a lot information about using virtual COM ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Thu Jul 20 2017, 04:08 PM
can it be possible ?? Alihaider.007No.You will nee...
Alihaider.007 Avatar
Posted By Alihaider.007
Thu Jul 20 2017, 03:14 PM
Sir I am using AT89S8253 I have trying to use this...
Utsavi Avatar
Posted By Utsavi
Mon Jul 17 2017, 09:57 AM
Hello Ajay_Bhargav,Thanks for your reply. Yes, you...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Jul 17 2017, 06:41 AM
I do not think it would be a good idea to kind of ...
ajay_bhargav Avatar
Posted By ajay_bhargav
Mon Jul 17 2017, 06:32 AM
Tasking EDE has linker script facility available y...
Ramweit Avatar
Posted By Ramweit
Thu Jul 13 2017, 06:58 AM
Thank you for response,Sim300 gsm Modem am using h...