Traffic Control System - 8051

Thread Starter

hira287

Joined May 8, 2009
7
Hello everyone!

This is my first post over here. The program code I made is that of a traffic light control system and it is not working properly, the way it is supposed to work.

The question statement is as below:


You have to implement an intelligent traffic control system using 89C52/8051 µcontroller, which operates on the basis that how many cars are present on the road.

The roads are served in clock wise fashion (north, east, south and west). During normal load on road, traffic signal remains green for 30seconds.
An entry from the key pad (4*3) determines the load(0-99) on that road. There are also 2 switches connected for selecting a road. (00ànorth, 01àeast, 10àsouth, 11àwest).

There is also a switch which a pedestrian can press at any moment and that road’s signal goes red [so the person can pass]. Handle the situation intelligently.

You have to decide the time of each road on basis of its load, if it’s above normal then you have to give more time for green light and vice versa. Handle the situation intelligently.

The time counter is also displayed on 7-segement displays.

And the program I have so far is this:


Rich (BB code):
org 000h
ljmp main


org 0003h; external interrupt subroutine - P3.2
ljmp ext0

org 0013h
ljmp ext1
                            ;~~~~~~~~~~~
                              ;MAIN
org 0030h 
                            ;~~~~~~~~~~~
main:

;-----------------
clr P2.4
clr P2.5
clr P2.6
clr P2.7

MOv TMOD, #11h; timer 1, mode 1 -16bit, timer 0 mode 1
mov ie, #10000101b
setb tcon.0; to set edge level interrupt
setb tcon.2
main2:
north:
setb p1.4; 1.4= north; 1=green, 0=red
clr p1.5
clr p1.6
clr p1.7
JB p2.4, north_manual
jnb P2.4, normal1
normal1:
mov R2, #30
lcall counter

east:
setb p1.5
clr p1.4
clr p1.6
clr p1.7
jb p2.5, east_manual
jnb p2.5, normal2
normal2:
mov R2, #30
lcall counter

south:
setb p1.6
clr p1.4
clr p1.5
clr p1.7
jb p2.6, south_manual
jnb p2.6, normal3
normal3:
mov R2, #30
lcall counter

west:
setb p1.7
clr p1.4
clr p1.5
clr p1.6
jb p2.7, east_manual
jnb p2.7, normal4
normal4:
mov R2, #30
lcall counter

sjmp main2

RET

north_manual:
mov b, r3
mov R2, b
lcall counter
RET
east_manual:
mov b, r4
mov R2, b
lcall counter
RET
south_manual:
mov b, r5
mov R2, b
lcall counter
RET
west_manual:
mov b, r6
mov R2, b
lcall counter
RET
;~~~~~~~~~~~~~~~~~~~~~~~
; Subroutine for keypad
;~~~~~~~~~~~~~~~~~~~~~~~
KEYPAD:
mov P2,#00000111b; convert the first three pins to input


K1: mov P1, #00h
    mov A, P2
    anl A, #00000111b
    cjne A, #00000111b, K1

K2: lcall delay
    mov A, P2
    anl A, #00000111b
    cjne A, #00000111b, over
    sjmp K2

over: lcall delay
      mov A, P2
      anl A, #00000111b
      cjne A, #00000111b, over1
      sjmp K2
over1: setb p1.0; ground row 0
       mov A, P2
       anl a, #00000111b
       cjne A, #00000111b, Row_0
       
       setb p1.1; ground row 1
       mov A, P2
       anl a, #00000111b
       cjne A, #00000111b, Row_1
       
       setb p1.2; ground row 2
       mov A, P2
       anl a, #00000111b
       cjne A, #00000111b, Row_2
       
       setb p1.3; ground row 3
       mov P1, #11110111b
       mov A, P2
       anl a, #00000111b
       cjne A, #00000111b, Row_3
       
       ljmp K2
       
Row_0: mov DPTR, #CODE0
       sjmp find
Row_1: mov DPTR, #CODE1
       sjmp find
Row_2: mov DPTR, #CODE2
       sjmp find
Row_3: mov DPTR, #CODE3
       sjmp find
