Thread: Electrical Engineering Class Code Help Please!!!!!

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    2

    Question Electrical Engineering Class Code Help Please!!!!!

    Our team of students have tried to write this code but have had no success with a successful build from mplab v8.33. Could someone please take a look at our code and tell us what it is that is or is not vibing write. We are getting Build Failed Syntax Error right from the jump. Any input or suggestions would be appreciated.

    Code:
    #include <p18c452.h>
    #use delay(clock=10000000)
    #fuses HS,WDT
    
    #byte    PORTD = 0x08            /* output port D definition */
    #bit	   EW_RED_LITE	= PORTD.5	/* definitions to actual outputs */
    #bit	   EW_YEL_LITE	= PORTD.6	/* used to control the lights */
    #bit	   EW_GRN_LITE	= PORTD.7
    #bit	   NS_RED_LITE	= PORTD.1
    #bit	   NS_YEL_LITE	= PORTD.2
    #bit	   NS_GRN_LITE	= PORTD.3
    
    #byte    PINB  = 0x06             /* input port B definition */
    #bit	   PED_XING_EW	= PINB.0     /* pedestrian crossing push button */
    #bit	   PED_XING_NS	= PINB.1     /* pedestrian crossing push button */
    #bit	   FOUR_WAY_STOP = PINB.3	 /* switch input for 4-Way Stop */
    
    char time_left;		// time in seconds spent in each state
    int current_state;	// current state of the lights
    char flash_toggle;	// toggle used for FLASHER state
    
    // This enumeration creates a simple way to add states to the machine // by name. Enumerations generate an integer value for each name
    // automatically, making the code easier to maintain.
    
    enum { EW_MOVING , EW_WARNING , NS_MOVING , NS_WARNING , FLASHER };
    
    // The actual state machine is here..
    void Do_States(void)
    {
    	switch(current_state)
    	{
    		case	EW_MOVING:		// east-west has the green!!
    			EW_GRN_LITE = 0;
    			NS_GRN_LITE = 1;	
    			NS_RED_LITE = 0;	// north-south has the red!!
    			EW_RED_LITE	= 1;
    			EW_YEL_LITE = 1;
    			NS_YEL_LITE = 1;
    			
    			if(PED_XING_EW || FOUR_WAY_STOP)	
    			{	// pedestrian wishes to cross, or 
    				// a 4-way stop is required
    				if(time_left > 10)
    					time_left = 10;	// shorten the time
    			}
    			if(time_left != 0)		// count down the time
    			{
    				--time_left;
    				return;			// return to main
    			}			         // time expired, so..
    			time_left = 5;	   // give 5 seconds to WARNING
    			current_state = EW_WARNING;	
                            // time expired, move
    			break;			// to the next state
    
    		case	EW_WARNING:
    			EW_GRN_LITE = 1;
    			NS_GRN_LITE = 1;
    			NS_RED_LITE = 0;	// north-south has the red..
    			EW_RED_LITE	= 1;
    			EW_YEL_LITE = 0;	// and east-west has the yellow
    			NS_YEL_LITE = 1;
    			
    			if(time_left != 0)		// count down the time
    			{
    				--time_left;
    				return;			// return to main
    			}			// time expired, so..
    			if(FOUR_WAY_STOP)	// if 4-way requested then start
       			current_state = FLASHER; // the flasher
    			else
    			{				// otherwise..
       			time_left = 30;	// give 30 seconds to MOVING
    				current_state = NS_MOVING;	
    }				// time expired, move
    			break;			// to the next state
    
    		case	NS_MOVING:
    			EW_GRN_LITE = 1;
    			NS_GRN_LITE = 0;	
    			NS_RED_LITE = 1;	// north-south has the green!!
    			EW_RED_LITE	= 0;	// east-west has the red!!
    			EW_YEL_LITE = 1;
    			NS_YEL_LITE = 1;
    			
    			if(PED_XING_NS || FOUR_WAY_STOP)	
    			{	// if a pedestrian wishes to cross, or 
    				// a 4-way stop is required..
    				if(time_left > 10)
    					time_left = 10;	// shorten the time
    			}
    			if(time_left != 0)		// count down the time
    			{
    				--time_left;
    				return;			// return to main
    			}			// time expired, so..
    			time_left = 5;	// give 5 seconds to WARNING
    			current_state = NS_WARNING;	// time expired, move
    			break;				// to the next state
    
    		case	NS_WARNING:
    			EW_GRN_LITE = 1;
    			NS_GRN_LITE = 1;
    			NS_RED_LITE = 1;	// north-south has the yellow..
    			EW_RED_LITE	= 0;
    			EW_YEL_LITE = 1;	// and east-west has the red..
    			NS_YEL_LITE = 0;
    			
    			if(time_left != 0)		// count down the time
    			{
    				--time_left;
    				return;			// return to main
    			}			// time expired, so..
    			if(FOUR_WAY_STOP)	// if 4-way requested then start
    			current_state = FLASHER; // the flasher
    			else
    			{				// otherwise..
       			time_left = 30;	// give 30 seconds to MOVING
    				current_state = EW_MOVING;	
          }				// time expired, move
    		break;			// to the next state
    
    		case	FLASHER:
       		EW_GRN_LITE = 1;	// all yellow and
    	    	NS_GRN_LITE = 1;	// green lites off
    			EW_YEL_LITE = 1;	
    			NS_YEL_LITE = 1;
    
    			flash_toggle ^= 1;	// toggle LSB..
    			if(flash_toggle & 1)
    			{
    				NS_RED_LITE = 0;	// blink red lights
       			EW_RED_LITE	= 1;
    			}
    			else
    			{
    				NS_RED_LITE = 1;	// alternately
    	     		EW_RED_LITE	= 0;
    			}
    			if(!FOUR_WAY_STOP)	// if no longer a 4-way stop
    				current_state = EW_WARNING;
    			break;			// then return to normal
    
    		default:
          	current_state = NS_WARNING;	
             break;	// set any unknown state to a good one!!
    	}
    }
    
    void main(void)
    {
       port_b_pullups(TRUE);
       set_tris_b(0xFF); // port B is all input
       set_tris_d(0x00); // port D is all output
       
    	current_state = NS_WARNING;	// initialize to a good starting
    						// state (as safe as possible)
    	while(1)
       {
    	  delay_ms(250);	// 1 second delay.. this time could
    	  delay_ms(250);
    	  delay_ms(250);
    	  delay_ms(250);
    				// be used for other needed processes
    
    	  Do_States();	// call the state machine, it knows
    				// where it is and what to do next
    	}
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> #use delay(clock=10000000)

    Are you using some non-standard C compiler? Because that's not valid C (nor are the #byte, #bit, and #fuses directives). Perhaps a bitfield is what you're looking for?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Our team of students have tried to write this code
    Did you try to write that code, or did you just copy it from this book?

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by bithub View Post
    Did you try to write that code, or did you just copy it from this book?
    Ouch! Busted!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Any input or suggestions would be appreciated.
    Start with a very simple program which just lights a LED, then build up from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    2
    The code originated from our last project assigned by our teacher to build the simple traffic light circuit using a microchip and some led's. He also gave us a folder with all of the files necessary for the compiler to build successfully like the source code and the hex file. We needed to modify that code to suit our purposes with another chip's function for an advanced project. But of course the build spits errors. We did not purposely copy this code from any book nor do we feel the need to apologize or feel remorseful as if we were caught doing something immoral. But thanks for the book link, it is quite helpful.
    Witty quote there salem...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your teacher gave you the code, why not ask them your support question?

    Whilst this is a C forum, few people here would have ever heard of "mplab v8.33", much less used it.

    If you had at least posted some error messages, that might have given some suggestions for things to try (missing option, missing library etc).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So presumably all those things that make no sense make sense to you and/or your original compiler (the #fuses, the #byte, the #bit). You're going to have to find out/remember what they mean, and then find out/remember what your new compiler uses instead, apparently.

    Of course, maybe your compiler just does say ERROR ERROR ERROR and doesn't tell you what the error is. But most do, and then reading it and doing what it says will go a long way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mixing C++ class and non-class code?
    By Depthcharge101 in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2009, 12:11 AM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM