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

Tue Sep 19 2017, 08:36 PM
Ted
In a missing pulse detector using a 555 timer connected as a monostable vibrator with a PNP transistor,how does the positve input pulse turn on the PNP transistor?
Sun Sep 03 2017, 10:29 AM
martymarty
How do I connect an <NOKIA 1100 LCD > to an atmega328P ?
poojab51296
Sun Aug 20 2017, 06:23 AM
i am intending to interface sd card with STM32F4 and want to write a file in sd card using SPI protocol but using HAL only. Can anyone help me PLEASE!!!!
poojab51296
Sun Aug 20 2017, 06:22 AM
hello
rawat742
Fri Aug 18 2017, 07:37 AM
hi @ angel patel i will attach a file i hope it will be helpfull for you
ExperimenterUK
Mon Aug 14 2017, 05:57 PM
@ angel patel... just log in and post your problem
Mon Aug 14 2017, 09:45 AM
angel patel
hey any one help to make the led scrolling display
desuza6
Sat Jul 29 2017, 03:09 AM
I see the code for step motor h l 298 to rotate 45 degree, but all delay time, if I want to input sensor to h the pulses to stepmotor
rammichael
Thu Jul 27 2017, 08:05 AM
okay fine
ExperimenterUK
Wed Jul 26 2017, 03:51 PM
Please post questions in the forum, the chat box is for chat

Downloads

Comments

butt_butt
Fri Sep 08 2017, 02:58 PM
RKTJ
Fri Sep 08 2017, 01:55 PM
ahmed2016
Thu Sep 07 2017, 05:55 AM
Johnleonarduc
Sat Sep 02 2017, 01:48 AM
Suseethra
Fri Sep 01 2017, 06:31 AM
ashokreddy424
Sat Aug 26 2017, 04:02 PM
abdullah6484
Tue Aug 22 2017, 10:21 PM
crank.tp
Mon Aug 21 2017, 02:17 PM

Online

Guests: 96, Members: 0 ...

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

Members: 37888
Newest member: butt_butt
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Sep 11 2017, 12:07 AM
The hardware for connecting to a PC or a modem is...
Suseethra Avatar
Posted By Suseethra
Sat Sep 09 2017, 09:43 AM
Hi,we are doing the same project(LED scrolling dis...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Aug 28 2017, 07:21 PM
First you have to decide which version of the 8051...
rawat742 Avatar
Posted By rawat742
Sat Aug 26 2017, 12:36 PM
Dear Sir, I want to purchase progra...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Aug 25 2017, 08:22 PM
Debuggers and programmers are different things.Whi...
rawat742 Avatar
Posted By rawat742
Thu Aug 24 2017, 04:30 PM
Dear Sir, if I am not purchase debugger then how ...
poojab51296 Avatar
Posted By poojab51296
Thu Aug 24 2017, 06:27 AM
I am using STM32 cube MX and Atollic truestudio so...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Aug 23 2017, 01:30 PM
It will be easier to help if you can post the comp...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Aug 23 2017, 01:24 PM
There is a company in china which makes 8051 micro...
ajay_bhargav Avatar
Posted By ajay_bhargav
Wed Aug 23 2017, 01:19 PM
Which library or driver are you using for SD card ...