Please update your Flash Player to view content.

                  FOLLOW  US





 
 

Deprecated: Function split() is deprecated in /home/dnatechi/public_html/components/com_jcomments/jcomments.php on line 1245

Deprecated: Function split() is deprecated in /home/dnatechi/public_html/components/com_jcomments/jcomments.php on line 1245
Traffic Light Controller
Code Library - 8051 Assembly
Written by Amol Shah   

Traffic Lights usually contain three lamps: red, amber and green. Here is a small project wherein you can control the Traffic Light of a four way intersection using a single controller. Consider four roads ROAD A, B, C and D and their corresponding signals red, amber and green each represented by a LED. So in all there are 12 LED’s each of which is connected to a controller pin.

In this code the duration of all green signals are the same and the duration of all amber signals are same. This duration is defined by DURATION_GREEN which is 10 seconds and DURATION_AMBER which is 2 seconds.

For generating delay we will be using Timer 0 in 16 bit INTERRUPT mode to generate One second delay. We load TH0 & TL0 with a specific value & on every clock cycle TH0 & TL0 increments by 1 and when the TIMER overflows from FFFF to 0000 a Timer0 overflow flag is set & interrupt is generated.  The maximum delay that can be generated using TIMER 0 in any mode is 65535 clock cycles & since we are using 12MHz crystal the maximum delay is 65.535ms. But we require 1second delay. So we generate 50ms delay using TIMER 0 & when 20 such delays have been generated we come to know that one second has elapsed. So basically 0.05ms X 20 =1 second. So we use a RAM location multiplier load it with 20 and whenever the timer overflows the ram location multiplier is decremented by one. Thus when multiplier becomes zero we come to know that one second has elapsed.

Another RAM location current_signal_duration is used it contains the total number of seconds for which the current signal has to be turned ON. This RAM location is decremented every time  one second has elapsed so when current_signal_duration becomes zero we come to know that it’s time to Turn OFF the current signal and move to the next signal.

//Define the Port PINS

a_red    bit           p0.0
a_amber    bit           p0.1
a_green     bit           p0.2
   
b_red bit           p0.3
b_amber   bit           p0.4
b_green      bit           p0.5
   
c_red  bit           p0.6
c_amber    bit           p0.7 
c_green   bit           p2.7
   
d_red  bit           p2.6
d_amber bit           p2.5
d_green  bit           p2.4

 

//Define RAM Locations

 multiplier equ        30h        
 current_signal_duration equ        31h        

 

//DEFINE BIT ADDRESS

signal_f             bit        00h  ;if 1 then switch to next signal   
   

 //DEFINE Constants

MULTIPLIER_DEFAULT  DATA     20
DURATION_GREEN DATA     10 
DURATION_AMBER   DATA     2
   

//TIMER SUBROUTINES

 
timer:

                push acc

                push b

                push psw

                push dph

                push dpl

                clr TF0

                mov th0,#3ch

                mov tl0,#0B0h

                call check_timer

                pop dpl

                pop dph

                pop psw

                pop b

                pop acc

                reti    

 
   

check_timer:

djnz multiplier,r1_check_timer 

 

 

mov multiplier,#MULTIPLIER_DEFAULT

;One second ended reload value

djnz current_signal_duration,r1_check_timer  
setb signal_f ;signal duration ended move to nxt signal 

r1_check_timer:

 
ret  
   

//MAIN Program

 

org 0000h

                jmp main

 
   

org 000bh

                jmp timer

 
   
org 0030h

main:

         mov multiplier,#MULTIPLIER_DEFAULT

 
setb EA                                                 ;Enable Interrupt
setb ET0        ;Enable timer 0 Interrupt

clr signal_f

mov TMOD,#01h

mov th0,#3ch

mov tl0,#0B0h

 // Timer 0 Mode 1(16 bit mode)
setb tcon.4 ;start timer
   
loop:   

mov  current_signal_duration

 ,#DURATION_GREEN

clr a_green

         setb a_amber

setb a_red

setb b_green

setb b_amber

