#include "C:\\Documents and Settings\\Marek\\Desktop\\Bakalarka 2009\\PIC628A program\\StpDrvPic628\\StepperDriverPic628_Auto.h"



#define lf 0x0A
#define cr 0x0D

#define SERINTdivH APROCFREQ/16/9600-1 // Serial Interface using USART hardware
#define SERINTdiv APROCFREQ/64/9600-1
#if SERINTdivH>255
#define NoBRGH 1
#endif
#define TXSTA0 TXSTA
#define SPBRG0 SPBRG
#define RCS

bool stop = 1;
unsigned char steps = 0;
unsigned char goalstep = 0;
char lastchar = "*";
bool freeride;
unsigned char ledCounter = 0;
//
// This file includes all user definable routines. It may be changed at will as
// it will not be regenerated once the application has been generated for the
// first time.
//

//*******************************************************************************
//
// Insert your interrupt handling code if required here. 
// Note quick interrupts are used so code must be simple
// See the manual for details of quick interrupts.
//
void SendChar(char send)
{
 #asmline 	bsf	STATUS,RP0	; bank 1
 #asmline	btfss	TXSTA,TRMT 	; TSR is empty
 #asmline	goto	$-1			
 #asmline	bcf		STATUS,RP0	; bank 0
 TXREG = send; 
}

void SendCommand(char *command, char length)
{
  int i;
  SendChar(cr);  
  SendChar(lf);
  for(i = 0; i < length; i++)
  {
    SendChar(*command);
    command++;
  }
  SendChar(cr);  
  SendChar(lf);
}

void UserInterrupt()
{
	// Insert your code here
 if(PIR1 & (1 << TMR2IF))
 {
 	ledCounter++;
 	if(ledCounter == 50)
 	{

 	  led = !led;
 	  ledCounter = 0;
 	}
	PIR1 &= ~(1 << TMR2IF);
 }
 if(PIR1 & (1 << TMR1IF))
 {
	if(!stop) 
  	{
  		en1 = 0;
  		en2 = 0;
    		clk1 = !clk1;
    		clk2 = !clk2;
		if((clk1 == 1)||(clk2 == 1)) steps++;
		
		if((steps == 10)&&(goalstep >= 10)&&(freeride == 0))
			{
				SendChar(10);
				goalstep -= 10;
			  	steps = 0;
			}
		if((steps == 10)&&(freeride == 1))
			{
				SendChar(10);
				goalstep = 0;
			  	steps = 0;
			}
		if((steps == goalstep)&&(freeride == 0))
			{
				SendChar(steps);
				steps = 0;
				goalstep = 0;
				stop = 1;
				en1 = 1;
				en2 = 1;
			}

	}
	TMR1H = 0x90;
	PIR1 &= ~(1 << TMR1IF);
 }

 if(PIR1 & (1 << RCIF))
   if((RCSTA & (1 << OERR))||(RCSTA & (1 << FERR))) 
   {
   	// USART error
	RCSTA &= ~(1 << CREN);
	RCSTA |=  (1 << CREN);
   }
   else
   {
	char Recchar = RCREG;
   	if(lastchar == '*')
   	  lastchar = Recchar;
   	else
   	{
   	  	switch(lastchar)
   	  	{
   	  		case('F'):
   	  		{
   	  			dir1 = 0;
   	  			dir2 = 1;
				goalstep = Recchar;
				if(goalstep == 0) freeride = 1; else freeride = 0;
				SendChar(steps);
				steps = 0;
				stop = 0;
				break;   	  				
   	  		}
   	  		case('B'):
   	  		{
   	  			dir1 = 1;
   	  			dir2 = 0;
			 	goalstep = Recchar;
			 	if(goalstep == 0) freeride = 1; else freeride = 0;
				SendChar(steps);
				steps = 0;
				stop = 0;
				break;     	  				
   	  		}
   	  		case('L'):
   	  		{
   	  			dir1 = 0;
   	  			dir2 = 0;
				goalstep = Recchar;
				if(goalstep == 0) freeride = 1; else freeride = 0;
				SendChar(steps);
				steps = 0;
				stop = 0;  	  				
				break;
   	  		}
   	  		case('R'):
   	  		{
   	  			dir1 = 1;
   	  			dir2 = 1;
				goalstep = Recchar;
				if(goalstep == 0) freeride = 1; else freeride = 0;
				SendChar(steps);
				steps = 0;
				stop = 0;   	  				
				break;
   	  		}
   	  		case('S'):
   	  		{
   	  			dir1 = 0;
   	  			dir2 = 1;
				SendChar(steps);
				steps = 0;
				en1 = 1;
				en2 = 1;
				stop = 1;
				break;
   	  		}
   	  		case('+'):
   	  		{
   	  			SendCommand("Hello",5);
				break;
   	  		}
   	  	}
   	  lastchar = '*';	
   	  }
   }
 #asmline SETPCLATH UserIntReturn,-1  ; SETPCLATH for interrupt routine
 #asmline goto UserIntReturn	      ; Assembler - go back to interrupt routine
}


//*******************************************************************************
//
// Insert your initialisation code if required here. 
// Note that when this routine is called Interrupts will not be enabled - the 
// Application Designer will enable them before the main loop
//

void UserInitialise()
{
	#ifdef NoBRGH
  	TXSTA=(1<<TXEN);
  	SPBRG=SERINTdiv;
	#else
  	 TXSTA=(1<<BRGH)|(1<<TXEN);
  	 SPBRG=SERINTdivH;
 	#endif
  	RCSTA=(1<<SPEN)|(1<<CREN);
	INTCON|=(1<<PEIE);
	PIE1|=(1<<TMR1IE);
	PIE1|=(1<<TMR2IE);
	PIE1|=(1<<RCIE);
	CMCON = 0x07;
	en1 = 1;
	en2 = 1;
	rst1 = 1;
	rst2 = 1;
	
}

//*******************************************************************************
//
// Insert your main loop code if required here. This routine will be called
// as part of the main loop code
//

void UserLoop()
{
}

//
// User occurrence code
//
