Problem with AT89C51 C source code (density based traffic light)

Discussion in 'Embedded Systems and Microcontrollers' started by Jang_angol, Feb 24, 2016.

  1. Jang_angol

    Thread Starter New Member

    Feb 24, 2016
    1
    0
    Hello everyone,
    I am working on a 4 way density based traffic light controlled by AT89C51 microcontroller. The problem is, I dont know whether this source code is correct or not. I found this code and placed it in proteus and there are many errors detected.
    Please help me guys.

    Code (C):
    1. #include<reg51.h>
    2. #define density_level P1
    3. //void green_delay();
    4. //Lights declaration
    5. sbit ar = P0^0;
    6. sbit ag = P0^1;
    7. sbit br = P0^2;
    8. sbit bg = P0^3;
    9. sbit cr = P0^4;
    10. sbit cg = P0^5;
    11. sbit dr = P0^6;
    12. sbit dg = P0^7;
    13. //sensors declarartion
    14. sbit IRaa=P1^0;
    15. sbit IRab=P1^1;
    16. sbit IRba=P1^2;
    17. sbit IRbb=P1^3;
    18. sbit IRca=P1^4;
    19. sbit IRcb=P1^5;
    20. sbit IRda=P1^6;
    21. sbit IRdb=P1^7;
    22. int a[]=
    23. {
    24. void main()
    25. {
    26. P1=0XFF;
    27. P3=0X00;
    28. P0=0X00;
    29. P2=0X00;
    30. ar=1;
    31. br=1;
    32. cr=1;
    33. dr=1;
    34. while(1)
    35. {
    36. int check_high;
    37. bit a=0,b=0,c=0,d=0,high=0;
    38. int lane_a,lane_b,lane_c,lane_d;
    39. for (check_high=0;chec k_high<3;check_hig h++)
    40. {
    41. switch (density_level)
    42. {
    43. case 0XFC :
    44. if(a==0)
    45. {
    46. ar = 0;
    47. a=1;
    48. for(lane_a=0;lane_a<9;lane_a++)
    49. {
    50. ag = 1;
    51. }
    52. ag = 0;
    53. }
    54. break;
    55. case 0XF3 :
    56. if(b==0)
    57. {
    58. br = 0;
    59. b=1;
    60. for(lane_b=0;lane_b<9;lane_b++)
    61. {
    62. bg = 1;
    63. }
    64. }
    65. bg = 0;
    66. break;
    67. case 0XCF :
    68. if(c==0)
    69. {
    70. cr = 0;
    71. c=1;
    72. for(lane_c=0;lane_c<9;lane_c++)
    73. { cg = 1;
    74. }
    75. } cg = 0;
    76. break;
    77. case 0X3F :
    78. if(d==0)
    79. {
    80. dr = 0;
    81. d=1;
    82. for(lane_d=0;lane_d<9;lane_d++)
    83.  
    84. {
    85. dg = 1;
    86. }
    87. }
    88. dg = 0;
    89. break;
    90. }
    91. P0 = P0&0XFF;
    92. }
    93. }
    94. }
    95. /*
    96. {
    97. while(high==1)
    98. {
    99. int check2;
    100. for (check2=0;check2<2;check2++)
    101. {
    102. {
    103. if(IRaa==0)
    104. //check lane a
    105. {
    106. if(IRab==0)
    107. { ar = 0;
    108. ag = 1;
    109. green_delay();
    110. ag = 0;
    111. }
    112. else
    113. {
    114. ag = 0;
    115. //ay = 1;
    116. //yellow_delay();
    117. //ay = 0;
    118. ar = 1;
    119. }
    120. }
    121. }
    122. {
    123. if(IRba==0)
    124. //check lane b
    125. {
    126. if(IRbb==0)
    127. {
    128. br = 0;
    129. bg = 1;
    130. }
    131. else
    132. {
    133. bg = 0;
    134. //by = 1;
    135. //yellow_delay();
    136. //by = 0;
    137. br = 1;
    138. }
    139. }
    140. }
    141. {
    142. if(IRca==0)
    143. //check lane c
    144. {
    145. if(IRcb==0)
    146. {
    147. cr = 0;
    148. cg = 1;
    149. }
    150. else
    151. {
    152. cg = 0;
    153. //cy = 1;
    154. //yellow_delay();
    155. //cy = 0;
    156. cr = 1;
    157. }
    158. }
    159. }
    160. {
    161. if(IRda==0)
    162. //check lane d
    163. {
    164. if(IRdb==0)
    165. {
    166. dr = 0;
    167. dg = 1;
    168. }
    169. else
    170. {
    171. dg = 0;
    172. //dy = 1;
    173. //yellow_delay();
    174. //dy = 0;
    175. dr = 1;
    176. }
    177. }
    178. }
    179. }
    180. }
    181. }
    182. }
    183. */ /*void green_delay()
    184. {
    185. int y;
    186. for (y=0; y<1000; y++);
    187. } */
    Mod edit: added code tags
     
    Last edited by a moderator: Feb 24, 2016
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    I don't know if its correct either. What errors are you talking about? Compiler errors, problems in the program logic or??

    The problem with rooting around the internet for code is that you never know if its any good and copying others' work won't lead to any understanding of what's going on to help you fix it. Its far better to design your own solution, maybe after looking at others' code, code it, debug it and ship it. That way, you'll never have to worry about understanding it.

    From experience I can tell you that you'll burn more time trying to make some internet junk work for your application than if you just sit down, solve the logic and write the code yourself.

    Good luck.
     
    Last edited: Feb 24, 2016
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    362
    The code works. The errors are related to the thing about 3 to 8 lines below the middle. Have that variable named fixed, and replaced with the right one, the code burns into chip just fine.
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    What are the line numbers and variable name you are referencing?
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,605
    I wonder how good can code be when over half the lines have been commented out?
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    Me too. I don't know if that was in the original code or the TS did it while debugging. Still waiting to hear from @dannyf .
     
Loading...