/************************************************************************************************************************
*	Program:		valve2.c							*
*	Description:	This program was written to test the pneumatic		                *
*					valves and cylinders.  It will prompt the user	*
*					to select the desired function (manual or 		*
*					automatic positioning) and to select the desired      *
*					characteristics of the PWM signal for the		*
*					valves.					*
*	Programmer:		Scott Nortman					*
*               Revised by:                             Scott Kanowitz
*	Date:			January 28, 2000					*
************************************************************************************************************************/

/**************************** Constants *********************************/
//#define valve TOC
#define tolerance 10 
#define valve_sensor analog(1)
/**************************** End of Constants **************************/

/*************************** Interrupts *********************************/
#pragma interrupt_handler TOC2_isr
/*************************** End of Interrupts **************************/

/************************** Includes ************************************/
#include <tjpbase.h>
#include <analog.h>
#include <vectors.h>
#include <mil.h>
#include <hc11.h>
/************************ End of includes *******************************/

/************************** Global Variables*****************************/
unsigned int duty_cycle, period;

/************************ End of Global Variables ***********************/


/************************** Function Declarations  **********************/
void valve_position (unsigned int);
void PWM_ON ();
void PWM_OFF();
/******************** End of Function Declarations  *********************/


/**************************** Main **************************************/

unsigned int x, y, z;
void main(void)
{
	/* VT100 clear screen */ 
	char clear[]= "\x1b\x5B\x32\x4A\x04"; 
	
	/* VT100 position cursor at (x,y) = (3,12) command is "\x1b[3;12H"*/
  	char place[]= "\x1b[1;1H";	/*Home*/
	
	SET_BIT(PACTL, 0x80);  	
  	init_analog();
  	init_clocktjp();
  	init_serial();

  	INTR_ON(); /*turn on HC11 interrupt system*/

  	printf("%s", clear);  
  	printf("%s", place);
  	
  	printf("\tTitle:\t\tvalve.c\n"
  "\tProgrammer:\tScott Nortman\n"
  "\tDate:\t\tJanuary, 2000\n"
  "\tVersion\t\t1\n\n");
  
	printf("PNEUMATIC CYLINDER DIAGNOSTIC PROGRAM\n\n");
	
	while(1)
	{
		printf("Enter the number of clocks for the period\n");
		period = read_int();
		printf("Enter number of e clocks for the duty cycle\n");
		duty_cycle = read_int();
		printf("Enter the desired position\n");
		z = read_int();
		
		valve_position(z);
		
//		printf("%s", clear);  
 //		printf("%s", place);
		
	}/*End While*/
  	
}/*End Main*/


/**************************** End of Main *******************************/



/************************** Function Defintions *************************/

/********************************valve_position**************************/
void valve_position (unsigned int position)
// This function sets the desired position of the pneumatic cylinder
// shaft +/- the tolerance.  The cylinder moves to its new position
// at a speed determined by the PWM function.
{
	
	//printf("You're  at the beginning of valve_position.\n");
	if (position > (valve_sensor + tolerance))	//If the cylinder is too far out, move it in
	{
		//printf("The analog value is:\t%d\n", valve_sensor);
		SET_BIT (PORTA, 0x80);  //This sets PORTA bit7 to move cylinder in
		PWM_ON();  //After the bit is set, turn on PWM to move the cylinder
		
		while (position > (valve_sensor + tolerance)){//wait until position is in desired range
		//printf("The analog value is:\t%d\n", valve_sensor);
		}
		
		PWM_OFF(); //Turn off PWM system
		return;
	} 
	
	if (position < (valve_sensor - tolerance))
	{
		//printf("The analog value is:\t%d\n", valve_sensor);
		CLEAR_BIT (PORTA, 0x80);  //Tnis sets PORTA bit 7 to move the cylinder out
		PWM_ON();  //After the bit is set, turn on the PWM to move cylinder
		
		while (position < (valve_sensor - tolerance)){//wait until position is in desired range
			//printf("The analog value is:\t%d\n", valve_sensor);
			}	
		
		PWM_OFF();
		return;
	}
}
    


/***************************PWM_ON*****************************/
void PWM_ON()//This is the function to generate PWM
{//This turns on the PWM system on OC2 and enables on
	printf("You're at PWM.\n");
	SET_BIT (TMSK1, 0x40); //Enable interrupt on OC2
	SET_BIT (TCTL1, 0xC0); //Enable on
	
}


/****************************PWM_OFF***************************/
void PWM_OFF()
{//This turns off the PWM system on OC2 and sets the line to 0
//	CLEAR_BIT(TCTL1, 0xC0); //disconnect pin fom interrupt
	CLEAR_BIT (TMSK1, 0x40); //Disable interrupt on OC2
	SET_BIT (TCTL1, 0x80); //set OC2 line to 0 on compare
	SET_BIT (CFORC, 0x40); //force successful compare
}


/***************************OC2_ISR****************************/
void TOC2_isr()
{//OC2 interrupt; adds duty cycle time to next interrupt and clears flags

	if (TCTL1 & 0x40)
		{
			CLEAR_BIT (TCTL1, 0x40); //Clear bit to set for falling edge next interrupt
			SET_BIT(TCTL1, 0x80);
			TOC2 += duty_cycle;		//Add time to TOC2 for duty cycle
		}

	else
		{
			SET_BIT (TCTL1, 0xC0);
			TOC2 += (period - duty_cycle);
		}

	
	CLEAR_FLAG(TFLG1, 0x40); //Clear interrupt flag
}


/************************** End of Function Defintions ******************/






