First C Program, See Any Errors?

Thread Starter

ke5nnt

Joined Mar 1, 2009
384
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!

Rich (BB code):
//
//    6-LIGHT, 4-WAY INTERSECTION TRAFFIC CONTROL SIGNAL
//    JAN-14-2011
//    PIC16F628A
//    HIGH-TECH C COMPILER, MPLAB IDE
//
//    THIS PROGRAM IS FOR A 6-LIGHT INTERSECTION TRAFFIC CONTROL
//    SIGNAL WHERE BOTH THE MAIN TRAFFIC LANES (MAIN) AND SECONDARY
//    TRAFFIC LANES (SDRY) HAVE A SINGLE SET OF RED, YELLOW, GREEN
//    LIGHTS WHICH WILL CYCLE TO SAFELY CONTROL TRAFFIC WHILE
//    OPERATED IN THE NORMAL CYCLE.  THE SIGNAL ALSO HAS 2 NIGHT-
//    MODE CYCLES WHERE MAIN TRAFFIC LANES RECEIVE A FLASHING YELLOW
//    SIGNAL WHILE SECONDARY TRAFFIC LANES RECEIVE A FLASHING RED
//    SIGNAL, OR ALL TRAFFIC LANES RECEIVE A FLASHING RED SIGNAL.
//    SELECTION OF EITHER OF THESE NIGHT-MODE CYCLES IS DEPENDENT ON
//    THE POSITION OF A SINGLE ON-OFF-ON SWITCH WHICH WILL INPUT
//    HIGH TO RA0 OR RA1.  WHEN BOTH INPUTS ARE LOW, NORMAL CYCLE
//    OPERATION RESULTS.  THE SIGNAL IS PROGRAMMED TO NEVER ALLOW AN
//    UNSAFE SIGNAL CONDITION, THEREFORE, TRANSITION FROM NORMAL
//    OPERATION TO A NIGHT-MODE CYCLE OR VISE-VERSA IS NOT
//    INSTANTANEOUS, AND WILL OCCUR ONLY AT THE PROGRAMMED SAFE
//    POINT.
//
/****************************************************************/

// CONFIGURATION

#include <htc.h>
#define _XTAL_FREQ 48000
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & BORDIS & LVPDIS & UNPROTECT & UNPROTECT);

//    INTERNAL 48KHz CLOCK, PWRT, NO PROTECTION

/***************************************************************/

// GLOBAL VARIABLES

unsigned char count;

/***************************************************************/

void main()
{
 OSCF=0;                    //LOW POWER 48KHZ INTOSC
 CMCON=0x07;                //COMPARATORS OFF
 TRISA=0x03;                //RA0 AND RA1 INPUT, OTHERS OUTPUT
 TRISB=0;                    //ALL PORT B ARE OUTPUTS

 while(1)
 {
    while (RA0==1)            //WHILE INPUT AT RA0, NMRR
    {
     PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
     __delay_ms(500);
     PORTB=0x01;            //RB0 ON, MAIN RED, ALL OTHERS OFF
     __delay_ms(500);
    }
    while (RA1==1)            //WHILE INPUT AT RA1, NMRY
    {
     PORTB=0x08;            //RB3 ON, SECONDARY RED, ALL OTHERS OFF
     __delay_ms(500);
     PORTB=0x02;            //RB1 ON, MAIN YELLOW, ALL OTHERS OFF
     __delay_ms(500);
    }
    if ((RA0!=1)&&(RA1!=1))    //RA0 AND RA1 NO INPUT
    {
     PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
        for (count=0; count<3; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
        }
     PORTB=0x0C;            //RB2 ON, RB3 ON, MAIN GREEN, SDRY RED
        for (count=0; count<20; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 20 TIMES, 20-SECOND DELAY
        }
     PORTB=0x0A;            //RB1 ON, RB3 ON, MAIN YELLOW, SDRY RED
        for (count=0; count<4; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
        }
     PORTB=0x09;            //RB0 ON, RB3 ON, MAIN RED, SDRY RED
        for (count=0; count<3; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 3 TIMES, 3-SECOND DELAY
        }
     PORTB=0x21;            //RB0 ON, RB5 ON, MAIN RED, SDRY GREEN
        for (count=0; count<15; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 15 TIMES, 15-SECOND DELAY
        }
     PORTB=0x11;            //RB0 ON, RB4 ON, MAIN RED, SDRY YELLOW
        for (count=0; count<4; count++)
        {
         __delay_ms(1000);    //LOOP 1 SECOND DELAY 4 TIMES, 4-SECOND DELAY
        }
    }
 }
}
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.
 

Attachments

nerdegutta

Joined Dec 15, 2009
2,684
Hi looks nice, but...

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

Rich (BB code):
<snip>
void TimeDelay(sec)
{
for (count=0;count<sec;count++)
{
__delay_ms(1000);

}
}


sec=4;
TimeDelay(sec);
<snip>
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:
 
Top