Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

RC Car Control Programming

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
11 Jan 2012CPOL6 min read 243.9K   15.9K   94  
High level design of RC car with programming the microcontroller and user interface on a PC
   /*************************************************
 *                                              	*
 *     COPYRIGHT (c) SolAlem Technologies      		*
 *     Author : Solomon Amognu 10/30/2010        	*
 *                                              	*
 *     A program to control the rover in project    *
 *     PC Controlled RC car system					*
 *                                              	*
 *     The IO devices set up is as follows:		   	*
 *     		P0.0 - LED for ambient light debugging 	*
 *			P0.1 - LED for sensor error debugging	*
 *			P2.0 - IR LED for collission check		*
 *			P2.1 - IR sensor for collission check	*
 *			P1.0 - |_					 			*
 *			P1.1 - |  right motor controller		*
 *			P1.4 - |_								*
 *			P1.5 - |  left motor controller			*
 *													*
 **************************************************/

	#include < Reg52.h >  

  	char const num[  ] = {0x00, 0x01, 0x02, 0x03, 0x11}; 
  
/*------------------------------------------------------------*-
DELAY_HARDWARE_50ms()
Hardware delay of 50ms.
*** Assumes 12MHz 8051 (12 osc cycles) ***
-*------------------------------------------------------------*/

	void DELAY_HARDWARE_50ms(void)
	{
		// Configure Timer 0 as a 16-bit timer
		TMOD &= 0xF0; 		// Clear all T0 bits (T1 left unchanged)
		TMOD |= 0x01; 		// Set required T0 bits (T1 left unchanged)
		ET0 = 0; 			// No interupts
							// Values for 50 ms delay
		TH0 = 0x3C; 		// Timer 0 initial value (High Byte)
		TL0 = 0xB0; 		// Timer 0 initial value (Low Byte)
		TF0 = 0; 			// Clear overflow flag
		TR0 = 1; 			// Start timer 0
		while (TF0 == 0);	// Loop until Timer 0 overflows (TF0 == 1)
		TR0 = 0; 			// Stop Timer 0
	}
   
   // some 'sec' milliseconds wait function 
   void wait (int sec)
   {         
		unsigned int i;
		for (  i = 0; i < (sec / 50); i++   )
        {
            DELAY_HARDWARE_50ms();
        }	  
   }

   // serial port initializing function 
   void serial_init(void)
   {
        TMOD = 0x20;			 // T1 in mode 2, 8-bit auto reload
        SCON = 0x50;			 // 8-bit data,  none parity bit, 1 stop bit
        TH1  = 0xFD;			 //12MHz freq. 12 osc. cycle and 9600 baud rate
        TL1  = 0xFD;
        TR1  = 1;				 // Run the timer
   }

   // serial port reading function
   unsigned char serial_read(void)
   {
   		bit over = 0;
		while(!RI || !over)
		{
			wait(500);
			over = 1;
			RI = 0;
 			return SBUF;  
		}				  	//wait some time till recieve flag is set and read the buffer
 	}
 
   void main( void )
        {
		
		P0 = 0;						// initialize P0 
		P1 = 0;						// initialize P1 
        P2 = 0; 					// initialize P2 

		while(1)
		{
         	unsigned char val = 0x00; 
		 	unsigned char var1 = 0x00;
		 	unsigned char var2 = 0x00;

		 	var1 = P2;					/*read IR sensor*/
		 	wait(50); 					/* delay for fraction of second */
		 	P2 = num[1];				/*turn IR LED ON*/
		 	wait(200); 					/* delay for fraction of second */
		 	var2 = P2;					/*read IR sensor again*/
		 	wait(50); 					/* delay for fraction of second */
		 	P2 = num[0];				/*turn IR LED OFF*/
			
			if(var1 == num[2])
		 	{
		 		if(var2 == num[1])
					P0 = num[2];	   			// Set sensor error flag
				if(var2 == num[3])
					P0 = num[1];	   			// Set high ambiet light flag
		 	
				serial_init();
				val = serial_read();			//Read the serial port for any command
				P1 = val;						//Send the command to the motors 
			}
		 	
			if(var1 == num[0])
		 	{
				if(var2 == num[3])
				{
					P1 = num[4];				// drive the two motors backward
					wait(1000);					//delay for a second
					P1 = num[0];	
				}
				if(var2 == num[1])
				{
					serial_init();
					val = serial_read();		//Read the serial port for any command
					P1 = val;					//Send the command to the motors
				}			
				
				P0 = num[0];					//Set the flags to zero
		 	}
		}	
      }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Self Employeed
Ethiopia Ethiopia
I am just someone from the Horn of Africa. (FYI... Africa is the only continent with a horn)

Comments and Discussions