find: rrc A
      jnc match
      inc dptr
      sjmp find
match: clr A
       movc A, @A+dptr
       ;    traffic db 0; traffic=variable to store traffic
      
       ljmp K1

org 0400h

code0: DB   '0', '1', '2'
code1: DB   '4', '5', '6'
code2: DB   '7', '8', '9'
code3: DB   '#', '0', '*'

RET

;-------

;CounterandKeypad
traffic:
acall keypad
subb A, #30
mov B, #10
mul AB
mov R1, A
acall keypad
subb A, #30
add A, R1
mov R2, A; saving a copy of A
RET

counter:

back7: mov A, R2; R2 contains traffic, average=30
mov B, #10
div AB


mov R1, A; save a copy of A
mov A, B
mov DPTR, #temp
movc A, @A+DPTR
clr p0.7 ; p0.7 and p2.3 --> selection lines
setb p2.3; select 1st Led, common cathode selection
mov P0, A

ljmp shortDelay
mov A, R1; reload A for second digit
movc A, @A+DPTR
setb p0.7; second led
clr p2.3
mov P0, A
lcall delayOneSec
djnz R2, back7

temp: DB 3Fh, 06h, 5Bh, 4Fh, 66h, 6D, 7Dh, 07h, 7Fh, 67h; hex codes for 0-9 

RET




;~~~~~~~~~~~~~~~~~~~~~~
;for External Interrupt
;~~~~~~~~~~~~~~~~~~~~~~
ext0:
;Subroutine for Selecting Roads

select:



jb p3.4, label1
jnb p3.4, label2
label1: jb p3.5, label3; for 3
jnb p3.5, label; for 2
label2: jb p3.5, labelE1
jnb p3.5, labelN0

labelE1:; for east
acall traffic
setb p2.5; bit to indicate that a manual traffic has been entered
mov r4,A
 
labelN0:; for north

lcall traffic
setb p2.4

mov R3,A

label3:; for west
lcall traffic
setb p2.6
mov r6, A

label:; for south
lcall traffic
setb p2.7
mov R6, A
reti
;~~~~~~~~~~~
ext1:
;~~~~~~~~~~~

jb p0.0 ,n_p        ;program if interrupt was called when noth's green light was on
jb p0.1,e_p        ;east
jb p0.3,w_p        ;west
jb p0.2,s_p

n_p:
clr p1.4 
clr p1.5
clr p1.6
clr p1.7
mov R1, #30
lcall counter
ljmp north

e_p:
clr p1.4 
clr p1.5
clr p1.6
clr p1.7
mov R1, #30
lcall counter
ljmp east

w_p:
clr p1.4 
clr p1.5
clr p1.6
clr p1.7
mov R1, #30
lcall counter
ljmp west

s_p:
clr p1.4 
clr p1.5
clr p1.6
clr p1.7
mov R1, 30
lcall counter
ljmp south

reti
;~~~~~~~~~~~~


delayOneSec:

mov r7,#14
rept1:
mov tl1,#00h
mov th1,#00h
setb tr1
again: 
jnb tf1,again
clr tf1
clr tr1
djnz r7,rept1
;ret
/*mov TMOD, 01h; Timer 0, mode 1- 16 bit
mov TL0,#00h
mov TH0,#00h
setb TR0
here:jnb TF0,here
djnz R4, back2     */

RET
;~~~~~~~~~~~~~~~~~~~~~~~~~



delay: ;Delay for 20ms


mov tl0,#0B7h
mov th0,#0FEh
setb TR0
again1: JNB TF0, again1
clr TR0
clr TF0
RET



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ShortDelay:      ; delay for 2.5ms
mov R7, #9
here9:mov R6, #255
here2:DJNZ R0, here2
here3:DJNZ R7, here9

RET
END
It doesn’t work on Proteus, the traffic lights do not go on and off with the one second delay, and my display function on a 2 digit seven segment, doesn’t show anything. The interrupts and the keypad don’t work either. I am not very good at assembly and in detecting errors, especially the logical ones so I would greatly appreciate it if someone could help.

Thanks.
 
Top