#include <stdio.h>
#include <hc11.h>

#define top_of_robot *(unsigned char volatile *)(0xB590)
#define linetrack *(unsigned char volatile *) (0XB5A0)
#define	limitswitches *(unsigned char volatile *) (0xB5C0)

unsigned int OC2hi, OC2lo, OC3hi, OC3lo;	/* Duty cycle data */
unsigned int T_counts=0x4e20, per_on2=40, per_on3=40,cntflag=0;


/* Prototypes */
void initoc(void);
void rtoc2(void);
void rtoc3(void);
void cnts2(void);
void cnts3(void);
void display (int);


void main(void)
{
 	int visual = 0, counter = 0;
	int temp1=0, temp2=0, sensorzero=0, sensorone=0;
	int rightline = 0, leftline = 0;
	//             0  1 2  3  
	int two[4]=  {0,40,50,40};
	int three[4]={0,50,40,40};
	//                  0  1  2  3
	int	angletwo[4]	 ={00,40,50,40};		  
	int anglethree[4]={00,50,40,40};
	
	OPTION = 0x93;
	
	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;
	
	visual = 6;
	display(visual);
	cntflag = 0;

while (1)
{	 
	ADCTL=0x01;
	while((ADCTL & 0x80) != 0x80);
	while(!(ADR1 < 70 && ADR1 > 30))
	{	 
        	
		 ADCTL=0x01;
		 while((ADCTL & 0x80) != 0x80);
	}
		  
			visual = 7;
		    display(visual);
	  	   	initoc();
			cntflag = 0;
			while (cntflag <= 10){
			top_of_robot = 0x03;
			cnts2();
			cnts3();
			}
	        cntflag = 0;
	
		leftline = linetrack & 0xC0;
		while (temp1 != 0 )
		{     
		while (leftline != 0x00)
		{  TMSK1 = 0x00;
			  PORTA = 0x00;
			  leftline = linetrack & 0xC0;
		}	  
		      TMSK1 = 0x00;
			  PORTA = 0x00;
			  while (cntflag <= 100)
			  {
			  }
		  	  if(temp1 == 0x01)
			  {
			  per_on2 = two[temp1];
		  	  per_on3 = three[temp1];
			  }
			  if (temp1 == 0x02)
			  {
			  per_on2 = two[temp1];
		  	  per_on3 = three[temp1];
			  }
			  if (temp1 == 0x02 && temp2 == 0x04)
			  {
			  per_on2 = 20;
			  per_on3 = 40;
			  }
			  if (temp1 == 0x01 && temp2 == 0x08)
			  {
			  per_on2 = 40;
			  per_on3 = 20;
			  }
			  if (temp1 == 0x03 && temp2 == 0x0C)
			  {
			  per_on2 = 40; 
			  per_on3 = 40;
			  }
		  	  TMSK1 = 0x60;
			  top_of_robot = 0x03;
		  	  cnts2();
		  	  cnts3();
			  cntflag = 0;
			  temp1 = linetrack & 0x03;
			  temp2 = linetrack & 0x0C;
		 }
		 leftline = linetrack & 0xC0;
       	 while (temp1 == 0x03)
		 {
		 while (leftline != 0x00)
		{  TMSK1 = 0x00;
			  PORTA = 0x00;
			  leftline = linetrack & 0xC0;
		}	
		   	  while (temp2 == 0x00)
		  	  {
		  	  	TMSK1 = 0x60;
				top_of_robot = 0x03;
		  	 	per_on2 = 30;
		  	 	per_on3 = 30;
		  	 	cnts2();
		  	 	cnts3();
				cntflag = 0;
				
		  	  }
		  	 top_of_robot = 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 + OC2lo;
		PORTA = PORTA | 0x40;
		}
	else
		TOC2 = TOC2 + OC2hi;
		PORTA = PORTA |  0xBF;
	
	TFLG1 = 0x40;	/* Clear flag OC2F */
	asm("	rti");	/* This is an interrupt service routine */
}

void rtoc3(void)
{	
	if ((PORTA & 0X20) == 0) {
		TOC3 = TOC3 + OC3lo;
		PORTA = PORTA | 0x20;
	}	
	else
		TOC3 = TOC3 + OC3hi;
		PORTA = PORTA | 0xDF;
	
	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");
}
void display (FRED)
{	 
	 unsigned int ad;
	 unsigned char ascii;
	 int cnt,old=0,wait=0;
	 unsigned char string1[]="The End ";
	 unsigned char string2[]="Duck    ";
	 unsigned char string3[]="Deer    ";
	 unsigned char string4[]="Rabbit  ";
	 unsigned char string5[]="Done    ";
	 unsigned char string6[]="Stop    ";
	 unsigned char string7[]="Go      ";
	 unsigned char string8[]="IEEE USM";
	 unsigned char string9[]="        ";
	 unsigned char nothing[]="None    ";
	 unsigned int digit[8] = {0xb5B7, 0xb5B6, 0xb5B5, 0xb5B4,0xb5Bf, 0xb5Be, 0xb5Bd,
	 		  				   0xb5Bc};				 
 
	 asm (" ldaa #$00");
	 asm (" staa $b590");
	 while (old <= 100){old ++;}
	 asm (" ldaa #$03");
	 asm (" staa $b5B0");
	 asm (" staa $b5B8");
	 
	 
	
	 if (FRED == 1){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string1[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 2){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string2[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }  
	 else if (FRED == 3){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string3[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 4){
	    for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string4[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 5){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string5[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 6){
	    for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string6[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 7){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string7[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 else if (FRED == 9){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string8[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	  else if (FRED == 8){
	 	for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=string9[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 }
	 /*else{
	    for(cnt=0; cnt<8; cnt++)
	 	{
	 	 ascii=nothing[cnt];
	 	 ad=digit[cnt];
	 	 asm (" ldaa %ascii");
	 	 asm (" ldy %ad");
	 	 asm (" staa 0,y");
	 	 }
	 } */ 
	 	 
	 
}