# Problem with AT89C51 C source code (density based traffic light)

#### Jang_angol

Joined Feb 24, 2016
1
Hello everyone,
I am working on a 4 way density based traffic light controlled by AT89C51 microcontroller. The problem is, I dont know whether this source code is correct or not. I found this code and placed it in proteus and there are many errors detected.

C:
#include<reg51.h>
#define density_level P1
//void green_delay();
//Lights declaration
sbit ar = P0^0;
sbit ag = P0^1;
sbit br = P0^2;
sbit bg = P0^3;
sbit cr = P0^4;
sbit cg = P0^5;
sbit dr = P0^6;
sbit dg = P0^7;
//sensors declarartion
sbit IRaa=P1^0;
sbit IRab=P1^1;
sbit IRba=P1^2;
sbit IRbb=P1^3;
sbit IRca=P1^4;
sbit IRcb=P1^5;
sbit IRda=P1^6;
sbit IRdb=P1^7;
int a[]=
{
void main()
{
P1=0XFF;
P3=0X00;
P0=0X00;
P2=0X00;
ar=1;
br=1;
cr=1;
dr=1;
while(1)
{
int check_high;
bit a=0,b=0,c=0,d=0,high=0;
int lane_a,lane_b,lane_c,lane_d;
for (check_high=0;chec k_high<3;check_hig h++)
{
switch (density_level)
{
case 0XFC :
if(a==0)
{
ar = 0;
a=1;
for(lane_a=0;lane_a<9;lane_a++)
{
ag = 1;
}
ag = 0;
}
break;
case 0XF3 :
if(b==0)
{
br = 0;
b=1;
for(lane_b=0;lane_b<9;lane_b++)
{
bg = 1;
}
}
bg = 0;
break;
case 0XCF :
if(c==0)
{
cr = 0;
c=1;
for(lane_c=0;lane_c<9;lane_c++)
{ cg = 1;
}
} cg = 0;
break;
case 0X3F :
if(d==0)
{
dr = 0;
d=1;
for(lane_d=0;lane_d<9;lane_d++)

{
dg = 1;
}
}
dg = 0;
break;
}
P0 = P0&0XFF;
}
}
}
/*
{
while(high==1)
{
int check2;
for (check2=0;check2<2;check2++)
{
{
if(IRaa==0)
//check lane a
{
if(IRab==0)
{ ar = 0;
ag = 1;
green_delay();
ag = 0;
}
else
{
ag = 0;
//ay = 1;
//yellow_delay();
//ay = 0;
ar = 1;
}
}
}
{
if(IRba==0)
//check lane b
{
if(IRbb==0)
{
br = 0;
bg = 1;
}
else
{
bg = 0;
//by = 1;
//yellow_delay();
//by = 0;
br = 1;
}
}
}
{
if(IRca==0)
//check lane c
{
if(IRcb==0)
{
cr = 0;
cg = 1;
}
else
{
cg = 0;
//cy = 1;
//yellow_delay();
//cy = 0;
cr = 1;
}
}
}
{
if(IRda==0)
//check lane d
{
if(IRdb==0)
{
dr = 0;
dg = 1;
}
else
{
dg = 0;
//dy = 1;
//yellow_delay();
//dy = 0;
dr = 1;
}
}
}
}
}
}
}
*/ /*void green_delay()
{
int y;
for (y=0; y<1000; y++);
} */

Last edited by a moderator:

#### JohnInTX

Joined Jun 26, 2012
3,825
The problem is, I dont know whether this source code is correct or not. I found this code and placed it in proteus and there are many errors detected.
I don't know if its correct either. What errors are you talking about? Compiler errors, problems in the program logic or??

The problem with rooting around the internet for code is that you never know if its any good and copying others' work won't lead to any understanding of what's going on to help you fix it. Its far better to design your own solution, maybe after looking at others' code, code it, debug it and ship it. That way, you'll never have to worry about understanding it.

From experience I can tell you that you'll burn more time trying to make some internet junk work for your application than if you just sit down, solve the logic and write the code yourself.

Good luck.

Last edited:

#### dannyf

Joined Sep 13, 2015
2,197
there are many errors detected.
The code works. The errors are related to the thing about 3 to 8 lines below the middle. Have that variable named fixed, and replaced with the right one, the code burns into chip just fine.

#### JohnInTX

Joined Jun 26, 2012
3,825
The code works. The errors are related to the thing about 3 to 8 lines below the middle. Have that variable named fixed, and replaced with the right one, the code burns into chip just fine.
What are the line numbers and variable name you are referencing?

#### ErnieM

Joined Apr 24, 2011
8,007
I wonder how good can code be when over half the lines have been commented out?

#### JohnInTX

Joined Jun 26, 2012
3,825
I wonder how good can code be when over half the lines have been commented out?
Me too. I don't know if that was in the original code or the TS did it while debugging. Still waiting to hear from @dannyf .