help with Traffic light program

Discussion in 'Programmer's Corner' started by jacobh213, Feb 22, 2012.

  1. jacobh213

    Thread Starter New Member

    Feb 22, 2012
    1
    0
    Hi im FAIRLY NEW to this but I have to write a c program for a traffic light in MPLAB and I cant seem to get my program to compile. any help will be greatly appreciated. thanks in advance. the guidlines are:
    1) First, The NS RGB will show the Green color while the EW RGB will be on RED for
    the amount of ‘x1’ seconds (use your group’s table number and the table below to
    determine the value of the variables x1, x2, x3, and x3). The NS COUNTER will
    show the number of seconds being counting down, i.e., this counter will start with the
    value specified by ‘x1’ and count down to 1 and get turn off 1 second later. The EW
    counter will remain off.
    2) After ‘x1’ seconds have elapsed, the program will check the EW input. If 0 (no car
    present), the program will wait an additional ‘x2’ seconds and then go to step 3). Both
    NS and EW Counters will remain off. If the EW switch is 1, then skip directly to step
    3).
    3) The program will change the NS RGB from GREEN to YELLOW and wait for 2
    seconds.
    4) Next, the NS RGB will change from YELLOW to RED while the EW RGB will turn
    to GREEN. These lights will be on for ‘x3’ seconds. The EW Counter will display the
    down counting of ‘x3’ while the NS Counter will stay off.
    5) After ‘x3’ second have passed, the NS input switch will be tested. If 0, the program
    will wait an additional ‘x4’ seconds and then jump to step 6). Both counter displays
    will stay off. If NS input is 1, go directly to step 6)
    6) The EW RGB will be changed from GREEN to YELLOW and will stay on for 2
    seconds.​
    7) Go to step 1 and repeat the same process.

    Code ( (Unknown Language)):
    1.  
    2. #include <p18f4321.h>
    3. #include <usart.h>
    4. #pragma config OSC = INTIO2
    5. #pragma config WDT=OFF
    6. #pragma config LVP=OFF
    7. #pragma config BOR =OFF
    8. #define oldelay 255
    9. #define NS PORTBbits.RB1
    10. #define EW PORTBbits.RB0
    11. #define NSR PORTCbits.RC0
    12. #define NSY PORTCbits.RC1
    13. #define NSG PORTCbits.RC2
    14. #define EWR PORTDbits.RD3
    15. #define EWY PORTDbits.RD4
    16. #define EWG PORTDbits.RD5
    17. void Initialize(void);
    18. void WAIT_FOR_half_SECOND(void);
    19. void WAIT_FOR_one_SECOND (void);
    20. void WAIT_FOR_N_SECOND (int);
    21. void set_NS(int);
    22. void set_EW(int);
    23.  
    24. void Init_UART(){
    25. OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
    26. USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX &
    27. USART_BRGH_HIGH, 25);
    28. OSCCON = 0x60;
    29. }
    30. void main(void)
    31. {
    32. unsigned int Counter_Display [10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x83,0xF8,0x80,0x98};
    33. int input;
    34. int i;
    35. while(1)
    36. {
    37.  
    38. set_NS(3); //Initial condition. NS is green
    39. set_EW(1); //Initial condition. EW is red
    40.  
    41. for (i=5;i>0;--i)
    42. { //loop 5 times for NW counter. x1 = 5 seconds
    43. WAIT_FOR_N_SECOND(5); // x1=5 seconds
    44. PORTC = Counter_Display[i]; //NW counter countdown x1
    45. PORTD = Counter_Display[0]; //EW counter not running
    46. }
    47. if (EW==0)
    48. { //check EW for car/value=0
    49. WAIT_FOR_N_SECOND(2); //wait addtional two seconds due to EW=0
    50. }
    51. else
    52. {
    53. set_NS(2); //NS turns yellow
    54. set_EW(1); //EW remains red
    55. WAIT_FOR_N_SECOND(2); //wait two seconds before next transition
    56. }
    57. for (i=3;i>0;--i)
    58. { //EW counter starts after two seconds
    59. set_NS(1); //NS is red
    60. set_EW(3); //EW is green
    61. PORTC = Counter_Display[0];
    62. PORTD = Counter_Display[i]; //loop 3 times EW counter.
    63. }
    64. if (NS==0)
    65. { //check EW for car/value=0
    66. WAIT_FOR_N_SECOND(4); //wait addtional two seconds due to EW=0
    67. }
    68. else
    69. {
    70. set_NS(1); //NS turns yellow
    71. set_EW(2); //EW remains red
    72. WAIT_FOR_N_SECOND(2); //wait two seconds before next transition
    73. }
    74. }
    75. }
    76.  
    77. void Initialize()
    78. {
    79. ADCON0=0x01; //select channel AN0, and turn on the ADDC subsystem
    80. ADCON1=0x0E; //set pin 2 as analog signal, VDD-VSS as reference voltage and left justify the result
    81. ADCON2=0x29; //Set the bit conversion time (TAD) and acquisition time
    82. TRISB =0x00;
    83. TRISC =0x00;
    84. TRISD =0x00;
    85. }
    86.  
    87. void set_NS(int input)
    88. {
    89. switch(input)
    90. {
    91. case 1:
    92. NSG=0; //red light
    93. NSY=1;
    94. NSR=1;
    95. break;
    96.  
    97. case 2:
    98. NSG=1;
    99. NSY=0; //yellow light
    100. NSR=1;
    101. break;
    102.  
    103. case 3:
    104. NSG=1;
    105. NSY=1;
    106. NSR=0; //green light
    107. break;
    108.  
    109. }
    110. }
    111. void set_EW(int input)
    112. {
    113. switch(input)
    114. {
    115. case 1:
    116. EWG=0; //red light
    117. EWY=1;
    118. EWR=1;
    119. break;
    120. case 2:
    121. EWG=1;
    122. EWY=0; //yellow light
    123. EWR=1;
    124. break;
    125. case 3:
    126. EWG=1; //green light
    127. EWY=1;
    128. EWR=0;
    129. break;
    130. }
    131. }
    132.  
    133. void WAIT_FOR_half_SECOND(void)
    134. {
    135. int i,j;
    136. for( i=0; i <50; i++)
    137. {
    138. for (j=0;j<100; j++)
    139. {
    140. }
    141. }
    142. }
    143. void WAIT_FOR_one_SECOND (void)
    144. {
    145. //set 1SEC_CLOCK signal to logic high
    146. WAIT_FOR_half_SECOND ();
    147. //set 1SEC_CLOCK signal to logic low
    148. WAIT_FOR_half_SECOND ();
    149. }
    150.  
    151. void WAIT_FOR_N_SECOND(int time)
    152. {
    153.  
    154. int i;
    155. for(i=0;i>time;--i)
    156. {
    157. display(time);
    158. WAIT_FOR_one_SECOND();
    159. }
    160. }
    161. [/i][/i]
     
    Last edited by a moderator: Feb 22, 2012
Loading...