First C Program, See Any Errors?

Discussion in 'Programmer's Corner' started by ke5nnt, Jan 18, 2011.

  1. ke5nnt

    Thread Starter Active Member

    Mar 1, 2009
    384
    15
    First, let me thank those who helped me get this far. You can only read a book, not ask it questions, so thank you AAC for your help.

    On to the program. This is my first C program, so I'd like to know if anyone sees any errors, or if you have suggestions as to how I could have written the code more efficiently, I'd certainly be interested in that as well. This is a very simple program my first time out that just uses a global variable, some time delays, and a couple of if statements.

    One of my first projects when I was learning Assembly was what seems to be a favorite for projects and for beginners; a Traffic Signal. I figured, why not make that my first C project too. Attached to this thread is a flow chart which illustrates what the program does, and a schematic for the hardware. I put it together on my breadboard and everything is working quite well, though I noticed my time delays seem to be off a bit. Maybe the clock isn't running at my intended speed or I did something else wrong, but the program seems to be running a bit fast. Anyways, on to the code!

    Code ( (Unknown Language)):
    1.  
    2. //
    3. //    6-LIGHT, 4-WAY INTERSECTION TRAFFIC CONTROL SIGNAL
    4. //    JAN-14-2011
    5. //    PIC16F628A
    6. //    HIGH-TECH C COMPILER, MPLAB IDE
    7. //
    8. //    THIS PROGRAM IS FOR A 6-LIGHT INTERSECTION TRAFFIC CONTROL
    9. //    SIGNAL WHERE BOTH THE MAIN TRAFFIC LANES (MAIN) AND SECONDARY
    10. //    TRAFFIC LANES (SDRY) HAVE A SINGLE SET OF RED, YELLOW, GREEN
    11. //    LIGHTS WHICH WILL CYCLE TO SAFELY CONTROL TRAFFIC WHILE
    12. //    OPERATED IN THE NORMAL CYCLE.  THE SIGNAL ALSO HAS 2 NIGHT-
    13. //    MODE CYCLES WHERE MAIN TRAFFIC LANES RECEIVE A FLASHING YELLOW
    14. //    SIGNAL WHILE SECONDARY TRAFFIC LANES RECEIVE A FLASHING RED
    15. //    SIGNAL, OR ALL TRAFFIC LANES RECEIVE A FLASHING RED SIGNAL.
    16. //    SELECTION OF EITHER OF THESE NIGHT-MODE CYCLES IS DEPENDENT ON
    17. //    THE POSITION OF A SINGLE ON-OFF-ON SWITCH WHICH WILL INPUT
    18. //    HIGH TO RA0 OR RA1.  WHEN BOTH INPUTS ARE LOW, NORMAL CYCLE
    19. //    OPERATION RESULTS.  THE SIGNAL IS PROGRAMMED TO NEVER ALLOW AN
    20. //    UNSAFE SIGNAL CONDITION, THEREFORE, TRANSITION FROM NORMAL
    21. //    OPERATION TO A NIGHT-MODE CYCLE OR VISE-VERSA IS NOT
    22. //    INSTANTANEOUS, AND WILL OCCUR ONLY AT THE PROGRAMMED SAFE
    23. //    POINT.
    24. //
    25. /****************************************************************/
    26.  
    27. // CONFIGURATION
    28.  
    29. #include <htc.h>
    30. #define _XTAL_FREQ 48000
    31. __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & BORDIS & LVPDIS & UNPROTECT & UNPROTECT);
    32.  
    33. //    INTERNAL 48KHz CLOCK, PWRT, NO PROTECTION
    34.  
    35. /***************************************************************/
    36.  
    37. // GLOBAL VARIABLES
    38.  
    39. unsigned char count;
    40.  
    41. /***************************************************************/
    42.  
    43. void main()
    44. {
    45.  OSCF=0;                    //LOW POWER 48KHZ INTOSC
    46.  CMCON=0x07;                //COMPARATORS OFF
    47.  TRISA=0x03;                //RA0 AND RA1 INPUT, OTHERS OUTPUT
    48.  TRISB=0;                    //ALL PORT B ARE OUTPUTS
    49.  
    50.  while(1)
    51.  {
    52.     while (RA0==1)            //WHILE INPUT AT RA0, NMRR
    53.     {
    54.      PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
    55.      __delay_ms(500);
    56.      PORTB=0x01;            //RB0 ON, MAIN RED, ALL OTHERS OFF
    57.      __delay_ms(500);
    58.     }
    59.     while (RA1==1)            //WHILE INPUT AT RA1, NMRY
    60.     {
    61.      PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
    62.      __delay_ms(500);
    63.      PORTB=0x02;            //RB1 ON, MAIN YELLOW, ALL OTHERS OFF
    64.      __delay_ms(500);
    65.     }
    66.     if ((RA0!=1)&&(RA1!=1))    //RA0 AND RA1 NO INPUT
    67.     {
    68.      PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
    69.         for (count=0; count<3; count++)
    70.         {
    71.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
    72.         }
    73.      PORTB=0x0C;            //RB2 ON, RB3 ON, MAIN GREEN, SDRY RED
    74.         for (count=0; count<20; count++)
    75.         {
    76.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 20 TIMES, 20-SECOND DELAY
    77.         }
    78.      PORTB=0x0A;            //RB1 ON, RB3 ON, MAIN YELLOW, SDRY RED
    79.         for (count=0; count<4; count++)
    80.         {
    81.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
    82.         }
    83.      PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
    84.         for (count=0; count<3; count++)
    85.         {
    86.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
    87.         }
    88.      PORTB=0x21;            //RB0 ON, RB5 ON, MAIN RED, SDRY GREEN
    89.         for (count=0; count<15; count++)
    90.         {
    91.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 15 TIMES, 15-SECOND DELAY
    92.         }
    93.      PORTB=0x11;            //RB0 ON, RB4 ON, MAIN RED, SDRY YELLOW
    94.         for (count=0; count<4; count++)
    95.         {
    96.          __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
    97.         }
    98.     }
    99.  }
    100. }
    Thanks for the feedback! EDIT: I just saw on my .pdf that I have the supply voltage listed as 12V. This is incorrect for this project and should be listed as a 9V battery, my bad.
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Hi looks nice, but...

    Maybe you could do something about the delay-loops? Something like this for a 4 seconds delay:

    Code ( (Unknown Language)):
    1.  
    2. <snip>
    3. void TimeDelay(sec)
    4. {
    5. for (count=0;count<sec;count++)
    6. {
    7. __delay_ms(1000);
    8.  
    9. }
    10. }
    11.  
    12.  
    13. sec=4;
    14. TimeDelay(sec);
    15. <snip>
    16.  
    17.  
    18.  
    Then you could just change the sec value, and use it as a parameter in the TimeDelay function.

    I'm not sure, just typing before I think...:rolleyes:
     
Loading...