Traffic Control System - 8051

Discussion in 'Embedded Systems and Microcontrollers' started by hira287, May 8, 2009.

  1. hira287

    Thread Starter New Member

    May 8, 2009
    7
    0
    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:



    And the program I have so far is this:


    Code ( (Unknown Language)):
    1. org 000h
    2. ljmp main
    3.  
    4.  
    5. org 0003h; external interrupt subroutine - P3.2
    6. ljmp ext0
    7.  
    8. org 0013h
    9. ljmp ext1
    10.                             ;~~~~~~~~~~~
    11.                               ;MAIN
    12. org 0030h
    13.                             ;~~~~~~~~~~~
    14. main:
    15.  
    16. ;-----------------
    17. clr P2.4
    18. clr P2.5
    19. clr P2.6
    20. clr P2.7
    21.  
    22. MOv TMOD, #11h; timer 1, mode 1 -16bit, timer 0 mode 1
    23. mov ie, #10000101b
    24. setb tcon.0; to set edge level interrupt
    25. setb tcon.2
    26. main2:
    27. north:
    28. setb p1.4; 1.4= north; 1=green, 0=red
    29. clr p1.5
    30. clr p1.6
    31. clr p1.7
    32. JB p2.4, north_manual
    33. jnb P2.4, normal1
    34. normal1:
    35. mov R2, #30
    36. lcall counter
    37.  
    38. east:
    39. setb p1.5
    40. clr p1.4
    41. clr p1.6
    42. clr p1.7
    43. jb p2.5, east_manual
    44. jnb p2.5, normal2
    45. normal2:
    46. mov R2, #30
    47. lcall counter
    48.  
    49. south:
    50. setb p1.6
    51. clr p1.4
    52. clr p1.5
    53. clr p1.7
    54. jb p2.6, south_manual
    55. jnb p2.6, normal3
    56. normal3:
    57. mov R2, #30
    58. lcall counter
    59.  
    60. west:
    61. setb p1.7
    62. clr p1.4
    63. clr p1.5
    64. clr p1.6
    65. jb p2.7, east_manual
    66. jnb p2.7, normal4
    67. normal4:
    68. mov R2, #30
    69. lcall counter
    70.  
    71. sjmp main2
    72.  
    73. RET
    74.  
    75. north_manual:
    76. mov b, r3
    77. mov R2, b
    78. lcall counter
    79. RET
    80. east_manual:
    81. mov b, r4
    82. mov R2, b
    83. lcall counter
    84. RET
    85. south_manual:
    86. mov b, r5
    87. mov R2, b
    88. lcall counter
    89. RET
    90. west_manual:
    91. mov b, r6
    92. mov R2, b
    93. lcall counter
    94. RET
    95. ;~~~~~~~~~~~~~~~~~~~~~~~
    96. ; Subroutine for keypad
    97. ;~~~~~~~~~~~~~~~~~~~~~~~
    98. KEYPAD:
    99. mov P2,#00000111b; convert the first three pins to input
    100.  
    101.  
    102. K1: mov P1, #00h
    103.     mov A, P2
    104.     anl A, #00000111b
    105.     cjne A, #00000111b, K1
    106.  
    107. K2: lcall delay
    108.     mov A, P2
    109.     anl A, #00000111b
    110.     cjne A, #00000111b, over
    111.     sjmp K2
    112.  
    113. over: lcall delay
    114.       mov A, P2
    115.       anl A, #00000111b
    116.       cjne A, #00000111b, over1
    117.       sjmp K2
    118. over1: setb p1.0; ground row 0
    119.        mov A, P2
    120.        anl a, #00000111b
    121.        cjne A, #00000111b, Row_0
    122.        
    123.        setb p1.1; ground row 1
    124.        mov A, P2
    125.        anl a, #00000111b
    126.        cjne A, #00000111b, Row_1
    127.        
    128.        setb p1.2; ground row 2
    129.        mov A, P2
    130.        anl a, #00000111b
    131.        cjne A, #00000111b, Row_2
    132.        
    133.        setb p1.3; ground row 3
    134.        mov P1, #11110111b
    135.        mov A, P2
    136.        anl a, #00000111b
    137.        cjne A, #00000111b, Row_3
    138.        
    139.        ljmp K2
    140.        
    141. Row_0: mov DPTR, #CODE0
    142.        sjmp find
    143. Row_1: mov DPTR, #CODE1
    144.        sjmp find
    145. Row_2: mov DPTR, #CODE2
    146.        sjmp find
    147. Row_3: mov DPTR, #CODE3
    148.        sjmp find
    149. find: rrc A
    150.       jnc match
    151.       inc dptr
    152.       sjmp find
    153. match: clr A
    154.        movc A, @A+dptr
    155.        ;    traffic db 0; traffic=variable to store traffic
    156.      
    157.        ljmp K1
    158.  
    159. org 0400h
    160.  
    161. code0: DB   '0', '1', '2'
    162. code1: DB   '4', '5', '6'
    163. code2: DB   '7', '8', '9'
    164. code3: DB   '#', '0', '*'
    165.  
    166. RET
    167.  
    168. ;-------
    169.  
    170. ;CounterandKeypad
    171. traffic:
    172. acall keypad
    173. subb A, #30
    174. mov B, #10
    175. mul AB
    176. mov R1, A
    177. acall keypad
    178. subb A, #30
    179. add A, R1
    180. mov R2, A; saving a copy of A
    181. RET
    182.  
    183. counter:
    184.  
    185. back7: mov A, R2; R2 contains traffic, average=30
    186. mov B, #10
    187. div AB
    188.  
    189.  
    190. mov R1, A; save a copy of A
    191. mov A, B
    192. mov DPTR, #temp
    193. movc A, @A+DPTR
    194. clr p0.7 ; p0.7 and p2.3 --> selection lines
    195. setb p2.3; select 1st Led, common cathode selection
    196. mov P0, A
    197.  
    198. ljmp shortDelay
    199. mov A, R1; reload A for second digit
    200. movc A, @A+DPTR
    201. setb p0.7; second led
    202. clr p2.3
    203. mov P0, A
    204. lcall delayOneSec
    205. djnz R2, back7
    206.  
    207. temp: DB 3Fh, 06h, 5Bh, 4Fh, 66h, 6D, 7Dh, 07h, 7Fh, 67h; hex codes for 0-9
    208.  
    209. RET
    210.  
    211.  
    212.  
    213.  
    214. ;~~~~~~~~~~~~~~~~~~~~~~
    215. ;for External Interrupt
    216. ;~~~~~~~~~~~~~~~~~~~~~~
    217. ext0:
    218. ;Subroutine for Selecting Roads
    219.  
    220. select:
    221.  
    222.  
    223.  
    224. jb p3.4, label1
    225. jnb p3.4, label2
    226. label1: jb p3.5, label3; for 3
    227. jnb p3.5, label; for 2
    228. label2: jb p3.5, labelE1
    229. jnb p3.5, labelN0
    230.  
    231. labelE1:; for east
    232. acall traffic
    233. setb p2.5; bit to indicate that a manual traffic has been entered
    234. mov r4,A
    235.  
    236. labelN0:; for north
    237.  
    238. lcall traffic
    239. setb p2.4
    240.  
    241. mov R3,A
    242.  
    243. label3:; for west
    244. lcall traffic
    245. setb p2.6
    246. mov r6, A
    247.  
    248. label:; for south
    249. lcall traffic
    250. setb p2.7
    251. mov R6, A
    252. reti
    253. ;~~~~~~~~~~~
    254. ext1:
    255. ;~~~~~~~~~~~
    256.  
    257. jb p0.0 ,n_p        ;program if interrupt was called when noth's green light was on
    258. jb p0.1,e_p        ;east
    259. jb p0.3,w_p        ;west
    260. jb p0.2,s_p
    261.  
    262. n_p:
    263. clr p1.4
    264. clr p1.5
    265. clr p1.6
    266. clr p1.7
    267. mov R1, #30
    268. lcall counter
    269. ljmp north
    270.  
    271. e_p:
    272. clr p1.4
    273. clr p1.5
    274. clr p1.6
    275. clr p1.7
    276. mov R1, #30
    277. lcall counter
    278. ljmp east
    279.  
    280. w_p:
    281. clr p1.4
    282. clr p1.5
    283. clr p1.6
    284. clr p1.7
    285. mov R1, #30
    286. lcall counter
    287. ljmp west
    288.  
    289. s_p:
    290. clr p1.4
    291. clr p1.5
    292. clr p1.6
    293. clr p1.7
    294. mov R1, 30
    295. lcall counter
    296. ljmp south
    297.  
    298. reti
    299. ;~~~~~~~~~~~~
    300.  
    301.  
    302. delayOneSec:
    303.  
    304. mov r7,#14
    305. rept1:
    306. mov tl1,#00h
    307. mov th1,#00h
    308. setb tr1
    309. again:
    310. jnb tf1,again
    311. clr tf1
    312. clr tr1
    313. djnz r7,rept1
    314. ;ret
    315. /*mov TMOD, 01h; Timer 0, mode 1- 16 bit
    316. mov TL0,#00h
    317. mov TH0,#00h
    318. setb TR0
    319. here:jnb TF0,here
    320. djnz R4, back2     */
    321.  
    322. RET
    323. ;~~~~~~~~~~~~~~~~~~~~~~~~~
    324.  
    325.  
    326.  
    327. delay: ;Delay for 20ms
    328.  
    329.  
    330. mov tl0,#0B7h
    331. mov th0,#0FEh
    332. setb TR0
    333. again1: JNB TF0, again1
    334. clr TR0
    335. clr TF0
    336. RET
    337.  
    338.  
    339.  
    340. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    341. ShortDelay:      ; delay for 2.5ms
    342. mov R7, #9
    343. here9:mov R6, #255
    344. here2:DJNZ R0, here2
    345. here3:DJNZ R7, here9
    346.  
    347. RET
    348. 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.
     
Loading...