GIDForums  

Go Back   GIDForums > Computer Programming Forums > Assembly Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 05-May-2009, 05:52
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 76
hira will become famous soon enough

Traffic Control Program 8051


Hello everyone!

I am supposed to write a program of a traffic control system. The statement of the question is as below:

Quote:
You have to implement an intelligent traffic control system using 89C52/8051 µcontroller, which operates on the basis of the number of cars 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.

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

The program I have written so far is this:


Code:
org 000h ljmp main org 0003h; external interrupt subroutine - P3.2 LJMP ext0 org 0013h ljmp ext1 ;~~~~~~~~~~~ ;MAIN org 0030h ;~~~~~~~~~~~ main: P0.0 BIT n; 1=green, 0=red P0.1 BIT e P0.2 BIT s P0.3 BIT w ;----------------- mov ie, 10000101b setb tcon.0; to set edge level interrupt setb tcon.2 north: setb n; north green, all others red clr e clr s clr w JB setBit_ne, north_manual lcall CountDown east: setb e; east green, all others red clr n clr s clr w jnb setBit_ne, east_manual lcall CountDown south: setb s clr e clr n clr w JB setBit_sw, south_manual lcall CountDown west: setb w clr e clr s clr n jnb setBit_sw, west_manual lcall CountDown RET north_manual: mov R1, north_traffic east_manual: mov R1, east_traffic south_manual: mov R1, south_traffic west_manual: mov R1, west_traffic ;~~~~~~~~~~~~~~~~~~~~~~ ;for External Interrupt ;~~~~~~~~~~~~~~~~~~~~~~ ext0: ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ;Subroutine for Selecting Roads ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ select: P3.4 BIT sw1 P3.5 BIT sw2 P0.4 BIT setBit_ne; for north, bit=1, east=0 P0.5 BIT setBit_sw jb sw1, label1 jnb sw1, label2 label1: jb sw2, label3; for 3 jnb sw2, label; for 2 label2: jb sw2, labelE1 jnb sw2, labelN0 labelE1:; for east clr setBit_ne; bit to indicate that a manual traffic has been entered acall keypad east_traffic db 00 mov east_traffic, traffic; Gets the traffic (from the keypad) at the north road and stores it in the variable labelN0:; for north setb setBit_ne acall keypad north_traffic db 00 mov north_traffic, traffic label3:; for west clr setBit_sw acall keypad west_traffic db 00 mov west_traffic, traffic label:; for south setb setBit_sw acall keypad south_traffic db 00 mov south_traffic, traffic RET ;~~~~~~~~~~~ ext1: ~~~~~~~~~~~ jb n ,n_p ;program if interrupt was called when north's green light was on jb e,e_p ;east jb w,w_p ;west jb s,s_p n_p: mov n, #00 mov R1, #30 acall countdown sjmp north e_p: mov e, #00h mov R1, #30 acall countdown sjmp east w_p: mov w, #00 mov R1, #30 acall countdown sjmp west s_p: mov s, #00h mov R1, 30 acall countdown sjmp south ;~~~~~~~~~~~~~~~~~~~~~~~ ; Subroutine for keypad ;~~~~~~~~~~~~~~~~~~~~~~~ KEYPAD: mov P2, #0FFh K1: mov P1, 0h 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: mov P1, #11111110b mov A, P2 anl, #00000111b cjne A, #00000111b, Row_0 mov P1, #11111101b mov A, P2 anl, #00000111b cjne A, #00000111b, Row_1 mov P1, #11111011b mov A, P2 anl, #00000111b cjne A, #00000111b, Row_2 mov P1, #11110111b mov A, P2 anl, #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 mov traffic, A ljmp K1 org 0400h code0: DB '0', '1', '2' code1: DB '4', '5', '6' code2: DB '7', '8', '9' code3: DB '#', '0', '*' RET ;~~~~~~~~~~~~~~~~~~~~~~ ;subroutine for Counter ;~~~~~~~~~~~~~~~~~~~~~~ CountDown: div R1, 10 mov R0, #9h back: mov dptr, #temp back2: clr A movc A, @A+dptr mov P1, #00000001b; to select first LED mov P2, A inc dptr lcall delayOneSec djnz R0, back2 clr R3 clr A mov DPTR, #temp mov A, R3 movc A, @A+DPTR mov P1, #00000010b; to select second LED mov P2, A lcall shortDelay inc R3 mov R0, #9h djnz R1, back org 0500h temp: DB 3Fh, 06h, 5Bh, 4Fh, 66h, 6Dh, 7Dh, 07h, 7Fh, 67h; hex code for 0-9 RET ;~~~~~~~~~~~~ ;DELAYS delayOneSec: mov R4,#14 back2: 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 TMOD #10h; timer 1, mode 1 -16bit mov TH1, #B7 mov TL1, #FFh setb TR1 again: JNB TF0, again clr TR1 clr TF1 RET ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ShortDelay: mov R5, #255 mov R6, #60 here2:DJNZ R5, here2 here3:DJNZ R6, here3 RET

Could anyone please have a look at it, and find out the errors, my countdown subroutine isn't working properly I think, or maybe there are some other problems too. What I am basically doing is running the traffic lights in a clockwise fashion in the main program. Before the countdown it checks whether manual traffic has been added via the keyboard. If yes, then it adjusts the delay according to the traffic so if traffic was 50, for 50 seconds.

For the road selection what I am doing is setting up two switches with an enter button, which is connected to an interrupt. When a road is selected, the user presses enter, and it goes into its interrupt where it stores the traffic for each road. The key point over here is that when a user selects traffic, the clockwise operation of the four lights should not cease. They should keep on operating as before but when the road that has been selected arrives, the delay and the countdown is adjusted according to this new traffic.

Hope I have been clear. Please help.
Last edited by hira : 05-May-2009 at 06:27.
  #2  
Old 07-May-2009, 18:59
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 76
hira will become famous soon enough

Re: Traffic Control Program 8051


So my new error-free program is this:

CPP / C++ / C 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
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.6
clr p1.4
clr p1.5
clr p1.7
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

east_manual:
mov b, r4
mov R2, b
lcall counter

south_manual:
mov b, r5
mov R2, b
lcall counter

west_manual:
mov b, r6
mov R2, b
lcall counter

;~~~~~~~~~~~~~~~~~~~~~~~
; 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
ret
;~~~~~~~~~~~
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


;~~~~~~~~~~~~


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

but it is giving some sort of a logical error coz the output on proteus isn't right. I would really appreciate it if someone could take a look.
  #3  
Old 08-May-2009, 12:02
Howard_L Howard_L is online now
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 800
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Traffic Control Program 8051


What assembler are you using?
  #4  
Old 08-May-2009, 12:30
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 76
hira will become famous soon enough

Re: Traffic Control Program 8051


Using Keil.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 10:24
Systemize, Outsource, and Grow with the Traffic Assistant Program oms3ta Member Announcements, Advertisements & Offers 0 07-Nov-2007 18:30
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 11:13
Help! Some basal questions about MFC xutingnjupt MS Visual C++ / MFC Forum 1 05-Dec-2004 04:38

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.