clr b_red

setb c_green

setb c_amber

clr c_red

setb d_green

setb d_amber

clr d_red

 

  

 

 jnb signal_f,$

 clr signal_f                               

 

mov current_signal_duration

 ,#DURATION_AMBER

setb a_green

clr a_amber     

jnb signal_f,$

clr signal_f

 

 

 

mov current_signal_duration,

 #DURATION_GREEN

setb a_amber

clr a_red

clr b_green        

jnb signal_f,$

clr signal_f

 

 

 

mov current_signal_duration,     

 #DURATION_AMBER

 setb b_green

clr b_amber

jnb signal_f,$

clr signal_f

 

 

 

 mov current_signal_duration,

 #DURATION_GREEN

 setb b_amber  

clr b_red

clr c_green

jnb signal_f,$

clr signal_f

 

 

 

mov current_signal_duration,             

 #DURATION_AMBER

 setb c_green

clr c_amber

jnb signal_f,$

clr signal_f

 

 

 

mov current_signal_duration,             

 #DURATION_GREEN

 setb c_amber

clr c_red

clr d_green        

jnb signal_f,$

clr signal_f

 

End

 

 

For learning how to interface LED to 8051 microcontroller please click here.                                                                                                                                            

 

Comments  

 
-1 # F.2012 2011-11-20 19:47
hello
i just want to know if i can use this code for AT89C51
and what you mean by $include(timer.inc)
Reply | Reply with quote | Quote
 
 
0 # amol shah 2011-11-21 13:45
Quoting F.2012:
hello
i just want to know if i can use this code for AT89C51
and what you mean by $include(timer.inc)

Yes the code will work on AT89c51 as well....
$include(timer.inc) not needed in the code...have rectified the error
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-11-22 12:03
Hello
Thank you sir for the replay
I had change MULTIPLIER_DEFA ULT DATA 20 to
DURATION_Red DATA 20
would that make any difference?
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-11-22 14:41
You do not require duration_red we have defined the duration for the green and amber signal, the duration of the red signal will be set automatically......
MULTIPLIER_DEFA ULT DATA 20 is used to generate 1 second delay this 1 second is then in turn used to generate 10 second delay for green and 2 second delay for amber light
Reply | Reply with quote | Quote
 
 
0 # innocence 2011-11-29 20:18
hello,
I entered your code that is above in keil uvision 3 but I can not obtain the hex file that is need to download in AT89C51 and I do not know what is the problem because this the first time that I used this program
do you have that hex file?
Thank you
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-11-29 20:56
Click on Projects > Options for Target > Output
and then check create Hex file
Reply | Reply with quote | Quote
 
 
0 # innocence 2011-11-30 00:13
thank you for helping me
now I did this steps and I download the hex fiel in at89c51 but also I faced problem
after interfaced at89c51 by the 12 LEDs
I want to make sure if you did the circuit of traffic light contrller after writing this code
and does it work?
Reply | Reply with quote | Quote
 
 
0 # innocence 2011-11-30 00:35
please can you give me the hex file that you obtained, because I want to compare it with my hex file
please........
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-11-30 00:53
hello
i had use the code but the circuit did not work, all the lamps turn ON at the same time
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-11-30 08:31
Post your circuit diagram and code.....I will check it...all led wont be ON at the same time
did you connect the led as shown in the following article : dnatechindia.com/.../...
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-11-30 11:05
i had use the code above, the Hex file where like this

:10000000C0E0C0F0 C0D0C083C082C28D758C3C758A
:100010008AB0111FD082D083D0D0D0F0D0E032D5BA
:0B0020003008753014D53102D20022E8
:02000000802E50
:02000B0080F380
:10003000753014D2AFD2A9C20075 8C3C758AB0D28B
:100040008C75310AC282D281D280D285D284C28 399
:10005000D2A7D287 C286D2A4D2A5C2A 63000FDC242
:1000600000753102D282C2813000FDC20075310AB2
:10007000D281C280C2853000FDC200753102D285B6
:10008000C2843000FDC20075310AD284C283C2A787
:100090003000FDC200753102D2A7C2873000FDC218
:0F00A0000075310AD287C286C2A43000FDC200AB
:00000001FF

