Traffic Light Controller
Written by Amol Shah, on Sep-2008
Views 1129    

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 th0,#3ch

                mov tl0,#0B0h

                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

               

                mov current_signal_duration,#DURATION_AMBER

                setb d_green

                clr d_amber       

                jnb signal_f,$

                clr signal_f

                ajmp loop

 

$include(timer.inc)

End

 

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



Add this page to your favorite Social Bookmarking websites
Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!
Quote this article in website Favoured Print Send to friend Related articles Save this to del.icio.us

Users' Comments (0) RSS feed comment

No comment posted

Add your comment



mXcomment 1.0.5 © 2007-2009 - visualclinic.fr
License Creative Commons - Some rights reserved
 
Next >
Home arrow Code Library arrow 8051 ASSEMBLY arrow Traffic Light Controller