

	#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
		 	}
		}	
      }

