/* C Listing 11.4 */

#include <hc11.h>	/* Include register definitions */
#include <stdio.h>
#define motcont *(unsigned char volatile *)(0xB590)
#define linetrack *(unsigned char volatile *) (0XB5A0)


/* Declarations */
unsigned int OC2hi, OC2lo, OC3hi, OC3lo;	/* Duty cycle data */
unsigned int T_counts=0x4E20, per_on2=60, per_on3=70, cntflag=0;


/* Prototypes */
void initoc(void);
void rtoc2(void);
void rtoc3(void);
void cnts2(void);
void cnts3(void);
void RTIINTR (void);


void main(void)
{
 	int temp1=0, temp2=0, sensorzero=0, sensorone=0;
	int rightline = 0, leftline = 0;
	//             0  1 2  3  
	int two[4]=  {0,55,70,60};
	int three[4]={0,70,55,60};
	//                  0  1  2  3
	int	angletwo[4]	 ={00,60,70,60};		  
	int anglethree[4]={00,70,60,60};
	
	setbaud(BAUD9600);
	INTR_OFF();
	/* set up jump vector for buffalo */
	asm("	ldaa #$7e");
	asm("	staa $dc");
	asm("	staa $eb");
	asm("	staa $d9");
	asm("	ldd #_rtoc2");
	asm("	std $dd");
	asm("	ldd #_rtoc3");
	asm("	std $da");
	asm("	ldd #_RTIINTR");
	asm("	std $ec");
	
	TMSK2=0x40;
	PACTL=PACTL & 0xFC;
	PACTL=PACTL | 0x00;
	
	motcont =0x03;
	cnts2();
	cnts3();
	
	initoc();
	
	cntflag = 0;
	
	while(1)
	{	 
		 
		 while (cntflag == 1)
		 {
			temp2 = linetrack & 0x0C;	
		 	temp1 = linetrack & 0x03;
			leftline = linetrack & 0xC0;
			rightline = linetrack & 0x30;
  		 	motcont =0x03;	
		 
		 	while ( (temp1 == 0x00)  &&  (temp2 == 0x00) )
			{
		 	   TMSK1 = 0X60;
			   per_on2 = 50;
			   per_on3 = 50;
			   cnts2();
			   cnts3(); 
			   temp2 = linetrack & 0x0C;	
		 	   temp1 = linetrack & 0x03;
			}
		
		    while ( (temp1 == 0x00) && (temp2 != 0x00) )
		    {	  
			  
		 	  TMSK1= 0x00;
			  PORTA = 0x00;
			  cntflag = 0;
			  
			  while (cntflag <= 200)
			  {
					
			  }
			  cntflag = 0;
			  
			  while (cntflag <= 120)
			  {
			  
		  	   		if (sensorzero == 1)
			  		{
			   	 	   motcont = 0x01;
					   per_on2 = 60;
				 	   per_on3 = 70;
			  		}
			  		if (sensorone == 1)
			  		{
					   motcont = 0x02;
			   	 	   per_on2 = 70;
				 	   per_on3 = 60;
			  		}
			 		TMSK1 = 0x60;
			  		cnts2();
			  		cnts3();
			  		temp1 = linetrack & 0x03;
					cntflag = 0;
					while (cntflag <= 183)
					{
					  TMSK1 = 0x00;
					  PORTA = 0x00;
					}
					if (temp1 == 0)
					{
					cntflag = 0; 
					}
					
					
              }
			  temp1 = linetrack & 0x03;
			  temp2 = linetrack & 0x0C;
	          
			}
		while ( temp1 != 0x00 && temp2 == 0X00 )
		{
		   	if (sensorzero == 1)
			{
			   motcont = 0x03; 
			   per_on2 = 50; 
			   per_on3 = 70;
			}
			if (sensorone == 1)
			{
			   motcont = 0x03;
			   per_on2 = 70; 
			   per_on3 = 50;
			}
			TMSK1 = 0x60;
			cnts2();
			cnts3();
			temp2 = linetrack & 0x0C; 
			temp1 = linetrack & 0x03;
			while (cntflag <= 183)
			{
			   TMSK1 = 0x00;
			   PORTA = 0x00;
			}
			cntflag = 0;
		}
		/*while (temp1 == 0x03 && (temp2 == 0x08 || temp2 == 0x04))
		{
		 	  if (temp2 == 0x08)
			  {
			  motcont = 0x03; 
			  per_on2 = 70;
			  per_on3 = 30;
			  }
			  if (temp2 == 0x03)
			  {
			  motcont = 0x03; 
			  per_on2 = 30;
			  per_on3 = 70;
			  }
			  TMSK1 = 0x60;
			  cnts2();
			  cnts3();
			  temp1 = linetrack & 0x03;
			  temp2 = linetrack & 0x0C;
			  while (cntflag <= 183)
			  {
				TMSK1 = 0x00;
				PORTA = 0x00;
			  }
		}*/	
		while ((two[temp1] != 0) && (three[temp1] != 0 ))
		{   
		  	  sensorzero = 0;
			  sensorone = 0;
		  	  if(temp1 == 0x01)
			  {
		  	  sensorzero = 1;
			  }
		  	  if (temp1 == 0x02)
			  {
		  	  sensorone = 1;
			  }
		  	  TMSK1 = 0x60;
			  motcont = 0x03;
		  	  per_on2 = two[temp1];
		  	  per_on3 = three[temp1];
		  	  cnts2();
		  	  cnts3();
			  cntflag = 0;
			  temp1 = linetrack & 0x03;
			  temp2 = linetrack & 0x0C;
		 }
       	 while (temp1 == 0x03)
		 {
		   	  if (temp2 == 0x00)
		  	  {
		  	  	TMSK1 = 0x60;
				motcont = 0x03;
		  	 	per_on2 = 30;
		  	 	per_on3 = 30;
		  	 	cnts2();
		  	 	cnts3();
				cntflag = 0;
				
		  	  }
		  	 motcont = 0x03;
		  	 per_on2 = angletwo[temp2];
		  	 per_on3 = anglethree[temp2];
		  	 cnts2();
		 	 cnts3();
			 temp1 = linetrack & 0x03; 
		}
		  

	    cntflag = 0;
	  }
		
    }

	
}