I had connect the LEDs as you did and I connect the reset pin to the VCC through capacitore and and other sid to the ground through resistor, I also connect EA pin to VCC, and I connect pin 18 and 19 to the Crystal and then to the ground
Reply | Reply with quote | Quote
 
 
+1 # Amol Shah 2011-11-30 12:21
Place the timer and check timer subroutine after the main program and the recompile
Schematic for traffic light control:
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-12-02 15:11
Hello
sir did you test the code befor?
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-12-02 18:07
Quoting F.2012:
Hello
sir did you test the code befor?

I have mailed you the complete details along with the working video of it. check and reply
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-12-03 16:53
I had try the code but still all the LED lamps turn ON at the same time
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-12-03 17:41
cross check your hardware......i have tested the code and its right
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-12-04 14:35
I had use 4MHz crystal will that make difference?
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-12-05 10:50
Not much....just the timing will change.
Reply | Reply with quote | Quote
 
 
0 # smantha 2011-12-12 01:13
can i get the details of this code and a working video,
and does this code work with 8051?
and what assembel to use..??
please reply me as soon as possible.
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-12-12 10:11
This code is for 8051 based controller, you can use keil compiler and assembler. In this code we have used a timer in interrupt mode for its working check this link:
dnatechindia.com/.../...
Other details have been explained above.
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-12-11 16:29
Hello
the circuit worked but there is Problem with c_red and d_red. when C_green turn on the the c_red keep working untill c_green turn OFF?
Reply | Reply with quote | Quote
 
 
0 # Amol Shah 2011-12-12 10:06
check if c_green pin is shorted to C-red pin
Reply | Reply with quote | Quote
 
 
0 # F.2012 2011-12-28 22:23
hello
sir I am still get the same problem the RED LED turn ON with green?.......
Reply | Reply with quote | Quote
 
 
+1 # Imran Mehmood 2012-01-01 13:01
sir plz send me whole details of dis project i mdoing this project plz sir mail me all details...
Reply | Reply with quote | Quote
 
 
-1 # F.2012 2012-01-02 19:36
hello
sir as you said the c_green pin is shorted to C-red pin
how can I solve this problem?
Reply | Reply with quote | Quote
 
 
-1 # Rk92269 2012-01-26 22:40
hello amol sir u r doing a great job..
I have taken this project so can u please forward the details of coding,hex file,video...

I am waiting
email= rohitkakde92269 @gmail.com
Reply | Reply with quote | Quote
 
 
0 # Mishthi 2012-02-28 12:49
sir plz mail me the circuit diagram with component specification..
i am working on this project currently..
waiting 4 ur response..
mishthi.fishy@gamail.com
Reply | Reply with quote | Quote
 
 
0 # hhhh 2012-04-14 14:28
hello sir ,
v r presently working on this project . your code helped us a lot . but we have one problem , we are unable to check the output and create hex file. could u pls explain us the steps to verify the code that is to see the output before burning it to the microcontroller .
my email id is : sons.2190@gmail.com
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

 

Featured Products

Rs1 800.00

Rs1 800.00

 

Random Products







Rs8.00



Rs320.00
Rs288.00
You Save: 10.00%



Latest Video

Random Articles

Search Products

User Login






 
 
.
 TUTORIALS
.
  8051 Tutorial
 
  Power Supply
 
CODE LIBRARY
.

  8051 Assembly
 
  C Code Library

.
 PROJECTS
.
  Project List
 
  Synopsis and Abstract
 
DOWNLOADS
.

  Datasheets
 
  Download

.
 POPULAR
.
  About Us
 
  DNA Shop
 
  Totorial
 
  Code Library
 
  Download
 

.
 CONTACT US
.

Mr. Amol Shah

DNA Technology Nashik,

Maharashtra 422009 India

Phone : 0253-3023939

Mobile : 09225145135

Email : contact@dnatechindia.com