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
Sun Feb 04 2018, 09:38 PM
Hi Dave. Good to see you.
DavesGarage
Tue Jan 09 2018, 07:55 PM
It's been a few years since I poked around here - Hi all...
ExperimenterUK
Mon Jan 08 2018, 07:19 PM
Hi TPS. it's been a while..
۞ TPS ۞
Sun Jan 07 2018, 06:42 PM
hello
Thu Jan 04 2018, 01:50 PM
sayyad affrid
its AT89S52
Thu Jan 04 2018, 01:50 PM
sayyad affrid
sir, i want embedded c code for ultrasonic sensor having buzzer interfaced with AT9S52
Mon Dec 18 2017, 08:12 PM
zafir
i am making digital voltmeter using AT89C51, ADC and LCD 16x2,,, kindly someone please send me hex file in assembly language
Fri Dec 15 2017, 12:18 PM
mehari
can not find dhtxx.md file in proteus 8.0 can anybody help me please?
sahar7478
Fri Dec 08 2017, 12:47 PM
can i get gsm a6 library? can anybody help? tia
Faiqa
Sat Nov 25 2017, 08:32 AM
Can i get the code of Monitering and controlling of green house effect? ... Can anyone help me ?

Downloads

Comments

justotemon
Sat Feb 17 2018, 07:14 AM
nicolekristen
Fri Feb 16 2018, 12:19 PM
pavithrann
Wed Feb 14 2018, 04:46 PM
Tue Feb 13 2018, 03:18 PM
SDN1964
Sun Feb 11 2018, 12:00 PM
Nickynet
Sat Feb 10 2018, 11:02 PM
paulfjujo
Thu Feb 08 2018, 09:01 AM
holdthedoor264
Tue Feb 06 2018, 06:49 AM

Online

Guests: 59, Members: 0 ...

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

Members: 37963
Newest member: justotemon
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Feb 14 2018, 08:43 PM
>strange to don't define the way Write or Read..fo...
paulfjujo Avatar
Posted By paulfjujo
Wed Feb 14 2018, 02:25 PM
hello,Thank for your answer, but today i wrote thi...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 13 2018, 09:43 PM
Glad you sorted it.I'm sure you will remember if f...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 13 2018, 09:38 PM
>When i initialise UART_Init (9600); i get error=1...
Martinv Avatar
Posted By Martinv
Tue Feb 13 2018, 09:26 PM
Mmmm feel quite silly, but learned a lot. I must ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 13 2018, 08:07 PM
Can you zip and attach your full code.Using a seco...
paulfjujo Avatar
Posted By paulfjujo
Tue Feb 13 2018, 12:28 PM
hello,i tried this litle code with MikroC for 8051...
Martinv Avatar
Posted By Martinv
Tue Feb 13 2018, 09:22 AM
Thanks for the reply. The part of the main loop t...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 13 2018, 01:12 AM
Unless "READSMS" has some very long delays I doubt...
paulfjujo Avatar
Posted By paulfjujo
Sun Feb 11 2018, 06:52 PM
hello,The chip is probably locked.Why not build t...