void initoc(void)
/* Initialize OC2 */
{
	TOC2 = TCNT;
	TOC3 = TCNT;
	TCTL1 = 0x00;
	TFLG1 = 0x60;
	TMSK1 = 0x60;
	INTR_ON();
}

void rtoc2(void)
/* Interrupt Service Routine for OC2 PWM */
{   
	if ((PORTA & 0X40) == 0){ 
		TOC2 = TOC2 + OC2hi;
		PORTA = PORTA | 0x40;
		}
	else{
		TOC2 = TOC2 + OC2lo;
		PORTA = PORTA & 0xBF;
		}
	//TCTL1 = TCTL1 ^ 0x40;	/* invert OL2 to toggle next OC2 edge */
	TFLG1 = 0x40;	/* Clear flag OC2F */
	asm("	rti");	/* This is an interrupt service routine */
}

void rtoc3(void)
{	
	if ((PORTA & 0X20) == 0) {
		TOC3 = TOC3 + OC3hi;
		PORTA = PORTA | 0x20;
	}	
	else{
		TOC3 = TOC3 + OC3lo;
		PORTA = PORTA & 0xDF;
	}
	//TCTL1 = TCTL1 ^ 0x10;	/* invert OL2 to toggle next OC2 edge */
	TFLG1 = 0x20;	/* Clear flag OC2F */
	asm("	rti");	/* This is an interrupt service routine */
}
void cnts2 (void)
{
	OC2hi=T_counts * (float)per_on2/100;
	OC2lo=T_counts - OC2hi;
}

void cnts3 (void)
{
  	OC3hi=T_counts * (float)per_on3/100;
	OC3lo=T_counts - OC3hi;
}	

void RTIINTR (void)
{
  TFLG2 = 0x40;
  
  cntflag ++;
  
  asm("	  rti");
}
